Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 espaco 1
(function() {
2
  var AjaxMonitor, Bar, DocumentMonitor, ElementMonitor, ElementTracker, EventLagMonitor, Evented, Events, NoTargetError, RequestIntercept, SOURCE_KEYS, Scaler, SocketRequestTracker, XHRRequestTracker, animation, avgAmplitude, bar, cancelAnimation, cancelAnimationFrame, defaultOptions, extend, extendNative, getFromDOM, getIntercept, handlePushState, ignoreStack, init, now, options, requestAnimationFrame, result, runAnimation, scalers, shouldIgnoreURL, shouldTrack, source, sources, uniScaler, _WebSocket, _XDomainRequest, _XMLHttpRequest, _i, _intercept, _len, _pushState, _ref, _ref1, _replaceState,
3
    __slice = [].slice,
4
    __hasProp = {}.hasOwnProperty,
5
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
6
    __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
7
 
8
  defaultOptions = {
9
    catchupTime: 500,
10
    initialRate: .03,
11
    minTime: 500,
12
    ghostTime: 500,
13
    maxProgressPerFrame: 10,
14
    easeFactor: 1.25,
15
    startOnPageLoad: true,
16
    restartOnPushState: true,
17
    restartOnRequestAfter: 500,
18
    target: 'body',
19
    elements: {
20
      checkInterval: 100,
21
      selectors: ['body']
22
    },
23
    eventLag: {
24
      minSamples: 10,
25
      sampleCount: 3,
26
      lagThreshold: 3
27
    },
28
    ajax: {
29
      trackMethods: ['GET'],
30
      trackWebSockets: true,
31
      ignoreURLs: []
32
    }
33
  };
34
 
35
  now = function() {
36
    var _ref;
37
    return (_ref = typeof performance !== "undefined" && performance !== null ? typeof performance.now === "function" ? performance.now() : void 0 : void 0) != null ? _ref : +(new Date);
38
  };
39
 
40
  requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
41
 
42
  cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame;
43
 
44
  if (requestAnimationFrame == null) {
45
    requestAnimationFrame = function(fn) {
46
      return setTimeout(fn, 50);
47
    };
48
    cancelAnimationFrame = function(id) {
49
      return clearTimeout(id);
50
    };
51
  }
52
 
53
  runAnimation = function(fn) {
54
    var last, tick;
55
    last = now();
56
    tick = function() {
57
      var diff;
58
      diff = now() - last;
59
      if (diff >= 33) {
60
        last = now();
61
        return fn(diff, function() {
62
          return requestAnimationFrame(tick);
63
        });
64
      } else {
65
        return setTimeout(tick, 33 - diff);
66
      }
67
    };
68
    return tick();
69
  };
70
 
71
  result = function() {
72
    var args, key, obj;
73
    obj = arguments[0], key = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
74
    if (typeof obj[key] === 'function') {
75
      return obj[key].apply(obj, args);
76
    } else {
77
      return obj[key];
78
    }
79
  };
80
 
81
  extend = function() {
82
    var key, out, source, sources, val, _i, _len;
83
    out = arguments[0], sources = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
84
    for (_i = 0, _len = sources.length; _i < _len; _i++) {
85
      source = sources[_i];
86
      if (source) {
87
        for (key in source) {
88
          if (!__hasProp.call(source, key)) continue;
89
          val = source[key];
90
          if ((out[key] != null) && typeof out[key] === 'object' && (val != null) && typeof val === 'object') {
91
            extend(out[key], val);
92
          } else {
93
            out[key] = val;
94
          }
95
        }
96
      }
97
    }
98
    return out;
99
  };
100
 
101
  avgAmplitude = function(arr) {
102
    var count, sum, v, _i, _len;
103
    sum = count = 0;
104
    for (_i = 0, _len = arr.length; _i < _len; _i++) {
105
      v = arr[_i];
106
      sum += Math.abs(v);
107
      count++;
108
    }
109
    return sum / count;
110
  };
111
 
112
  getFromDOM = function(key, json) {
113
    var data, e, el;
114
    if (key == null) {
115
      key = 'options';
116
    }
117
    if (json == null) {
118
      json = true;
119
    }
120
    el = document.querySelector("[data-pace-" + key + "]");
121
    if (!el) {
122
      return;
123
    }
124
    data = el.getAttribute("data-pace-" + key);
125
    if (!json) {
126
      return data;
127
    }
128
    try {
129
      return JSON.parse(data);
130
    } catch (_error) {
131
      e = _error;
132
      return typeof console !== "undefined" && console !== null ? console.error("Error parsing inline pace options", e) : void 0;
133
    }
134
  };
135
 
136
  Evented = (function() {
137
    function Evented() {}
138
 
139
    Evented.prototype.on = function(event, handler, ctx, once) {
140
      var _base;
141
      if (once == null) {
142
        once = false;
143
      }
144
      if (this.bindings == null) {
145
        this.bindings = {};
146
      }
147
      if ((_base = this.bindings)[event] == null) {
148
        _base[event] = [];
149
      }
150
      return this.bindings[event].push({
151
        handler: handler,
152
        ctx: ctx,
153
        once: once
154
      });
155
    };
156
 
157
    Evented.prototype.once = function(event, handler, ctx) {
158
      return this.on(event, handler, ctx, true);
159
    };
160
 
161
    Evented.prototype.off = function(event, handler) {
162
      var i, _ref, _results;
163
      if (((_ref = this.bindings) != null ? _ref[event] : void 0) == null) {
164
        return;
165
      }
166
      if (handler == null) {
167
        return delete this.bindings[event];
168
      } else {
169
        i = 0;
170
        _results = [];
171
        while (i < this.bindings[event].length) {
172
          if (this.bindings[event][i].handler === handler) {
173
            _results.push(this.bindings[event].splice(i, 1));
174
          } else {
175
            _results.push(i++);
176
          }
177
        }
178
        return _results;
179
      }
180
    };
181
 
182
    Evented.prototype.trigger = function() {
183
      var args, ctx, event, handler, i, once, _ref, _ref1, _results;
184
      event = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
185
      if ((_ref = this.bindings) != null ? _ref[event] : void 0) {
186
        i = 0;
187
        _results = [];
188
        while (i < this.bindings[event].length) {
189
          _ref1 = this.bindings[event][i], handler = _ref1.handler, ctx = _ref1.ctx, once = _ref1.once;
190
          handler.apply(ctx != null ? ctx : this, args);
191
          if (once) {
192
            _results.push(this.bindings[event].splice(i, 1));
193
          } else {
194
            _results.push(i++);
195
          }
196
        }
197
        return _results;
198
      }
199
    };
200
 
201
    return Evented;
202
 
203
  })();
204
 
205
  if (window.Pace == null) {
206
    window.Pace = {};
207
  }
208
 
209
  extend(Pace, Evented.prototype);
210
 
211
  options = Pace.options = extend({}, defaultOptions, window.paceOptions, getFromDOM());
212
 
213
  _ref = ['ajax', 'document', 'eventLag', 'elements'];
214
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
215
    source = _ref[_i];
216
    if (options[source] === true) {
217
      options[source] = defaultOptions[source];
218
    }
219
  }
220
 
221
  NoTargetError = (function(_super) {
222
    __extends(NoTargetError, _super);
223
 
224
    function NoTargetError() {
225
      _ref1 = NoTargetError.__super__.constructor.apply(this, arguments);
226
      return _ref1;
227
    }
228
 
229
    return NoTargetError;
230
 
231
  })(Error);
232
 
233
  Bar = (function() {
234
    function Bar() {
235
      this.progress = 0;
236
    }
237
 
238
    Bar.prototype.getElement = function() {
239
      var targetElement;
240
      if (this.el == null) {
241
        targetElement = document.querySelector(options.target);
242
        if (!targetElement) {
243
          throw new NoTargetError;
244
        }
245
        this.el = document.createElement('div');
246
        this.el.className = "pace pace-active";
247
        document.body.className = document.body.className.replace(/pace-done/g, '');
248
        document.body.className += ' pace-running';
249
        this.el.innerHTML = '<div class="pace-progress">\n  <div class="pace-progress-inner"></div>\n</div>\n<div class="pace-activity"></div>';
250
        if (targetElement.firstChild != null) {
251
          targetElement.insertBefore(this.el, targetElement.firstChild);
252
        } else {
253
          targetElement.appendChild(this.el);
254
        }
255
      }
256
      return this.el;
257
    };
258
 
259
    Bar.prototype.finish = function() {
260
      var el;
261
      el = this.getElement();
262
      el.className = el.className.replace('pace-active', '');
263
      el.className += ' pace-inactive';
264
      document.body.className = document.body.className.replace('pace-running', '');
265
      return document.body.className += ' pace-done';
266
    };
267
 
268
    Bar.prototype.update = function(prog) {
269
      this.progress = prog;
270
      return this.render();
271
    };
272
 
273
    Bar.prototype.destroy = function() {
274
      try {
275
        this.getElement().parentNode.removeChild(this.getElement());
276
      } catch (_error) {
277
        NoTargetError = _error;
278
      }
279
      return this.el = void 0;
280
    };
281
 
282
    Bar.prototype.render = function() {
283
      var el, progressStr;
284
      if (document.querySelector(options.target) == null) {
285
        return false;
286
      }
287
      el = this.getElement();
288
      el.children[0].style.width = "" + this.progress + "%";
289
      if (!this.lastRenderedProgress || this.lastRenderedProgress | 0 !== this.progress | 0) {
290
        el.children[0].setAttribute('data-progress-text', "" + (this.progress | 0) + "%");
291
        if (this.progress >= 100) {
292
          progressStr = '99';
293
        } else {
294
          progressStr = this.progress < 10 ? "0" : "";
295
          progressStr += this.progress | 0;
296
        }
297
        el.children[0].setAttribute('data-progress', "" + progressStr);
298
      }
299
      return this.lastRenderedProgress = this.progress;
300
    };
301
 
302
    Bar.prototype.done = function() {
303
      return this.progress >= 100;
304
    };
305
 
306
    return Bar;
307
 
308
  })();
309
 
310
  Events = (function() {
311
    function Events() {
312
      this.bindings = {};
313
    }
314
 
315
    Events.prototype.trigger = function(name, val) {
316
      var binding, _j, _len1, _ref2, _results;
317
      if (this.bindings[name] != null) {
318
        _ref2 = this.bindings[name];
319
        _results = [];
320
        for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
321
          binding = _ref2[_j];
322
          _results.push(binding.call(this, val));
323
        }
324
        return _results;
325
      }
326
    };
327
 
328
    Events.prototype.on = function(name, fn) {
329
      var _base;
330
      if ((_base = this.bindings)[name] == null) {
331
        _base[name] = [];
332
      }
333
      return this.bindings[name].push(fn);
334
    };
335
 
336
    return Events;
337
 
338
  })();
