|
1
|
|
|
import { CountUp } from './dist/countUp.js'; |
|
2
|
|
|
|
|
3
|
|
|
window.onload = function () { |
|
4
|
|
|
var input = function (id) { |
|
5
|
|
|
return document.getElementById(id); |
|
6
|
|
|
}; |
|
7
|
|
|
var code, stars, endVal, options; |
|
8
|
|
|
var demo = new CountUp('myTargetElement', 100); |
|
9
|
|
|
var codeVisualizer = document.getElementById('codeVisualizer'); |
|
10
|
|
|
var errorSection = document.getElementById('errorSection'); |
|
11
|
|
|
document.getElementById('version').innerHTML = demo.version; |
|
12
|
|
|
// HANDLERS |
|
13
|
|
|
input('startVal').onchange = updateCodeVisualizer; |
|
14
|
|
|
input('endVal').onchange = updateCodeVisualizer; |
|
15
|
|
|
input('decimalPlaces').onchange = updateCodeVisualizer; |
|
16
|
|
|
input('duration').onchange = updateCodeVisualizer; |
|
17
|
|
|
input('separator').onchange = updateCodeVisualizer; |
|
18
|
|
|
input('decimal').onchange = updateCodeVisualizer; |
|
19
|
|
|
input('prefix').onchange = updateCodeVisualizer; |
|
20
|
|
|
input('suffix').onchange = updateCodeVisualizer; |
|
21
|
|
|
input('useEasing').onclick = updateCodeVisualizer; |
|
22
|
|
|
input('useGrouping').onclick = updateCodeVisualizer; |
|
23
|
|
|
input('useOnComplete').onclick = updateCodeVisualizer; |
|
24
|
|
|
input('easingFnsDropdown').onchange = updateCodeVisualizer; |
|
25
|
|
|
input('numeralsDropdown').onchange = updateCodeVisualizer; |
|
26
|
|
|
document.getElementById('swapValues').onclick = function () { |
|
27
|
|
|
var oldStartVal = input('startVal').value; |
|
28
|
|
|
var oldEndVal = input('endVal').value; |
|
29
|
|
|
input('startVal').value = oldEndVal; |
|
30
|
|
|
input('endVal').value = oldStartVal; |
|
31
|
|
|
updateCodeVisualizer(); |
|
32
|
|
|
}; |
|
33
|
|
|
document.getElementById('start').onclick = createCountUp; |
|
34
|
|
|
document.getElementById('apply').onclick = createCountUp; |
|
35
|
|
|
document.getElementById('pauseResume').onclick = function () { |
|
36
|
|
|
code += '<br>demo.pauseResume();'; |
|
37
|
|
|
codeVisualizer.innerHTML = code; |
|
38
|
|
|
demo.pauseResume(); |
|
39
|
|
|
}; |
|
40
|
|
|
document.getElementById('reset').onclick = function () { |
|
41
|
|
|
code += '<br>demo.reset();'; |
|
42
|
|
|
codeVisualizer.innerHTML = code; |
|
43
|
|
|
demo.reset(); |
|
44
|
|
|
}; |
|
45
|
|
|
document.getElementById('update').onclick = function () { |
|
46
|
|
|
var updateVal = input('updateVal').value; |
|
47
|
|
|
var num = updateVal ? updateVal : 0; |
|
48
|
|
|
code += "<br>demo.update(" + num + ");"; |
|
49
|
|
|
codeVisualizer.innerHTML = code; |
|
50
|
|
|
demo.update(num); |
|
51
|
|
|
}; |
|
52
|
|
|
input('updateVal').onchange = function () { |
|
53
|
|
|
var updateVal = input('updateVal').value; |
|
54
|
|
|
var num = updateVal ? updateVal : 0; |
|
55
|
|
|
code += '<br>demo.update(' + num + ');'; |
|
56
|
|
|
codeVisualizer.innerHTML = code; |
|
57
|
|
|
}; |
|
58
|
|
|
// OPTION VALUES |
|
59
|
|
|
var easingFunctions = { |
|
60
|
|
|
easeOutExpo: function (t, b, c, d) { |
|
61
|
|
|
return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b; |
|
62
|
|
|
}, |
|
63
|
|
|
outQuintic: function (t, b, c, d) { |
|
64
|
|
|
var ts = (t /= d) * t; |
|
65
|
|
|
var tc = ts * t; |
|
66
|
|
|
return b + c * (tc * ts + -5 * ts * ts + 10 * tc + -10 * ts + 5 * t); |
|
67
|
|
|
}, |
|
68
|
|
|
outCubic: function (t, b, c, d) { |
|
69
|
|
|
var ts = (t /= d) * t; |
|
70
|
|
|
var tc = ts * t; |
|
71
|
|
|
return b + c * (tc + -3 * ts + 3 * t); |
|
72
|
|
|
} |
|
73
|
|
|
}; |
|
74
|
|
|
function getEasingFn() { |
|
75
|
|
|
var fn = input('easingFnsDropdown').value; |
|
76
|
|
|
if (fn === 'easeOutExpo') { |
|
77
|
|
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
if (typeof easingFunctions[fn] === 'undefined') { |
|
80
|
|
|
return undefined; |
|
81
|
|
|
} |
|
82
|
|
|
return easingFunctions[fn]; |
|
83
|
|
|
} |
|
84
|
|
|
function getEasingFnBody(fn) { |
|
85
|
|
|
fn = typeof fn === 'undefined' ? getEasingFn() : fn; |
|
86
|
|
|
if (typeof fn === 'undefined') { |
|
87
|
|
|
return 'undefined function'; |
|
88
|
|
|
} |
|
89
|
|
|
if (fn !== null) { |
|
90
|
|
|
return fn.toString().replace(/^ {8}/gm, ''); |
|
91
|
|
|
} |
|
92
|
|
|
return ''; |
|
93
|
|
|
} |
|
94
|
|
|
function getNumerals() { |
|
95
|
|
|
var numeralsCode = input('numeralsDropdown').value; |
|
96
|
|
|
// optionally provide alternates for 0-9 |
|
97
|
|
|
switch (numeralsCode) { |
|
98
|
|
|
case 'ea': // Eastern Arabic |
|
99
|
|
|
return ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩']; |
|
100
|
|
|
case 'fa': // Farsi |
|
101
|
|
|
return ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']; |
|
102
|
|
|
default: |
|
103
|
|
|
return null; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
var stringifyArray = function (arr) { return '[\'' + arr.join('\', \'') + '\']'; }; |
|
107
|
|
|
// COUNTUP AND CODE VISUALIZER |
|
108
|
|
|
function createCountUp() { |
|
109
|
|
|
establishOptionsFromInputs(); |
|
110
|
|
|
demo = new CountUp('myTargetElement', endVal, options); |
|
111
|
|
|
if (!demo.error) { |
|
112
|
|
|
errorSection.style.display = 'none'; |
|
113
|
|
|
if (input('useOnComplete').checked) { |
|
114
|
|
|
demo.start(methodToCallOnComplete); |
|
115
|
|
|
} |
|
116
|
|
|
else { |
|
117
|
|
|
demo.start(); |
|
118
|
|
|
} |
|
119
|
|
|
updateCodeVisualizer(); |
|
120
|
|
|
} |
|
121
|
|
|
else { |
|
122
|
|
|
errorSection.style.display = 'block'; |
|
123
|
|
|
document.getElementById('error').innerHTML = demo.error; |
|
124
|
|
|
console.error(demo.error); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
function methodToCallOnComplete() { |
|
128
|
|
|
console.log('COMPLETE!'); |
|
|
|
|
|
|
129
|
|
|
alert('COMPLETE!'); |
|
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
function establishOptionsFromInputs() { |
|
132
|
|
|
endVal = Number(input('endVal').value); |
|
133
|
|
|
options = { |
|
134
|
|
|
startVal: input('startVal').value, |
|
135
|
|
|
decimalPlaces: input('decimalPlaces').value, |
|
136
|
|
|
duration: Number(input('duration').value), |
|
137
|
|
|
useEasing: input('useEasing').checked, |
|
138
|
|
|
useGrouping: input('useGrouping').checked, |
|
139
|
|
|
easingFn: typeof getEasingFn() === 'undefined' ? null : getEasingFn(), |
|
140
|
|
|
separator: input('separator').value, |
|
141
|
|
|
decimal: input('decimal').value, |
|
142
|
|
|
prefix: input('prefix').value, |
|
143
|
|
|
suffix: input('suffix').value, |
|
144
|
|
|
numerals: getNumerals() |
|
145
|
|
|
}; |
|
146
|
|
|
// unset null values so they don't overwrite defaults |
|
147
|
|
|
for (var key in options) { |
|
148
|
|
|
if (options.hasOwnProperty(key)) { |
|
149
|
|
|
if (options[key] === null) { |
|
150
|
|
|
delete options[key]; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
function updateCodeVisualizer() { |
|
156
|
|
|
establishOptionsFromInputs(); |
|
157
|
|
|
code = ''; |
|
158
|
|
|
if (options.useEasing && options.easingFn) { |
|
159
|
|
|
code += 'const easingFn = '; |
|
160
|
|
|
var split = getEasingFnBody(options.easingFn).split('\n'); |
|
161
|
|
|
for (var line in split) { |
|
162
|
|
|
if (split.hasOwnProperty(line)) { |
|
163
|
|
|
code += split[line].replace(' ', ' ') + '<br>'; |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
function indentedLine(keyPair, singleLine) { |
|
168
|
|
|
if (singleLine === void 0) { singleLine = false; } |
|
|
|
|
|
|
169
|
|
|
var delimeter = (singleLine) ? ';' : ','; |
|
170
|
|
|
return "  " + keyPair + delimeter + "<br>"; |
|
171
|
|
|
} |
|
172
|
|
|
var opts = ''; |
|
173
|
|
|
opts += (options.startVal !== '0') ? indentedLine("startVal: " + options.startVal) : ''; |
|
174
|
|
|
opts += (options.decimalPlaces !== '0') ? indentedLine("decimalPlaces: " + options.decimalPlaces) : ''; |
|
175
|
|
|
opts += (options.duration !== 2) ? indentedLine("duration: " + options.duration) : ''; |
|
176
|
|
|
opts += (options.useEasing) ? '' : indentedLine("useEasing: " + options.useEasing); |
|
177
|
|
|
opts += (options.useEasing && options.easingFn) ? indentedLine("easingFn") : ''; |
|
178
|
|
|
opts += (options.useGrouping) ? '' : indentedLine("useGrouping: " + options.useGrouping); |
|
179
|
|
|
opts += (options.separator !== ',') ? indentedLine("separator: '" + options.separator + "'") : ''; |
|
180
|
|
|
opts += (options.decimal !== '.') ? indentedLine("decimal: '" + options.decimal + "'") : ''; |
|
181
|
|
|
opts += (options.prefix.length) ? indentedLine("prefix: '" + options.prefix + "'") : ''; |
|
182
|
|
|
opts += (options.suffix.length) ? indentedLine("suffix: '" + options.suffix + "'") : ''; |
|
183
|
|
|
opts += (options.numerals && options.numerals.length) ? |
|
184
|
|
|
indentedLine("numerals: '" + stringifyArray(options.numerals) + "'") : ''; |
|
185
|
|
|
if (opts.length) { |
|
186
|
|
|
code += "const options = {<br>" + opts + "};<br>"; |
|
187
|
|
|
code += "let demo = new CountUp('myTargetElement', " + endVal + ", options);<br>"; |
|
188
|
|
|
} |
|
189
|
|
|
else { |
|
190
|
|
|
code += "let demo = new CountUp('myTargetElement', " + endVal + ");<br>"; |
|
191
|
|
|
} |
|
192
|
|
|
code += 'if (!demo.error) {<br>'; |
|
193
|
|
|
code += (input('useOnComplete').checked) ? |
|
194
|
|
|
indentedLine('demo.start(methodToCallOnComplete)', true) : indentedLine('demo.start()', true); |
|
195
|
|
|
code += '} else {<br>'; |
|
196
|
|
|
code += indentedLine('console.error(demo.error)', true); |
|
197
|
|
|
code += '}'; |
|
198
|
|
|
codeVisualizer.innerHTML = code; |
|
199
|
|
|
} |
|
200
|
|
|
// get current star count |
|
201
|
|
|
var repoInfoUrl = 'https://api.github.com/repos/inorganik/CountUp.js'; |
|
202
|
|
|
var getStars = new XMLHttpRequest(); |
|
203
|
|
|
getStars.open('GET', repoInfoUrl, true); |
|
204
|
|
|
getStars.timeout = 5000; |
|
205
|
|
|
getStars.onreadystatechange = function () { |
|
206
|
|
|
// 2: received headers, 3: loading, 4: done |
|
207
|
|
|
if (getStars.readyState === 4) { |
|
208
|
|
|
if (getStars.status === 200) { |
|
209
|
|
|
if (getStars.responseText !== 'undefined') { |
|
210
|
|
|
if (getStars.responseText.length > 0) { |
|
211
|
|
|
var data = JSON.parse(getStars.responseText); |
|
212
|
|
|
stars = data.stargazers_count; |
|
213
|
|
|
// change input values |
|
214
|
|
|
input('endVal').value = stars; |
|
215
|
|
|
createCountUp(); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
}; |
|
221
|
|
|
getStars.onerror = function () { |
|
222
|
|
|
console.error('error getting stars:', getStars.status); |
|
223
|
|
|
stars = getStars.status; |
|
224
|
|
|
demo.start(); |
|
225
|
|
|
}; |
|
226
|
|
|
getStars.send(); |
|
227
|
|
|
} |
|
228
|
|
|
|