parsley/app/javascript/components/AppRating.vue

124 lines
2.6 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>
2024-10-01 09:32:09 -05:00
<script setup>
import { computed, ref, useTemplateRef } from "vue";
const emit = defineEmits(["update:modelValue"]);
const props = defineProps({
starCount: {
required: false,
type: Number,
default: 5
},
readonly: {
required: false,
type: Boolean,
default: false
},
step: {
required: false,
type: Number,
default: 0.5
},
modelValue: {
required: false,
type: Number,
default: 0
}
});
2018-04-03 10:29:57 -05:00
2024-10-01 09:32:09 -05:00
const temporaryValue = ref(null);
const ratingPercent = computed(() => ((props.modelValue || 0) / props.starCount) * 100.0);
const wrapperEl = useTemplateRef("wrapper");
2018-04-13 23:32:34 -05:00
2024-10-01 09:32:09 -05:00
const temporaryPercent = computed(() => {
if (temporaryValue.value !== null) {
return (temporaryValue.value / props.starCount) * 100.0;
} else {
return null;
}
});
const filledStyle = computed(() => {
const width = temporaryPercent.value || ratingPercent.value;
return {
width: width + "%"
};
});
function handleClick(evt) {
if (temporaryValue.value !== null) {
emit("update:modelValue", temporaryValue.value);
}
}
2018-04-03 10:29:57 -05:00
2024-10-01 09:32:09 -05:00
function handleMousemove(evt) {
if (props.readonly) {
return;
}
2018-04-13 23:32:34 -05:00
2024-10-01 09:32:09 -05:00
const wrapperBox = wrapperEl.value.getBoundingClientRect();
const wrapperWidth = wrapperBox.right - wrapperBox.left;
const mousePosition = evt.clientX;
2018-04-13 23:32:34 -05:00
2024-10-01 09:32:09 -05:00
if (mousePosition > wrapperBox.left && mousePosition < wrapperBox.right) {
const filledRatio = ((mousePosition - wrapperBox.left) / wrapperWidth);
2018-04-03 10:29:57 -05:00
2024-10-01 09:32:09 -05:00
const totalSteps = props.starCount / props.step;
const filledSteps = Math.round(totalSteps * filledRatio);
2018-04-03 10:29:57 -05:00
2024-10-01 09:32:09 -05:00
temporaryValue.value = filledSteps * props.step;
2018-04-03 10:29:57 -05:00
}
2024-10-01 09:32:09 -05:00
}
function handleMouseleave(evt) {
temporaryValue.value = null;
}
2018-04-03 10:29:57 -05:00
</script>
<style lang="scss" scoped>
2024-09-29 09:44:40 -05:00
@use "bulma/sass/utilities" as bulma;
2018-04-04 19:46:02 -05:00
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 {
2024-09-29 09:44:40 -05:00
color: bulma.$yellow;
2018-04-03 10:29:57 -05:00
position: absolute;
top: 0;
left: 0;
overflow-x: hidden;
}
}
</style>