339
 
340
  _XMLHttpRequest = window.XMLHttpRequest;
341
 
342
  _XDomainRequest = window.XDomainRequest;
343
 
344
  _WebSocket = window.WebSocket;
345
 
346
  extendNative = function(to, from) {
347
    var e, key, val, _results;
348
    _results = [];
349
    for (key in from.prototype) {
350
      try {
351
        val = from.prototype[key];
352
        if ((to[key] == null) && typeof val !== 'function') {
353
          _results.push(to[key] = val);
354
        } else {
355
          _results.push(void 0);
356
        }
357
      } catch (_error) {
358
        e = _error;
359
      }
360
    }
361
    return _results;
362
  };
363
 
364
  ignoreStack = [];
365
 
366
  Pace.ignore = function() {
367
    var args, fn, ret;
368
    fn = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
369
    ignoreStack.unshift('ignore');
370
    ret = fn.apply(null, args);
371
    ignoreStack.shift();
372
    return ret;
373
  };
374
 
375
  Pace.track = function() {
376
    var args, fn, ret;
377
    fn = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
378
    ignoreStack.unshift('track');
379
    ret = fn.apply(null, args);
380
    ignoreStack.shift();
381
    return ret;
382
  };
383
 
384
  shouldTrack = function(method) {
385
    var _ref2;
386
    if (method == null) {
387
      method = 'GET';
388
    }
389
    if (ignoreStack[0] === 'track') {
390
      return 'force';
391
    }
392
    if (!ignoreStack.length && options.ajax) {
393
      if (method === 'socket' && options.ajax.trackWebSockets) {
394
        return true;
395
      } else if (_ref2 = method.toUpperCase(), __indexOf.call(options.ajax.trackMethods, _ref2) >= 0) {
396
        return true;
397
      }
398
    }
399
    return false;
400
  };
