25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

221 satır
4.7 KiB

  1. /*! NProgress (c) 2013, Rico Sta. Cruz
  2. * http://ricostacruz.com/nprogress */
  3. ;(function(factory) {
  4. if (typeof module === 'object') {
  5. module.exports = factory(this.jQuery || require('dom'));
  6. } else {
  7. this.NProgress = factory(this.jQuery);
  8. }
  9. })(function($) {
  10. var NProgress = {};
  11. NProgress.version = '0.1.0';
  12. var Settings = NProgress.settings = {
  13. minimum: 0.08,
  14. easing: 'ease',
  15. speed: 200,
  16. trickle: true,
  17. trickleRate: 0.02,
  18. trickleSpeed: 800,
  19. template: '<div class="bar" role="bar"><div class="peg"></div></div><div class="spinner"><div class="spinner-icon"></div></div>'
  20. };
  21. /**
  22. * Updates configuration.
  23. *
  24. * NProgress.configure({
  25. * minimum: 0.1
  26. * });
  27. */
  28. NProgress.configure = function(options) {
  29. $.extend(Settings, options);
  30. return this;
  31. };
  32. /**
  33. * Last number.
  34. */
  35. NProgress.status = null;
  36. /**
  37. * Sets the progress bar status, where `n` is a number from `0.0` to `1.0`.
  38. *
  39. * NProgress.set(0.4);
  40. * NProgress.set(1.0);
  41. */
  42. NProgress.set = function(n) {
  43. var started = NProgress.isStarted();
  44. n = clamp(n, Settings.minimum, 1);
  45. NProgress.status = (n === 1 ? null : n);
  46. var $progress = NProgress.render(!started),
  47. $bar = $progress.find('[role="bar"]'),
  48. speed = Settings.speed,
  49. ease = Settings.easing;
  50. $progress[0].offsetWidth; /* Repaint */
  51. $progress.queue(function(next) {
  52. $bar.css({
  53. transition: 'all '+speed+'ms '+ease,
  54. transform: 'translate3d('+toBarPerc(n)+'%,0,0)'
  55. });
  56. if (n === 1) {
  57. // Fade out
  58. $progress.css({ transition: 'none', opacity: 1 });
  59. $progress[0].offsetWidth; /* Repaint */
  60. setTimeout(function() {
  61. $progress.css({ transition: 'all '+speed+'ms linear', opacity: 0 });
  62. setTimeout(function() {
  63. NProgress.remove();
  64. next();
  65. }, speed);
  66. }, speed);
  67. } else {
  68. setTimeout(next, speed);
  69. }
  70. });
  71. return this;
  72. };
  73. NProgress.isStarted = function() {
  74. return typeof NProgress.status === 'number';
  75. };
  76. /**
  77. * Shows the progress bar.
  78. * This is the same as setting the status to 0%, except that it doesn't go backwards.
  79. *
  80. * NProgress.start();
  81. *
  82. */
  83. NProgress.start = function() {
  84. if (!NProgress.status) NProgress.set(0);
  85. var work = function() {
  86. setTimeout(function() {
  87. if (!NProgress.status) return;
  88. NProgress.trickle();
  89. work();
  90. }, Settings.trickleSpeed);
  91. };
  92. if (Settings.trickle) work();
  93. return this;
  94. };
  95. /**
  96. * Hides the progress bar.
  97. * This is the *sort of* the same as setting the status to 100%, with the
  98. * difference being `done()` makes some placebo effect of some realistic motion.
  99. *
  100. * NProgress.done();
  101. *
  102. * If `true` is passed, it will show the progress bar even if its hidden.
  103. *
  104. * NProgress.done(true);
  105. */
  106. NProgress.done = function(force) {
  107. if (!force && !NProgress.status) return this;
  108. return NProgress.inc(0.3 + 0.5 * Math.random()).set(1);
  109. };
  110. /**
  111. * Increments by a random amount.
  112. */
  113. NProgress.inc = function(amount) {
  114. var n = NProgress.status;
  115. if (!n) {
  116. return NProgress.start();
  117. } else {
  118. if (typeof amount !== 'number') {
  119. amount = (1 - n) * clamp(Math.random() * n, 0.1, 0.95);
  120. }
  121. n = clamp(n + amount, 0, 0.994);
  122. return NProgress.set(n);
  123. }
  124. };
  125. NProgress.trickle = function() {
  126. return NProgress.inc(Math.random() * Settings.trickleRate);
  127. };
  128. /**
  129. * (Internal) renders the progress bar markup based on the `template`
  130. * setting.
  131. */
  132. NProgress.render = function(fromStart) {
  133. if (NProgress.isRendered()) return $("#nprogress");
  134. $('html').addClass('nprogress-busy');
  135. var $el = $("<div id='nprogress'>")
  136. .html(Settings.template);
  137. var perc = fromStart ? '-100' : toBarPerc(NProgress.status || 0);
  138. $el.find('[role="bar"]').css({
  139. transition: 'all 0 linear',
  140. transform: 'translate3d('+perc+'%,0,0)'
  141. });
  142. $el.appendTo(document.body);
  143. return $el;
  144. };
  145. /**
  146. * (Internal) Removes the element. Opposite of render().
  147. */
  148. NProgress.remove = function() {
  149. $('html').removeClass('nprogress-busy');
  150. $('#nprogress').remove();
  151. };
  152. /**
  153. * Checks if the progress bar is rendered.
  154. */
  155. NProgress.isRendered = function() {
  156. return ($("#nprogress").length > 0);
  157. };
  158. /**
  159. * Helpers
  160. */
  161. function clamp(n, min, max) {
  162. if (n < min) return min;
  163. if (n > max) return max;
  164. return n;
  165. }
  166. /**
  167. * (Internal) converts a percentage (`0..1`) to a bar translateX
  168. * percentage (`-100%..0%`).
  169. */
  170. function toBarPerc(n) {
  171. return (-1 + n) * 100;
  172. }
  173. return NProgress;
  174. });