turbolinks
Version:
Turbolinks makes navigating your web application faster
163 lines (130 loc) • 4.5 kB
text/coffeescript
#= require ./http_request
class Turbolinks.Visit
constructor: ( , location, ) ->
= Turbolinks.uuid()
= Turbolinks.Location.wrap(location)
= .adapter
= "initialized"
= {}
start: ->
if is "initialized"
= "started"
.visitStarted(this)
cancel: ->
if is "started"
?.cancel()
= "canceled"
complete: ->
if is "started"
= "completed"
.visitCompleted?(this)
.visitCompleted(this)
fail: ->
if is "started"
= "failed"
.visitFailed?(this)
changeHistory: ->
unless
actionForHistory = if .isEqualTo( ) then "replace" else
method = getHistoryMethodForAction(actionForHistory)
[method]( , )
= true
issueRequest: ->
if and not ?
= 0
= new Turbolinks.HttpRequest this, ,
.send()
getCachedSnapshot: ->
if snapshot = .getCachedSnapshotForLocation( )
if not .anchor? or snapshot.hasAnchor( .anchor)
if is "restore" or snapshot.isPreviewable()
snapshot
hasCachedSnapshot: ->
?
loadCachedSnapshot: ->
if snapshot =
isPreview =
->
.render({snapshot, isPreview}, )
.visitRendered?(this)
unless isPreview
loadResponse: ->
if ?
->
if .failed
.render(error: , )
.visitRendered?(this)
else
.render(snapshot: , )
.visitRendered?(this)
followRedirect: ->
if and not
=
.replaceHistoryWithLocationAndRestorationIdentifier( , )
= true
# HTTP Request delegate
requestStarted: ->
.visitRequestStarted?(this)
requestProgressed: ( ) ->
.visitRequestProgressed?(this)
requestCompletedWithResponse: ( , redirectedToLocation) ->
= Turbolinks.Location.wrap(redirectedToLocation) if redirectedToLocation?
.visitRequestCompleted(this)
requestFailedWithStatusCode: (statusCode, ) ->
.visitRequestFailedWithStatusCode(this, statusCode)
requestFinished: ->
.visitRequestFinished?(this)
# Scrolling
performScroll: =>
unless
if is "restore"
or
else
or
= true
scrollToRestoredPosition: ->
position = ?.scrollPosition
if position?
.scrollToPosition(position)
true
scrollToAnchor: ->
if .anchor?
.scrollToAnchor( .anchor)
true
scrollToTop: ->
.scrollToPosition(x: 0, y: 0)
# Instrumentation
recordTimingMetric: (name) ->
[name] ?= new Date().getTime()
getTimingMetrics: ->
Turbolinks.copyObject( )
# Private
getHistoryMethodForAction = (action) ->
switch action
when "replace" then "replaceHistoryWithLocationAndRestorationIdentifier"
when "advance", "restore" then "pushHistoryWithLocationAndRestorationIdentifier"
shouldIssueRequest: ->
if is "restore"
not
else
true
cacheSnapshot: ->
unless
.cacheSnapshot()
= true
render: (callback) ->
= requestAnimationFrame =>
= null
callback.call(this)
cancelRender: ->
cancelAnimationFrame( ) if