|
| 1 | +<template> |
| 2 | + <div ref="container" style="width: 100%"></div> |
| 3 | +</template> |
| 4 | + |
| 5 | +<script> |
| 6 | +import mixin from '../widget-mixin' |
| 7 | +
|
| 8 | +export default { |
| 9 | + name: 'oh-colorpicker', |
| 10 | + mixins: [mixin], |
| 11 | + data () { |
| 12 | + return { |
| 13 | + colorpicker: null, |
| 14 | + delayCommand: false, |
| 15 | + delayUpdate: false, |
| 16 | + pendingCommand: null, |
| 17 | + pendingUpdate: null, |
| 18 | + init: false |
| 19 | + } |
| 20 | + }, |
| 21 | + mounted () { |
| 22 | + if (this.color) { |
| 23 | + this.initColorpicker() |
| 24 | + } |
| 25 | + }, |
| 26 | + beforeDestroy () { |
| 27 | + this.colorpicker.destroy() |
| 28 | + }, |
| 29 | + computed: { |
| 30 | + color () { |
| 31 | + const state = this.context.store[this.config.item].state |
| 32 | + if (state.split(',').length === 3) { |
| 33 | + let color = this.context.store[this.config.item].state.split(',') |
| 34 | + color[0] = parseInt(color[0]) |
| 35 | + color[1] = color[1] / 100 |
| 36 | + color[2] = color[2] / 100 |
| 37 | + return color |
| 38 | + } |
| 39 | + return null |
| 40 | + } |
| 41 | + }, |
| 42 | + watch: { |
| 43 | + color (val) { |
| 44 | + if (this.colorpicker) { |
| 45 | + this.updateValue(val) |
| 46 | + } else { |
| 47 | + this.initColorpicker() |
| 48 | + } |
| 49 | + } |
| 50 | + }, |
| 51 | + methods: { |
| 52 | + initColorpicker () { |
| 53 | + const vm = this |
| 54 | + this.colorpicker = this.$f7.colorPicker.create(Object.assign({}, this.config, { |
| 55 | + containerEl: this.$refs.container, |
| 56 | + modules: this.config.modules || ['hsb-sliders'], |
| 57 | + value: { |
| 58 | + hsb: this.color |
| 59 | + }, |
| 60 | + on: { |
| 61 | + change (colorpicker, value) { |
| 62 | + // skip the first update |
| 63 | + if (!vm.init || vm.context.store[vm.config.item].state === '-') { |
| 64 | + vm.init = true |
| 65 | + return |
| 66 | + } |
| 67 | + if (!value.hsb) return |
| 68 | + vm.sendCommand(value.hsb) |
| 69 | + } |
| 70 | + } |
| 71 | + })) |
| 72 | + }, |
| 73 | + sendCommand (hsb) { |
| 74 | + this.pendingUpdate = [...hsb] |
| 75 | + this.pendingCommand = [...hsb] |
| 76 | + const state = this.commandFromHSB(hsb) |
| 77 | + if (state !== this.context.store[this.config.item].state) { |
| 78 | + if (!this.delayCommand) { |
| 79 | + this.delayCommand = true |
| 80 | + this.delayUpdate = true |
| 81 | + this.$store.dispatch('sendCommand', { itemName: this.config.item, cmd: state }) |
| 82 | + setTimeout(() => { |
| 83 | + const pendingCommand = [...this.pendingCommand] |
| 84 | + this.pendingCommand = null |
| 85 | + this.delayCommand = false |
| 86 | + if (pendingCommand != null) { |
| 87 | + this.sendCommand(pendingCommand) |
| 88 | + } |
| 89 | + }, 200) |
| 90 | + } |
| 91 | + setTimeout(() => { |
| 92 | + this.updateValue(this.pendingUpdate) |
| 93 | + this.delayUpdate = false |
| 94 | + }, 2000) |
| 95 | + } |
| 96 | + }, |
| 97 | + commandFromHSB (hsb) { |
| 98 | + let state = [...hsb] |
| 99 | + state[0] = Math.round(state[0]) % 360 |
| 100 | + state[1] = Math.round(state[1] * 100) |
| 101 | + state[2] = Math.round(state[2] * 100) |
| 102 | + state = state.join(',') |
| 103 | + return state |
| 104 | + }, |
| 105 | + updateValue (val) { |
| 106 | + if (!this.delayUpdate) { |
| 107 | + this.colorpicker.setValue({ hsb: this.pendingUpdate || val }) |
| 108 | + this.pendingUpdate = null |
| 109 | + } else { |
| 110 | + this.pendingUpdate = val |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | +</script> |
0 commit comments