nprogress.js 11 KB

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