401
 
402
  RequestIntercept = (function(_super) {
403
    __extends(RequestIntercept, _super);
404
 
405
    function RequestIntercept() {
406
      var monitorXHR,
407
        _this = this;
408
      RequestIntercept.__super__.constructor.apply(this, arguments);
409
      monitorXHR = function(req) {
410
        var _open;
411
        _open = req.open;
412
        return req.open = function(type, url, async) {
413
          if (shouldTrack(type)) {
414
            _this.trigger('request', {
415
              type: type,
416
              url: url,
417
              request: req
418
            });
419
          }
420
          return _open.apply(req, arguments);
421
        };
422
      };
423
      window.XMLHttpRequest = function(flags) {
424
        var req;
425
        req = new _XMLHttpRequest(flags);
426
        monitorXHR(req);
427
        return req;
428
      };
429
      extendNative(window.XMLHttpRequest, _XMLHttpRequest);
430
      if (_XDomainRequest != null) {
431
        window.XDomainRequest = function() {
432
          var req;
433
          req = new _XDomainRequest;
434
          monitorXHR(req);
435
          return req;
436
        };
437
        extendNative(window.XDomainRequest, _XDomainRequest);
438
      }
439
      if ((_WebSocket != null) && options.ajax.trackWebSockets) {
440
        window.WebSocket = function(url, protocols) {
441
          var req;
442
          if (protocols != null) {
443
            req = new _WebSocket(url, protocols);
444
          } else {
445
            req = new _WebSocket(url);
446
          }
447
          if (shouldTrack('socket')) {
448
            _this.trigger('request', {
449
              type: 'socket',
450
              url: url,
451
              protocols: protocols,
452
              request: req
453
            });
454
          }
455
          return req;
456
        };
457
        extendNative(window.WebSocket, _WebSocket);
458
      }
459
    }
460
 
461
    return RequestIntercept;
462
 
463
  })(Events);
