| 1 |
/** |
| 2 |
* NProgress, (c) 2013, 2014 Rico Sta. Cruz - https://ricostacruz.com/nprogress * @license MIT |
| 3 |
*/ |
| 4 |
|
| 5 |
;(function(root, factory) { |
| 6 |
|
| 7 |
if (typeof define === 'function' && define.amd) { |
| 8 |
define(factory); |
| 9 |
} else if (typeof exports === 'object') { |
| 10 |
module.exports = factory(); |
| 11 |
} else { |
| 12 |
root.NProgress = factory(); |
| 13 |
} |
| 14 |
|
| 15 |
})(this, function() { |
| 16 |
var NProgress = {}; |
| 17 |
|
| 18 |
NProgress.version = '0.1.6'; |
| 19 |
|
| 20 |
var Settings = NProgress.settings = { |
| 21 |
minimum: 0.08, |
| 22 |
easing: 'ease', |
| 23 |
positionUsing: '', |
| 24 |
speed: 200, |
| 25 |
trickle: true, |
| 26 |
trickleRate: 0.02, |
| 27 |
trickleSpeed: 800, |
| 28 |
showSpinner: true, |
| 29 |
barSelector: '[role="bar"]', |
| 30 |
spinnerSelector: '[role="spinner"]', |
| 31 |
parent: 'body', |
| 32 |
template: '<div class="bar" role="bar"><div class="peg"></div></div><div class="spinner" role="spinner"><div class="spinner-icon"></div></div>' |
| 33 |
}; |
| 34 |
|
| 35 |
/** |
| 36 |
* Updates configuration. |
| 37 |
* |
| 38 |
* NProgress.configure({ |
| 39 |
* minimum: 0.1 |
| 40 |
* }); |
| 41 |
*/ |
| 42 |
NProgress.configure = function(options) { |
| 43 |
var key, value; |
| 44 |
for (key in options) { |
| 45 |
value = options[key]; |
| 46 |
if (value !== undefined && options.hasOwnProperty(key)) Settings[key] = value; |
| 47 |
} |
| 48 |
|
| 49 |
return this; |
| 50 |
}; |
| 51 |
|
| 52 |
/** |
| 53 |
* Last number. |
| 54 |
*/ |
| 55 |
|
| 56 |
NProgress.status = null; |
| 57 |
|
| 58 |
/** |
| 59 |
* Sets the progress bar status, where `n` is a number from `0.0` to `1.0`. |
| 60 |
* |
| 61 |
* NProgress.set(0.4); |
| 62 |
* NProgress.set(1.0); |
| 63 |
*/ |
| 64 |
|
| 65 |
NProgress.set = function(n) { |
| 66 |
var started = NProgress.isStarted(); |
| 67 |
|
| 68 |
n = clamp(n, Settings.minimum, 1); |
| 69 |
NProgress.status = (n === 1 ? null : n); |
| 70 |
|
| 71 |
var progress = NProgress.render(!started), |
| 72 |
bar = progress.querySelector(Settings.barSelector), |
| 73 |
speed = Settings.speed, |
| 74 |
ease = Settings.easing; |
| 75 |
|
| 76 |
progress.offsetWidth; /* Repaint */ |
| 77 |
|
| 78 |
queue(function(next) { |
| 79 |
// Set positionUsing if it hasn't already been set |
| 80 |
if (Settings.positionUsing === '') Settings.positionUsing = NProgress.getPositioningCSS(); |
| 81 |
|
| 82 |
// Add transition |
| 83 |
css(bar, barPositionCSS(n, speed, ease)); |
| 84 |
|
| 85 |
if (n === 1) { |
| 86 |
// Fade out |
| 87 |
css(progress, { |
| 88 |
transition: 'none', |
| 89 |
opacity: 1 |
| 90 |
}); |
| 91 |
progress.offsetWidth; /* Repaint */ |
| 92 |
|
| 93 |
setTimeout(function() { |
| 94 |
css(progress, { |
| 95 |
transition: 'all ' + speed + 'ms linear', |
| 96 |
opacity: 0 |
| 97 |
}); |
| 98 |
setTimeout(function() { |
| 99 |
NProgress.remove(); |
| 100 |
next(); |
| 101 |
}, speed); |
| 102 |
}, speed); |
| 103 |
} else { |
| 104 |
setTimeout(next, speed); |
| 105 |
} |
| 106 |
}); |
| 107 |
|
| 108 |
return this; |
| 109 |
}; |
| 110 |
|
| 111 |
NProgress.isStarted = function() { |
| 112 |
return typeof NProgress.status === 'number'; |
| 113 |
}; |
| 114 |
|
| 115 |
/** |
| 116 |
* Shows the progress bar. |
| 117 |
* This is the same as setting the status to 0%, except that it doesn't go backwards. |
| 118 |
* |
| 119 |
* NProgress.start(); |
| 120 |
* |
| 121 |
*/ |
| 122 |
NProgress.start = function() { |
| 123 |
if (!NProgress.status) NProgress.set(0); |
| 124 |
|
| 125 |
var work = function() { |
| 126 |
setTimeout(function() { |
| 127 |
if (!NProgress.status) return; |
| 128 |
NProgress.trickle(); |
| 129 |
work(); |
| 130 |
}, Settings.trickleSpeed); |
| 131 |
}; |
| 132 |
|
| 133 |
if (Settings.trickle) work(); |
| 134 |
|
| 135 |
return this; |
| 136 |
}; |
| 137 |
|
| 138 |
/** |
| 139 |
* Hides the progress bar. |
| 140 |
* This is the *sort of* the same as setting the status to 100%, with the |
| 141 |
* difference being `done()` makes some placebo effect of some realistic motion. |
| 142 |
* |
| 143 |
* NProgress.done(); |
| 144 |
* |
| 145 |
* If `true` is passed, it will show the progress bar even if its hidden. |
| 146 |
* |
| 147 |
* NProgress.done(true); |
| 148 |
*/ |
| 149 |
|
| 150 |
NProgress.done = function(force) { |
| 151 |
if (!force && !NProgress.status) return this; |
| 152 |
|
| 153 |
return NProgress.inc(0.3 + 0.5 * Math.random()).set(1); |
| 154 |
}; |
| 155 |
|
| 156 |
/** |
| 157 |
* Increments by a random amount. |
| 158 |
*/ |
| 159 |
|
| 160 |
NProgress.inc = function(amount) { |
| 161 |
var n = NProgress.status; |
| 162 |
|
| 163 |
if (!n) { |
| 164 |
return NProgress.start(); |
| 165 |
} else { |
| 166 |
if (typeof amount !== 'number') { |
| 167 |
amount = (1 - n) * clamp(Math.random() * n, 0.1, 0.95); |
| 168 |
} |
| 169 |
|
| 170 |
n = clamp(n + amount, 0, 0.994); |
| 171 |
return NProgress.set(n); |
| 172 |
} |
| 173 |
}; |
| 174 |
|
| 175 |
NProgress.trickle = function() { |
| 176 |
return NProgress.inc(Math.random() * Settings.trickleRate); |
| 177 |
}; |
| 178 |
|
| 179 |
/** |
| 180 |
* Waits for all supplied jQuery promises and |
| 181 |
* increases the progress as the promises resolve. |
| 182 |
* |
| 183 |
* @param $promise jQUery Promise |
| 184 |
*/ |
| 185 |
(function() { |
| 186 |
var initial = 0, current = 0; |
| 187 |
|
| 188 |
NProgress.promise = function($promise) { |
| 189 |
if (!$promise || $promise.state() == "resolved") { |
| 190 |
return this; |
| 191 |
} |
| 192 |
|
| 193 |
if (current == 0) { |
| 194 |
NProgress.start(); |
| 195 |
} |
| 196 |
|
| 197 |
initial++; |
| 198 |
current++; |
| 199 |
|
| 200 |
$promise.always(function() { |
| 201 |
current--; |
| 202 |
if (current == 0) { |
| 203 |
initial = 0; |
| 204 |
NProgress.done(); |
| 205 |
} else { |
| 206 |
NProgress.set((initial - current) / initial); |
| 207 |
} |
| 208 |
}); |
| 209 |
|
| 210 |
return this; |
| 211 |
}; |
| 212 |
|
| 213 |
})(); |
| 214 |
|
| 215 |
/** |
| 216 |
* (Internal) renders the progress bar markup based on the `template` |
| 217 |
* setting. |
| 218 |
*/ |
| 219 |
|
| 220 |
NProgress.render = function(fromStart) { |
| 221 |
if (NProgress.isRendered()) return document.getElementById('nprogress'); |
| 222 |
|
| 223 |
addClass(document.documentElement, 'nprogress-busy'); |
| 224 |
|
| 225 |
var progress = document.createElement('div'); |
| 226 |
progress.id = 'nprogress'; |
| 227 |
progress.innerHTML = Settings.template; |
| 228 |
|
| 229 |
var bar = progress.querySelector(Settings.barSelector), |
| 230 |
perc = fromStart ? '-100' : toBarPerc(NProgress.status || 0), |
| 231 |
parent = document.querySelector(Settings.parent), |
| 232 |
spinner; |
| 233 |
|
| 234 |
css(bar, { |
| 235 |
transition: 'all 0 linear', |
| 236 |
transform: 'translate3d(' + perc + '%,0,0)' |
| 237 |
}); |
| 238 |
|
| 239 |
if (!Settings.showSpinner) { |
| 240 |
spinner = progress.querySelector(Settings.spinnerSelector); |
| 241 |
spinner && removeElement(spinner); |
| 242 |
} |
| 243 |
|
| 244 |
if (parent != document.body) { |
| 245 |
addClass(parent, 'nprogress-custom-parent'); |
| 246 |
} |
| 247 |
|
| 248 |
parent.appendChild(progress); |
| 249 |
return progress; |
| 250 |
}; |
| 251 |
|
| 252 |
/** |
| 253 |
* Removes the element. Opposite of render(). |
| 254 |
*/ |
| 255 |
|
| 256 |
NProgress.remove = function() { |
| 257 |
removeClass(document.documentElement, 'nprogress-busy'); |
| 258 |
removeClass(document.querySelector(Settings.parent), 'nprogress-custom-parent') |
| 259 |
var progress = document.getElementById('nprogress'); |
| 260 |
progress && removeElement(progress); |
| 261 |
}; |
| 262 |
|
| 263 |
/** |
| 264 |
* Checks if the progress bar is rendered. |
| 265 |
*/ |
| 266 |
|
| 267 |
NProgress.isRendered = function() { |
| 268 |
return !!document.getElementById('nprogress'); |
| 269 |
}; |
| 270 |
|
| 271 |
/** |
| 272 |
* Determine which positioning CSS rule to use. |
| 273 |
*/ |
| 274 |
|
| 275 |
NProgress.getPositioningCSS = function() { |
| 276 |
// Sniff on document.body.style |
| 277 |
var bodyStyle = document.body.style; |
| 278 |
|
| 279 |
// Sniff prefixes |
| 280 |
var vendorPrefix = ('WebkitTransform' in bodyStyle) ? 'Webkit' : |
| 281 |
('MozTransform' in bodyStyle) ? 'Moz' : |
| 282 |
('msTransform' in bodyStyle) ? 'ms' : |
| 283 |
('OTransform' in bodyStyle) ? 'O' : ''; |
| 284 |
|
| 285 |
if (vendorPrefix + 'Perspective' in bodyStyle) { |
| 286 |
// Modern browsers with 3D support, e.g. Webkit, IE10 |
| 287 |
return 'translate3d'; |
| 288 |
} else if (vendorPrefix + 'Transform' in bodyStyle) { |
| 289 |
// Browsers without 3D support, e.g. IE9 |
| 290 |
return 'translate'; |
| 291 |
} else { |
| 292 |
// Browsers without translate() support, e.g. IE7-8 |
| 293 |
return 'margin'; |
| 294 |
} |
| 295 |
}; |
| 296 |
|
| 297 |
/** |
| 298 |
* Helpers |
| 299 |
*/ |
| 300 |
|
| 301 |
function clamp(n, min, max) { |
| 302 |
if (n < min) return min; |
| 303 |
if (n > max) return max; |
| 304 |
return n; |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* (Internal) converts a percentage (`0..1`) to a bar translateX |
| 309 |
* percentage (`-100%..0%`). |
| 310 |
*/ |
| 311 |
|
| 312 |
function toBarPerc(n) { |
| 313 |
return (-1 + n) * 100; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
/** |
| 318 |
* (Internal) returns the correct CSS for changing the bar's |
| 319 |
* position given an n percentage, and speed and ease from Settings |
| 320 |
*/ |
| 321 |
|
| 322 |
function barPositionCSS(n, speed, ease) { |
| 323 |
var barCSS; |
| 324 |
|
| 325 |
if (Settings.positionUsing === 'translate3d') { |
| 326 |
barCSS = { transform: 'translate3d('+toBarPerc(n)+'%,0,0)' }; |
| 327 |
} else if (Settings.positionUsing === 'translate') { |
| 328 |
barCSS = { transform: 'translate('+toBarPerc(n)+'%,0)' }; |
| 329 |
} else { |
| 330 |
barCSS = { 'margin-left': toBarPerc(n)+'%' }; |
| 331 |
} |
| 332 |
|
| 333 |
barCSS.transition = 'all '+speed+'ms '+ease; |
| 334 |
|
| 335 |
return barCSS; |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* (Internal) Queues a function to be executed. |
| 340 |
*/ |
| 341 |
|
| 342 |
var queue = (function() { |
| 343 |
var pending = []; |
| 344 |
|
| 345 |
function next() { |
| 346 |
var fn = pending.shift(); |
| 347 |
if (fn) { |
| 348 |
fn(next); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
return function(fn) { |
| 353 |
pending.push(fn); |
| 354 |
if (pending.length == 1) next(); |
| 355 |
}; |
| 356 |
})(); |
| 357 |
|
| 358 |
/** |
| 359 |
* (Internal) Applies css properties to an element, similar to the jQuery |
| 360 |
* css method. |
| 361 |
* |
| 362 |
* While this helper does assist with vendor prefixed property names, it |
| 363 |
* does not perform any manipulation of values prior to setting styles. |
| 364 |
*/ |
| 365 |
|
| 366 |
var css = (function() { |
| 367 |
var cssPrefixes = [ 'Webkit', 'O', 'Moz', 'ms' ], |
| 368 |
cssProps = {}; |
| 369 |
|
| 370 |
function camelCase(string) { |
| 371 |
return string.replace(/^-ms-/, 'ms-').replace(/-([\da-z])/gi, function(match, letter) { |
| 372 |
return letter.toUpperCase(); |
| 373 |
}); |
| 374 |
} |
| 375 |
|
| 376 |
function getVendorProp(name) { |
| 377 |
var style = document.body.style; |
| 378 |
if (name in style) return name; |
| 379 |
|
| 380 |
var i = cssPrefixes.length, |
| 381 |
capName = name.charAt(0).toUpperCase() + name.slice(1), |
| 382 |
vendorName; |
| 383 |
while (i--) { |
| 384 |
vendorName = cssPrefixes[i] + capName; |
| 385 |
if (vendorName in style) return vendorName; |
| 386 |
} |
| 387 |
|
| 388 |
return name; |
| 389 |
} |
| 390 |
|
| 391 |
function getStyleProp(name) { |
| 392 |
name = camelCase(name); |
| 393 |
return cssProps[name] || (cssProps[name] = getVendorProp(name)); |
| 394 |
} |
| 395 |
|
| 396 |
function applyCss(element, prop, value) { |
| 397 |
prop = getStyleProp(prop); |
| 398 |
element.style[prop] = value; |
| 399 |
} |
| 400 |
|
| 401 |
return function(element, properties) { |
| 402 |
var args = arguments, |
| 403 |
prop, |
| 404 |
value; |
| 405 |
|
| 406 |
if (args.length == 2) { |
| 407 |
for (prop in properties) { |
| 408 |
value = properties[prop]; |
| 409 |
if (value !== undefined && properties.hasOwnProperty(prop)) applyCss(element, prop, value); |
| 410 |
} |
| 411 |
} else { |
| 412 |
applyCss(element, args[1], args[2]); |
| 413 |
} |
| 414 |
} |
| 415 |
})(); |
| 416 |
|
| 417 |
/** |
| 418 |
* (Internal) Determines if an element or space separated list of class names contains a class name. |
| 419 |
*/ |
| 420 |
|
| 421 |
function hasClass(element, name) { |
| 422 |
var list = typeof element == 'string' ? element : classList(element); |
| 423 |
return list.indexOf(' ' + name + ' ') >= 0; |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* (Internal) Adds a class to an element. |
| 428 |
*/ |
| 429 |
|
| 430 |
function addClass(element, name) { |
| 431 |
var oldList = classList(element), |
| 432 |
newList = oldList + name; |
| 433 |
|
| 434 |
if (hasClass(oldList, name)) return; |
| 435 |
|
| 436 |
// Trim the opening space. |
| 437 |
element.className = newList.substring(1); |
| 438 |
} |
| 439 |
|
| 440 |
/** |
| 441 |
* (Internal) Removes a class from an element. |
| 442 |
*/ |
| 443 |
|
| 444 |
function removeClass(element, name) { |
| 445 |
var oldList = classList(element), |
| 446 |
newList; |
| 447 |
|
| 448 |
if (!hasClass(element, name)) return; |
| 449 |
|
| 450 |
// Replace the class name. |
| 451 |
newList = oldList.replace(' ' + name + ' ', ' '); |
| 452 |
|
| 453 |
// Trim the opening and closing spaces. |
| 454 |
element.className = newList.substring(1, newList.length - 1); |
| 455 |
} |
| 456 |
|
| 457 |
/** |
| 458 |
* (Internal) Gets a space separated list of the class names on the element. |
| 459 |
* The list is wrapped with a single space on each end to facilitate finding |
| 460 |
* matches within the list. |
| 461 |
*/ |
| 462 |
|
| 463 |
function classList(element) { |
| 464 |
return (' ' + (element.className || '') + ' ').replace(/\s+/gi, ' '); |
| 465 |
} |
| 466 |
|
| 467 |
/** |
| 468 |
* (Internal) Removes an element from the DOM. |
| 469 |
*/ |
| 470 |
|
| 471 |
function removeElement(element) { |
| 472 |
element && element.parentNode && element.parentNode.removeChild(element); |
| 473 |
} |
| 474 |
|
| 475 |
return NProgress; |
| 476 |
}); |
| 477 |
|
| 478 |
|