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