PageSourceSearch

https://www.imperial-rac.com/wp-content/plugins/ng-filter/assets/j…/loading-bar.js?ver=7.1.2

js imperial-rac.com collected 2026-10-04 02:41:32 UTC 9,399 bytes, 314 lines download raw bytes

vendor: 9,399 bytes, lines 1-314
1/*! 
2 * angular-loading-bar v0.6.0
3 * https://chieffancypants.github.io/angular-loading-bar
4 * Copyright (c) 2014 Wes Cruver
5 * License: MIT
6 */
7/*
8 * angular-loading-bar
9 *
10 * intercepts XHR requests and creates a loading bar.
11 * Based on the excellent nprogress work by rstacruz (more info in readme)
12 *
13 * (c) 2013 Wes Cruver
14 * License: MIT
15 */
16
17
18(function() {
19
20'use strict';
21
22// Alias the loading bar for various backwards compatibilities since the project has matured:
23angular.module('angular-loading-bar', ['cfp.loadingBarInterceptor']);
24angular.module('chieffancypants.loadingBar', ['cfp.loadingBarInterceptor']);
25
26
27/**
28 * loadingBarInterceptor service
29 *
30 * Registers itself as an Angular interceptor and listens for XHR requests.
31 */
32angular.module('cfp.loadingBarInterceptor', ['cfp.loadingBar'])
33  .config(['$httpProvider', function ($httpProvider) {
34
35    var interceptor = ['$q', '$cacheFactory', '$timeout', '$rootScope', 'cfpLoadingBar', function ($q, $cacheFactory, $timeout, $rootScope, cfpLoadingBar) {
36
37      /**
38       * The total number of requests made
39       */
40      var reqsTotal = 0;
41
42      /**
43       * The number of requests completed (either successfully or not)
44       */
45      var reqsCompleted = 0;
46
47      /**
48       * The amount of time spent fetching before showing the loading bar
49       */
50      var latencyThreshold = cfpLoadingBar.latencyThreshold;
51
52      /**
53       * $timeout handle for latencyThreshold
54       */
55      var startTimeout;
56
57
58      /**
59       * calls cfpLoadingBar.complete() which removes the
60       * loading bar from the DOM.
61       */
62      function setComplete() {
63        $timeout.cancel(startTimeout);
64        cfpLoadingBar.complete();
65        reqsCompleted = 0;
66        reqsTotal = 0;
67      }
68
69      /**
70       * Determine if the response has already been cached
71       * @param  {Object}  config the config option from the request
72       * @return {Boolean} retrns true if cached, otherwise false
73       */
74      function isCached(config) {
75        var cache;
76        var defaultCache = $cacheFactory.get('$http');
77        var defaults = $httpProvider.defaults;
78
79        // Choose the proper cache source. Borrowed from angular: $http service
80        if ((config.cache || defaults.cache) && config.cache !== false &&
81          (config.method === 'GET' || config.method === 'JSONP')) {
82            cache = angular.isObject(config.cache) ? config.cache
83              : angular.isObject(defaults.cache) ? defaults.cache
84              : defaultCache;
85        }
86
87        var cached = cache !== undefined ?
88          cache.get(config.url) !== undefined : false;
89
90        if (config.cached !== undefined && cached !== config.cached) {
91          return config.cached;
92        }
93        config.cached = cached;
94        return cached;
95      }
96
97
98      return {
99        'request': function(config) {
100          // Check to make sure this request hasn't already been cached and that
101          // the requester didn't explicitly ask us to ignore this request:
102          if (!config.ignoreLoadingBar && !isCached(config)) {
103            $rootScope.$broadcast('cfpLoadingBar:loading', {url: config.url});
104            if (reqsTotal === 0) {
105              startTimeout = $timeout(function() {
106                cfpLoadingBar.start();
107              }, latencyThreshold);
108            }
109            reqsTotal++;
110            cfpLoadingBar.set(reqsCompleted / reqsTotal);
111          }
112          return config;
113        },
114
115        'response': function(response) {
116          if (!response.config.ignoreLoadingBar && !isCached(response.config)) {
117            reqsCompleted++;
118            $rootScope.$broadcast('cfpLoadingBar:loaded', {url: response.config.url});
119            if (reqsCompleted >= reqsTotal) {
120              setComplete();
121            } else {
122              cfpLoadingBar.set(reqsCompleted / reqsTotal);
123            }
124          }
125          return response;
126        },
127
128        'responseError': function(rejection) {
129          if (!rejection.config.ignoreLoadingBar && !isCached(rejection.config)) {
130            reqsCompleted++;
131            $rootScope.$broadcast('cfpLoadingBar:loaded', {url: rejection.config.url});
132            if (reqsCompleted >= reqsTotal) {
133              setComplete();
134            } else {
135              cfpLoadingBar.set(reqsCompleted / reqsTotal);
136            }
137          }
138          return $q.reject(rejection);
139        }
140      };
141    }];
142
143    $httpProvider.interceptors.push(interceptor);
144  }]);
145
146
147/**
148 * Loading Bar
149 *
150 * This service handles adding and removing the actual element in the DOM.
151 * Generally, best practices for DOM manipulation is to take place in a
152 * directive, but because the element itself is injected in the DOM only upon
153 * XHR requests, and it's likely needed on every view, the best option is to
154 * use a service.
155 */
156angular.module('cfp.loadingBar', [])
157  .provider('cfpLoadingBar', function() {
158
159    this.includeSpinner = true;
160    this.includeBar = true;
161    this.latencyThreshold = 100;
162    this.startSize = 0.02;
163    this.parentSelector = 'body';
164    this.spinnerTemplate = '<div id="loading-bar-spinner"><div class="spinner-icon"></div></div>';
165    this.loadingBarTemplate = '<div id="loading-bar"><div class="bar"><div class="peg"></div></div></div>';
166
167    this.$get = ['$injector', '$document', '$timeout', '$rootScope', function ($injector, $document, $timeout, $rootScope) {
168      var $animate;
169      var $parentSelector = this.parentSelector,
170        loadingBarContainer = angular.element(this.loadingBarTemplate),
171        loadingBar = loadingBarContainer.find('div').eq(0),
172        spinner = angular.element(this.spinnerTemplate);
173
174      var incTimeout,
175        completeTimeout,
176        started = false,
177        status = 0;
178
179      var includeSpinner = this.includeSpinner;
180      var includeBar = this.includeBar;
181      var startSize = this.startSize;
182
183      /**
184       * Inserts the loading bar element into the dom, and sets it to 2%
185       */
186      function _start() {
187        if (!$animate) {
188          $animate = $injector.get('$animate');
189        }
190
191        var $parent = $document.find($parentSelector).eq(0);
192        $timeout.cancel(completeTimeout);
193
194        // do not continually broadcast the started event:
195        if (started) {
196          return;
197        }
198
199        $rootScope.$broadcast('cfpLoadingBar:started');
200        started = true;
201
202        if (includeBar) {
203          $animate.enter(loadingBarContainer, $parent);
204        }
205
206        if (includeSpinner) {
207          $animate.enter(spinner, $parent);
208        }
209
210        _set(startSize);
211      }
212
213      /**
214       * Set the loading bar's width to a certain percent.
215       *
216       * @param n any value between 0 and 1
217       */
218      function _set(n) {
219        if (!started) {
220          return;
221        }
222        var pct = (n * 100) + '%';
223        loadingBar.css('width', pct);
224        status = n;
225
226        // increment loadingbar to give the illusion that there is always
227        // progress but make sure to cancel the previous timeouts so we don't
228        // have multiple incs running at the same time.
229        $timeout.cancel(incTimeout);
230        incTimeout = $timeout(function() {
231          _inc();
232        }, 250);
233      }
234
235      /**
236       * Increments the loading bar by a random amount
237       * but slows down as it progresses
238       */
239      function _inc() {
240        if (_status() >= 1) {
241          return;
242        }
243
244        var rnd = 0;
245
246        // TODO: do this mathmatically instead of through conditions
247
248        var stat = _status();
249        if (stat >= 0 && stat < 0.25) {
250          // Start out between 3 - 6% increments
251          rnd = (Math.random() * (5 - 3 + 1) + 3) / 100;
252        } else if (stat >= 0.25 && stat < 0.65) {
253          // increment between 0 - 3%
254          rnd = (Math.random() * 3) / 100;
255        } else if (stat >= 0.65 && stat < 0.9) {
256          // increment between 0 - 2%
257          rnd = (Math.random() * 2) / 100;
258        } else if (stat >= 0.9 && stat < 0.99) {
259          // finally, increment it .5 %
260          rnd = 0.005;
261        } else {
262          // after 99%, don't increment:
263          rnd = 0;
264        }
265
266        var pct = _status() + rnd;
267        _set(pct);
268      }
269
270      function _status() {
271        return status;
272      }
273
274      function _completeAnimation() {
275        status = 0;
276        started = false;
277      }
278
279      function _complete() {
280        if (!$animate) {
281          $animate = $injector.get('$animate');
282        }
283
284        $rootScope.$broadcast('cfpLoadingBar:completed');
285        _set(1);
286
287        $timeout.cancel(completeTimeout);
288
289        // Attempt to aggregate any start/complete calls within 500ms:
290        completeTimeout = $timeout(function() {
291          var promise = $animate.leave(loadingBarContainer, _completeAnimation);
292          if (promise && promise.then) {
293            promise.then(_completeAnimation);
294          }
295          $animate.leave(spinner);
296        }, 500);
297      }
298
299      return {
300        start            : _start,
301        set              : _set,
302        status           : _status,
303        inc              : _inc,
304        complete         : _complete,
305        includeSpinner   : this.includeSpinner,
306        latencyThreshold : this.latencyThreshold,
307        parentSelector   : this.parentSelector,
308        startSize        : this.startSize
309      };
310
311
312    }];     //
313  });       // wtf javascript. srsly
314})();       //

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.