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