iview/src/components/color-picker/hue.vue

102 lines
2.5 KiB
Vue
Raw Normal View History

2017-08-17 17:25:38 +08:00
<template>
2018-05-15 14:26:58 +02:00
<div
:class="[prefixCls + '-hue']"
tabindex="0"
@click="$el.focus()"
@keydown.esc="handleEscape"
@keydown.left="handleLeft"
@keydown.right="handleRight"
@keydown.up="handleUp"
@keydown.down="handleDown"
>
<div
ref="container"
:class="[prefixCls + '-hue-container']"
@mousedown="handleMouseDown"
@touchmove="handleChange"
@touchstart="handleChange">
<div
:style="{top: 0, left: `${percent}%`}"
:class="[prefixCls + '-hue-pointer']">
<div :class="[prefixCls + '-hue-picker']"></div>
2017-08-17 17:25:38 +08:00
</div>
</div>
</div>
</template>
2018-05-15 14:26:58 +02:00
2017-08-17 17:25:38 +08:00
<script>
2018-05-15 14:26:58 +02:00
import HASMixin from './hsaMixin';
import Prefixes from './prefixMixin';
import {clamp} from './utils';
export default {
name: 'Hue',
mixins: [HASMixin, Prefixes],
data() {
const normalStep = 1 / 360 * 25;
const jumpStep = 20 * normalStep;
return {
left: -normalStep,
right: normalStep,
up: jumpStep,
down: -jumpStep,
powerKey: 'shiftKey',
percent: clamp(this.value.hsl.h * 100 / 360, 0, 100),
};
},
watch: {
value () {
this.percent = clamp(this.value.hsl.h * 100 / 360, 0, 100);
}
},
2018-05-15 14:26:58 +02:00
methods: {
change(percent) {
this.percent = clamp(percent, 0, 100);
2017-08-17 17:25:38 +08:00
2018-05-15 14:26:58 +02:00
const {h, s, l, a} = this.value.hsl;
const newHue = clamp(percent / 100 * 360, 0, 360);
if (h !== newHue) {
this.$emit('change', {h: newHue, s, l, a, source: 'hsl'});
2017-08-17 17:25:38 +08:00
}
},
2018-05-15 14:26:58 +02:00
handleSlide(e, direction) {
e.preventDefault();
e.stopPropagation();
2017-08-17 17:25:38 +08:00
2018-05-15 14:26:58 +02:00
if (e[this.powerKey]) {
this.change(direction < 0 ? 0 : 100);
return;
}
2017-08-17 17:25:38 +08:00
2018-05-15 14:26:58 +02:00
this.change(this.percent + direction);
},
handleChange(e) {
e.preventDefault();
e.stopPropagation();
2017-08-17 17:25:38 +08:00
2018-05-15 14:26:58 +02:00
const left = this.getLeft(e);
2017-08-17 17:25:38 +08:00
2018-05-15 14:26:58 +02:00
if (left < 0) {
this.change(0);
return;
}
2017-08-17 17:25:38 +08:00
2018-05-15 14:26:58 +02:00
const {clientWidth} = this.$refs.container;
if (left > clientWidth) {
this.change(100);
return;
2017-08-17 17:25:38 +08:00
}
2018-05-15 14:26:58 +02:00
this.change(left * 100 / clientWidth);
},
},
};
</script>