forked from webaverse/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress.js
82 lines (72 loc) · 1.97 KB
/
progress.js
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
throw new Error('dead code')
import './nanobar.js';
const nanobar = new Nanobar({
target: document.getElementById('progress-bar'),
});
/* nanobar.go(30);
nanobar.go(76); */
class Progress {
constructor() {
this.numerator = 0;
this.denominator = 1;
this.trickleTimer = null;
}
setNumerator(n) {
this.numerator = n;
this.update();
}
setDemoninator(d) {
this.denominator = d;
this.update();
}
setNumeratorDenominator(n, d) {
this.numerator = n;
this.denominator = d;
this.update();
}
addNumerator() {
this.setNumerator(this.numerator + 1);
}
update() {
if (this.numerator < this.denominator) {
nanobar.go((this.numerator + 1) / (this.denominator + 1) * 100);
} else {
nanobar.go(100);
}
}
trickle() {
// Logic adapted from https://github.com/rstacruz/nprogress/blob/6de3a45c33fe1f142b73935206a6f1e74a230a3f/nprogress.js#L158-L188
// As per https://github.com/jacoborus/nanobar/issues/36#issuecomment-207096298
this.trickleTimer = setInterval(() => {
let amount;
if (this.numerator >= 0 && this.numerator < 0.25) {
// Start out between 3 - 6% increments
amount = (Math.random() * (5 - 3 + 1) + 3) / 100;
} else if (this.numerator >= 0.25 && this.numerator < 0.65) {
// increment between 0 - 3%
amount = (Math.random() * 3) / 100;
} else if (this.numerator >= 0.65 && this.numerator < 0.9) {
// increment between 0 - 2%
amount = (Math.random() * 2) / 100;
} else if (this.numerator >= 0.9 && this.numerator < 0.99) {
// finally, increment it .5 %
amount = 0.005;
} else {
// after 99%, don't increment:
amount = 0;
}
this.setNumerator(this.numerator + amount);
this.update();
}, 500);
}
stopTrickle() {
if (this.trickleTimer) {
clearInterval(this.trickleTimer);
}
nanobar.go(100);
}
}
const progress = new Progress();
export {
progress,
};