c6faec44
梁灏
init ColorPicker
|
1
|
<template>
|
93a5f34f
梁灏
update ColorPicker
|
2
|
<Dropdown ref="picker" trigger="click" :transfer="transfer" :placement="placement" @on-visible-change="handleToggleVisible">
|
b6bda1dc
梁灏
update ColorPicker
|
3
4
5
|
<div :class="wrapClasses">
<i class="ivu-icon ivu-icon-arrow-down-b ivu-input-icon ivu-input-icon-normal"></i>
<div :class="inputClasses">
|
0aefe4aa
梁灏
update ColorPicker
|
6
|
<div :class="[prefixCls + '-color']">
|
c0fa72ca
梁灏
update ColorPicker
|
7
8
9
10
|
<div :class="[prefixCls + '-color-empty']" v-if="value === '' && !visible">
<Icon type="ios-close-empty"></Icon>
</div>
<div v-else :style="{backgroundColor: displayedColor}"></div>
|
0aefe4aa
梁灏
update ColorPicker
|
11
|
</div>
|
b6bda1dc
梁灏
update ColorPicker
|
12
13
14
|
</div>
</div>
<Dropdown-menu slot="list">
|
9af2f01c
梁灏
update ColorPicker
|
15
|
<div :class="[prefixCls + '-picker']">
|
e7893a68
梁灏
update ColorPicker
|
16
17
18
|
<div :class="[prefixCls + '-picker-panel']">
<Saturation v-model="saturationColors" @change="childChange"></Saturation>
</div>
|
9af2f01c
梁灏
update ColorPicker
|
19
|
<div :class="[prefixCls + '-picker-hue-slider']">
|
e7893a68
梁灏
update ColorPicker
|
20
|
<Hue v-model="saturationColors" @change="childChange"></Hue>
|
9af2f01c
梁灏
update ColorPicker
|
21
22
|
</div>
<div v-if="alpha" :class="[prefixCls + '-picker-alpha-slider']">
|
e7893a68
梁灏
update ColorPicker
|
23
|
<Alpha v-model="saturationColors" @change="childChange"></Alpha>
|
9af2f01c
梁灏
update ColorPicker
|
24
25
|
</div>
<recommend-colors v-if="colors.length" :list="colors" :class="[prefixCls + '-picker-colors']"></recommend-colors>
|
dab39476
梁灏
update ColorPicker
|
26
|
<recommend-colors v-if="!colors.length && recommend" :list="recommendedColor" :class="[prefixCls + '-picker-colors']"></recommend-colors>
|
0aefe4aa
梁灏
update ColorPicker
|
27
|
<Confirm @on-pick-success="handleSuccess" @on-pick-clear="handleClear"></Confirm>
|
b6bda1dc
梁灏
update ColorPicker
|
28
29
30
|
</div>
</Dropdown-menu>
</Dropdown>
|
c6faec44
梁灏
init ColorPicker
|
31
32
|
</template>
<script>
|
e7893a68
梁灏
update ColorPicker
|
33
34
|
import tinycolor from 'tinycolor2';
|
c0fa72ca
梁灏
update ColorPicker
|
35
|
import Icon from '../icon/icon.vue';
|
b6bda1dc
梁灏
update ColorPicker
|
36
37
|
import Dropdown from '../dropdown/dropdown.vue';
import DropdownMenu from '../dropdown/dropdown-menu.vue';
|
9af2f01c
梁灏
update ColorPicker
|
38
39
|
import RecommendColors from './recommend-colors.vue';
import Confirm from '../date-picker/base/confirm.vue';
|
e7893a68
梁灏
update ColorPicker
|
40
41
42
43
44
|
import Saturation from './saturation.vue';
import Hue from './hue.vue';
import Alpha from './alpha.vue';
|
b6bda1dc
梁灏
update ColorPicker
|
45
46
47
48
49
|
import { oneOf } from '../../utils/assist';
const prefixCls = 'ivu-color-picker';
const inputPrefixCls = 'ivu-input';
|
e7893a68
梁灏
update ColorPicker
|
50
|
function _colorChange (data, oldHue) {
|
c0fa72ca
梁灏
update ColorPicker
|
51
|
data = data === '' ? '#ff0000' : data;
|
e7893a68
梁灏
update ColorPicker
|
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
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
|
99
|
export default {
|
b6bda1dc
梁灏
update ColorPicker
|
100
|
name: 'ColorPicker',
|
c0fa72ca
梁灏
update ColorPicker
|
101
|
components: { Icon, Dropdown, DropdownMenu, Confirm, RecommendColors, Saturation, Hue, Alpha },
|
b6bda1dc
梁灏
update ColorPicker
|
102
103
|
props: {
value: {
|
0aefe4aa
梁灏
update ColorPicker
|
104
|
type: String
|
b6bda1dc
梁灏
update ColorPicker
|
105
106
107
108
109
|
},
alpha: {
type: Boolean,
default: false
},
|
9af2f01c
梁灏
update ColorPicker
|
110
111
112
113
|
recommend: {
type: Boolean,
default: false
},
|
b6bda1dc
梁灏
update ColorPicker
|
114
115
116
117
118
|
format: {
validator (value) {
return oneOf(value, ['hsl', 'hsv', 'hex', 'rgb']);
}
},
|
9af2f01c
梁灏
update ColorPicker
|
119
120
121
122
123
124
|
colors: {
type: Array,
default () {
return [];
}
},
|
b6bda1dc
梁灏
update ColorPicker
|
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
disabled: {
type: Boolean,
default: false
},
size: {
validator (value) {
return oneOf(value, ['small', 'large', 'default']);
}
},
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
}
},
|
c6faec44
梁灏
init ColorPicker
|
145
|
data () {
|
b6bda1dc
梁灏
update ColorPicker
|
146
|
return {
|
e7893a68
梁灏
update ColorPicker
|
147
|
val: _colorChange(this.value),
|
b6bda1dc
梁灏
update ColorPicker
|
148
|
prefixCls: prefixCls,
|
0aefe4aa
梁灏
update ColorPicker
|
149
|
visible: false,
|
9af2f01c
梁灏
update ColorPicker
|
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
recommendedColor: [
'#2d8cf0',
'#19be6b',
'#ff9900',
'#ed3f14',
'#00b5ff',
'#19c919',
'#f9e31c',
'#ea1a1a',
'#9b1dea',
'#00c2b1',
'#ac7a33',
'#1d35ea',
'#42bd82',
'#f16b62',
'#ea4ca3',
'#0d94aa',
'#febd79',
'#3b90fc',
'#000000',
'#ffffff'
]
|
b6bda1dc
梁灏
update ColorPicker
|
172
173
174
|
};
},
computed: {
|
e7893a68
梁灏
update ColorPicker
|
175
176
177
178
179
180
|
saturationColors: {
get () {
return this.val;
},
set (newVal) {
this.val = newVal;
|
e7893a68
梁灏
update ColorPicker
|
181
182
|
}
},
|
b6bda1dc
梁灏
update ColorPicker
|
183
184
185
|
wrapClasses () {
return [
`${prefixCls}-rel`,
|
9673dcb0
梁灏
update ColorPicker
|
186
|
`${prefixCls}-${this.size}`,
|
b6bda1dc
梁灏
update ColorPicker
|
187
188
189
190
191
192
193
194
195
196
197
198
199
|
`${inputPrefixCls}-wrapper`,
`${inputPrefixCls}-wrapper-${this.size}`
];
},
inputClasses () {
return [
`${prefixCls}-input`,
`${inputPrefixCls}`,
`${inputPrefixCls}-${this.size}`,
{
[`${inputPrefixCls}-disabled`]: this.disabled
}
];
|
0aefe4aa
梁灏
update ColorPicker
|
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
},
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})`;
|
b6bda1dc
梁灏
update ColorPicker
|
215
|
}
|
c6faec44
梁灏
init ColorPicker
|
216
|
},
|
e7893a68
梁灏
update ColorPicker
|
217
218
219
|
watch: {
value (newVal) {
this.val = _colorChange(newVal);
|
0aefe4aa
梁灏
update ColorPicker
|
220
221
222
|
},
visible () {
this.val = _colorChange(this.value);
|
e7893a68
梁灏
update ColorPicker
|
223
224
|
}
},
|
b6bda1dc
梁灏
update ColorPicker
|
225
|
methods: {
|
e7893a68
梁灏
update ColorPicker
|
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
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
|
240
|
|
e7893a68
梁灏
update ColorPicker
|
241
242
243
244
245
246
247
248
249
250
251
252
253
|
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
|
254
255
256
257
|
},
handleToggleVisible (visible) {
this.visible = visible;
},
|
93a5f34f
梁灏
update ColorPicker
|
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
getFormatColor () {
const value = this.saturationColors;
let color;
if (this.format) {
if (this.format === 'hsl') {
color = tinycolor(value.hsl).toHslString();
} else if (this.format === 'hsv') {
color = tinycolor(value.hsv).toHsvString();
}
} else if (this.alpha) {
color = `rgba(${value.rgba.r}, ${value.rgba.g}, ${value.rgba.b}, ${value.rgba.a})`;
} else {
color = value.hex;
}
return color;
},
|
0aefe4aa
梁灏
update ColorPicker
|
274
|
handleSuccess () {
|
93a5f34f
梁灏
update ColorPicker
|
275
276
|
this.$emit('input', this.getFormatColor());
this.$refs.picker.handleClose();
|
0aefe4aa
梁灏
update ColorPicker
|
277
278
279
|
},
handleClear () {
this.$emit('input', '');
|
93a5f34f
梁灏
update ColorPicker
|
280
|
this.$refs.picker.handleClose();
|
e7893a68
梁灏
update ColorPicker
|
281
|
}
|
b6bda1dc
梁灏
update ColorPicker
|
282
|
}
|
c6faec44
梁灏
init ColorPicker
|
283
284
|
};
</script>
|