123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- <template>
- <view
- :id="elId"
- class="tn-rate-class tn-rate"
- @touchmove.stop.prevent="touchMove"
- >
- <view class="tn-rate__wrap" :class="[elClass]" v-for="(item, index) in count" :key="index">
- <view
- class="tn-rate__wrap__icon"
- :class="[`tn-icon-${(allowHalf && halfIcon ? activeIndex > index + 1 : activeIndex > index) ? elActionIcon : elInactionIcon}`]"
- :style="[iconStyle(index)]"
- @tap="click(index + 1, $event)"
- >
-
- <view
- v-if="showHalfIcon(index)"
- class="tn-rate__wrap__icon--half"
- :class="[`tn-icon-${elActionIcon}`]"
- :style="[halfIconStyle]"
- ></view>
- </view>
- </view>
- </view>
- </template>
- <script>
- export default {
- name: 'tn-rate',
- props: {
-
- value: {
- type: Number,
- default: 0
- },
-
- count: {
- type: Number,
- default: 5
- },
-
- minCount: {
- type: Number,
- default: 0
- },
-
- disabled: {
- type: Boolean,
- default: false
- },
-
- allowHalf: {
- type: Boolean,
- default: false
- },
-
- size: {
- type: Number,
- default: 32
- },
-
- activeIcon: {
- type: String,
- default: 'star-fill'
- },
-
- inactiveIcon: {
- type: String,
- default: 'star'
- },
-
- activeColor: {
- type: String,
- default: '#01BEFF'
- },
-
- inactiveColor: {
- type: String,
- default: '#AAAAAA'
- },
-
- gutter: {
- type: Number,
- default: 10
- },
-
- colors: {
- type: Array,
- default() {
- return []
- }
- },
-
- icons: {
- type: Array,
- default() {
- return []
- }
- }
- },
- computed: {
-
- showHalfIcon(index) {
- return index => {
- return this.allowHalf && Math.ceil(this.activeIndex) === index + 1 && this.halfIcon
- }
- },
-
- elActionIcon() {
- const len = this.icons.length
-
-
- if (len && len <= this.count) {
- const step = Math.round(Math.ceil(this.activeIndex) / Math.round(this.count / len))
- if (step < 1) return this.icons[0]
- if (step > len) return this.icons[len - 1]
- return this.icons[step - 1]
- }
- return this.activeIcon
- },
-
- elInactionIcon() {
- const len = this.icons.length
-
-
- if (len && len <= this.count) {
- const step = Math.round(Math.ceil(this.activeIndex) / Math.round(this.count / len))
- if (step < 1) return this.icons[0]
- if (step > len) return this.icons[len - 1]
- return this.icons[step - 1]
- }
- return this.inactiveIcon
- },
-
- elActionColor() {
- const len = this.colors.length
-
-
- if (len && len <= this.count) {
- const step = Math.round(Math.ceil(this.activeIndex) / Math.round(this.count / len))
- if (step < 1) return this.colors[0]
- if (step > len) return this.colors[len - 1]
- return this.colors[step - 1]
- }
- return this.activeColor
- },
-
- iconStyle() {
- return index => {
- let style = {}
-
- style.fontSize = this.$t.string.getLengthUnitValue(this.size)
- style.padding = `0 ${this.$t.string.getLengthUnitValue(this.gutter / 2)}`
-
- if (this.allowHalf && this.halfIcon) {
- style.color = this.activeIndex > index + 1 ? this.elActionColor : this.inactiveColor
- } else {
- style.color = this.activeIndex > index ? this.elActionColor : this.inactiveColor
- }
- return style
- }
- },
-
- halfIconStyle() {
- let style = {}
-
- style.fontSize = this.$t.string.getLengthUnitValue(this.size)
- style.padding = `0 ${this.$t.string.getLengthUnitValue(this.gutter / 2)}`
- style.color = this.elActionColor
- return style
- }
- },
- data() {
- return {
-
- elId: this.$t.uuid(),
- elClass: this.$t.uuid(),
-
- starBoxLeft: 0,
-
- activeIndex: this.value,
-
- starWidth: 0,
-
- starWidthArr: [],
-
- halfIcon: false,
- }
- },
- watch: {
- value(val) {
- this.activeIndex = val
- if (this.allowHalf && (val % 1 === 0.5)) {
- this.halfIcon = true
- } else {
- this.halfIcon = false
- }
- },
- size() {
-
- this.$nextTick(() => {
- this.getElRectById()
- this.getElRectByClass()
- })
- }
- },
- mounted() {
- this.getElRectById()
- this.getElRectByClass()
- },
- methods: {
-
- getElRectById() {
- this._tGetRect('#'+this.elId).then(res => {
- this.starBoxLeft = res.left
- })
- },
-
- getElRectByClass() {
- this._tGetRect('.'+this.elClass).then(res => {
- this.starWidth = res.width
-
- for (let i = 0; i < this.count; i++) {
- this.starWidthArr[i] = (i + 1) * this.starWidth
- }
- })
- },
-
- touchMove(e) {
- if (this.disabled) return
- if (!e.changedTouches[0]) return
-
- const movePageX = e.changedTouches[0].pageX
-
- const distance = movePageX - this.starBoxLeft
-
-
- if (distance <= 0) {
- this.activeIndex = 0
- }
-
-
- let index = Math.ceil(distance / this.starWidth)
- if (this.allowHalf) {
- const iconHalfWidth = this.starWidthArr[index - 1] - (this.starWidth / 2)
- if (distance < iconHalfWidth) {
- this.halfIcon = true
- index -= 0.5
- } else {
- this.halfIcon = false
- }
- }
- this.activeIndex = index > this.count ? this.count : index
-
- if (this.activeIndex < this.minCount) this.activeIndex = this.minCount
-
- this.emitEvent()
- },
-
- click(index, e) {
- if (this.disabled) return
-
- if (this.allowHalf) {
- if (!e.changedTouches[0]) return
- const movePageX = e.changedTouches[0].pageX
-
- const distance = movePageX - this.starBoxLeft
- const iconHalfWidth = this.starWidthArr[index - 1] - (this.starWidth / 2)
- if (distance < iconHalfWidth) {
- this.halfIcon = true
- } else {
- this.halfIcon = false
- }
- }
-
-
- if (index == 1) {
- if (this.allowHalf && this.allowHalf) {
- if ((this.activeIndex === 0.5 && this.halfIcon) ||
- (this.activeIndex === 1 && !this.halfIcon)) {
- this.activeIndex = 0
- } else {
- this.activeIndex = this.halfIcon ? 0.5 : 1
- }
- } else {
- if (this.activeIndex == 1) {
- this.activeIndex = 0
- } else {
- this.activeIndex = 1
- }
- }
- } else {
- this.activeIndex = (this.allowHalf && this.halfIcon) ? index - 0.5 : index
- }
-
- if (this.activeIndex < this.minCount) this.activeIndex = this.minCount
-
- this.emitEvent()
- },
-
- emitEvent() {
- this.$emit('change', this.activeIndex)
-
- this.$emit('input', this.activeIndex)
- }
- }
- }
- </script>
- <style lang="scss" scoped>
-
- .tn-rate {
- display: inline-flex;
- align-items: center;
- margin: 0;
- padding: 0;
-
- &__wrap {
-
- &__icon {
- position: relative;
- box-sizing: border-box;
-
- &--half {
- position: absolute;
- top: 0;
- left: 0;
- display: inline-block;
- overflow: hidden;
- width: 50%;
- }
- }
- }
- }
- </style>
|