464
 
465
  _intercept = null;
466
 
467
  getIntercept = function() {
468
    if (_intercept == null) {
469
      _intercept = new RequestIntercept;
470
    }
471
    return _intercept;
472
  };
473
 
474
  shouldIgnoreURL = function(url) {
475
    var pattern, _j, _len1, _ref2;
476
    _ref2 = options.ajax.ignoreURLs;
477
    for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
478
      pattern = _ref2[_j];
479
      if (typeof pattern === 'string') {
480
        if (url.indexOf(pattern) !== -1) {
481
          return true;
482
        }
483
      } else {
484
        if (pattern.test(url)) {
485
          return true;
486
        }
487
      }
488
    }
489
    return false;
490
  };
491
 
492
  getIntercept().on('request', function(_arg) {
493
    var after, args, request, type, url;
494
    type = _arg.type, request = _arg.request, url = _arg.url;
495
    if (shouldIgnoreURL(url)) {
496
      return;
497
    }
498
    if (!Pace.running && (options.restartOnRequestAfter !== false || shouldTrack(type) === 'force')) {
499
      args = arguments;
500
      after = options.restartOnRequestAfter || 0;
501
      if (typeof after === 'boolean') {
502
        after = 0;
503
      }
504
      return setTimeout(function() {
505
        var stillActive, _j, _len1, _ref2, _ref3, _results;
506
        if (type === 'socket') {
507
          stillActive = request.readyState < 2;
508
        } else {
509
          stillActive = (0 < (_ref2 = request.readyState) && _ref2 < 4);
510
        }
511
        if (stillActive) {
512
          Pace.restart();
513
          _ref3 = Pace.sources;
514
          _results = [];
515
          for (_j = 0, _len1 = _ref3.length; _j < _len1; _j++) {
516
            source = _ref3[_j];
517
            if (source instanceof AjaxMonitor) {
518
              source.watch.apply(source, args);
519
              break;
520
            } else {
521
              _results.push(void 0);
522
            }
523
          }
524
          return _results;
525
        }
526
      }, after);
527
    }
528
  });
