2017-01-17 11:26:02 +08:00
|
|
|
<template>
|
|
|
|
<div :class="classes" @mouseleave="handleMouseleave">
|
2017-01-17 15:30:10 +08:00
|
|
|
<div
|
|
|
|
v-for="item in count"
|
|
|
|
:class="starCls(item)"
|
|
|
|
@mousemove="handleMousemove(item, $event)"
|
|
|
|
@click="handleClick(item)">
|
|
|
|
<span :class="[prefixCls + '-star-content']" type="half"></span>
|
2017-01-17 11:26:02 +08:00
|
|
|
</div>
|
2017-03-02 19:31:21 +08:00
|
|
|
<div :class="[prefixCls + '-text']" v-if="showText" v-show="currentValue > 0">
|
|
|
|
<slot>{{ currentValue }} <template v-if="currentValue <= 1">{{ t('i.rate.star') }}</template><template v-else>{{ t('i.rate.stars') }}</template></slot>
|
2017-01-17 15:49:34 +08:00
|
|
|
</div>
|
2017-01-17 11:26:02 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<script>
|
2017-01-17 15:49:34 +08:00
|
|
|
import Locale from '../../mixins/locale';
|
2017-03-02 19:31:21 +08:00
|
|
|
import Emitter from '../../mixins/emitter';
|
2017-01-17 15:49:34 +08:00
|
|
|
|
2017-01-17 11:26:02 +08:00
|
|
|
const prefixCls = 'ivu-rate';
|
|
|
|
|
|
|
|
export default {
|
2017-03-02 19:31:21 +08:00
|
|
|
mixins: [ Locale, Emitter ],
|
2017-01-17 11:26:02 +08:00
|
|
|
props: {
|
|
|
|
count: {
|
|
|
|
type: Number,
|
|
|
|
default: 5
|
|
|
|
},
|
|
|
|
value: {
|
|
|
|
type: Number,
|
|
|
|
default: 0
|
|
|
|
},
|
|
|
|
allowHalf: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
disabled: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2017-01-17 15:49:34 +08:00
|
|
|
},
|
|
|
|
showText: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2017-01-17 11:26:02 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
prefixCls: prefixCls,
|
2017-01-17 15:30:10 +08:00
|
|
|
hoverIndex: -1,
|
|
|
|
isHover: false,
|
2017-03-02 19:31:21 +08:00
|
|
|
isHalf: false,
|
|
|
|
currentValue: 0
|
2017-01-17 11:26:02 +08:00
|
|
|
};
|
|
|
|
},
|
2017-03-02 19:31:21 +08:00
|
|
|
// created () {
|
|
|
|
// this.currentValue = this.value;
|
|
|
|
// this.setHalf(this.currentValue);
|
|
|
|
// },
|
|
|
|
mounted () {
|
|
|
|
},
|
2017-01-17 11:26:02 +08:00
|
|
|
computed: {
|
|
|
|
classes () {
|
|
|
|
return [
|
|
|
|
`${prefixCls}`,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-disabled`]: this.disabled
|
|
|
|
}
|
2017-01-17 12:04:33 +08:00
|
|
|
];
|
2017-01-17 11:26:02 +08:00
|
|
|
}
|
|
|
|
},
|
2017-01-17 15:30:10 +08:00
|
|
|
watch: {
|
|
|
|
value: {
|
|
|
|
immediate: true,
|
|
|
|
handler (val) {
|
2017-03-02 19:31:21 +08:00
|
|
|
this.currentValue = val;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// valur (val) {
|
|
|
|
// console.log(val);
|
|
|
|
// this.currentValue = val;
|
|
|
|
// console.log(this.currentValue);
|
|
|
|
// },
|
|
|
|
currentValue: {
|
|
|
|
immediate: true,
|
2017-03-03 10:35:38 +08:00
|
|
|
handler () {
|
2017-03-02 19:31:21 +08:00
|
|
|
this.setHalf(this.currentValue);
|
2017-01-17 15:30:10 +08:00
|
|
|
}
|
|
|
|
}
|
2017-03-02 19:31:21 +08:00
|
|
|
// currentValue () {
|
|
|
|
// this.setHalf(this.currentValue);
|
|
|
|
// }
|
2017-01-17 15:30:10 +08:00
|
|
|
},
|
2017-01-17 11:26:02 +08:00
|
|
|
methods: {
|
|
|
|
starCls (value) {
|
|
|
|
const hoverIndex = this.hoverIndex;
|
2017-03-02 19:31:21 +08:00
|
|
|
const currentIndex = this.isHover ? hoverIndex : this.currentValue;
|
2017-01-17 15:30:10 +08:00
|
|
|
|
2017-01-17 11:26:02 +08:00
|
|
|
let full = false;
|
2017-01-17 15:30:10 +08:00
|
|
|
let isLast = false;
|
|
|
|
|
2017-03-02 19:31:21 +08:00
|
|
|
if (currentIndex >= value) full = true;
|
2017-01-17 11:26:02 +08:00
|
|
|
|
2017-01-17 15:30:10 +08:00
|
|
|
if (this.isHover) {
|
2017-03-02 19:31:21 +08:00
|
|
|
isLast = currentIndex === value;
|
2017-01-17 15:30:10 +08:00
|
|
|
} else {
|
2017-03-02 19:31:21 +08:00
|
|
|
isLast = Math.ceil(this.currentValue) === value;
|
2017-01-17 11:26:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
`${prefixCls}-star`,
|
|
|
|
{
|
2017-01-17 15:30:10 +08:00
|
|
|
[`${prefixCls}-star-full`]: (!isLast && full) || (isLast && !this.isHalf),
|
|
|
|
[`${prefixCls}-star-half`]: isLast && this.isHalf,
|
2017-01-17 11:26:02 +08:00
|
|
|
[`${prefixCls}-star-zero`]: !full
|
|
|
|
}
|
2017-01-17 12:04:33 +08:00
|
|
|
];
|
2017-01-17 11:26:02 +08:00
|
|
|
},
|
2017-01-17 15:30:10 +08:00
|
|
|
handleMousemove(value, event) {
|
2017-01-17 11:26:02 +08:00
|
|
|
if (this.disabled) return;
|
|
|
|
|
2017-01-17 15:30:10 +08:00
|
|
|
this.isHover = true;
|
2017-01-17 11:26:02 +08:00
|
|
|
if (this.allowHalf) {
|
2017-01-17 15:30:10 +08:00
|
|
|
const type = event.target.getAttribute('type') || false;
|
|
|
|
this.isHalf = type === 'half';
|
2017-01-17 11:26:02 +08:00
|
|
|
} else {
|
2017-01-17 15:30:10 +08:00
|
|
|
this.isHalf = false;
|
2017-01-17 11:26:02 +08:00
|
|
|
}
|
2017-03-02 19:31:21 +08:00
|
|
|
this.hoverIndex = value;
|
2017-01-17 11:26:02 +08:00
|
|
|
},
|
|
|
|
handleMouseleave () {
|
2017-01-17 15:30:10 +08:00
|
|
|
if (this.disabled) return;
|
|
|
|
|
|
|
|
this.isHover = false;
|
2017-03-02 19:31:21 +08:00
|
|
|
this.setHalf(this.currentValue);
|
2017-01-17 11:26:02 +08:00
|
|
|
this.hoverIndex = -1;
|
|
|
|
},
|
2017-01-17 15:30:10 +08:00
|
|
|
setHalf (val) {
|
|
|
|
this.isHalf = val.toString().indexOf('.') >= 0;
|
|
|
|
},
|
|
|
|
handleClick (value) {
|
|
|
|
if (this.disabled) return;
|
2017-03-02 19:31:21 +08:00
|
|
|
// value++;
|
2017-01-17 15:30:10 +08:00
|
|
|
if (this.isHalf) value -= 0.5;
|
2017-03-02 19:31:21 +08:00
|
|
|
this.currentValue = value;
|
|
|
|
this.$emit('on-change', this.currentValue);
|
|
|
|
this.$emit('input', this.currentValue);
|
|
|
|
// @todo
|
|
|
|
// this.$dispatch('on-form-change', value);
|
2017-01-17 11:26:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|