Blame view

src/components/color-picker/hue.vue 2.46 KB
e7893a68   梁灏   update ColorPicker
1
  <template>
f2bcd4ad   Graham Fairweather   Color keyboard co...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
      <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>
e7893a68   梁灏   update ColorPicker
22
23
24
25
              </div>
          </div>
      </div>
  </template>
f2bcd4ad   Graham Fairweather   Color keyboard co...
26
  
e7893a68   梁灏   update ColorPicker
27
  <script>
f2bcd4ad   Graham Fairweather   Color keyboard co...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  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),
          };
      },
  
87ec8082   梁灏   fixed ColorPicker...
51
52
53
54
55
56
      watch: {
          value () {
              this.percent = clamp(this.value.hsl.h * 100 / 360, 0, 100);
          }
      },
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
57
58
59
      methods: {
          change(percent) {
              this.percent = clamp(percent, 0, 100);
e7893a68   梁灏   update ColorPicker
60
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
61
62
63
64
65
              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'});
e7893a68   梁灏   update ColorPicker
66
67
              }
          },
f2bcd4ad   Graham Fairweather   Color keyboard co...
68
69
70
          handleSlide(e, direction) {
              e.preventDefault();
              e.stopPropagation();
e7893a68   梁灏   update ColorPicker
71
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
72
73
74
75
              if (e[this.powerKey]) {
                  this.change(direction < 0 ? 0 : 100);
                  return;
              }
e7893a68   梁灏   update ColorPicker
76
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
77
78
79
80
81
              this.change(this.percent + direction);
          },
          handleChange(e) {
              e.preventDefault();
              e.stopPropagation();
e7893a68   梁灏   update ColorPicker
82
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
83
              const left = this.getLeft(e);
e7893a68   梁灏   update ColorPicker
84
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
85
86
87
88
              if (left < 0) {
                  this.change(0);
                  return;
              }
e7893a68   梁灏   update ColorPicker
89
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
90
91
92
93
94
              const {clientWidth} = this.$refs.container;
  
              if (left > clientWidth) {
                  this.change(100);
                  return;
e7893a68   梁灏   update ColorPicker
95
              }
f2bcd4ad   Graham Fairweather   Color keyboard co...
96
97
98
99
100
101
  
              this.change(left * 100 / clientWidth);
          },
      },
  };
  </script>