@@ -8,14 +8,31 @@ import {
8
8
} from "./utilities" ;
9
9
import setCSS from "./css" ;
10
10
11
- const NProgress = { } ;
11
+ /**
12
+ * The current progress number from 0...1, or `null` if not started.
13
+ * @type {number | null }
14
+ */
15
+
16
+ let status = null ;
12
17
13
18
/**
14
- * Last number .
15
- * @type { null | number }
19
+ * Returns the current percentage .
20
+ * @return {number | null
16
21
*/
17
22
18
- NProgress . status = null ;
23
+ function getPercent ( ) {
24
+ return status ;
25
+ }
26
+
27
+ /**
28
+ * (Internal) sets the percent value.
29
+ * This is only used in tests. Please use `set()` instead.
30
+ * @param {number } value
31
+ */
32
+
33
+ function setPercent ( value ) {
34
+ status = value ;
35
+ }
19
36
20
37
/**
21
38
* @typedef CSSDefinition
@@ -39,6 +56,25 @@ let Settings = {
39
56
'<div class="bar" role="bar"><div class="peg"></div></div><div class="spinner" role="spinner"><div class="spinner-icon"></div></div>' ,
40
57
} ;
41
58
59
+ /**
60
+ * Default export for CommonJS and for chaining
61
+ */
62
+
63
+ const NProgress = {
64
+ configure,
65
+ done,
66
+ getPercent,
67
+ inc,
68
+ isRendered,
69
+ isStarted,
70
+ remove,
71
+ render,
72
+ set,
73
+ settings : Settings ,
74
+ start,
75
+ trickle : inc ,
76
+ } ;
77
+
42
78
/**
43
79
* Updates configuration.
44
80
*
@@ -74,7 +110,7 @@ function set(n) {
74
110
var started = isStarted ( ) ;
75
111
76
112
n = clamp ( n , Settings . minimum , 1 ) ;
77
- NProgress . status = n === 1 ? null : n ;
113
+ setPercent ( n === 1 ? null : n ) ;
78
114
79
115
var progress = render ( ! started ) ,
80
116
bar = progress . querySelector ( Settings . barSelector ) ,
@@ -121,7 +157,7 @@ function set(n) {
121
157
}
122
158
123
159
function isStarted ( ) {
124
- return typeof NProgress . status === "number" ;
160
+ return typeof getPercent ( ) === "number" ;
125
161
}
126
162
127
163
/**
@@ -132,12 +168,12 @@ function isStarted() {
132
168
* NProgress.start();
133
169
*/
134
170
135
- NProgress . start = function ( ) {
136
- if ( ! NProgress . status ) set ( 0 ) ;
171
+ function start ( ) {
172
+ if ( ! getPercent ( ) ) set ( 0 ) ;
137
173
138
174
var work = function ( ) {
139
175
setTimeout ( function ( ) {
140
- if ( ! NProgress . status ) return ;
176
+ if ( ! getPercent ( ) ) return ;
141
177
inc ( ) ;
142
178
work ( ) ;
143
179
} , Settings . trickleSpeed ) ;
@@ -146,7 +182,7 @@ NProgress.start = function () {
146
182
if ( Settings . trickle ) work ( ) ;
147
183
148
184
return NProgress ;
149
- } ;
185
+ }
150
186
151
187
/**
152
188
* Hides the progress bar.
@@ -162,13 +198,13 @@ NProgress.start = function () {
162
198
* @param {boolean | null | void } force
163
199
*/
164
200
165
- NProgress . done = function ( force ) {
166
- if ( ! force && ! NProgress . status ) return NProgress ;
201
+ function done ( force ) {
202
+ if ( ! force && ! getPercent ( ) ) return NProgress ;
167
203
168
204
inc ( 0.3 + 0.5 * Math . random ( ) ) ;
169
205
set ( 1 ) ;
170
206
return NProgress ;
171
- } ;
207
+ }
172
208
173
209
/**
174
210
* Increments by a random amount.
@@ -178,10 +214,10 @@ NProgress.done = function (force) {
178
214
*/
179
215
180
216
function inc ( amount ) {
181
- var n = NProgress . status ;
217
+ var n = getPercent ( ) ;
182
218
183
219
if ( ! n ) {
184
- return NProgress . start ( ) ;
220
+ return start ( ) ;
185
221
} else if ( n > 1 ) {
186
222
return ;
187
223
} else {
@@ -205,14 +241,16 @@ function inc(amount) {
205
241
}
206
242
207
243
/**
244
+ * Returns the parent node as an HTMLElement.
208
245
* @return {HTMLElement }
209
246
*/
210
247
211
- function getParent ( ) {
248
+ function getParentElement ( ) {
212
249
if ( typeof Settings . parent === "string" ) {
213
250
let parent = document . querySelector ( Settings . parent ) ;
214
251
if ( ! parent )
215
252
throw new Error ( `NProgress: Invalid parent '${ Settings . parent } '` ) ;
253
+
216
254
return parent ;
217
255
} else {
218
256
return Settings . parent ;
@@ -240,10 +278,10 @@ function render(fromStart) {
240
278
if ( ! bar )
241
279
throw new Error ( `NProgress: No bar found for '${ Settings . barSelector } '` ) ;
242
280
243
- let perc = fromStart ? "-100" : toBarPerc ( NProgress . status || 0 ) ;
281
+ let perc = fromStart ? "-100" : toBarPerc ( getPercent ( ) || 0 ) ;
244
282
245
283
/** @type HTMLElement */
246
- let parent = getParent ( ) ;
284
+ let parent = getParentElement ( ) ;
247
285
248
286
/** @type HTMLElement | null */
249
287
let spinner ;
@@ -289,28 +327,24 @@ function isRendered() {
289
327
return ! ! document . getElementById ( "nprogress" ) ;
290
328
}
291
329
292
- // Default export for commonjs / import NProgress
293
- NProgress . configure = configure ;
294
- NProgress . inc = inc ;
295
- NProgress . isRendered = isRendered ;
296
- NProgress . isStarted = isStarted ;
297
- NProgress . remove = remove ;
298
- NProgress . render = render ;
299
- NProgress . set = set ;
300
- NProgress . settings = Settings ;
301
- NProgress . trickle = inc ;
302
-
303
- // Export for ESM
330
+ /*
331
+ * Export for ESM
332
+ */
333
+
304
334
export {
305
335
configure ,
306
- inc ,
336
+ done ,
337
+ getPercent ,
307
338
inc as trickle ,
339
+ inc ,
308
340
isRendered ,
309
341
isStarted ,
310
342
remove ,
311
343
render ,
312
344
set ,
345
+ setPercent as _setPercent ,
313
346
Settings as settings ,
347
+ start ,
314
348
} ;
315
349
316
350
export default NProgress ;
0 commit comments