forked from andrucz/ionic2-rating
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathionic2-rating.ts
189 lines (158 loc) · 3.8 KB
/
ionic2-rating.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
import { Component, forwardRef, Input } from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
const noop = () => {
};
export const RATING_CONTROL_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => Ionic2Rating),
multi: true
};
@Component({
selector: 'rating',
styles: [`
ul.rating li {
display: inline;
border: 0px;
background: none;
padding: 5px 10px;
}
ul.rating li i {
font-size: 30px;
}
`],
template: `
<ul class="rating" (keydown)="onKeyDown($event)">
<li *ngFor="let starIndex of starIndexes" tappable (click)="rate(starIndex + 1)">
<ion-icon [name]="getStarIconName(starIndex)">
</ion-icon>
</li>
</ul>`,
providers: [RATING_CONTROL_VALUE_ACCESSOR]
})
export class Ionic2Rating implements ControlValueAccessor {
_max: number = 5;
_readOnly: boolean = false;
_emptyStarIconName: string = 'star-outline';
_halfStarIconName: string = 'star-half';
_starIconName: string = 'star';
_nullable: boolean = false;
@Input()
get max() {
return this._max;
}
set max(val: any) {
const newValue = this.getNumberPropertyValue(val);
if (newValue !== this._max) {
this._max = newValue;
this.createStarIndexes();
}
}
@Input()
get readOnly() {
return this._readOnly;
}
set readOnly(val: any) {
this._readOnly = this.isTrueProperty(val);
}
@Input()
get emptyStarIconName() {
return this._emptyStarIconName;
}
set emptyStarIconName(val: any) {
this._emptyStarIconName = val;
}
@Input()
get halfStarIconName() {
return this._halfStarIconName;
}
set halfStarIconName(val: any) {
this._halfStarIconName = val;
}
@Input()
get starIconName() {
return this._starIconName;
}
set starIconName(val: any) {
this._starIconName = val;
}
@Input()
get nullable() {
return this._nullable;
}
set nullable(val: any) {
this._nullable = this.isTrueProperty(val);
}
innerValue: any;
starIndexes: Array<number>;
onChangeCallback: (_: any) => void = noop;
ngOnInit() {
// ngFor needs an array
this.createStarIndexes();
}
createStarIndexes() {
this.starIndexes = Array(this.max).fill(1).map((x, i) => i);
}
getStarIconName(starIndex: number) {
if (this.value === undefined) {
return this.emptyStarIconName;
}
if (this.value > starIndex) {
if (this.value < starIndex + 1) {
return this.halfStarIconName;
} else {
return this.starIconName;
}
} else {
return this.emptyStarIconName;
}
}
get value(): any {
return this.innerValue;
}
set value(value: any) {
if (value !== this.innerValue) {
this.innerValue = value;
this.onChangeCallback(value);
}
}
writeValue(value: any) {
if (value !== this.innerValue) {
this.innerValue = value;
}
}
registerOnChange(fn: any) {
this.onChangeCallback = fn;
}
registerOnTouched(fn: any) {
}
onKeyDown(event: any) {
if (/(37|38|39|40)/.test(event.which)) {
event.preventDefault();
event.stopPropagation();
let newValue = this.value + ((event.which == 38 || event.which == 39) ? 1 : -1);
return this.rate(newValue);
}
}
rate(value: number) {
if (this.readOnly || value < 0 || value > this.max) {
return;
}
if (value === this.value && this.nullable) {
value = null;
}
this.value = value;
}
private isTrueProperty(val: any): boolean {
if (typeof val === 'string') {
val = val.toLowerCase().trim();
return (val === 'true' || val === 'on');
}
return !!val;
}
private getNumberPropertyValue(val: any): number {
if (typeof val === 'string') {
return parseInt(val.trim());
}
return val;
}
}