nprogress.js 12 KB

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