Blame view

src/components/rate/rate.vue 4.19 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
49d380cf   梁灏   init Rate component
48
49
50
51
52
              }
          },
          data () {
              return {
                  prefixCls: prefixCls,
962c40bd   梁灏   update Rate
53
54
                  hoverIndex: -1,
                  isHover: false,
b324bb4d   梁灏   fixed #1761
55
                  isHalf: this.allowHalf && this.value.toString().indexOf('.') >= 0,
e2c6ff2b   梁灏   update Rate
56
                  currentValue: this.value
49d380cf   梁灏   init Rate component
57
58
              };
          },
49d380cf   梁灏   init Rate component
59
60
61
62
63
64
65
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-disabled`]: this.disabled
                      }
fa0241a5   梁灏   fixed #212
66
                  ];
49d380cf   梁灏   init Rate component
67
68
              }
          },
962c40bd   梁灏   update Rate
69
          watch: {
e2c6ff2b   梁灏   update Rate
70
71
              value (val) {
                  this.currentValue = val;
6aa72722   huixisheng   Support rate
72
              },
e2c6ff2b   梁灏   update Rate
73
74
              currentValue (val) {
                  this.setHalf(val);
962c40bd   梁灏   update Rate
75
              }
962c40bd   梁灏   update Rate
76
          },
49d380cf   梁灏   init Rate component
77
78
79
          methods: {
              starCls (value) {
                  const hoverIndex = this.hoverIndex;
6aa72722   huixisheng   Support rate
80
                  const currentIndex = this.isHover ? hoverIndex : this.currentValue;
962c40bd   梁灏   update Rate
81
  
49d380cf   梁灏   init Rate component
82
                  let full = false;
962c40bd   梁灏   update Rate
83
84
                  let isLast = false;
  
6aa72722   huixisheng   Support rate
85
                  if (currentIndex >= value) full = true;
49d380cf   梁灏   init Rate component
86
  
962c40bd   梁灏   update Rate
87
                  if (this.isHover) {
6aa72722   huixisheng   Support rate
88
                      isLast = currentIndex === value;
962c40bd   梁灏   update Rate
89
                  } else {
6aa72722   huixisheng   Support rate
90
                      isLast = Math.ceil(this.currentValue) === value;
49d380cf   梁灏   init Rate component
91
92
93
94
95
                  }
  
                  return [
                      `${prefixCls}-star`,
                      {
962c40bd   梁灏   update Rate
96
97
                          [`${prefixCls}-star-full`]: (!isLast && full) || (isLast && !this.isHalf),
                          [`${prefixCls}-star-half`]: isLast && this.isHalf,
49d380cf   梁灏   init Rate component
98
99
                          [`${prefixCls}-star-zero`]: !full
                      }
fa0241a5   梁灏   fixed #212
100
                  ];
49d380cf   梁灏   init Rate component
101
              },
962c40bd   梁灏   update Rate
102
              handleMousemove(value, event) {
49d380cf   梁灏   init Rate component
103
104
                  if (this.disabled) return;
  
962c40bd   梁灏   update Rate
105
                  this.isHover = true;
49d380cf   梁灏   init Rate component
106
                  if (this.allowHalf) {
962c40bd   梁灏   update Rate
107
108
                      const type = event.target.getAttribute('type') || false;
                      this.isHalf = type === 'half';
49d380cf   梁灏   init Rate component
109
                  } else {
962c40bd   梁灏   update Rate
110
                      this.isHalf = false;
49d380cf   梁灏   init Rate component
111
                  }
6aa72722   huixisheng   Support rate
112
                  this.hoverIndex = value;
49d380cf   梁灏   init Rate component
113
114
              },
              handleMouseleave () {
962c40bd   梁灏   update Rate
115
116
117
                  if (this.disabled) return;
  
                  this.isHover = false;
6aa72722   huixisheng   Support rate
118
                  this.setHalf(this.currentValue);
49d380cf   梁灏   init Rate component
119
120
                  this.hoverIndex = -1;
              },
962c40bd   梁灏   update Rate
121
              setHalf (val) {
77e4c204   梁灏   fixed #1761
122
                  this.isHalf = this.allowHalf && val.toString().indexOf('.') >= 0;
962c40bd   梁灏   update Rate
123
124
125
              },
              handleClick (value) {
                  if (this.disabled) return;
e2c6ff2b   梁灏   update Rate
126
  //                 value++;
962c40bd   梁灏   update Rate
127
                  if (this.isHalf) value -= 0.5;
6aa72722   huixisheng   Support rate
128
                  this.currentValue = value;
e2c6ff2b   梁灏   update Rate
129
130
                  this.$emit('input', value);
                  this.$emit('on-change', value);
cd78c9c4   梁灏   some comps suppor...
131
                  this.dispatch('FormItem', 'on-form-change', value);
49d380cf   梁灏   init Rate component
132
133
134
              }
          }
      };
b5e1430d   Sergio Crisostomo   Fix rating when i...
135
  </script>