529
 
530
  AjaxMonitor = (function() {
531
    function AjaxMonitor() {
532
      var _this = this;
533
      this.elements = [];
534
      getIntercept().on('request', function() {
535
        return _this.watch.apply(_this, arguments);
536
      });
537
    }
538
 
539
    AjaxMonitor.prototype.watch = function(_arg) {
540
      var request, tracker, type, url;
541
      type = _arg.type, request = _arg.request, url = _arg.url;
542
      if (shouldIgnoreURL(url)) {
543
        return;
544
      }
545
      if (type === 'socket') {
546
        tracker = new SocketRequestTracker(request);
547
      } else {
548
        tracker = new XHRRequestTracker(request);
549
      }
550
      return this.elements.push(tracker);
551
    };
552
 
553
    return AjaxMonitor;
554
 
555
  })();
556
 
557
  XHRRequestTracker = (function() {
558
    function XHRRequestTracker(request) {
559
      var event, size, _j, _len1, _onreadystatechange, _ref2,
560
        _this = this;
561
      this.progress = 0;
562
      if (window.ProgressEvent != null) {
563
        size = null;
564
        request.addEventListener('progress', function(evt) {
565
          if (evt.lengthComputable) {
566
            return _this.progress = 100 * evt.loaded / evt.total;
567
          } else {
568
            return _this.progress = _this.progress + (100 - _this.progress) / 2;
569
          }
570
        });
571
        _ref2 = ['load', 'abort', 'timeout', 'error'];
572
        for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
573
          event = _ref2[_j];
574
          request.addEventListener(event, function() {
575
            return _this.progress = 100;
576
          });
577
        }
578
      } else {
579
        _onreadystatechange = request.onreadystatechange;
580
        request.onreadystatechange = function() {
581
          var _ref3;
582
          if ((_ref3 = request.readyState) === 0 || _ref3 === 4) {
583
            _this.progress = 100;
584
          } else if (request.readyState === 3) {
585
            _this.progress = 50;
586
          }
587
          return typeof _onreadystatechange === "function" ? _onreadystatechange.apply(null, arguments) : void 0;
588
        };
589
      }
590
    }
591
 
592
    return XHRRequestTracker;
593
 
594
  })();
595
 
596
  SocketRequestTracker = (function() {
597
    function SocketRequestTracker(request) {
598
      var event, _j, _len1, _ref2,
599
        _this = this;
600
      this.progress = 0;
601
      _ref2 = ['error', 'open'];
602
      for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
603
        event = _ref2[_j];
604
        request.addEventListener(event, function() {
605
          return _this.progress = 100;
606
        });
607
      }
608
    }
609
 
610
    return SocketRequestTracker;
611
 
612
  })();
613
 
614
  ElementMonitor = (function() {
615
    function ElementMonitor(options) {
616
      var selector, _j, _len1, _ref2;
617
      if (options == null) {
618
        options = {};
619
      }
620
      this.elements = [];
621
      if (options.selectors == null) {
622
        options.selectors = [];
623
      }
624
      _ref2 = options.selectors;
625
      for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
626
        selector = _ref2[_j];
627
        this.elements.push(new ElementTracker(selector));
628
      }
629
    }
630
 
631
    return ElementMonitor;
632
 
633
  })();
634
 
635
  ElementTracker = (function() {
636
    function ElementTracker(selector) {
637
      this.selector = selector;
638
      this.progress = 0;
639
      this.check();
640
    }
641
 
642
    ElementTracker.prototype.check = function() {
643
      var _this = this;
644
      if (document.querySelector(this.selector)) {
645
        return this.done();
646
      } else {
647
        return setTimeout((function() {
648
          return _this.check();
649
        }), options.elements.checkInterval);
650
      }
651
    };
652
 
653
    ElementTracker.prototype.done = function() {
654
      return this.progress = 100;
655
    };
656
 
657
    return ElementTracker;
658
 
659
  })();
660
 
