Blame view

src/components/rate/rate.vue 6.08 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
          <div
              v-for="item in count"
              :class="starCls(item)"
              @mousemove="handleMousemove(item, $event)"
552408d5   wqzwh   feature: rate组件增加...
8
              :key="item"
962c40bd   梁灏   update Rate
9
              @click="handleClick(item)">
b9ecbd53   梁灏   update Rate #3853
10
              <template v-if="!showCharacter">
552408d5   wqzwh   feature: rate组件增加...
11
                  <span :class="[prefixCls + '-star-content']" type="half"></span>
b9ecbd53   梁灏   update Rate #3853
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
              </template>
              <template v-else>
                  <template v-if="character !== ''">
                      <span :class="[prefixCls + '-star-first']" type="half">{{ character }}</span>
                      <span :class="[prefixCls + '-star-second']">{{ character }}</span>
                  </template>
                  <template v-else>
                      <span :class="[prefixCls + '-star-first']" type="half">
                          <i :class="iconClasses" type="half"></i>
                      </span>
                      <span :class="[prefixCls + '-star-second']">
                          <i :class="iconClasses"></i>
                      </span>
                  </template>
              </template>
49d380cf   梁灏   init Rate component
27
          </div>
6aa72722   huixisheng   Support rate
28
          <div :class="[prefixCls + '-text']" v-if="showText" v-show="currentValue > 0">
9212149e   梁灏   update Rate
29
              <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
30
          </div>
49d380cf   梁灏   init Rate component
31
32
33
      </div>
  </template>
  <script>
e5ac7925   梁灏   update Rate
34
      import Locale from '../../mixins/locale';
6aa72722   huixisheng   Support rate
35
      import Emitter from '../../mixins/emitter';
e5ac7925   梁灏   update Rate
36
  
b9ecbd53   梁灏   update Rate #3853
37
38
      import Icon from '../icon/icon.vue';
  
