@@ -15,10 +15,6 @@ defaultOptions =
1515 # update before disappearing
1616 ghostTime : 250
1717
18- # How frequently in ms should we check for the elements being tested for
19- # using the element monitor?
20- elementCheckInterval : 100
21-
2218 # Its easy for a bunch of the bar to be eaten in the first few frames
2319 # before we know how much there is to load. This limits how much of
2420 # the bar can be used per frame
@@ -27,6 +23,18 @@ defaultOptions =
2723 # This tweaks the animation easing
2824 easeFactor : 1.25
2925
26+ # Should we restart the browser when pushState or replaceState is called? (Generally
27+ # means ajax navigation has occured)
28+ restartOnPushState : true
29+
30+ elements :
31+ # How frequently in ms should we check for the elements being tested for
32+ # using the element monitor?
33+ checkInterval : 100
34+
35+ # What elements should we wait for before deciding the page is fully loaded (not required)
36+ selectors : [' body' ]
37+
3038now = ->
3139 performance ? .now ? () ? + new Date
3240
@@ -62,7 +70,10 @@ result = (obj, key, args...) ->
6270extend = (out , sources ... ) ->
6371 for source in sources when source
6472 for own key, val of source
65- out[key] = val
73+ if out[key]? and typeof out[key] is ' object' and val? and typeof val is ' object'
74+ extend (out[key], val)
75+ else
76+ out[key] = val
6677 out
6778
6879getOptionsFromDOM = ->
@@ -217,29 +228,28 @@ class RequestTracker
217228 _onreadystatechange? (arguments ... )
218229
219230class ElementMonitor
220- constructor : (selectors ... ) ->
231+ constructor : (options = {} ) ->
221232 @elements = []
222233
223- for set in selectors
224- @elements .push new ElementTracker set
234+ options .selectors ?= []
235+ for selector in options .selectors
236+ @elements .push new ElementTracker selector
225237
226238class ElementTracker
227- constructor : (@selectors ) ->
239+ constructor : (@selector ) ->
228240 @progress = 0
229241
230- if typeof @selectors is ' string'
231- @selectors = [@selectors ]
232-
233242 @ check ()
234243
235244 check : ->
236- matches = document .querySelectorAll (@selectors .join (' ,' ))
237-
238- if matches .length
239- @progress = 100 * matches .length / @selectors .length
245+ if document .querySelector (@selector )
246+ @ done ()
240247 else
241248 setTimeout (=> @ check ()),
242- options .elementCheckInterval
249+ options .elements .checkInterval
250+
251+ done : ->
252+ @progress = 100
243253
244254class DocumentMonitor
245255 states :
@@ -316,10 +326,11 @@ class Scaler
316326 # know it's done.
317327 @progress += scaling * @rate * frameTime
318328
329+ @progress = Math .min (@lastProgress + options .maxProgressPerFrame , @progress )
330+
319331 @progress = Math .max (0 , @progress )
320332 @progress = Math .min (100 , @progress )
321333
322- @progress = Math .min (@lastProgress + options .maxProgressPerFrame , @progress )
323334 @lastProgress = @progress
324335
325336 @progress
@@ -331,24 +342,38 @@ uniScaler = null
331342animation = null
332343cancelAnimation = null
333344
345+ handlePushState = ->
346+ if options .restartOnPushState
347+ Pace .restart ()
348+
334349# We reset the bar whenever it looks like an ajax navigation has occured.
335350if window .pushState ?
336351 _pushState = window .pushState
337352 window .pushState = ->
338- handlePageChange ()
353+ handlePushState ()
339354
340355 _pushState arguments ...
341356
342357if window .replaceState ?
343358 _replaceState = window .replaceState
344359 window .replaceState = ->
345- handlePageChange ()
360+ handlePushState ()
346361
347362 _replaceState arguments ...
348363
364+ SOURCE_KEYS =
365+ ajax : AjaxMonitor
366+ elements : ElementMonitor
367+ document : DocumentMonitor
368+ eventLag : EventLagMonitor
369+
349370do init = ->
350- sources = [new AjaxMonitor , new ElementMonitor (' body' ), new DocumentMonitor , new EventLagMonitor ]
351-
371+ sources = options .extraSources ? []
372+
373+ for type in [' ajax' , ' elements' , ' document' , ' eventLag' ]
374+ if options[type] isnt false
375+ sources .push new ELEMENT_KEYS [type](options[type])
376+
352377 bar = new Bar
353378
354379 # Each source of progress data has it's own scaler to smooth its output
@@ -358,7 +383,7 @@ do init = ->
358383 # remove sources
359384 uniScaler = new Scaler
360385
361- Pace .reset = ->
386+ Pace .stop = ->
362387 bar .destroy ()
363388
364389 # Not all browsers support cancelAnimationFrame
@@ -370,8 +395,8 @@ Pace.reset = ->
370395
371396 init ()
372397
373- handlePageChange = ->
374- Pace .reset ()
398+ Pace . restart = ->
399+ Pace .stop ()
375400 Pace .go ()
376401
377402Page .go = ->
@@ -421,7 +446,9 @@ Page.go = ->
421446 else
422447 enqueueNextFrame ()
423448
424- Pace .start = ->
449+ Pace .start = (_options ) ->
450+ extend options, _options
451+
425452 bar .render ()
426453
427454 # It's usually possible to render a bit before the document declares itself ready
0 commit comments