nprogress.js 12 KB

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