Blame view

src/components/color-picker/color-picker.vue 16.3 KB
c6faec44   梁灏   init ColorPicker
1
  <template>
f2bcd4ad   Graham Fairweather   Color keyboard co...
2
      <div
045806e6   Rookie_Zoe   fix #4826
3
          v-click-outside="handleClose"
f2bcd4ad   Graham Fairweather   Color keyboard co...
4
5
6
7
8
9
10
11
12
          :class="classes">
          <div
              ref="reference"
              :class="wrapClasses"
              @click="toggleVisible">
              <input
                  :name="name"
                  :value="currentValue"
                  type="hidden">
4beb8e75   梁灏   ColorPicker add g...
13
              <Icon :type="arrowType" :custom="customArrowType" :size="arrowSize" :class="arrowClasses"></Icon>
f2bcd4ad   Graham Fairweather   Color keyboard co...
14
15
16
17
18
19
20
21
22
              <div
                  ref="input"
                  :tabindex="disabled ? undefined : 0"
                  :class="inputClasses"
                  @keydown.tab="onTab"
                  @keydown.esc="onEscape"
                  @keydown.up="onArrow"
                  @keydown.down="onArrow"
              >
0aefe4aa   梁灏   update ColorPicker
23
                  <div :class="[prefixCls + '-color']">
f2bcd4ad   Graham Fairweather   Color keyboard co...
24
25
26
                      <div
                          v-show="value === '' && !visible"
                          :class="[prefixCls + '-color-empty']">
94394576   梁灏   update ColorPicke...
27
                          <i :class="[iconPrefixCls, iconPrefixCls + '-ios-close']"></i>
c0fa72ca   梁灏   update ColorPicker
28
                      </div>
f2bcd4ad   Graham Fairweather   Color keyboard co...
29
30
31
                      <div
                          v-show="value || visible"
                          :style="displayedColorStyle"></div>
0aefe4aa   梁灏   update ColorPicker
32
                  </div>
b6bda1dc   梁灏   update ColorPicker
33
34
              </div>
          </div>
14b7409b   梁灏   fix ColorPicker a...
35
          <transition name="transition-drop">
4109bbc6   梁灏   update ColorPicker
36
              <Drop
f2bcd4ad   Graham Fairweather   Color keyboard co...
37
                  v-transfer-dom
4109bbc6   梁灏   update ColorPicker
38
                  v-show="visible"
4109bbc6   梁灏   update ColorPicker
39
                  ref="drop"
f2bcd4ad   Graham Fairweather   Color keyboard co...
40
                  :placement="placement"
4109bbc6   梁灏   update ColorPicker
41
                  :data-transfer="transfer"
7bafe9d9   梁灏   fixes #4453 #4480...
42
                  :transfer="transfer"
f2bcd4ad   Graham Fairweather   Color keyboard co...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
                  :class="dropClasses"
              >
                  <transition name="fade">
                      <div
                          v-if="visible"
                          :class="[prefixCls + '-picker']">
                          <div :class="[prefixCls + '-picker-wrapper']">
                              <div :class="[prefixCls + '-picker-panel']">
                                  <Saturation
                                      ref="saturation"
                                      v-model="saturationColors"
                                      :focused="visible"
                                      @change="childChange"
                                      @keydown.native.tab="handleFirstTab"
                                  ></Saturation>
                              </div>
                              <div
                                  v-if="hue"
                                  :class="[prefixCls + '-picker-hue-slider']">
                                  <Hue
                                      v-model="saturationColors"
                                      @change="childChange"></Hue>
                              </div>
                              <div
                                  v-if="alpha"
                                  :class="[prefixCls + '-picker-alpha-slider']">
                                  <Alpha
                                      v-model="saturationColors"
                                      @change="childChange"></Alpha>
                              </div>
                              <recommend-colors
                                  v-if="colors.length"
                                  :list="colors"
                                  :class="[prefixCls + '-picker-colors']"
                                  @picker-color="handleSelectColor"></recommend-colors>
                              <recommend-colors
                                  v-if="!colors.length && recommend"
                                  :list="recommendedColor"
                                  :class="[prefixCls + '-picker-colors']"
                                  @picker-color="handleSelectColor"></recommend-colors>
