2017-02-07 16:11:53 +00:00
|
|
|
<template>
|
2017-09-14 09:28:07 +00:00
|
|
|
<div
|
|
|
|
:class="[
|
|
|
|
'el-color-picker',
|
|
|
|
disabled ? 'is-disabled' : '',
|
|
|
|
size ? `el-color-picker--${ size }` : ''
|
|
|
|
]"
|
|
|
|
v-clickoutside="hide">
|
|
|
|
<div class="el-color-picker__mask" v-if="disabled"></div>
|
|
|
|
<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="{
|
2017-02-22 07:14:54 +00:00
|
|
|
backgroundColor: displayedColor
|
2017-02-07 16:11:53 +00:00
|
|
|
}"></span>
|
2017-02-22 07:14:54 +00:00
|
|
|
<span class="el-color-picker__empty el-icon-close" v-if="!value && !showPanelColor"></span>
|
2017-02-07 16:11:53 +00:00
|
|
|
</span>
|
2017-09-14 09:28:07 +00:00
|
|
|
<span class="el-color-picker__icon el-icon-caret-bottom" v-show="value || showPanelColor"></span>
|
2017-02-07 16:11:53 +00:00
|
|
|
</div>
|
|
|
|
<picker-dropdown
|
|
|
|
ref="dropdown"
|
|
|
|
class="el-color-picker__panel"
|
|
|
|
v-model="showPicker"
|
|
|
|
@pick="confirmValue"
|
|
|
|
@clear="clearValue"
|
|
|
|
:color="color"
|
|
|
|
:show-alpha="showAlpha">
|
|
|
|
</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
|
2017-02-07 16:11:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
directives: { Clickoutside },
|
|
|
|
|
2017-02-22 07:14:54 +00:00
|
|
|
computed: {
|
|
|
|
displayedColor() {
|
|
|
|
if (!this.value && !this.showPanelColor) {
|
|
|
|
return 'transparent';
|
|
|
|
} else {
|
|
|
|
const { r, g, b } = this.color.toRgb();
|
|
|
|
return this.showAlpha
|
|
|
|
? `rgba(${ r }, ${ g }, ${ b }, ${ this.color.get('alpha') / 100 })`
|
|
|
|
: `rgb(${ r }, ${ g }, ${ b })`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-02-07 16:11:53 +00:00
|
|
|
watch: {
|
|
|
|
value(val) {
|
2017-05-04 14:41:47 +00:00
|
|
|
if (!val) {
|
|
|
|
this.showPanelColor = false;
|
|
|
|
} else if (val && val !== this.color.value) {
|
2017-02-07 16:11:53 +00:00
|
|
|
this.color.fromString(val);
|
|
|
|
}
|
2017-02-22 07:14:54 +00:00
|
|
|
},
|
|
|
|
color: {
|
|
|
|
deep: true,
|
|
|
|
handler() {
|
|
|
|
this.showPanelColor = true;
|
|
|
|
}
|
2017-07-10 03:59:08 +00:00
|
|
|
},
|
|
|
|
displayedColor(val) {
|
|
|
|
this.$emit('active-change', val);
|
2017-02-07 16:11:53 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
2017-09-14 09:28:07 +00:00
|
|
|
handleTrigger() {
|
|
|
|
if (this.disabled) return;
|
|
|
|
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);
|
2017-02-22 07:14:54 +00:00
|
|
|
this.showPanelColor = false;
|
2017-02-07 16:11:53 +00:00
|
|
|
this.showPicker = false;
|
2017-02-22 07:14:54 +00:00
|
|
|
this.resetColor();
|
2017-02-07 16:11:53 +00:00
|
|
|
},
|
|
|
|
hide() {
|
|
|
|
this.showPicker = false;
|
2017-02-22 07:14:54 +00:00
|
|
|
this.resetColor();
|
|
|
|
},
|
|
|
|
resetColor() {
|
|
|
|
this.$nextTick(_ => {
|
|
|
|
if (this.value) {
|
|
|
|
this.color.fromString(this.value);
|
|
|
|
} else {
|
|
|
|
this.showPanelColor = false;
|
|
|
|
}
|
|
|
|
});
|
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,
|
2017-02-22 07:14:54 +00:00
|
|
|
showPicker: false,
|
|
|
|
showPanelColor: false
|
2017-02-07 16:11:53 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
components: {
|
|
|
|
PickerDropdown
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|