nprogress.js 12 KB

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