4109bbc6   梁灏   update ColorPicker
83
                          </div>
f2bcd4ad   Graham Fairweather   Color keyboard co...
84
                          <div :class="[prefixCls + '-confirm']">
0fdbb26b   梁灏   ColorPicker add e...
85
86
                              <span :class="confirmColorClasses">
                                  <template v-if="editable">
a7dc3691   梁灏   add blur
87
                                      <i-input :value="formatColor" size="small" @on-enter="handleEditColor" @on-blur="handleEditColor"></i-input>
0fdbb26b   梁灏   ColorPicker add e...
88
89
90
                                  </template>
                                  <template v-else>{{formatColor}}</template>
                              </span>
f2bcd4ad   Graham Fairweather   Color keyboard co...
91
92
93
94
                              <i-button
                                  ref="clear"
                                  :tabindex="0"
                                  size="small"
f2bcd4ad   Graham Fairweather   Color keyboard co...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
                                  @click.native="handleClear"
                                  @keydown.enter="handleClear"
                                  @keydown.native.esc="closer"
                              >{{t('i.datepicker.clear')}}</i-button>
                              <i-button
                                  ref="ok"
                                  :tabindex="0"
                                  size="small"
                                  type="primary"
                                  @click.native="handleSuccess"
                                  @keydown.native.tab="handleLastTab"
                                  @keydown.enter="handleSuccess"
                                  @keydown.native.esc="closer"
                              >{{t('i.datepicker.ok')}}</i-button>
4109bbc6   梁灏   update ColorPicker
109
                          </div>
77950c30   梁灏   update ColorPicker
110
                      </div>
f2bcd4ad   Graham Fairweather   Color keyboard co...
111
                  </transition>
4109bbc6   梁灏   update ColorPicker
112
113
114
              </Drop>
          </transition>
      </div>
c6faec44   梁灏   init ColorPicker
115
  </template>
e7893a68   梁灏   update ColorPicker
116
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
117
118
  <script>
  import tinycolor from 'tinycolor2';
26369639   Graham Fairweather   Update v-click-ou...
119
  import {directive as clickOutside} from 'v-click-outside-x';
f2bcd4ad   Graham Fairweather   Color keyboard co...
120
121
122
123
124
125
  import TransferDom from '../../directives/transfer-dom';
  import Drop from '../../components/select/dropdown.vue';
  import RecommendColors from './recommend-colors.vue';
  import Saturation from './saturation.vue';
  import Hue from './hue.vue';
  import Alpha from './alpha.vue';
0fdbb26b   梁灏   ColorPicker add e...
126
  import iInput from '../input/input.vue';
4504bd9c   kk   unknow custom ele...
127
  import iButton from '../button/button.vue';
4beb8e75   梁灏   ColorPicker add g...
128
  import Icon from '../icon/icon.vue';
f2bcd4ad   Graham Fairweather   Color keyboard co...
129
130
131
132
133
  import Locale from '../../mixins/locale';
  import {oneOf} from '../../utils/assist';
  import Emitter from '../../mixins/emitter';
  import Prefixes from './prefixMixin';
  import {changeColor, toRGBAString} from './utils';