661
  DocumentMonitor = (function() {
662
    DocumentMonitor.prototype.states = {
663
      loading: 0,
664
      interactive: 50,
665
      complete: 100
666
    };
667
 
668
    function DocumentMonitor() {
669
      var _onreadystatechange, _ref2,
670
        _this = this;
671
      this.progress = (_ref2 = this.states[document.readyState]) != null ? _ref2 : 100;
672
      _onreadystatechange = document.onreadystatechange;
673
      document.onreadystatechange = function() {
674
        if (_this.states[document.readyState] != null) {
675
          _this.progress = _this.states[document.readyState];
676
        }
677
        return typeof _onreadystatechange === "function" ? _onreadystatechange.apply(null, arguments) : void 0;
678
      };
679
    }
680
 
681
    return DocumentMonitor;
682
 
683
  })();
684
 
685
  EventLagMonitor = (function() {
686
    function EventLagMonitor() {
687
      var avg, interval, last, points, samples,
688
        _this = this;
689
      this.progress = 0;
690
      avg = 0;
691
      samples = [];
692
      points = 0;
693
      last = now();
694
      interval = setInterval(function() {
695
        var diff;
696
        diff = now() - last - 50;
697
        last = now();
698
        samples.push(diff);
699
        if (samples.length > options.eventLag.sampleCount) {
700
          samples.shift();
701
        }
702
        avg = avgAmplitude(samples);
703
        if (++points >= options.eventLag.minSamples && avg < options.eventLag.lagThreshold) {
704
          _this.progress = 100;
705
          return clearInterval(interval);
706
        } else {
707
          return _this.progress = 100 * (3 / (avg + 3));
708
        }
709
      }, 50);
710
    }
711
 
712
    return EventLagMonitor;
713
 
714
  })();
715
 
716
  Scaler = (function() {
717
    function Scaler(source) {
718
      this.source = source;
719
      this.last = this.sinceLastUpdate = 0;
720
      this.rate = options.initialRate;
721
      this.catchup = 0;
722
      this.progress = this.lastProgress = 0;
723
      if (this.source != null) {
724
        this.progress = result(this.source, 'progress');
725
      }
726
    }
727
 
728
    Scaler.prototype.tick = function(frameTime, val) {
729
      var scaling;
730
      if (val == null) {
731
        val = result(this.source, 'progress');
732
      }
733
      if (val >= 100) {
734
        this.done = true;
735
      }
736
      if (val === this.last) {
737
        this.sinceLastUpdate += frameTime;
738
      } else {
739
        if (this.sinceLastUpdate) {
740
          this.rate = (val - this.last) / this.sinceLastUpdate;
741
        }
742
        this.catchup = (val - this.progress) / options.catchupTime;
743
        this.sinceLastUpdate = 0;
744
        this.last = val;
745
      }
746
      if (val > this.progress) {
747
        this.progress += this.catchup * frameTime;
748
      }
749
      scaling = 1 - Math.pow(this.progress / 100, options.easeFactor);
750
      this.progress += scaling * this.rate * frameTime;
751
      this.progress = Math.min(this.lastProgress + options.maxProgressPerFrame, this.progress);
752
      this.progress = Math.max(0, this.progress);
753
      this.progress = Math.min(100, this.progress);
754
      this.lastProgress = this.progress;
755
      return this.progress;
756
    };
757
 
758
    return Scaler;
759
 
760
  })();
761
 
762
  sources = null;
763
 
764
  scalers = null;
765
 
766
  bar = null;
767
 
768
  uniScaler = null;
769
 
770
  animation = null;
771
 
772
  cancelAnimation = null;
773
 
774
  Pace.running = false;
775
 
776
  handlePushState = function() {
777
    if (options.restartOnPushState) {
778
      return Pace.restart();
779
    }
780
  };
781
 
782
  if (window.history.pushState != null) {
783
    _pushState = window.history.pushState;
784
    window.history.pushState = function() {
785
      handlePushState();
786
      return _pushState.apply(window.history, arguments);
787
    };
788
  }
789
 
790
  if (window.history.replaceState != null) {
791
    _replaceState = window.history.replaceState;
792
    window.history.replaceState = function() {
793
      handlePushState();
794
      return _replaceState.apply(window.history, arguments);
795
    };
796
  }
