/
star-rating-config.service.ts
220 lines (162 loc) · 4.59 KB
/
star-rating-config.service.ts
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import {
StarRatingConfig,
starRatingColor,
starRatingSizes,
starRatingSpeed,
starRatingStarTypes,
starRatingLabelPosition
} from '../interfaces/star-rating-config.interface';
/**
* Configuration service for the StarRating component.
* You can inject this service, typically in your root component, and customize the values of its properties in
* order to provide default values for all the star ratings used in the application.
*/
export class StarRatingConfigService implements StarRatingConfig {
_classEmpty = 'default-star-empty-icon';
get classEmpty() {
return this._classEmpty;
}
set classEmpty(classEmpty: string) {
this._classEmpty = classEmpty;
}
private _classHalf = 'default-star-half-icon';
get classHalf(): string {
return this._classHalf;
}
set classHalf(classHalf: string) {
this._classHalf = classHalf;
}
private _classFilled = 'default-star-filled-icon';
get classFilled(): string {
return this._classFilled;
}
set classFilled(classFilled: string) {
this._classFilled = classFilled;
}
private _numOfStars = 5;
get numOfStars(): number {
return this._numOfStars;
}
set numOfStars(numOfStars: number) {
this._numOfStars = numOfStars;
}
private _size: starRatingSizes = 'medium';
get size(): starRatingSizes {
return this._size;
}
set size(size: starRatingSizes) {
this._size = size;
}
private _staticColor: starRatingColor;
get staticColor(): starRatingColor {
return this._staticColor;
}
set staticColor(value: starRatingColor) {
this._staticColor = value;
}
private _labelPosition: starRatingLabelPosition = 'left';
get labelPosition(): starRatingLabelPosition {
return this._labelPosition;
}
set labelPosition(labelPosition: starRatingLabelPosition) {
this._labelPosition = labelPosition;
}
private _speed: starRatingSpeed = 'noticeable';
get speed(): starRatingSpeed {
return this._speed;
}
set speed(speed: starRatingSpeed) {
this._speed = speed;
}
private _starType: starRatingStarTypes = 'svg';
get starType(): starRatingStarTypes {
return this._starType;
}
set starType(starType: starRatingStarTypes) {
this._starType = starType;
}
private _assetsPath = 'assets/images/';
get assetsPath(): string {
return this._assetsPath;
}
set assetsPath(assetsPath: string) {
this._assetsPath = assetsPath;
}
private _svgPath = this.assetsPath + 'star-rating.icons.svg';
get svgPath(): string {
return this._svgPath;
}
set svgPath(svgPath: string) {
this._svgPath = svgPath;
}
private _svgEmptySymbolId = 'star-empty';
get svgEmptySymbolId(): string {
return this._svgEmptySymbolId;
}
set svgEmptySymbolId(svgEmptySymbolId: string) {
this._svgEmptySymbolId = svgEmptySymbolId;
}
private _svgHalfSymbolId = 'star-half';
get svgHalfSymbolId(): string {
return this._svgHalfSymbolId;
}
set svgHalfSymbolId(svgHalfSymbolId: string) {
this._svgHalfSymbolId = svgHalfSymbolId;
}
private _svgFilledSymbolId = 'star-filled';
get svgFilledSymbolId(): string {
return this._svgFilledSymbolId;
}
set svgFilledSymbolId(svgFilledSymbolId: string) {
this._svgFilledSymbolId = svgFilledSymbolId;
}
private _svgPathEmpty: string = this.svgPath + '#' + this.svgEmptySymbolId;
get svgPathEmpty(): string {
return this._svgPathEmpty;
}
set svgPathEmpty(svgPathEmpty: string) {
this._svgPathEmpty = svgPathEmpty;
}
private _svgPathHalf: string = this.svgPath + '#' + this.svgHalfSymbolId;
get svgPathHalf(): string {
return this._svgPathHalf;
}
set svgPathHalf(svgPathHalf: string) {
this._svgPathHalf = svgPathHalf;
}
private _svgPathFilled: string = this.svgPath + '#' + this.svgFilledSymbolId;
get svgPathFilled(): string {
return this._svgPathFilled;
}
set svgPathFilled(svgPathFilled: string) {
this._svgPathFilled = svgPathFilled;
}
getColor(
rating: number,
numOfStars: number,
staticColor?: starRatingColor
): starRatingColor {
rating = rating || 0;
// if a fix color is set use this one
if (staticColor) {
return staticColor;
}
// calculate size of smallest fraction
const fractionSize = numOfStars / 3;
// apply color by fraction
let color: starRatingColor = 'default';
if (rating > 0) {
color = 'negative';
}
if (rating > fractionSize) {
color = 'ok';
}
if (rating > fractionSize * 2) {
color = 'positive';
}
return color;
}
getHalfStarVisible(rating: number): boolean {
return Math.abs(rating % 1) > 0;
}
}