-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount-up.js
More file actions
238 lines (204 loc) · 5.15 KB
/
Copy pathcount-up.js
File metadata and controls
238 lines (204 loc) · 5.15 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
(function() {
'use strict';
Polymer({
is: 'count-up',
properties: {
/**
* Start value.
*/
startValue: {
type: Number,
value: null
},
_startValue: {
type: Number,
computed: '_computeStartValue(_previousEndValue, startValue, updateTo)'
},
/**
* End value.
*/
endValue: {
type: Number,
observer: '_endValueChanged'
},
/**
* Update the current value from previous updateTo value instead of counting from startValue to endValue.
* endValue has no effect when updateTo is used.
*/
updateTo: {
type: Number,
value: null,
observer: '_updateToChanged'
},
/**
* Disables the count-up animation at the initial rendering.
*/
noAutostart: {
type: Boolean,
value: false
},
/**
* Number of decimals to show.
*/
decimals: {
type: Number,
value: 0,
observer: '_optionChanged'
},
/**
* Animation duration in seconds.
*/
duration: {
type: Number,
value: 2.5
},
/**
* Disable easing.
*/
noEasing: {
type: Boolean,
value: false
},
/**
* Custom easing function for the animation.
*/
easingFn: {
type: Function,
value: null
},
/**
* Disable grouping by hundreds.
*/
noGrouping: {
type: Boolean,
value: false,
observer: '_optionChanged'
},
/**
* Separator for groups (hundreds).
*/
separator: {
type: String,
value: ','
},
/**
* Decimal symbol.
*/
decimal: {
type: String,
value: '.'
},
/**
* Optional text after the number.
*/
suffix: {
type: String,
value: null
},
/**
* Optional text before the number.
*/
prefix: {
type: String,
value: null
},
/**
* Set to true to restart the animation when a property is changed.
* By default, only changing the endValue will animate the counter if noAutostart is not set to true.
*/
restartOnOptionsChanged: {
type: Boolean,
value: false
},
_countup: {
type: Function,
observer: '_countupChanged',
computed: '_setCountUp(_startValue, endValue, decimals, duration, prefix, suffix, separator, noGrouping, easingFn, noEasing)'
}
},
/* global CountUp */
_setCountUp: function(startValue, endValue, decimals, duration) {
return new CountUp(this.$.value, Number(startValue), Number(endValue), decimals, duration, {
useEasing: !this.noEasing,
easingFn: this.easingFn || null,
useGrouping: !this.noGrouping,
separator: this.separator,
decimal: this.decimal,
prefix: this.prefix || null,
suffix: this.suffix || null
});
},
_countupChanged: function(countup) {
if (this.updateTo && !this._updated) {
this.start();
}
if (this._updated && this.restartOnOptionsChanged && !this._noUpdate) {
this.start();
}
this._noUpdate = false;
this._updated = true;
},
/**
* Start the animation.
* @param {Object} cb Function to be executed at the end of the animation.
*/
start: function(cb) {
this._countup.start(cb);
},
/**
* Restarts the animation.
* Shortcut for reset + start.
* @param {Object} cb Function to be executed at the end of the animation.
*/
restart: function(cb) {
this.reset();
this.start(cb);
},
/**
* Reset the count to the startValue.
*/
reset: function() {
this._countup.reset();
},
/**
* Update the counter to a new value.
* @param {Number} value New value.
*/
update: function(value) {
this._countup.update(value);
},
/**
* Pause or resume the animation.
*/
pauseResume: function() {
this._countup.pauseResume();
},
_computeStartValue: function(previousEndValue, startValue, updateTo) {
if (updateTo) {
return !startValue ? previousEndValue : startValue;
} else {
return startValue;
}
},
/**
* Call start() only the first time that updateTo is set (called by _countupChanged)
* because start() will be called by _endValueChanged everytime that endValue changes.
* _noUpdate is set to prevent calling start() twice.
*/
_updateToChanged: function(updateTo) {
this._noUpdate = true;
this.endValue = updateTo;
},
_endValueChanged: function(endValue, previousValue) {
this._previousEndValue = previousValue !== undefined ? previousValue : 0;
if (!this.noAutostart && this._countup && !this.restartOnOptionsChanged) {
this.start();
}
},
_optionChanged: function(value, previousValue) {
if (previousValue !== undefined) {
setTimeout(() => this.update(this.endValue), 1);
}
}
});
}());