Blame view

src/components/color-picker/color-picker.vue 13.2 KB
c6faec44   梁灏   init ColorPicker
1
  <template>
4109bbc6   梁灏   update ColorPicker
2
3
      <div :class="classes" v-clickoutside="handleClose">
          <div ref="reference" @click="toggleVisible" :class="wrapClasses">
0460a1e8   梁灏   fixed #812
4
              <input type="hidden" :name="name" :value="currentValue">
b6bda1dc   梁灏   update ColorPicker
5
6
              <i class="ivu-icon ivu-icon-arrow-down-b ivu-input-icon ivu-input-icon-normal"></i>
              <div :class="inputClasses">
0aefe4aa   梁灏   update ColorPicker
7
                  <div :class="[prefixCls + '-color']">
1f61a559   梁灏   update ColorPicker
8
                      <div :class="[prefixCls + '-color-empty']" v-show="value === '' && !visible">
4109bbc6   梁灏   update ColorPicker
9
                          <i class="ivu-icon ivu-icon-ios-close-empty"></i>
c0fa72ca   梁灏   update ColorPicker
10
                      </div>
1f61a559   梁灏   update ColorPicker
11
                      <div v-show="value || visible" :style="{backgroundColor: displayedColor}"></div>
0aefe4aa   梁灏   update ColorPicker
12
                  </div>
b6bda1dc   梁灏   update ColorPicker
13
14
              </div>
          </div>
4109bbc6   梁灏   update ColorPicker
15
16
17
18
19
20
21
22
23
24
25
26
          <transition :name="transition">
              <Drop
                  v-show="visible"
                  @click.native="handleTransferClick"
                  :class="{ [prefixCls + '-transfer']: transfer }"
                  class-name="ivu-transfer-no-max-height"
                  :placement="placement"
                  ref="drop"
                  :data-transfer="transfer"
                  v-transfer-dom>
                  <div :class="[prefixCls + '-picker']">
                      <div :class="[prefixCls + '-picker-wrapper']">
2b2dc78a   Alexander Misel   Add saturation pr...
27
                          <div v-if="saturation" :class="[prefixCls + '-picker-panel']">
4109bbc6   梁灏   update ColorPicker
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
                              <Saturation v-model="saturationColors" @change="childChange"></Saturation>
                          </div>
                          <div :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>
77950c30   梁灏   update ColorPicker
46
                      </div>
4109bbc6   梁灏   update ColorPicker
47
48
49
                      <div :class="[prefixCls + '-confirm']">
                          <span :class="[prefixCls + '-confirm-color']">{{ formatColor }}</span>
                          <Confirm @on-pick-success="handleSuccess" @on-pick-clear="handleClear"></Confirm>
77950c30   梁灏   update ColorPicker
50
                      </div>
59dc2df0   梁灏   update ColorPicker
51
                  </div>
4109bbc6   梁灏   update ColorPicker
52
53
54
              </Drop>
          </transition>
      </div>
c6faec44   梁灏   init ColorPicker
55
56
  </template>
  <script>
e7893a68   梁灏   update ColorPicker
57
58
      import tinycolor from 'tinycolor2';
  
4109bbc6   梁灏   update ColorPicker
59
60
61
62
      import clickoutside from '../../directives/clickoutside';
      import TransferDom from '../../directives/transfer-dom';
  
      import Drop from '../../components/select/dropdown.vue';
9af2f01c   梁灏   update ColorPicker
63
64
      import RecommendColors from './recommend-colors.vue';
      import Confirm from '../date-picker/base/confirm.vue';
e7893a68   梁灏   update ColorPicker
65
66
67
68
      import Saturation from './saturation.vue';
      import Hue from './hue.vue';
      import Alpha from './alpha.vue';
  
b6bda1dc   梁灏   update ColorPicker
69
      import { oneOf } from '../../utils/assist';
2baba209   梁灏   update ColorPicker
70
      import Emitter from '../../mixins/emitter';
b6bda1dc   梁灏   update ColorPicker
71
72
73
74
  
      const prefixCls = 'ivu-color-picker';
      const inputPrefixCls = 'ivu-input';
  
