@@ -8,14 +8,31 @@ import {
88} from "./utilities" ;
99import setCSS from "./css" ;
1010
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 ;
1217
1318/**
14- * Last number .
15- * @type { null | number }
19+ * Returns the current percentage .
20+ * @return {number | null
1621 */
1722
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+ }
1936
2037/**
2138 * @typedef CSSDefinition
@@ -39,6 +56,25 @@ let Settings = {
3956 '<div class="bar" role="bar"><div class="peg"></div></div><div class="spinner" role="spinner"><div class="spinner-icon"></div></div>' ,
4057} ;
4158
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+
4278/**
4379 * Updates configuration.
4480 *
@@ -74,7 +110,7 @@ function set(n) {
74110 var started = isStarted ( ) ;
75111
76112 n = clamp ( n , Settings . minimum , 1 ) ;
77- NProgress . status = n === 1 ? null : n ;
113+ setPercent ( n === 1 ? null : n ) ;
78114
79115 var progress = render ( ! started ) ,
80116 bar = progress . querySelector ( Settings . barSelector ) ,
@@ -121,7 +157,7 @@ function set(n) {
121157}
122158
123159function isStarted ( ) {
124- return typeof NProgress . status === "number" ;
160+ return typeof getPercent ( ) === "number" ;
125161}
126162
127163/**
@@ -132,12 +168,12 @@ function isStarted() {
132168 * NProgress.start();
133169 */
134170
135- NProgress . start = function ( ) {
136- if ( ! NProgress . status ) set ( 0 ) ;
171+ function start ( ) {
172+ if ( ! getPercent ( ) ) set ( 0 ) ;
137173
138174 var work = function ( ) {
139175 setTimeout ( function ( ) {
140- if ( ! NProgress . status ) return ;
176+ if ( ! getPercent ( ) ) return ;
141177 inc ( ) ;
142178 work ( ) ;
143179 } , Settings . trickleSpeed ) ;
@@ -146,7 +182,7 @@ NProgress.start = function () {
146182 if ( Settings . trickle ) work ( ) ;
147183
148184 return NProgress ;
149- } ;
185+ }
150186
151187/**
152188 * Hides the progress bar.
@@ -162,13 +198,13 @@ NProgress.start = function () {
162198 * @param {boolean | null | void } force
163199 */
164200
165- NProgress . done = function ( force ) {
166- if ( ! force && ! NProgress . status ) return NProgress ;
201+ function done ( force ) {
202+ if ( ! force && ! getPercent ( ) ) return NProgress ;
167203
168204 inc ( 0.3 + 0.5 * Math . random ( ) ) ;
169205 set ( 1 ) ;
170206 return NProgress ;
171- } ;
207+ }
172208
173209/**
174210 * Increments by a random amount.
@@ -178,10 +214,10 @@ NProgress.done = function (force) {
178214 */
179215
180216function inc ( amount ) {
181- var n = NProgress . status ;
217+ var n = getPercent ( ) ;
182218
183219 if ( ! n ) {
184- return NProgress . start ( ) ;
220+ return start ( ) ;
185221 } else if ( n > 1 ) {
186222 return ;
187223 } else {
@@ -205,14 +241,16 @@ function inc(amount) {
205241}
206242
207243/**
244+ * Returns the parent node as an HTMLElement.
208245 * @return {HTMLElement }
209246 */
210247
211- function getParent ( ) {
248+ function getParentElement ( ) {
212249 if ( typeof Settings . parent === "string" ) {
213250 let parent = document . querySelector ( Settings . parent ) ;
214251 if ( ! parent )
215252 throw new Error ( `NProgress: Invalid parent '${ Settings . parent } '` ) ;
253+
216254 return parent ;
217255 } else {
218256 return Settings . parent ;
@@ -240,10 +278,10 @@ function render(fromStart) {
240278 if ( ! bar )
241279 throw new Error ( `NProgress: No bar found for '${ Settings . barSelector } '` ) ;
242280
243- let perc = fromStart ? "-100" : toBarPerc ( NProgress . status || 0 ) ;
281+ let perc = fromStart ? "-100" : toBarPerc ( getPercent ( ) || 0 ) ;
244282
245283 /** @type HTMLElement */
246- let parent = getParent ( ) ;
284+ let parent = getParentElement ( ) ;
247285
248286 /** @type HTMLElement | null */
249287 let spinner ;
@@ -289,28 +327,24 @@ function isRendered() {
289327 return ! ! document . getElementById ( "nprogress" ) ;
290328}
291329
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+
304334export {
305335 configure ,
306- inc ,
336+ done ,
337+ getPercent ,
307338 inc as trickle ,
339+ inc ,
308340 isRendered ,
309341 isStarted ,
310342 remove ,
311343 render ,
312344 set ,
345+ setPercent as _setPercent ,
313346 Settings as settings ,
347+ start ,
314348} ;
315349
316350export default NProgress ;
0 commit comments