2020-06-15 11:46:52 +02:00
|
|
|
<template>
|
|
|
|
<div class="color-picker-container">
|
2021-08-19 16:20:40 +02:00
|
|
|
<datalist :id="colorListID">
|
|
|
|
<option v-for="color in defaultColors" :key="color" :value="color" />
|
|
|
|
</datalist>
|
|
|
|
|
|
|
|
<div class="picker">
|
|
|
|
<input
|
|
|
|
class="picker__input"
|
|
|
|
type="color"
|
|
|
|
v-model="color"
|
|
|
|
:list="colorListID"
|
|
|
|
:class="{'is-empty': isEmpty}"
|
|
|
|
/>
|
|
|
|
<svg class="picker__pattern" v-show="isEmpty" viewBox="0 0 22 22" fill="fff">
|
|
|
|
<pattern id="checker" width="11" height="11" patternUnits="userSpaceOnUse" fill="FFF">
|
|
|
|
<rect fill="#cccccc" x="0" width="5.5" height="5.5" y="0"></rect>
|
|
|
|
<rect fill="#cccccc" x="5.5" width="5.5" height="5.5" y="5.5"></rect>
|
|
|
|
</pattern>
|
|
|
|
<rect width="22" height="22" fill="url(#checker)"></rect>
|
|
|
|
</svg>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<x-button :disabled="isEmpty" @click="reset" class="is-small ml-2" :shadow="false" type="secondary">
|
2021-06-24 01:24:57 +02:00
|
|
|
{{ $t('input.resetColor') }}
|
2021-01-17 18:57:57 +01:00
|
|
|
</x-button>
|
2020-06-15 11:46:52 +02:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2021-08-19 16:20:40 +02:00
|
|
|
const DEFAULT_COLORS = [
|
|
|
|
'#1973ff',
|
|
|
|
'#7F23FF',
|
|
|
|
'#ff4136',
|
|
|
|
'#ff851b',
|
|
|
|
'#ffeb10',
|
|
|
|
'#00db60',
|
|
|
|
]
|
|
|
|
|
|
|
|
function createRandomID() {
|
|
|
|
const ID_LENGTH = 9
|
|
|
|
return Math.random().toString(36).substr(2, ID_LENGTH)
|
|
|
|
}
|
2020-06-15 11:46:52 +02:00
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
export default {
|
|
|
|
name: 'colorPicker',
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
color: '',
|
|
|
|
lastChangeTimeout: null,
|
2021-08-19 16:20:40 +02:00
|
|
|
defaultColors: DEFAULT_COLORS,
|
|
|
|
colorListID: createRandomID(),
|
2020-09-05 22:35:52 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
value: {
|
|
|
|
required: true,
|
2020-06-15 11:46:52 +02:00
|
|
|
},
|
2020-09-05 22:35:52 +02:00
|
|
|
menuPosition: {
|
|
|
|
type: String,
|
|
|
|
default: 'top',
|
2020-06-15 11:46:52 +02:00
|
|
|
},
|
2020-09-05 22:35:52 +02:00
|
|
|
},
|
|
|
|
watch: {
|
2021-09-08 11:59:38 +02:00
|
|
|
value: {
|
|
|
|
handler(value) {
|
|
|
|
this.color = value
|
|
|
|
},
|
|
|
|
immediate: true,
|
2020-06-15 11:46:52 +02:00
|
|
|
},
|
2020-09-05 22:35:52 +02:00
|
|
|
color() {
|
|
|
|
this.update()
|
2020-06-15 11:46:52 +02:00
|
|
|
},
|
2020-09-05 22:35:52 +02:00
|
|
|
},
|
2021-01-23 18:41:13 +01:00
|
|
|
computed: {
|
2021-08-19 16:20:40 +02:00
|
|
|
isEmpty() {
|
2021-01-23 18:41:13 +01:00
|
|
|
return this.color === '#000000' || this.color === ''
|
|
|
|
},
|
|
|
|
},
|
2020-09-05 22:35:52 +02:00
|
|
|
methods: {
|
2021-01-31 12:19:34 +01:00
|
|
|
update(force = false) {
|
2020-06-21 20:27:39 +02:00
|
|
|
|
2021-08-19 16:20:40 +02:00
|
|
|
if(this.isEmpty && !force) {
|
2021-01-23 18:41:13 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
if (this.lastChangeTimeout !== null) {
|
|
|
|
clearTimeout(this.lastChangeTimeout)
|
|
|
|
}
|
2020-06-21 20:27:39 +02:00
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
this.lastChangeTimeout = setTimeout(() => {
|
|
|
|
this.$emit('input', this.color)
|
|
|
|
this.$emit('change')
|
|
|
|
}, 500)
|
|
|
|
},
|
|
|
|
reset() {
|
|
|
|
// FIXME: I havn't found a way to make it clear to the user the color war reset.
|
|
|
|
// Not sure if verte is capable of this - it does not show the change when setting this.color = ''
|
|
|
|
this.color = ''
|
2021-01-31 12:19:34 +01:00
|
|
|
this.update(true)
|
2020-06-15 11:46:52 +02:00
|
|
|
},
|
2020-09-05 22:35:52 +02:00
|
|
|
},
|
|
|
|
}
|
2021-08-19 16:20:40 +02:00
|
|
|
</script>
|