Blame view

src/components/rate/rate.vue 2.88 KB
49d380cf   梁灏   init Rate component
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
87
88
89
90
91
92
93
94
95
96
97
98
99
  <template>
      <div :class="classes" @mouseleave="handleMouseleave">
          <div v-for="item in count" :class="starCls(item)">
              <span
                  :class="[prefixCls + '-star-content']"
                  @mousemove="handleMousemove(item, $event)"
                  @click="handleClick(item)"></span>
          </div>
      </div>
  </template>
  <script>
      const prefixCls = 'ivu-rate';
  
      export default {
          props: {
              count: {
                  type: Number,
                  default: 5
              },
              value: {
                  type: Number,
                  default: 0
              },
              allowHalf: {
                  type: Boolean,
                  default: false
              },
              disabled: {
                  type: Boolean,
                  default: false
              }
          },
          data () {
              return {
                  prefixCls: prefixCls,
                  hoverIndex: -1
              };
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-disabled`]: this.disabled
                      }
                  ]
              }
          },
          methods: {
              starCls (value) {
                  const hoverIndex = this.hoverIndex;
                  let full = false;
  
                  if (hoverIndex >= value) {
                      full = true;
                  }
  
                  return [
                      `${prefixCls}-star`,
                      {
                          [`${prefixCls}-star-full`]: full,
                          [`${prefixCls}-star-zero`]: !full
                      }
                  ]
              },
              handleMousemove(value, event) {
                  if (this.disabled) return;
  
                  if (this.allowHalf) {
  //                    let target = event.target;
  //                    if (hasClass(target, 'el-rate__item')) {
  //                        target = target.querySelector('.el-rate__icon');
  //                    }
  //                    if (hasClass(target, 'el-rate__decimal')) {
  //                        target = target.parentNode;
  //                    }
  //                    this.pointerAtLeftHalf = event.offsetX * 2 <= target.clientWidth;
  //                    this.currentValue = this.pointerAtLeftHalf ? value - 0.5 : value;
                  } else {
                      this.currentValue = value;
                  }
                  this.hoverIndex = value;
              },
              handleMouseleave () {
                  if (this.disabled) {
                      return;
                  }
                  if (this.allowHalf) {
  //                    this.pointerAtLeftHalf = this.value !== Math.floor(this.value);
                  }
  //                this.currentValue = this.value;
                  this.hoverIndex = -1;
              },
              handleClick (value) {
  
              }
          }
      };
  </script>