49d380cf   梁灏   init Rate component
39
40
41
      const prefixCls = 'ivu-rate';
  
      export default {
0460a1e8   梁灏   fixed #812
42
          name: 'Rate',
6aa72722   huixisheng   Support rate
43
          mixins: [ Locale, Emitter ],
b9ecbd53   梁灏   update Rate #3853
44
          components: { Icon },
49d380cf   梁灏   init Rate component
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
          props: {
              count: {
                  type: Number,
                  default: 5
              },
              value: {
                  type: Number,
                  default: 0
              },
              allowHalf: {
                  type: Boolean,
                  default: false
              },
              disabled: {
                  type: Boolean,
                  default: false
e5ac7925   梁灏   update Rate
61
62
63
64
              },
              showText: {
                  type: Boolean,
                  default: false
0460a1e8   梁灏   fixed #812
65
66
67
              },
              name: {
                  type: String
1137640c   xiaofengsha   rate组件添加allowClea...
68
              },
e9528eed   Aresn   保持命名统一,将 allowCle...
69
              clearable: {
1137640c   xiaofengsha   rate组件添加allowClea...
70
71
                  type: Boolean,
                  default: false
552408d5   wqzwh   feature: rate组件增加...
72
73
74
75
              },
              character: {
                  type: String,
                  default: ''
b9ecbd53   梁灏   update Rate #3853
76
77
78
79
80
81
82
83
              },
              icon: {
                  type: String,
                  default: ''
              },
              customIcon: {
                  type: String,
                  default: ''
49d380cf   梁灏   init Rate component
84
85
86
87
88
              }
          },
          data () {
              return {
                  prefixCls: prefixCls,
962c40bd   梁灏   update Rate
89
90
                  hoverIndex: -1,
                  isHover: false,
b324bb4d   梁灏   fixed #1761
91
                  isHalf: this.allowHalf && this.value.toString().indexOf('.') >= 0,
e2c6ff2b   梁灏   update Rate
92
                  currentValue: this.value
49d380cf   梁灏   init Rate component
93
94
              };
          },
49d380cf   梁灏   init Rate component
95
96
97
98
99
100
101
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-disabled`]: this.disabled
                      }
fa0241a5   梁灏   fixed #212
102
                  ];
b9ecbd53   梁灏   update Rate #3853
103
104
105
106
107
108
109
110
111
112
113
114
              },
              iconClasses () {
                  return [
                      `ivu-icon`,
                      {
                          [`ivu-icon-${this.icon}`]: this.icon !== '',
                          [`${this.customIcon}`]: this.customIcon !== '',
                      }
                  ];
              },
              showCharacter () {
                  return this.character !== '' || this.icon !== '' || this.customIcon !== '';
49d380cf   梁灏   init Rate component
115
116
              }
          },
962c40bd   梁灏   update Rate
117
          watch: {
e2c6ff2b   梁灏   update Rate
118
119
              value (val) {
                  this.currentValue = val;
6aa72722   huixisheng   Support rate
120
              },
e2c6ff2b   梁灏   update Rate
121
122
              currentValue (val) {
                  this.setHalf(val);
962c40bd   梁灏   update Rate
123
              }
962c40bd   梁灏   update Rate
124
          },
49d380cf   梁灏   init Rate component
125
126
127
          methods: {
              starCls (value) {
                  const hoverIndex = this.hoverIndex;
6aa72722   huixisheng   Support rate
128
                  const currentIndex = this.isHover ? hoverIndex : this.currentValue;
962c40bd   梁灏   update Rate
129
  
49d380cf   梁灏   init Rate component
130
                  let full = false;
962c40bd   梁灏   update Rate
131
132
                  let isLast = false;
  
6aa72722   huixisheng   Support rate
133
                  if (currentIndex >= value) full = true;
49d380cf   梁灏   init Rate component
134
  
962c40bd   梁灏   update Rate
135
                  if (this.isHover) {
6aa72722   huixisheng   Support rate
136
                      isLast = currentIndex === value;
962c40bd   梁灏   update Rate
137
                  } else {
6aa72722   huixisheng   Support rate
138
                      isLast = Math.ceil(this.currentValue) === value;
49d380cf   梁灏   init Rate component
139
140
141
                  }
  
                  return [
552408d5   wqzwh   feature: rate组件增加...
142
                      {   
b9ecbd53   梁灏   update Rate #3853
143
144
                          [`${prefixCls}-star`]: !this.showCharacter,
                          [`${prefixCls}-star-chart`]: this.showCharacter,
962c40bd   梁灏   update Rate
145
146
                          [`${prefixCls}-star-full`]: (!isLast && full) || (isLast && !this.isHalf),
                          [`${prefixCls}-star-half`]: isLast && this.isHalf,
49d380cf   梁灏   init Rate component
147
148
                          [`${prefixCls}-star-zero`]: !full
                      }
fa0241a5   梁灏   fixed #212
149
                  ];
49d380cf   梁灏   init Rate component
150
              },
962c40bd   梁灏   update Rate
151
              handleMousemove(value, event) {
49d380cf   梁灏   init Rate component
152
153
                  if (this.disabled) return;
  
962c40bd   梁灏   update Rate
154
                  this.isHover = true;
49d380cf   梁灏   init Rate component
155
                  if (this.allowHalf) {
962c40bd   梁灏   update Rate
156
157
                      const type = event.target.getAttribute('type') || false;
                      this.isHalf = type === 'half';
49d380cf   梁灏   init Rate component
158
                  } else {
962c40bd   梁灏   update Rate
159
                      this.isHalf = false;
49d380cf   梁灏   init Rate component
160
                  }
6aa72722   huixisheng   Support rate
161
                  this.hoverIndex = value;
49d380cf   梁灏   init Rate component
162
163
              },
              handleMouseleave () {
962c40bd   梁灏   update Rate
164
165
166
                  if (this.disabled) return;
  
                  this.isHover = false;
6aa72722   huixisheng   Support rate
167
                  this.setHalf(this.currentValue);
49d380cf   梁灏   init Rate component
168
169
                  this.hoverIndex = -1;
              },
962c40bd   梁灏   update Rate
170
              setHalf (val) {
77e4c204   梁灏   fixed #1761
171
                  this.isHalf = this.allowHalf && val.toString().indexOf('.') >= 0;
962c40bd   梁灏   update Rate
172
173
174
              },
              handleClick (value) {
                  if (this.disabled) return;
1137640c   xiaofengsha   rate组件添加allowClea...
175
                  //value++;
962c40bd   梁灏   update Rate
176
                  if (this.isHalf) value -= 0.5;
1137640c   xiaofengsha   rate组件添加allowClea...
177
  
e9528eed   Aresn   保持命名统一,将 allowCle...
178
                  if(this.clearable && Math.abs(value - this.currentValue) < 0.01) {
1137640c   xiaofengsha   rate组件添加allowClea...
179
180
181
                      value = 0;
                  }
  
6aa72722   huixisheng   Support rate
182
                  this.currentValue = value;
e2c6ff2b   梁灏   update Rate
183
184
                  this.$emit('input', value);
                  this.$emit('on-change', value);
cd78c9c4   梁灏   some comps suppor...
185
                  this.dispatch('FormItem', 'on-form-change', value);
49d380cf   梁灏   init Rate component
186
187
188
              }
          }
      };
b5e1430d   Sergio Crisostomo   Fix rating when i...
189
  </script>