nprogress.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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: 200,
  19. trickle: true,
  20. trickleSpeed: 200,
  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.2) { amount = 0.1; }
  138. else if (n >= 0.2 && n < 0.5) { amount = 0.04; }
  139. else if (n >= 0.5 && n < 0.8) { amount = 0.02; }
  140. else if (n >= 0.8 && n < 0.99) { amount = 0.005; }
  141. else { amount = 0; }
  142. }
  143. n = clamp(n + amount, 0, 0.994);
  144. return NProgress.set(n);
  145. }
  146. };
  147. NProgress.trickle = function() {
  148. return NProgress.inc();
  149. };
  150. /**
  151. * Waits for all supplied jQuery promises and
  152. * increases the progress as the promises resolve.
  153. *
  154. * @param $promise jQUery Promise
  155. */
  156. (function() {
  157. var initial = 0, current = 0;
  158. NProgress.promise = function($promise) {
  159. if (!$promise || $promise.state() === "resolved") {
  160. return this;
  161. }
  162. if (current === 0) {
  163. NProgress.start();
  164. }
  165. initial++;
  166. current++;
  167. $promise.always(function() {
  168. current--;
  169. if (current === 0) {
  170. initial = 0;
  171. NProgress.done();
  172. } else {
  173. NProgress.set((initial - current) / initial);
  174. }
  175. });
  176. return this;
  177. };
  178. })();
  179. /**
  180. * (Internal) renders the progress bar markup based on the `template`
  181. * setting.
  182. */
  183. NProgress.render = function(fromStart) {
  184. if (NProgress.isRendered()) return document.getElementById('nprogress');
  185. addClass(document.documentElement, 'nprogress-busy');
  186. var progress = document.createElement('div');
  187. progress.id = 'nprogress';
  188. progress.innerHTML = Settings.template;
  189. var bar = progress.querySelector(Settings.barSelector),
  190. perc = fromStart ? '-100' : toBarPerc(NProgress.status || 0),
  191. parent = document.querySelector(Settings.parent),
  192. spinner;
  193. css(bar, {
  194. transition: 'all 0 linear',
  195. transform: 'translate3d(' + perc + '%,0,0)'
  196. });
  197. if (!Settings.showSpinner) {
  198. spinner = progress.querySelector(Settings.spinnerSelector);
  199. spinner && removeElement(spinner);
  200. }
  201. if (parent != document.body) {
  202. addClass(parent, 'nprogress-custom-parent');
  203. }
  204. parent.appendChild(progress);
  205. return progress;
  206. };
  207. /**
  208. * Removes the element. Opposite of render().
  209. */
  210. NProgress.remove = function() {
  211. removeClass(document.documentElement, 'nprogress-busy');
  212. removeClass(document.querySelector(Settings.parent), 'nprogress-custom-parent');
  213. var progress = document.getElementById('nprogress');
  214. progress && removeElement(progress);
  215. };
  216. /**
  217. * Checks if the progress bar is rendered.
  218. */
  219. NProgress.isRendered = function() {
  220. return !!document.getElementById('nprogress');
  221. };
  222. /**
  223. * Determine which positioning CSS rule to use.
  224. */
  225. NProgress.getPositioningCSS = function() {
  226. // Sniff on document.body.style
  227. var bodyStyle = document.body.style;
  228. // Sniff prefixes
  229. var vendorPrefix = ('WebkitTransform' in bodyStyle) ? 'Webkit' :
  230. ('MozTransform' in bodyStyle) ? 'Moz' :
  231. ('msTransform' in bodyStyle) ? 'ms' :
  232. ('OTransform' in bodyStyle) ? 'O' : '';
  233. if (vendorPrefix + 'Perspective' in bodyStyle) {
  234. // Modern browsers with 3D support, e.g. Webkit, IE10
  235. return 'translate3d';
  236. } else if (vendorPrefix + 'Transform' in bodyStyle) {
  237. // Browsers without 3D support, e.g. IE9
  238. return 'translate';
  239. } else {
  240. // Browsers without translate() support, e.g. IE7-8
  241. return 'margin';
  242. }
  243. };
  244. /**
  245. * Helpers
  246. */
  247. function clamp(n, min, max) {
  248. if (n < min) return min;
  249. if (n > max) return max;
  250. return n;
  251. }
  252. /**
  253. * (Internal) converts a percentage (`0..1`) to a bar translateX
  254. * percentage (`-100%..0%`).
  255. */
  256. function toBarPerc(n) {
  257. return (-1 + n) * 100;
  258. }
  259. /**
  260. * (Internal) returns the correct CSS for changing the bar's
  261. * position given an n percentage, and speed and ease from Settings
  262. */
  263. function barPositionCSS(n, speed, ease) {
  264. var barCSS;
  265. if (Settings.positionUsing === 'translate3d') {
  266. barCSS = { transform: 'translate3d('+toBarPerc(n)+'%,0,0)' };
  267. } else if (Settings.positionUsing === 'translate') {
  268. barCSS = { transform: 'translate('+toBarPerc(n)+'%,0)' };
  269. } else {
  270. barCSS = { 'margin-left': toBarPerc(n)+'%' };
  271. }
  272. barCSS.transition = 'all '+speed+'ms '+ease;
  273. return barCSS;
  274. }
  275. /**
  276. * (Internal) Queues a function to be executed.
  277. */
  278. var queue = (function() {
  279. var pending = [];
  280. function next() {
  281. var fn = pending.shift();
  282. if (fn) {
  283. fn(next);
  284. }
  285. }
  286. return function(fn) {
  287. pending.push(fn);
  288. if (pending.length == 1) next();
  289. };
  290. })();
  291. /**
  292. * (Internal) Applies css properties to an element, similar to the jQuery
  293. * css method.
  294. *
  295. * While this helper does assist with vendor prefixed property names, it
  296. * does not perform any manipulation of values prior to setting styles.
  297. */
  298. var css = (function() {
  299. var cssPrefixes = [ 'Webkit', 'O', 'Moz', 'ms' ],
  300. cssProps = {};
  301. function camelCase(string) {
  302. return string.replace(/^-ms-/, 'ms-').replace(/-([\da-z])/gi, function(match, letter) {
  303. return letter.toUpperCase();
  304. });
  305. }
  306. function getVendorProp(name) {
  307. var style = document.body.style;
  308. if (name in style) return name;
  309. var i = cssPrefixes.length,
  310. capName = name.charAt(0).toUpperCase() + name.slice(1),
  311. vendorName;
  312. while (i--) {
  313. vendorName = cssPrefixes[i] + capName;
  314. if (vendorName in style) return vendorName;
  315. }
  316. return name;
  317. }
  318. function getStyleProp(name) {
  319. name = camelCase(name);
  320. return cssProps[name] || (cssProps[name] = getVendorProp(name));
  321. }
  322. function applyCss(element, prop, value) {
  323. prop = getStyleProp(prop);
  324. element.style[prop] = value;
  325. }
  326. return function(element, properties) {
  327. var args = arguments,
  328. prop,
  329. value;
  330. if (args.length == 2) {
  331. for (prop in properties) {
  332. value = properties[prop];
  333. if (value !== undefined && properties.hasOwnProperty(prop)) applyCss(element, prop, value);
  334. }
  335. } else {
  336. applyCss(element, args[1], args[2]);
  337. }
  338. }
  339. })();
  340. /**
  341. * (Internal) Determines if an element or space separated list of class names contains a class name.
  342. */
  343. function hasClass(element, name) {
  344. var list = typeof element == 'string' ? element : classList(element);
  345. return list.indexOf(' ' + name + ' ') >= 0;
  346. }
  347. /**
  348. * (Internal) Adds a class to an element.
  349. */
  350. function addClass(element, name) {
  351. var oldList = classList(element),
  352. newList = oldList + name;
  353. if (hasClass(oldList, name)) return;
  354. // Trim the opening space.
  355. element.className = newList.substring(1);
  356. }
  357. /**
  358. * (Internal) Removes a class from an element.
  359. */
  360. function removeClass(element, name) {
  361. var oldList = classList(element),
  362. newList;
  363. if (!hasClass(element, name)) return;
  364. // Replace the class name.
  365. newList = oldList.replace(' ' + name + ' ', ' ');
  366. // Trim the opening and closing spaces.
  367. element.className = newList.substring(1, newList.length - 1);
  368. }
  369. /**
  370. * (Internal) Gets a space separated list of the class names on the element.
  371. * The list is wrapped with a single space on each end to facilitate finding
  372. * matches within the list.
  373. */
  374. function classList(element) {
  375. return (' ' + (element && element.className || '') + ' ').replace(/\s+/gi, ' ');
  376. }
  377. /**
  378. * (Internal) Removes an element from the DOM.
  379. */
  380. function removeElement(element) {
  381. element && element.parentNode && element.parentNode.removeChild(element);
  382. }
  383. return NProgress;
  384. });