app-color-picker.vue 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
<template>
    <div class="app-color-picker">
        <el-input
            v-model="curVal"
            size="small"
            ref="colorPicker"
            :disabled="disabled"
            :placeholder="placeholder"
            >
            <template slot="suffix">
                <el-color-picker ref="picker" v-model="colorValue" @change="colorChange" size="small">
                </el-color-picker>
                <Icon type="md-color-palette" @click="iconClick" />
            </template>
        </el-input>
    </div>
</template>

<script lang="ts">
import { Vue, Component, Watch, Prop, Model } from 'vue-property-decorator';
import CodeListService from '@codelist/codelist-service';
import { Subject, Subscription } from 'rxjs';

@Component({})
export default class AppColorPicker extends Vue {

    /**
     * 双向绑定表单项数据
     * 
     * @type {*}
     * @memberof AppColorPicker
     */
    @Model('change') public value: any;

    /**
     * 表单数据
     * 
     * @type {*}
     * @memberof AppColorPicker
     */
    @Prop() public data: any;

    /**
     * 表单通讯对象
     * 
     * @type {*}
     * @memberof AppColorPicker
     */
    @Prop() public formState?: Subject<any>;

    /**
     * 禁用状态
     * 
     * @type {*}
     * @memberof AppColorPicker
     */
    @Prop({default: false}) public disabled?: boolean;

    /**
     * 占位提示
     * 
     * @type {*}
     * @memberof AppColorPicker
     */
    @Prop() public placeholder?: string;

    /**
     * 上下文
     * 
     * @type {*}
     * @memberof AppColorPicker
     */
    @Prop() public context: any;

    /**
     * 视图参数
     * 
     * @type {*}
     * @memberof AppColorPicker
     */
    @Prop() public viewparam: any;

    /**
     * 颜色对应字段值
     * 
     * @type {*}
     * @memberof AppColorPicker
     */
    @Prop() public color: any;

    /**
     * 双向绑定颜色
     * 
     * @type {*}
     * @memberof AppColorPicker
     */
    public colorValue: any = null;

    /**
     * 获取输入框值
     * 
     * @type {*}
     * @memberof AppColorPicker
     */
    get curVal() {
        return this.value;
    }

    /**
     * 设置值
     * 
     * @type {*}
     * @memberof AppColorPicker
     */
    set curVal(val: any) {
        this.$emit('change', val);
    }

    /**
     * Vue生命周期
     * 
     * @memberof AppColorPicker
     */
    public created() {
        this.handleData();
    }

    /**
     * 数据处理
     * 
     * @memberof AppColorPicker
     */
    @Watch('value')
    public handleData() {
        if(!this.value && !this.color) {
            return;
        }
        this.colorValue = this.data[this.color];
        this.curVal = this.value;
        this.handleInputColor(this.colorValue);
    }

    /**
     * 数据处理
     * 
     * @memberof AppColorPicker
     */
    public colorChange(color: any) {
        this.handleInputColor(color);
        this.$emit('colorChange', { name: this.color, value: color });
    }

    /**
     * 设置输入框字体颜色
     * 
     * @memberof AppColorPicker
     */
    public handleInputColor(color: any) {
        let picker: any = this.$refs.colorPicker;
        if(picker) {
            let child: any = picker.$el.children[0];
            child.style.color = color;
        }
    }

    /**
     * 模拟点击事件
     * 
     * @memberof AppColorPicker
     */
    public iconClick() {
        let picker: any = this.$refs.picker;
        let e: any = document.createEvent('MouseEvent');
        e.initEvent('click', true, true);
        if(picker) {
            picker.$el.children[0].dispatchEvent(e);
        }
    }
}
</script>

182 183
<style lang="scss">
@import './app-color-picker.scss';
184
</style>