e7893a68   梁灏   update ColorPicker
134
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
135
136
  export default {
      name: 'ColorPicker',
e7893a68   梁灏   update ColorPicker
137
  
4beb8e75   梁灏   ColorPicker add g...
138
      components: {Drop, RecommendColors, Saturation, Hue, Alpha, iInput, iButton, Icon},
e7893a68   梁灏   update ColorPicker
139
  
26369639   Graham Fairweather   Update v-click-ou...
140
      directives: {clickOutside, TransferDom},
e7893a68   梁灏   update ColorPicker
141
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
142
      mixins: [Emitter, Locale, Prefixes],
e7893a68   梁灏   update ColorPicker
143
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
      props: {
          value: {
              type: String,
              default: undefined,
          },
          hue: {
              type: Boolean,
              default: true,
          },
          alpha: {
              type: Boolean,
              default: false,
          },
          recommend: {
              type: Boolean,
              default: false,
          },
          format: {
              type: String,
              validator(value) {
                  return oneOf(value, ['hsl', 'hsv', 'hex', 'rgb']);
2b2dc78a   Alexander Misel   Add saturation pr...
165
              },
f2bcd4ad   Graham Fairweather   Color keyboard co...
166
167
168
169
170
171
              default: undefined,
          },
          colors: {
              type: Array,
              default() {
                  return [];
b6bda1dc   梁灏   update ColorPicker
172
              },
f2bcd4ad   Graham Fairweather   Color keyboard co...
173
174
175
176
177
178
          },
          disabled: {
              type: Boolean,
              default: false,
          },
          size: {
f2bcd4ad   Graham Fairweather   Color keyboard co...
179
180
              validator(value) {
                  return oneOf(value, ['small', 'large', 'default']);
9af2f01c   梁灏   update ColorPicker
181
              },
737894de   梁灏   ColorPicker suppo...
182
              default () {
fe5ffd7f   梁灏   fixed #4196 #4165
183
                  return !this.$IVIEW || this.$IVIEW.size === '' ? 'default' : this.$IVIEW.size;
737894de   梁灏   ColorPicker suppo...
184
              }
f2bcd4ad   Graham Fairweather   Color keyboard co...
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
          },
          hideDropDown: {
              type: Boolean,
              default: false,
          },
          placement: {
              type: String,
              validator(value) {
                  return oneOf(value, [
                      'top',
                      'top-start',
                      'top-end',
                      'bottom',
                      'bottom-start',
                      'bottom-end',
                      'left',
                      'left-start',
                      'left-end',
                      'right',
                      'right-start',
                      'right-end',
                  ]);
b6bda1dc   梁灏   update ColorPicker
207
              },
f2bcd4ad   Graham Fairweather   Color keyboard co...
208
209
210
211
              default: 'bottom',
          },
          transfer: {
              type: Boolean,
737894de   梁灏   ColorPicker suppo...
212
              default () {
fe5ffd7f   梁灏   fixed #4196 #4165
213
                  return !this.$IVIEW || this.$IVIEW.transfer === '' ? false : this.$IVIEW.transfer;
737894de   梁灏   ColorPicker suppo...
214
              }
f2bcd4ad   Graham Fairweather   Color keyboard co...
215
216
217
218
219
          },
          name: {
              type: String,
              default: undefined,
          },
0fdbb26b   梁灏   ColorPicker add e...
220
221
222
223
          editable: {
              type: Boolean,
              default: true
          },
f2bcd4ad   Graham Fairweather   Color keyboard co...
224
225
226
227
228
229
230
231
232
233
234
235
      },
  
      data() {
          return {
              val: changeColor(this.value),
              currentValue: this.value,
              dragging: false,
              visible: false,
              recommendedColor: [
                  '#2d8cf0',
                  '#19be6b',
                  '#ff9900',
7401ec35   梁灏   update colors
236
                  '#ed4014',
f2bcd4ad   Graham Fairweather   Color keyboard co...
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
                  '#00b5ff',
                  '#19c919',
                  '#f9e31c',
                  '#ea1a1a',
                  '#9b1dea',
                  '#00c2b1',
                  '#ac7a33',
                  '#1d35ea',
                  '#8bc34a',
                  '#f16b62',
                  '#ea4ca3',
                  '#0d94aa',
                  '#febd79',
                  '#5d4037',
                  '#00bcd4',
                  '#f06292',
                  '#cddc39',
                  '#607d8b',
                  '#000000',
                  '#ffffff',
              ],
          };
      },
  
      computed: {
          arrowClasses() {
              return [
f2bcd4ad   Graham Fairweather   Color keyboard co...
264
265
266
267
268
269
270
271
272
273
                  `${this.inputPrefixCls}-icon`,
                  `${this.inputPrefixCls}-icon-normal`,
              ];
          },
          transition() {
              return oneOf(this.placement, ['bottom-start', 'bottom', 'bottom-end']) ? 'slide-up' : 'fade';
          },
          saturationColors: {
              get() {
                  return this.val;
9af2f01c   梁灏   update ColorPicker
274
              },
f2bcd4ad   Graham Fairweather   Color keyboard co...
275
276
277
              set(newVal) {
                  this.val = newVal;
                  this.$emit('on-active-change', this.formatColor);
b6bda1dc   梁灏   update ColorPicker
278
              },
f2bcd4ad   Graham Fairweather   Color keyboard co...
279
280
281
282
283
284
          },
          classes() {
              return [
                  `${this.prefixCls}`,
                  {
                      [`${this.prefixCls}-transfer`]: this.transfer,
4109bbc6   梁灏   update ColorPicker
285
                  },
f2bcd4ad   Graham Fairweather   Color keyboard co...
286
287
288
289
290
291
292
293
294
295
              ];
          },
          wrapClasses() {
              return [
                  `${this.prefixCls}-rel`,
                  `${this.prefixCls}-${this.size}`,
                  `${this.inputPrefixCls}-wrapper`,
                  `${this.inputPrefixCls}-wrapper-${this.size}`,
                  {
                      [`${this.prefixCls}-disabled`]: this.disabled,
b6bda1dc   梁灏   update ColorPicker
296
                  },
f2bcd4ad   Graham Fairweather   Color keyboard co...
297
              ];
b6bda1dc   梁灏   update ColorPicker
298
          },
f2bcd4ad   Graham Fairweather   Color keyboard co...
299
300
301
302
303
304
305
306
          inputClasses() {
              return [
                  `${this.prefixCls}-input`,
                  `${this.inputPrefixCls}`,
                  `${this.inputPrefixCls}-${this.size}`,
                  {
                      [`${this.prefixCls}-focused`]: this.visible,
                      [`${this.prefixCls}-disabled`]: this.disabled,
e7893a68   梁灏   update ColorPicker
307
                  },
f2bcd4ad   Graham Fairweather   Color keyboard co...
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
              ];
          },
          dropClasses() {
              return [
                  `${this.transferPrefixCls}-no-max-height`,
                  {
                      [`${this.prefixCls}-transfer`]: this.transfer,
                      [`${this.prefixCls}-hide-drop`]: this.hideDropDown,
                  },
              ];
          },
          displayedColorStyle() {
              return {backgroundColor: toRGBAString(this.visible ? this.saturationColors.rgba : tinycolor(this.value).toRgb())};
          },
          formatColor() {
              const {format, saturationColors} = this;
  
              if (format) {
                  if (format === 'hsl') {
                      return tinycolor(saturationColors.hsl).toHslString();
e7893a68   梁灏   update ColorPicker
328
                  }
f2bcd4ad   Graham Fairweather   Color keyboard co...
329
330
331
  
                  if (format === 'hsv') {
                      return tinycolor(saturationColors.hsv).toHsvString();
0aefe4aa   梁灏   update ColorPicker
332
                  }
59dc2df0   梁灏   update ColorPicker
333
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
334
335
                  if (format === 'hex') {
                      return saturationColors.hex;
59dc2df0   梁灏   update ColorPicker
336
                  }
f2bcd4ad   Graham Fairweather   Color keyboard co...
337
338
339
  
                  if (format === 'rgb') {
                      return toRGBAString(saturationColors.rgba);
4109bbc6   梁灏   update ColorPicker
340
                  }
f2bcd4ad   Graham Fairweather   Color keyboard co...
341
342
              } else if (this.alpha) {
                  return toRGBAString(saturationColors.rgba);
e7893a68   梁灏   update ColorPicker
343
              }
f2bcd4ad   Graham Fairweather   Color keyboard co...
344
345
  
              return saturationColors.hex;
e7893a68   梁灏   update ColorPicker
346
          },
0fdbb26b   梁灏   ColorPicker add e...
347
348
349
350
351
352
353
          confirmColorClasses () {
              return [
                  `${this.prefixCls}-confirm-color`,
                  {
                      [`${this.prefixCls}-confirm-color-editable`]: this.editable
                  }
              ];
4beb8e75   梁灏   ColorPicker add g...
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
          },
          // 3.4.0, global setting customArrow 有值时,arrow 赋值空
          arrowType () {
              let type = 'ios-arrow-down';
  
              if (this.$IVIEW) {
                  if (this.$IVIEW.colorPicker.customArrow) {
                      type = '';
                  } else if (this.$IVIEW.colorPicker.arrow) {
                      type = this.$IVIEW.colorPicker.arrow;
                  }
              }
              return type;
          },
          // 3.4.0, global setting
          customArrowType () {
              let type = '';
  
              if (this.$IVIEW) {
                  if (this.$IVIEW.colorPicker.customArrow) {
                      type = this.$IVIEW.colorPicker.customArrow;
                  }
              }
              return type;
          },
          // 3.4.0, global setting
          arrowSize () {
              let size = '';
  
              if (this.$IVIEW) {
                  if (this.$IVIEW.colorPicker.arrowSize) {
                      size = this.$IVIEW.colorPicker.arrowSize;
                  }
              }
              return size;
0fdbb26b   梁灏   ColorPicker add e...
389
          }
f2bcd4ad   Graham Fairweather   Color keyboard co...
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
      },
  
      watch: {
          value(newVal) {
              this.val = changeColor(newVal);
          },
          visible(val) {
              this.val = changeColor(this.value);
              this.$refs.drop[val ? 'update' : 'destroy']();
              this.$emit('on-open-change', Boolean(val));
          },
      },
  
      mounted() {
          this.$on('on-escape-keydown', this.closer);
          this.$on('on-dragging', this.setDragging);
      },
  
      methods: {
          setDragging(value) {
              this.dragging = value;
          },
          handleClose(event) {
              if (this.visible) {
                  if (this.dragging || event.type === 'mousedown') {
                      event.preventDefault();
                      return;
4109bbc6   梁灏   update ColorPicker
417
                  }
b6bda1dc   梁灏   update ColorPicker
418
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
419
420
421
422
                  if (this.transfer) {
                      const {$el} = this.$refs.drop;
                      if ($el === event.target || $el.contains(event.target)) {
                          return;
e7893a68   梁灏   update ColorPicker
423
424
425
                      }
                  }
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
                  this.closer(event);
                  return;
              }
  
              this.visible = false;
          },
          toggleVisible() {
              if (this.disabled) {
                  return;
              }
  
              this.visible = !this.visible;
              this.$refs.input.focus();
          },
          childChange(data) {
              this.colorChange(data);
          },
          colorChange(data, oldHue) {
              this.oldHue = this.saturationColors.hsl.h;
              this.saturationColors = changeColor(data, oldHue || this.oldHue);
          },
          closer(event) {
              if (event) {
                  event.preventDefault();
                  event.stopPropagation();
              }
  
              this.visible = false;
              this.$refs.input.focus();
          },
          handleButtons(event, value) {
              this.currentValue = value;
              this.$emit('input', value);
              this.$emit('on-change', value);
              this.dispatch('FormItem', 'on-form-change', value);
              this.closer(event);
          },
          handleSuccess(event) {
              this.handleButtons(event, this.formatColor);
              this.$emit('on-pick-success');
          },
          handleClear(event) {
              this.handleButtons(event, '');
              this.$emit('on-pick-clear');
          },
          handleSelectColor(color) {
              this.val = changeColor(color);
              this.$emit('on-active-change', this.formatColor);
          },
0fdbb26b   梁灏   ColorPicker add e...
475
476
477
478
          handleEditColor (event) {
              const value = event.target.value;
              this.handleSelectColor(value);
          },
f2bcd4ad   Graham Fairweather   Color keyboard co...
479
480
481
482
483
          handleFirstTab(event) {
              if (event.shiftKey) {
                  event.preventDefault();
                  event.stopPropagation();
                  this.$refs.ok.$el.focus();
e7893a68   梁灏   update ColorPicker
484
              }
f2bcd4ad   Graham Fairweather   Color keyboard co...
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
          },
          handleLastTab(event) {
              if (!event.shiftKey) {
                  event.preventDefault();
                  event.stopPropagation();
                  this.$refs.saturation.$el.focus();
              }
          },
          onTab(event) {
              if (this.visible) {
                  event.preventDefault();
              }
          },
          onEscape(event) {
              if (this.visible) {
                  this.closer(event);
              }
          },
          onArrow(event) {
              if (!this.visible) {
                  event.preventDefault();
                  event.stopPropagation();
                  this.visible = true;
              }
          },
      },
  };
2b2dc78a   Alexander Misel   Add saturation pr...
512
  </script>