Blame view

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