PageSourceSearch

https://mirrors-edge2.ru/template/ME2014/js/nprogress.js

js mirrors-edge2.ru collected 2026-10-09 23:27:02 UTC 12,064 bytes, 491 lines download raw bytes

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

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.