@@ -34,6 +34,12 @@ defaultOptions =
3434 # Should pace automatically restart when a Backbone route change occurs? Can also be an
3535 # array of route names. Ignored if Backbone.js is not included on the page.
3636 restartOnBackboneRoute : true
37+
38+ # Should we show the progress bar for every ajax request (not just regular or ajax-y page
39+ # navigation)? Set to false to disable.
40+ #
41+ # If so, how many ms does the request have to be running for before we show the progress?
42+ restartOnRequestAfter : 50
3743
3844 # What element should the pace element be appended to on the page?
3945 target : ' body'
@@ -272,18 +278,42 @@ class RequestIntercept extends Events
272278
273279 extendNative window .WebSocket , _WebSocket
274280
275- intercept = null
276- createIntercept = ->
277- if not intercept? and options .ajax isnt false
278- intercept = new RequestIntercept
281+ _intercept = null
282+ getIntercept = ->
283+ if not _intercept?
284+ _intercept = new RequestIntercept
285+ _intercept
286+
287+ if options .restartOnRequestAfter isnt false
288+ # If we want to start the progress bar
289+ # on every request, we need to hear the request
290+ # and then inject it into the new ajax monitor
291+ # start will have created.
292+
293+ getIntercept ().on ' request' , ({type, request}) ->
294+ if not Pace .running
295+ args = arguments
296+
297+ setTimeout ->
298+ if type is ' socket'
299+ stillActive = request .readyState < 2
300+ else
301+ stillActive = 0 < request .readyState < 4
302+
303+ if stillActive
304+ Pace .restart ()
305+
306+ for source in Pace .sources
307+ if source instanceof AjaxMonitor
308+ source .watch args...
309+ break
310+ , options .restartOnRequestAfter
279311
280312class AjaxMonitor
281313 constructor : ->
282314 @elements = []
283315
284- createIntercept ()
285-
286- intercept .on ' request' , => @ watch arguments ...
316+ getIntercept ().on ' request' , => @ watch arguments ...
287317
288318 watch : ({type, request}) ->
289319 if type is ' socket'
@@ -456,6 +486,7 @@ bar = null
456486uniScaler = null
457487animation = null
458488cancelAnimation = null
489+ Pace .running = false
459490
460491handlePushState = ->
461492 if options .restartOnPushState
@@ -526,6 +557,8 @@ do init = ->
526557 uniScaler = new Scaler
527558
528559Pace .stop = ->
560+ Pace .running = false
561+
529562 bar .destroy ()
530563
531564 # Not all browsers support cancelAnimationFrame
@@ -542,6 +575,8 @@ Pace.restart = ->
542575 Pace .go ()
543576
544577Pace .go = ->
578+ Pace .running = true
579+
545580 bar .render ()
546581
547582 cancelAnimation = false
@@ -584,13 +619,17 @@ Pace.go = ->
584619
585620 setTimeout ->
586621 bar .finish ()
622+
623+ Pace .running = false
587624 , Math .max (options .ghostTime , Math .min (options .minTime , now () - start))
588625 else
589626 enqueueNextFrame ()
590627
591628Pace .start = (_options ) ->
592629 extend options, _options
593630
631+ Pace .running = true
632+
594633 try
595634 bar .render ()
596635 catch NoTargetError
0 commit comments