Blame view

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