element/packages/color-picker/src/main.vue

180 lines
4.4 KiB
Vue
Raw Normal View History

2017-02-07 16:11:53 +00:00
<template>
2017-09-14 09:28:07 +00:00
<div
:class="[
'el-color-picker',
2018-01-29 09:54:38 +00:00
colorDisabled ? 'is-disabled' : '',
2017-10-12 09:50:06 +00:00
colorSize ? `el-color-picker--${ colorSize }` : ''
2017-09-14 09:28:07 +00:00
]"
v-clickoutside="hide">
2018-01-29 09:54:38 +00:00
<div class="el-color-picker__mask" v-if="colorDisabled"></div>
2017-09-14 09:28:07 +00:00
<div class="el-color-picker__trigger" @click="handleTrigger">
2017-02-07 16:11:53 +00:00
<span class="el-color-picker__color" :class="{ 'is-alpha': showAlpha }">
<span class="el-color-picker__color-inner"
:style="{
backgroundColor: displayedColor
2017-02-07 16:11:53 +00:00
}"></span>
<span class="el-color-picker__empty el-icon-close" v-if="!value && !showPanelColor"></span>
2017-02-07 16:11:53 +00:00
</span>
<span class="el-color-picker__icon el-icon-arrow-down" v-show="value || showPanelColor"></span>
2017-02-07 16:11:53 +00:00
</div>
<picker-dropdown
ref="dropdown"
:class="['el-color-picker__panel', popperClass || '']"
2017-02-07 16:11:53 +00:00
v-model="showPicker"
@pick="confirmValue"
@clear="clearValue"
:color="color"
:show-alpha="showAlpha"
:predefine="predefine">
2017-02-07 16:11:53 +00:00
</picker-dropdown>
</div>
</template>
<script>
import Color from './color';
import PickerDropdown from './components/picker-dropdown.vue';
import Clickoutside from 'element-ui/src/utils/clickoutside';
export default {
name: 'ElColorPicker',
props: {
2017-09-14 09:28:07 +00:00
value: String,
showAlpha: Boolean,
colorFormat: String,
disabled: Boolean,
size: String,
popperClass: String,
predefine: Array
2017-02-07 16:11:53 +00:00
},
inject: {
2018-01-29 09:54:38 +00:00
elForm: {
default: ''
},
elFormItem: {
default: ''
}
},
2017-10-12 09:50:06 +00:00
2017-02-07 16:11:53 +00:00
directives: { Clickoutside },
computed: {
displayedColor() {
if (!this.value && !this.showPanelColor) {
return 'transparent';
}
return this.displayedRgb(this.color, this.showAlpha);
2017-10-12 09:50:06 +00:00
},
_elFormItemSize() {
return (this.elFormItem || {}).elFormItemSize;
},
colorSize() {
return this.size || this._elFormItemSize || (this.$ELEMENT || {}).size;
2018-01-29 09:54:38 +00:00
},
colorDisabled() {
return this.disabled || (this.elForm || {}).disabled;
}
},
2017-02-07 16:11:53 +00:00
watch: {
value(val) {
if (!val) {
this.showPanelColor = false;
} else if (val && val !== this.color.value) {
2017-02-07 16:11:53 +00:00
this.color.fromString(val);
}
},
color: {
deep: true,
handler() {
this.showPanelColor = true;
}
2017-07-10 03:59:08 +00:00
},
displayedColor(val) {
if (!this.showPicker) return;
const currentValueColor = new Color({
enableAlpha: this.showAlpha,
format: this.colorFormat
});
currentValueColor.fromString(this.value);
const currentValueColorRgb = this.displayedRgb(currentValueColor, this.showAlpha);
if (val !== currentValueColorRgb) {
this.$emit('active-change', val);
}
2017-02-07 16:11:53 +00:00
}
},
methods: {
2017-09-14 09:28:07 +00:00
handleTrigger() {
2018-01-29 09:54:38 +00:00
if (this.colorDisabled) return;
2017-09-14 09:28:07 +00:00
this.showPicker = !this.showPicker;
},
2017-02-07 16:11:53 +00:00
confirmValue(value) {
this.$emit('input', this.color.value);
2017-02-24 19:32:39 +00:00
this.$emit('change', this.color.value);
2017-02-07 16:11:53 +00:00
this.showPicker = false;
},
clearValue() {
this.$emit('input', null);
2017-02-24 19:32:39 +00:00
this.$emit('change', null);
this.showPanelColor = false;
2017-02-07 16:11:53 +00:00
this.showPicker = false;
this.resetColor();
2017-02-07 16:11:53 +00:00
},
hide() {
this.showPicker = false;
this.resetColor();
},
resetColor() {
this.$nextTick(_ => {
if (this.value) {
this.color.fromString(this.value);
} else {
this.showPanelColor = false;
}
});
},
displayedRgb(color, showAlpha) {
if (!(color instanceof Color)) {
throw Error('color should be instance of Color Class');
}
const { r, g, b } = color.toRgb();
return showAlpha
? `rgba(${ r }, ${ g }, ${ b }, ${ color.get('alpha') / 100 })`
: `rgb(${ r }, ${ g }, ${ b })`;
2017-02-07 16:11:53 +00:00
}
},
mounted() {
const value = this.value;
if (value) {
this.color.fromString(value);
}
this.popperElm = this.$refs.dropdown.$el;
},
data() {
const color = new Color({
enableAlpha: this.showAlpha,
format: this.colorFormat
});
return {
color,
showPicker: false,
showPanelColor: false
2017-02-07 16:11:53 +00:00
};
},
components: {
PickerDropdown
}
};
</script>