Blame view

src/components/color-picker/hue.vue 2.85 KB
e7893a68   梁灏   update ColorPicker
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
  <template>
      <div class="ivu-color-picker-hue">
          <div class="ivu-color-picker-hue-container" ref="container"
               @mousedown="handleMouseDown"
               @touchmove="handleChange"
               @touchstart="handleChange">
              <div class="ivu-color-picker-hue-pointer" :style="{top: 0, left: pointerLeft}">
                  <div class="ivu-color-picker-hue-picker"></div>
              </div>
          </div>
      </div>
  </template>
  <script>
      export default {
          name: 'Hue',
          props: {
              value: Object
          },
          data () {
              return {
                  oldHue: 0,
                  pullDirection: ''
              };
          },
          computed: {
              colors () {
                  const h = this.value.hsl.h;
                  if (h !== 0 && h - this.oldHue > 0) this.pullDirection = 'right';
                  if (h !== 0 && h - this.oldHue < 0) this.pullDirection = 'left';
                  this.oldHue = h;
  
                  return this.value;
              },
              pointerLeft () {
                  if (this.colors.hsl.h === 0 && this.pullDirection === 'right') return '100%';
                  return (this.colors.hsl.h * 100) / 360 + '%';
              }
          },
          methods: {
              handleChange (e, skip) {
                  !skip && e.preventDefault();
  
                  const container = this.$refs.container;
                  const containerWidth = container.clientWidth;
  
                  const xOffset = container.getBoundingClientRect().left + window.pageXOffset;
                  const pageX = e.pageX || (e.touches ? e.touches[0].pageX : 0);
                  const left = pageX - xOffset;
  
                  let h;
                  let percent;
  
                  if (left < 0) {
                      h = 0;
                  } else if (left > containerWidth) {
                      h = 360;
                  } else {
                      percent = left * 100 / containerWidth;
                      h = (360 * percent / 100);
                  }
  
                  if (this.colors.hsl.h !== h) {
                      this.$emit('change', {
                          h: h,
                          s: this.colors.hsl.s,
                          l: this.colors.hsl.l,
                          a: this.colors.hsl.a,
                          source: 'hsl'
                      });
                  }
              },
              handleMouseDown (e) {
                  this.handleChange(e, true);
                  window.addEventListener('mousemove', this.handleChange);
                  window.addEventListener('mouseup', this.handleMouseUp);
              },
              handleMouseUp () {
                  this.unbindEventListeners();
              },
              unbindEventListeners () {
                  window.removeEventListener('mousemove', this.handleChange);
                  window.removeEventListener('mouseup', this.handleMouseUp);
              }
          }
      };
  </script>