Blame view

src/components/rate/rate.vue 4.03 KB
49d380cf   梁灏   init Rate component
1
2
  <template>
      <div :class="classes" @mouseleave="handleMouseleave">
962c40bd   梁灏   update Rate
3
4
5
6
7
8
          <div
              v-for="item in count"
              :class="starCls(item)"
              @mousemove="handleMousemove(item, $event)"
              @click="handleClick(item)">
              <span :class="[prefixCls + '-star-content']" type="half"></span>
49d380cf   梁灏   init Rate component
9
          </div>
6aa72722   huixisheng   Support rate
10
          <div :class="[prefixCls + '-text']" v-if="showText" v-show="currentValue > 0">
9212149e   梁灏   update Rate
11
              <slot><span>{{ currentValue }}</span> <span v-if="currentValue <= 1">{{ t('i.rate.star') }}</span><span v-else>{{ t('i.rate.stars') }}</span></slot>
e5ac7925   梁灏   update Rate
12
          </div>
49d380cf   梁灏   init Rate component
13
14
15
      </div>
  </template>
  <script>
e5ac7925   梁灏   update Rate
16
      import Locale from '../../mixins/locale';
6aa72722   huixisheng   Support rate
17
      import Emitter from '../../mixins/emitter';
e5ac7925   梁灏   update Rate
18
  
49d380cf   梁灏   init Rate component
19
20
21
      const prefixCls = 'ivu-rate';
  
      export default {
6aa72722   huixisheng   Support rate
22
          mixins: [ Locale, Emitter ],
49d380cf   梁灏   init Rate component
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
          props: {
              count: {
                  type: Number,
                  default: 5
              },
              value: {
                  type: Number,
                  default: 0
              },
              allowHalf: {
                  type: Boolean,
                  default: false
              },
              disabled: {
                  type: Boolean,
                  default: false
e5ac7925   梁灏   update Rate
39
40
41
42
              },
              showText: {
                  type: Boolean,
                  default: false
49d380cf   梁灏   init Rate component
43
44
45
46
47
              }
          },
          data () {
              return {
                  prefixCls: prefixCls,
962c40bd   梁灏   update Rate
48
49
                  hoverIndex: -1,
                  isHover: false,
b324bb4d   梁灏   fixed #1761
50
                  isHalf: this.allowHalf && this.value.toString().indexOf('.') >= 0,
e2c6ff2b   梁灏   update Rate
51
                  currentValue: this.value
49d380cf   梁灏   init Rate component
52
53
              };
          },
49d380cf   梁灏   init Rate component
54
55
56
57
58
59
60
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-disabled`]: this.disabled
                      }
fa0241a5   梁灏   fixed #212
61
                  ];
49d380cf   梁灏   init Rate component
62
63
              }
          },
962c40bd   梁灏   update Rate
64
          watch: {
e2c6ff2b   梁灏   update Rate
65
66
              value (val) {
                  this.currentValue = val;
6aa72722   huixisheng   Support rate
67
              },
e2c6ff2b   梁灏   update Rate
68
69
              currentValue (val) {
                  this.setHalf(val);
962c40bd   梁灏   update Rate
70
              }
962c40bd   梁灏   update Rate
71
          },
49d380cf   梁灏   init Rate component
72
73
74
          methods: {
              starCls (value) {
                  const hoverIndex = this.hoverIndex;
6aa72722   huixisheng   Support rate
75
                  const currentIndex = this.isHover ? hoverIndex : this.currentValue;
962c40bd   梁灏   update Rate
76
  
49d380cf   梁灏   init Rate component
77
                  let full = false;
962c40bd   梁灏   update Rate
78
79
                  let isLast = false;
  
6aa72722   huixisheng   Support rate
80
                  if (currentIndex >= value) full = true;
49d380cf   梁灏   init Rate component
81
  
962c40bd   梁灏   update Rate
82
                  if (this.isHover) {
6aa72722   huixisheng   Support rate
83
                      isLast = currentIndex === value;
962c40bd   梁灏   update Rate
84
                  } else {
6aa72722   huixisheng   Support rate
85
                      isLast = Math.ceil(this.currentValue) === value;
49d380cf   梁灏   init Rate component
86
87
88
89
90
                  }
  
                  return [
                      `${prefixCls}-star`,
                      {
962c40bd   梁灏   update Rate
91
92
                          [`${prefixCls}-star-full`]: (!isLast && full) || (isLast && !this.isHalf),
                          [`${prefixCls}-star-half`]: isLast && this.isHalf,
49d380cf   梁灏   init Rate component
93
94
                          [`${prefixCls}-star-zero`]: !full
                      }
fa0241a5   梁灏   fixed #212
95
                  ];
49d380cf   梁灏   init Rate component
96
              },
962c40bd   梁灏   update Rate
97
              handleMousemove(value, event) {
49d380cf   梁灏   init Rate component
98
99
                  if (this.disabled) return;
  
962c40bd   梁灏   update Rate
100
                  this.isHover = true;
49d380cf   梁灏   init Rate component
101
                  if (this.allowHalf) {
962c40bd   梁灏   update Rate
102
103
                      const type = event.target.getAttribute('type') || false;
                      this.isHalf = type === 'half';
49d380cf   梁灏   init Rate component
104
                  } else {
962c40bd   梁灏   update Rate
105
                      this.isHalf = false;
49d380cf   梁灏   init Rate component
106
                  }
6aa72722   huixisheng   Support rate
107
                  this.hoverIndex = value;
49d380cf   梁灏   init Rate component
108
109
              },
              handleMouseleave () {
962c40bd   梁灏   update Rate
110
111
112
                  if (this.disabled) return;
  
                  this.isHover = false;
6aa72722   huixisheng   Support rate
113
                  this.setHalf(this.currentValue);
49d380cf   梁灏   init Rate component
114
115
                  this.hoverIndex = -1;
              },
962c40bd   梁灏   update Rate
116
117
118
119
120
              setHalf (val) {
                  this.isHalf = val.toString().indexOf('.') >= 0;
              },
              handleClick (value) {
                  if (this.disabled) return;
e2c6ff2b   梁灏   update Rate
121
  //                 value++;
962c40bd   梁灏   update Rate
122
                  if (this.isHalf) value -= 0.5;
6aa72722   huixisheng   Support rate
123
                  this.currentValue = value;
e2c6ff2b   梁灏   update Rate
124
125
                  this.$emit('input', value);
                  this.$emit('on-change', value);
cd78c9c4   梁灏   some comps suppor...
126
                  this.dispatch('FormItem', 'on-form-change', value);
49d380cf   梁灏   init Rate component
127
128
129
              }
          }
      };
b5e1430d   Sergio Crisostomo   Fix rating when i...
130
  </script>