Blame view

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