parsley/app/javascript/components/AppRating.vue

136 lines
2.7 KiB
Vue
Raw Normal View History

2018-04-03 10:29:57 -05:00
<template>
<span ref="wrapper" class="rating" @click="handleClick" @mousemove="handleMousemove" @mouseleave="handleMouseleave">
<span class="set empty-set">
2018-09-12 17:17:15 -05:00
<app-iconic-icon v-for="i in starCount" :key="i" icon="star-empty" size="md"></app-iconic-icon>
2018-04-03 10:29:57 -05:00
</span>
<span class="set filled-set" :style="filledStyle">
2018-09-12 17:17:15 -05:00
<app-iconic-icon v-for="i in starCount" :key="i" icon="star" size="md"></app-iconic-icon>
2018-04-03 10:29:57 -05:00
</span>
</span>
</template>
<script>
export default {
props: {
starCount: {
required: false,
type: Number,
default: 5
},
2018-04-13 23:32:34 -05:00
readonly: {
required: false,
type: Boolean,
default: false
},
step: {
required: false,
type: Number,
default: 0.5
},
value: {
2018-04-03 10:29:57 -05:00
required: false,
type: Number,
default: 0
}
},
data() {
return {
2018-04-13 23:32:34 -05:00
temporaryValue: null
2018-04-03 10:29:57 -05:00
};
},
computed: {
ratingPercent() {
2018-04-13 23:32:34 -05:00
return ((this.value || 0) / this.starCount) * 100.0;
},
temporaryPercent() {
if (this.temporaryValue !== null) {
return (this.temporaryValue / this.starCount) * 100.0;
} else {
return null;
}
2018-04-03 10:29:57 -05:00
},
filledStyle() {
2018-04-13 23:32:34 -05:00
const width = this.temporaryPercent || this.ratingPercent;
2018-04-03 10:29:57 -05:00
return {
width: width + "%"
};
}
},
methods: {
handleClick(evt) {
2018-04-13 23:32:34 -05:00
if (this.temporaryValue !== null) {
this.$emit("input", this.temporaryValue);
}
2018-04-03 10:29:57 -05:00
},
handleMousemove(evt) {
2018-04-13 23:32:34 -05:00
if (this.readonly) {
return;
}
2018-04-03 10:29:57 -05:00
const wrapperBox = this.$refs.wrapper.getBoundingClientRect();
const wrapperWidth = wrapperBox.right - wrapperBox.left;
const mousePosition = evt.clientX;
if (mousePosition > wrapperBox.left && mousePosition < wrapperBox.right) {
2018-04-13 23:32:34 -05:00
const filledRatio = ((mousePosition - wrapperBox.left) / wrapperWidth);
const totalSteps = this.starCount / this.step;
const filledSteps = Math.round(totalSteps * filledRatio);
this.temporaryValue = filledSteps * this.step;
2018-04-03 10:29:57 -05:00
}
},
handleMouseleave(evt) {
2018-04-13 23:32:34 -05:00
this.temporaryValue = null;
2018-04-03 10:29:57 -05:00
}
},
components: {
}
}
</script>
<style lang="scss" scoped>
2018-04-04 19:46:02 -05:00
@import "../styles/variables";
2018-04-03 10:29:57 -05:00
span.rating {
position: relative;
display: inline-block;
.set {
white-space: nowrap;
2018-09-12 17:17:15 -05:00
svg.iconic {
width: 1.5em;
height: 1.5em;
}
2018-04-03 10:29:57 -05:00
}
.empty-set {
color: gray;
}
.filled-set {
2018-04-04 19:46:02 -05:00
color: $yellow;
2018-04-03 10:29:57 -05:00
position: absolute;
top: 0;
left: 0;
overflow-x: hidden;
}
}
</style>