2018-03-15 16:12:32 -04:00
|
|
|
var clone = require("../util/clone.js")
|
|
|
|
|
var newUid = require("../uniqueid.js")
|
|
|
|
|
var trigger = require("../events/trigger.js")
|
|
|
|
|
|
2018-04-26 09:27:50 -04:00
|
|
|
module.exports = function(responseText, request, href, options) {
|
|
|
|
|
options = clone(options || this.options)
|
|
|
|
|
options.request = request
|
2018-03-15 16:12:32 -04:00
|
|
|
|
|
|
|
|
// Fail if unable to load HTML via AJAX
|
|
|
|
|
if (responseText === false) {
|
2018-04-26 09:27:50 -04:00
|
|
|
trigger(document, "pjax:complete pjax:error", options)
|
2018-03-15 16:12:32 -04:00
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// push scroll position to history
|
|
|
|
|
var currentState = window.history.state || {}
|
|
|
|
|
window.history.replaceState({
|
|
|
|
|
url: currentState.url || window.location.href,
|
|
|
|
|
title: currentState.title || document.title,
|
|
|
|
|
uid: currentState.uid || newUid(),
|
|
|
|
|
scrollPos: [document.documentElement.scrollLeft || document.body.scrollLeft,
|
|
|
|
|
document.documentElement.scrollTop || document.body.scrollTop]
|
|
|
|
|
},
|
|
|
|
|
document.title, window.location)
|
|
|
|
|
|
|
|
|
|
// Check for redirects
|
|
|
|
|
var oldHref = href
|
|
|
|
|
if (request.responseURL) {
|
|
|
|
|
if (href !== request.responseURL) {
|
|
|
|
|
href = request.responseURL
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (request.getResponseHeader("X-PJAX-URL")) {
|
|
|
|
|
href = request.getResponseHeader("X-PJAX-URL")
|
|
|
|
|
}
|
|
|
|
|
else if (request.getResponseHeader("X-XHR-Redirected-To")) {
|
|
|
|
|
href = request.getResponseHeader("X-XHR-Redirected-To")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add back the hash if it was removed
|
|
|
|
|
var a = document.createElement("a")
|
|
|
|
|
a.href = oldHref
|
|
|
|
|
var oldHash = a.hash
|
|
|
|
|
a.href = href
|
|
|
|
|
if (oldHash && !a.hash) {
|
|
|
|
|
a.hash = oldHash
|
|
|
|
|
href = a.href
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.state.href = href
|
2018-04-26 09:27:50 -04:00
|
|
|
this.state.options = options
|
2018-03-15 16:12:32 -04:00
|
|
|
|
|
|
|
|
try {
|
2018-07-22 18:40:33 -04:00
|
|
|
this.loadContent(responseText, options)
|
2018-03-15 16:12:32 -04:00
|
|
|
}
|
|
|
|
|
catch (e) {
|
2018-04-26 09:27:50 -04:00
|
|
|
trigger(document, "pjax:error", options)
|
2018-03-15 16:12:32 -04:00
|
|
|
|
|
|
|
|
if (!this.options.debug) {
|
|
|
|
|
if (console && console.error) {
|
|
|
|
|
console.error("Pjax switch fail: ", e)
|
|
|
|
|
}
|
|
|
|
|
return this.latestChance(href)
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
throw e
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|