797
 
798
  SOURCE_KEYS = {
799
    ajax: AjaxMonitor,
800
    elements: ElementMonitor,
801
    document: DocumentMonitor,
802
    eventLag: EventLagMonitor
803
  };
804
 
805
  (init = function() {
806
    var type, _j, _k, _len1, _len2, _ref2, _ref3, _ref4;
807
    Pace.sources = sources = [];
808
    _ref2 = ['ajax', 'elements', 'document', 'eventLag'];
809
    for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
810
      type = _ref2[_j];
811
      if (options[type] !== false) {
812
        sources.push(new SOURCE_KEYS[type](options[type]));
813
      }
814
    }
815
    _ref4 = (_ref3 = options.extraSources) != null ? _ref3 : [];
816
    for (_k = 0, _len2 = _ref4.length; _k < _len2; _k++) {
817
      source = _ref4[_k];
818
      sources.push(new source(options));
819
    }
820
    Pace.bar = bar = new Bar;
821
    scalers = [];
822
    return uniScaler = new Scaler;
823
  })();
824
 
825
  Pace.stop = function() {
826
    Pace.trigger('stop');
827
    Pace.running = false;
828
    bar.destroy();
829
    cancelAnimation = true;
830
    if (animation != null) {
831
      if (typeof cancelAnimationFrame === "function") {
832
        cancelAnimationFrame(animation);
833
      }
834
      animation = null;
835
    }
836
    return init();
837
  };
838
 
839
  Pace.restart = function() {
840
    Pace.trigger('restart');
841
    Pace.stop();
842
    return Pace.start();
843
  };
844
 
845
  Pace.go = function() {
846
    var start;
847
    Pace.running = true;
848
    bar.render();
849
    start = now();
850
    cancelAnimation = false;
851
    return animation = runAnimation(function(frameTime, enqueueNextFrame) {
852
      var avg, count, done, element, elements, i, j, remaining, scaler, scalerList, sum, _j, _k, _len1, _len2, _ref2;
853
      remaining = 100 - bar.progress;
854
      count = sum = 0;
855
      done = true;
856
      for (i = _j = 0, _len1 = sources.length; _j < _len1; i = ++_j) {
857
        source = sources[i];
858
        scalerList = scalers[i] != null ? scalers[i] : scalers[i] = [];
859
        elements = (_ref2 = source.elements) != null ? _ref2 : [source];
860
        for (j = _k = 0, _len2 = elements.length; _k < _len2; j = ++_k) {
861
          element = elements[j];
862
          scaler = scalerList[j] != null ? scalerList[j] : scalerList[j] = new Scaler(element);
863
          done &= scaler.done;
864
          if (scaler.done) {
865
            continue;
866
          }
867
          count++;
868
          sum += scaler.tick(frameTime);
869
        }
870
      }
871
      avg = sum / count;
872
      bar.update(uniScaler.tick(frameTime, avg));
873
      if (bar.done() || done || cancelAnimation) {
874
        bar.update(100);
875
        Pace.trigger('done');
876
        return setTimeout(function() {
877
          bar.finish();
878
          Pace.running = false;
879
          return Pace.trigger('hide');
880
        }, Math.max(options.ghostTime, Math.max(options.minTime - (now() - start), 0)));
881
      } else {
882
        return enqueueNextFrame();
883
      }
884
    });
885
  };
886
 
887
  Pace.start = function(_options) {
888
    extend(options, _options);
889
    Pace.running = true;
890
    try {
891
      bar.render();
892
    } catch (_error) {
893
      NoTargetError = _error;
894
    }
895
    if (!document.querySelector('.pace')) {
896
      return setTimeout(Pace.start, 50);
897
    } else {
898
      Pace.trigger('start');
899
      return Pace.go();
900
    }
901
  };
902
 
903
  if (typeof define === 'function' && define.amd) {
904
    define(function() {
905
      return Pace;
906
    });
907
  } else if (typeof exports === 'object') {
908
    module.exports = Pace;
909
  } else {
910
    if (options.startOnPageLoad) {
911
      Pace.start();
912
    }
913
  }
914
 
915
}).call(this);