e7893a68   梁灏   update ColorPicker
75
      function _colorChange (data, oldHue) {
59dc2df0   梁灏   update ColorPicker
76
          data = data === '' ? '#2d8cf0' : data;
e7893a68   梁灏   update ColorPicker
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
          const alpha = data && data.a;
          let color;
  
          // hsl is better than hex between conversions
          if (data && data.hsl) {
              color = tinycolor(data.hsl);
          } else if (data && data.hex && data.hex.length > 0) {
              color = tinycolor(data.hex);
          } else {
              color = tinycolor(data);
          }
  
          if (color && (color._a === undefined || color._a === null)) {
              color.setAlpha(alpha || 1);
          }
  
          const hsl = color.toHsl();
          const hsv = color.toHsv();
  
          if (hsl.s === 0) {
              hsv.h = hsl.h = data.h || (data.hsl && data.hsl.h) || oldHue || 0;
          }
  
          // when the hsv.v is less than 0.0164 (base on test)
          // because of possible loss of precision
          // the result of hue and saturation would be miscalculated
          if (hsv.v < 0.0164) {
              hsv.h = data.h || (data.hsv && data.hsv.h) || 0;
              hsv.s = data.s || (data.hsv && data.hsv.s) || 0;
          }
  
          if (hsl.l < 0.01) {
              hsl.h = data.h || (data.hsl && data.hsl.h) || 0;
              hsl.s = data.s || (data.hsl && data.hsl.s) || 0;
          }
  
          return {
              hsl: hsl,
              hex: color.toHexString().toUpperCase(),
              rgba: color.toRgb(),
              hsv: hsv,
              oldHue: data.h || oldHue || hsl.h,
              source: data.source,
              a: data.a || color.getAlpha()
          };
      }
  
c6faec44   梁灏   init ColorPicker
124
      export default {
b6bda1dc   梁灏   update ColorPicker
125
          name: 'ColorPicker',
2baba209   梁灏   update ColorPicker
126
          mixins: [ Emitter ],
4109bbc6   梁灏   update ColorPicker
127
128
          components: { Drop, Confirm, RecommendColors, Saturation, Hue, Alpha },
          directives: { clickoutside, TransferDom },
b6bda1dc   梁灏   update ColorPicker
129
130
          props: {
              value: {
0aefe4aa   梁灏   update ColorPicker
131
                  type: String
b6bda1dc   梁灏   update ColorPicker
132
              },
2b2dc78a   Alexander Misel   Add saturation pr...
133
134
135
136
              saturation: {
                  type: Boolean,
                  default: true
              },
b6bda1dc   梁灏   update ColorPicker
137
138
139
140
              alpha: {
                  type: Boolean,
                  default: false
              },
9af2f01c   梁灏   update ColorPicker
141
142
143
144
              recommend: {
                  type: Boolean,
                  default: false
              },
b6bda1dc   梁灏   update ColorPicker
145
146
147
148
149
              format: {
                  validator (value) {
                      return oneOf(value, ['hsl', 'hsv', 'hex', 'rgb']);
                  }
              },
9af2f01c   梁灏   update ColorPicker
150
151
152
153
154
155
              colors: {
                  type: Array,
                  default () {
                      return [];
                  }
              },
b6bda1dc   梁灏   update ColorPicker
156
157
158
159
160
161
162
              disabled: {
                  type: Boolean,
                  default: false
              },
              size: {
                  validator (value) {
                      return oneOf(value, ['small', 'large', 'default']);
4109bbc6   梁灏   update ColorPicker
163
164
                  },
                  default: 'default'
b6bda1dc   梁灏   update ColorPicker
165
166
167
168
169
170
171
172
173
174
              },
              placement: {
                  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']);
                  },
                  default: 'bottom'
              },
              transfer: {
                  type: Boolean,
                  default: false
0460a1e8   梁灏   fixed #812
175
176
177
              },
              name: {
                  type: String
b6bda1dc   梁灏   update ColorPicker
178
179
              }
          },
