-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathstar-rating.vue
185 lines (179 loc) · 4.96 KB
/
star-rating.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<template>
<div class="star-rating" :aria-label="rating + ' of 5'">
<div v-for="(star, index) in stars" :key="index" class="star-container">
<svg
class="star-svg"
:style="[
{ fill: `url(#gradient${star.raw})` },
{ width: styleStarWidth },
{ height: styleStarHeight },
]"
aria-hidden="true"
>
<polygon :points="getStarPoints" style="fill-rule: nonzero" />
<defs>
<!--
id has to be unique to each star fullness(dynamic offset) - it indicates fullness above
-->
<linearGradient :id="`gradient${star.raw}`">
<stop
id="stop1"
:offset="star.percent"
stop-opacity="1"
:stop-color="getFullFillColor(star)"
></stop>
<stop
id="stop2"
:offset="star.percent"
stop-opacity="0"
:stop-color="getFullFillColor(star)"
></stop>
<stop
id="stop3"
:offset="star.percent"
stop-opacity="1"
:stop-color="styleEmptyStarColor"
></stop>
<stop
id="stop4"
offset="100%"
stop-opacity="1"
:stop-color="styleEmptyStarColor"
></stop>
</linearGradient>
</defs>
</svg>
</div>
<div v-if="isIndicatorActive" class="indicator">{{ rating }}</div>
</div>
</template>
<script>
export default {
name: "stars-rating",
components: {},
props: {
rating: {
type: Number,
default: 4.3,
},
starStyle: {
type: Object,
},
isIndicatorActive: {
type: Boolean,
default: true,
},
},
data: function () {
return {
stars: [],
emptyStar: 0,
fullStar: 1,
totalStars: 5,
// Binded Nested Props registered as data/computed and not props
styleStarWidth: 100,
styleStarHeight: 100,
styleEmptyStarColor: "#737373",
styleFullStarColor: "#ed8a19",
};
},
directives: {},
computed: {
getStarPoints () {
let centerX = this.styleStarWidth / 2;
let centerY = this.styleStarWidth / 2;
let innerCircleArms = 5; // a 5 arms star
let innerRadius = this.styleStarWidth / innerCircleArms;
let innerOuterRadiusRatio = 2.5; // Unique value - determines fatness/sharpness of star
let outerRadius = innerRadius * innerOuterRadiusRatio;
return this.calcStarPoints(
centerX,
centerY,
innerCircleArms,
innerRadius,
outerRadius
);
},
},
methods: {
calcStarPoints(
centerX,
centerY,
innerCircleArms,
innerRadius,
outerRadius
) {
let angle = Math.PI / innerCircleArms;
let angleOffsetToCenterStar = 60;
let totalArms = innerCircleArms * 2;
let points = "";
for (let i = 0; i < totalArms; i++) {
let isEvenIndex = i % 2 == 0;
let r = isEvenIndex ? outerRadius : innerRadius;
let currX = centerX + Math.cos(i * angle + angleOffsetToCenterStar) * r;
let currY = centerY + Math.sin(i * angle + angleOffsetToCenterStar) * r;
points += currX + "," + currY + " ";
}
return points;
},
initStars() {
for (let i = 0; i < this.totalStars; i++) {
this.stars.push({
raw: this.emptyStar,
percent: this.emptyStar + "%",
});
}
},
setStars() {
let fullStarsCounter = Math.floor(this.rating);
for (let i = 0; i < this.stars.length; i++) {
if (fullStarsCounter !== 0) {
this.stars[i].raw = this.fullStar;
this.stars[i].percent = this.calcStarFullness(this.stars[i]);
fullStarsCounter--;
} else {
let surplus = Math.round((this.rating % 1) * 10) / 10; // Support just one decimal
let roundedOneDecimalPoint = Math.round(surplus * 10) / 10;
this.stars[i].raw = roundedOneDecimalPoint;
return (this.stars[i].percent = this.calcStarFullness(this.stars[i]));
}
}
},
getFullFillColor(starData) {
return starData.raw !== this.emptyStar
? this.styleFullStarColor
: this.styleEmptyStarColor;
},
calcStarFullness(starData) {
let starFullnessPercent = starData.raw * 100 + "%";
return starFullnessPercent;
},
setNestedConfigStyles(objToFlatten) {
if (typeof objToFlatten === "object") {
for (let i in objToFlatten) {
let newKey =
"style" + i.charAt(0).toUpperCase() + i.substring(1, i.length);
this[newKey] = objToFlatten[i];
}
}
},
},
created() {
this.setNestedConfigStyles(this.starStyle);
this.initStars();
this.setStars();
},
};
</script>
<style scoped lang="scss">
.star-rating {
display: flex;
align-items: center;
.star-container {
display: flex;
}
.star-container:not(:last-child) {
margin-right: 5px;
}
}
</style>