2019-02-11 23:17:28 -05:00
|
|
|
var clone = require("../util/clone");
|
|
|
|
|
var newUid = require("../uniqueid");
|
|
|
|
|
var trigger = require("../events/trigger");
|
2018-03-15 16:12:32 -04:00
|
|
|
|
2018-04-26 09:27:50 -04:00
|
|
|
module.exports = function(responseText, request, href, options) {
|
2019-02-11 23:17:28 -05:00
|
|
|
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) {
|
2019-02-11 23:17:28 -05:00
|
|
|
trigger(document, "pjax:complete pjax:error", options);
|
2018-03-15 16:12:32 -04:00
|
|
|
|
2019-02-11 23:17:28 -05:00
|
|
|
return;
|
2018-03-15 16:12:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// push scroll position to history
|
2019-02-11 23:17:28 -05:00
|
|
|
var currentState = window.history.state || {};
|
|
|
|
|
window.history.replaceState(
|
|
|
|
|
{
|
2018-03-15 16:12:32 -04:00
|
|
|
url: currentState.url || window.location.href,
|
|
|
|
|
title: currentState.title || document.title,
|
|
|
|
|
uid: currentState.uid || newUid(),
|
2019-02-11 23:17:28 -05:00
|
|
|
scrollPos: [
|
|
|
|
|
document.documentElement.scrollLeft || document.body.scrollLeft,
|
|
|
|
|
document.documentElement.scrollTop || document.body.scrollTop
|
|
|
|
|
]
|
2018-03-15 16:12:32 -04:00
|
|
|
},
|
2019-02-11 23:17:28 -05:00
|
|
|
document.title,
|
|
|
|
|
window.location.href
|
|
|
|
|
);
|
2018-03-15 16:12:32 -04:00
|
|
|
|
|
|
|
|
// Check for redirects
|
2019-02-11 23:17:28 -05:00
|
|
|
var oldHref = href;
|
2018-03-15 16:12:32 -04:00
|
|
|
if (request.responseURL) {
|
|
|
|
|
if (href !== request.responseURL) {
|
2019-02-11 23:17:28 -05:00
|
|
|
href = request.responseURL;
|
2018-03-15 16:12:32 -04:00
|
|
|
}
|
2019-02-11 23:17:28 -05:00
|
|
|
} 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");
|
2018-03-15 16:12:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add back the hash if it was removed
|
2019-02-11 23:17:28 -05:00
|
|
|
var a = document.createElement("a");
|
|
|
|
|
a.href = oldHref;
|
|
|
|
|
var oldHash = a.hash;
|
|
|
|
|
a.href = href;
|
2018-03-15 16:12:32 -04:00
|
|
|
if (oldHash && !a.hash) {
|
2019-02-11 23:17:28 -05:00
|
|
|
a.hash = oldHash;
|
|
|
|
|
href = a.href;
|
2018-03-15 16:12:32 -04:00
|
|
|
}
|
|
|
|
|
|
2019-02-11 23:17:28 -05:00
|
|
|
this.state.href = href;
|
|
|
|
|
this.state.options = options;
|
2018-03-15 16:12:32 -04:00
|
|
|
|
|
|
|
|
try {
|
2019-02-11 23:17:28 -05:00
|
|
|
this.loadContent(responseText, options);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
trigger(document, "pjax:error", options);
|
2018-03-15 16:12:32 -04:00
|
|
|
|
|
|
|
|
if (!this.options.debug) {
|
|
|
|
|
if (console && console.error) {
|
2019-02-11 23:17:28 -05:00
|
|
|
console.error("Pjax switch fail: ", e);
|
2018-03-15 16:12:32 -04:00
|
|
|
}
|
2019-02-11 23:17:28 -05:00
|
|
|
return this.latestChance(href);
|
|
|
|
|
} else {
|
|
|
|
|
throw e;
|
2018-03-15 16:12:32 -04:00
|
|
|
}
|
|
|
|
|
}
|
2019-02-11 23:17:28 -05:00
|
|
|
};
|