c6faec44   梁灏   init ColorPicker
180
          data () {
b6bda1dc   梁灏   update ColorPicker
181
              return {
e7893a68   梁灏   update ColorPicker
182
                  val: _colorChange(this.value),
0460a1e8   梁灏   fixed #812
183
                  currentValue: this.value,
b6bda1dc   梁灏   update ColorPicker
184
                  prefixCls: prefixCls,
0aefe4aa   梁灏   update ColorPicker
185
                  visible: false,
4109bbc6   梁灏   update ColorPicker
186
                  disableCloseUnderTransfer: false,  // transfer 模式下,点击Drop也会触发关闭
9af2f01c   梁灏   update ColorPicker
187
188
189
190
191
192
193
194
195
196
197
198
199
                  recommendedColor: [
                      '#2d8cf0',
                      '#19be6b',
                      '#ff9900',
                      '#ed3f14',
                      '#00b5ff',
                      '#19c919',
                      '#f9e31c',
                      '#ea1a1a',
                      '#9b1dea',
                      '#00c2b1',
                      '#ac7a33',
                      '#1d35ea',
59dc2df0   梁灏   update ColorPicker
200
                      '#8bc34a',
9af2f01c   梁灏   update ColorPicker
201
202
203
204
                      '#f16b62',
                      '#ea4ca3',
                      '#0d94aa',
                      '#febd79',
59dc2df0   梁灏   update ColorPicker
205
206
207
208
209
                      '#5d4037',
                      '#00bcd4',
                      '#f06292',
                      '#cddc39',
                      '#607d8b',
9af2f01c   梁灏   update ColorPicker
210
211
212
                      '#000000',
                      '#ffffff'
                  ]
b6bda1dc   梁灏   update ColorPicker
213
214
215
              };
          },
          computed: {
4109bbc6   梁灏   update ColorPicker
216
217
218
219
220
221
222
              transition () {
                  if (this.placement === 'bottom-start' || this.placement === 'bottom' || this.placement === 'bottom-end') {
                      return 'slide-up';
                  } else {
                      return 'fade';
                  }
              },
e7893a68   梁灏   update ColorPicker
223
224
225
226
227
228
              saturationColors: {
                  get () {
                      return this.val;
                  },
                  set (newVal) {
                      this.val = newVal;
8dab987e   梁灏   update ColorPicker
229
                      this.$emit('on-active-change', this.formatColor);
e7893a68   梁灏   update ColorPicker
230
231
                  }
              },
8105945f   梁灏   update ColorPicker
232
233
234
235
236
237
238
239
              classes () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-transfer`]: this.transfer
                      }
                  ];
              },
b6bda1dc   梁灏   update ColorPicker
240
241
242
              wrapClasses () {
                  return [
                      `${prefixCls}-rel`,
9673dcb0   梁灏   update ColorPicker
243
                      `${prefixCls}-${this.size}`,
b6bda1dc   梁灏   update ColorPicker
244
245
246
247
248
249
250
251
252
253
254
255
256
                      `${inputPrefixCls}-wrapper`,
                      `${inputPrefixCls}-wrapper-${this.size}`
                  ];
              },
              inputClasses () {
                  return [
                      `${prefixCls}-input`,
                      `${inputPrefixCls}`,
                      `${inputPrefixCls}-${this.size}`,
                      {
                          [`${inputPrefixCls}-disabled`]: this.disabled
                      }
                  ];
0aefe4aa   梁灏   update ColorPicker
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
              },
              displayedColor () {
                  let color;
                  if (this.visible) {
                      const rgba = this.saturationColors.rgba;
                      color = {
                          r: rgba.r,
                          g: rgba.g,
                          b: rgba.b,
                          a: rgba.a
                      };
                  } else {
                      color = tinycolor(this.value).toRgb();
                  }
                  return `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a})`;
59dc2df0   梁灏   update ColorPicker
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
              },
              formatColor () {
                  const value = this.saturationColors;
                  const format = this.format;
                  let color;
  
                  const rgba = `rgba(${value.rgba.r}, ${value.rgba.g}, ${value.rgba.b}, ${value.rgba.a})`;
                  if (format) {
                      if (format === 'hsl') {
                          color = tinycolor(value.hsl).toHslString();
                      } else if (format === 'hsv') {
                          color = tinycolor(value.hsv).toHsvString();
                      } else if (format === 'hex') {
                          color = value.hex;
                      } else if (format === 'rgb') {
                          color = rgba;
                      }
                  } else if (this.alpha) {
                      color = rgba;
                  } else {
                      color = value.hex;
                  }
                  return color;
b6bda1dc   梁灏   update ColorPicker
295
              }
c6faec44   梁灏   init ColorPicker
296
          },
e7893a68   梁灏   update ColorPicker
297
298
299
          watch: {
              value (newVal) {
                  this.val = _colorChange(newVal);
0aefe4aa   梁灏   update ColorPicker
300
              },
4109bbc6   梁灏   update ColorPicker
301
              visible (val) {
0aefe4aa   梁灏   update ColorPicker
302
                  this.val = _colorChange(this.value);
4109bbc6   梁灏   update ColorPicker
303
304
305
306
307
                  if (val) {
                      this.$refs.drop.update();
                  } else {
                      this.$refs.drop.destroy();
                  }
e7893a68   梁灏   update ColorPicker
308
309
              }
          },
b6bda1dc   梁灏   update ColorPicker
310
          methods: {
4109bbc6   梁灏   update ColorPicker
311
312
313
314
315
316
317
318
319
320
321
322
323
324
              // 开启 transfer 时,点击 Drop 即会关闭,这里不让其关闭
              handleTransferClick () {
                  if (this.transfer) this.disableCloseUnderTransfer = true;
              },
              handleClose () {
                  if (this.disableCloseUnderTransfer) {
                      this.disableCloseUnderTransfer = false;
                      return false;
                  }
                  this.visible = false;
              },
              toggleVisible () {
                  this.visible = !this.visible;
              },
e7893a68   梁灏   update ColorPicker
325
326
327
328
329
330
331
332
333
334
335
336
337
338
              childChange (data) {
                  this.colorChange(data);
              },
              colorChange (data, oldHue) {
                  this.oldHue = this.saturationColors.hsl.h;
                  this.saturationColors = _colorChange(data, oldHue || this.oldHue);
              },
              isValidHex (hex) {
                  return tinycolor(hex).isValid();
              },
              simpleCheckForValidColor (data) {
                  const keysToCheck = ['r', 'g', 'b', 'a', 'h', 's', 'l', 'v'];
                  let checked = 0;
                  let passed = 0;
b6bda1dc   梁灏   update ColorPicker
339
  
e7893a68   梁灏   update ColorPicker
340
341
342
343
344
345
346
347
348
349
350
351
352
                  for (let i = 0; i < keysToCheck.length; i++) {
                      const letter = keysToCheck[i];
                      if (data[letter]) {
                          checked++;
                          if (!isNaN(data[letter])) {
                              passed++;
                          }
                      }
                  }
  
                  if (checked === passed) {
                      return data;
                  }
0aefe4aa   梁灏   update ColorPicker
353
              },
0aefe4aa   梁灏   update ColorPicker
354
              handleSuccess () {
59dc2df0   梁灏   update ColorPicker
355
                  const color = this.formatColor;
0460a1e8   梁灏   fixed #812
356
                  this.currentValue = color;
59dc2df0   梁灏   update ColorPicker
357
358
                  this.$emit('input', color);
                  this.$emit('on-change', color);
2baba209   梁灏   update ColorPicker
359
                  this.dispatch('FormItem', 'on-form-change', color);
4109bbc6   梁灏   update ColorPicker
360
                  this.handleClose();
0aefe4aa   梁灏   update ColorPicker
361
362
              },
              handleClear () {
0460a1e8   梁灏   fixed #812
363
                  this.currentValue = '';
0aefe4aa   梁灏   update ColorPicker
364
                  this.$emit('input', '');
8dab987e   梁灏   update ColorPicker
365
                  this.$emit('on-change', '');
2baba209   梁灏   update ColorPicker
366
                  this.dispatch('FormItem', 'on-form-change', '');
4109bbc6   梁灏   update ColorPicker
367
                  this.handleClose();
1f61a559   梁灏   update ColorPicker
368
369
370
              },
              handleSelectColor (color) {
                  this.val = _colorChange(color);
e7893a68   梁灏   update ColorPicker
371
              }
b6bda1dc   梁灏   update ColorPicker
372
          }
c6faec44   梁灏   init ColorPicker
373
      };
2b2dc78a   Alexander Misel   Add saturation pr...
374
  </script>