Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
262 lines (225 loc) · 6.63 KB
/
Copy pathindex.js
File metadata and controls
262 lines (225 loc) · 6.63 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import {
assign, clamp, toBarPerc, queue, removeElement, randomInc,
} from './util';
import './styles.css';
const DEFAULTS = {
minimum: 0.08,
easing: 'linear',
speed: 200,
trickle: true,
trickleSpeed: 200,
showSpinner: true,
barSelector: 'div.bar',
barLabel: 'processing request',
spinnerSelector: 'div.spinner',
spinnerLabel: 'processing request',
parent: 'body',
template: `
<div class="bar" role="progressbar" aria-valuemin="0" aria-valuemax="1">
<div class="peg"></div>
</div>
<div class="spinner" role="progressbar" aria-valuemin="0" aria-valuemax="1">
<div class="spinner-icon"></div>
</div>
`,
};
const NProgress = () => {
const localSettings = DEFAULTS;
let localStatus = null;
let initialPromises = 0;
let currentPromises = 0;
/**
* @return {boolean} If the progress bar is rendered.
*/
function isRendered() {
return !!document.getElementById('nprogress');
}
/**
* @return {boolean} If there is curent progress.
*/
function isStarted() {
return typeof localStatus === 'number';
}
/**
* Renders the progress bar markup based on the `template` setting.
*
* @return {HTMLElement} The element rendered.
*/
function render() {
if (isRendered()) {
return document.getElementById('nprogress');
}
document.documentElement.classList.add('nprogress-busy');
const progress = document.createElement('div');
progress.id = 'nprogress';
progress.innerHTML = localSettings.template;
const perc = isStarted() ? '-100' : toBarPerc(localStatus || 0);
const bar = progress.querySelector(localSettings.barSelector);
bar.setAttribute('aria-label', localSettings.barLabel);
bar.style.transform = `translate3d(${perc}%,0,0)`;
bar.style.transition = 'all 0 linear';
const spinner = progress.querySelector(localSettings.spinnerSelector);
if (spinner) {
if (!localSettings.showSpinner) {
removeElement(spinner);
} else {
spinner.setAttribute('aria-label', localSettings.spinnerLabel);
}
}
const parent = document.querySelector(localSettings.parent);
if (parent) {
if (parent !== document.body) {
parent.classList.add('nprogress-custom-parent');
}
parent.appendChild(progress);
}
return progress;
}
return {
/**
* Updates configuration.
*
* @param {object} options - options to override/set
* @return {object} The NProgress object.
*/
configure(options) {
assign(localSettings, options);
return this;
},
/**
* Sets the progress bar status, where `n` is a number from `0.0` to `1.0`.
*
* @param {number} value - progress to set
* @return {object} The NProgress object.
*/
set(value) {
const clamppedValue = clamp(value, localSettings.minimum, 1);
localStatus = (clamppedValue === 1 ? null : clamppedValue);
const progress = render();
// Repaint
progress.offsetWidth; // eslint-disable-line no-unused-expressions
queue((next) => {
// Add transition
const { speed, easing } = localSettings;
const bar = progress.querySelector(localSettings.barSelector);
bar.setAttribute('aria-valuenow', clamppedValue);
bar.style.transform = `translate3d(${toBarPerc(clamppedValue)}%,0,0)`;
bar.style.transition = `all ${speed}ms ${easing}`;
if (clamppedValue === 1) {
// Fade out
progress.style.transition = 'none';
progress.style.opacity = 1;
// Repaint
progress.offsetWidth; // eslint-disable-line no-unused-expressions
setTimeout(() => {
progress.style.transition = `all ${speed}ms linear`;
progress.style.opacity = 0;
setTimeout(() => {
this.remove();
next();
}, speed);
}, speed);
} else {
setTimeout(next, speed);
}
});
return this;
},
/**
* Shows the progress bar.
* This is the same as setting the status to 0%, except that it doesn't go backwards.
*
* @return {object} The NProgress object.
*/
start() {
if (!localStatus) {
this.set(0);
}
const work = () => {
setTimeout(() => {
if (!localStatus) {
return;
}
this.inc();
work();
}, localSettings.trickleSpeed);
};
if (localSettings.trickle) {
work();
}
return this;
},
/**
* Hides the progress bar.
* This is the *sort of* the same as setting the status to 100%, with the
* difference being `done()` makes some placebo effect of some realistic motion.
*
* @param {boolean} force - show the progress bar complete even if its hidden
* @return {object} The NProgress object.
*/
done(force) {
if (!force && !localStatus) {
return this;
}
const halfRandom = 0.5 * Math.random();
return this.inc(0.3 + halfRandom).set(1);
},
/**
* Increments progress bar by given amount.
*
* @param {number} [amount] - amount to increment the progress bar by
* @return {object} The NProgress object.
*/
inc(amount = randomInc(localStatus)) {
if (!localStatus) {
return this.start();
}
const clamppedStatus = clamp(localStatus + amount, 0, 0.994);
return this.set(clamppedStatus);
},
/**
* Removes the element. Opposite of render().
*/
remove() {
document.documentElement.classList.remove('nprogress-busy');
document.querySelector(localSettings.parent).classList.remove('nprogress-custom-parent');
const progress = document.getElementById('nprogress');
if (progress) {
removeElement(progress);
}
},
/**
* Waits for all supplied promises and increases the progress as the promises resolve.
*
* @param $promise Promise
* @return {object} The NProgress object.
*/
promise($promise) {
if (currentPromises === 0) {
this.start();
}
initialPromises += 1;
currentPromises += 1;
const promiseResolution = () => {
currentPromises -= 1;
if (currentPromises === 0) {
initialPromises = 0;
this.done();
} else {
this.set((initialPromises - currentPromises) / initialPromises);
}
};
$promise
.then(promiseResolution)
.catch(promiseResolution);
return this;
},
get status() {
return localStatus;
},
get settings() {
return localSettings;
},
};
};
export default NProgress();