2019-03-03 01:37:45 -05:00
|
|
|
|
var on = require("../events/on");
|
|
|
|
|
|
var clone = require("../util/clone");
|
2014-05-22 06:20:38 +02:00
|
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
|
var attrState = "data-pjax-state";
|
2014-05-22 06:20:38 +02:00
|
|
|
|
|
|
|
|
|
|
var linkAction = function(el, event) {
|
2018-04-09 23:36:32 -04:00
|
|
|
|
if (isDefaultPrevented(event)) {
|
2019-03-03 01:37:45 -05:00
|
|
|
|
return;
|
2018-04-09 23:36:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-23 13:22:31 -05:00
|
|
|
|
// Since loadUrl modifies options and we may add our own modifications below,
|
|
|
|
|
|
// clone it so the changes don't persist
|
2019-03-03 01:37:45 -05:00
|
|
|
|
var options = clone(this.options);
|
2018-01-23 13:22:31 -05:00
|
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
|
var attrValue = checkIfShouldAbort(el, event);
|
2018-04-09 23:36:32 -04:00
|
|
|
|
if (attrValue) {
|
2019-03-03 01:37:45 -05:00
|
|
|
|
el.setAttribute(attrState, attrValue);
|
|
|
|
|
|
return;
|
2018-04-09 23:36:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
|
event.preventDefault();
|
2018-04-09 23:36:32 -04:00
|
|
|
|
|
|
|
|
|
|
// don’t do "nothing" if user try to reload the page by clicking the same link twice
|
|
|
|
|
|
if (
|
|
|
|
|
|
this.options.currentUrlFullReload &&
|
|
|
|
|
|
el.href === window.location.href.split("#")[0]
|
|
|
|
|
|
) {
|
2019-03-03 01:37:45 -05:00
|
|
|
|
el.setAttribute(attrState, "reload");
|
|
|
|
|
|
this.reload();
|
|
|
|
|
|
return;
|
2018-04-09 23:36:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
|
el.setAttribute(attrState, "load");
|
2018-04-09 23:36:32 -04:00
|
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
|
options.triggerElement = el;
|
|
|
|
|
|
this.loadUrl(el.href, options);
|
|
|
|
|
|
};
|
2018-04-09 23:36:32 -04:00
|
|
|
|
|
|
|
|
|
|
function checkIfShouldAbort(el, event) {
|
2014-05-22 06:20:38 +02:00
|
|
|
|
// Don’t break browser special behavior on links (like page in new window)
|
2019-03-03 01:37:45 -05:00
|
|
|
|
if (
|
|
|
|
|
|
event.which > 1 ||
|
|
|
|
|
|
event.metaKey ||
|
|
|
|
|
|
event.ctrlKey ||
|
|
|
|
|
|
event.shiftKey ||
|
|
|
|
|
|
event.altKey
|
|
|
|
|
|
) {
|
|
|
|
|
|
return "modifier";
|
2014-05-22 06:20:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// we do test on href now to prevent unexpected behavior if for some reason
|
|
|
|
|
|
// user have href that can be dynamically updated
|
|
|
|
|
|
|
|
|
|
|
|
// Ignore external links.
|
2019-03-03 01:37:45 -05:00
|
|
|
|
if (
|
|
|
|
|
|
el.protocol !== window.location.protocol ||
|
|
|
|
|
|
el.host !== window.location.host
|
|
|
|
|
|
) {
|
|
|
|
|
|
return "external";
|
2014-05-22 06:20:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Ignore anchors on the same page (keep native behavior)
|
2019-03-03 01:37:45 -05:00
|
|
|
|
if (
|
|
|
|
|
|
el.hash &&
|
|
|
|
|
|
el.href.replace(el.hash, "") ===
|
|
|
|
|
|
window.location.href.replace(location.hash, "")
|
|
|
|
|
|
) {
|
|
|
|
|
|
return "anchor";
|
2014-05-22 06:20:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Ignore empty anchor "foo.html#"
|
|
|
|
|
|
if (el.href === window.location.href.split("#")[0] + "#") {
|
2019-03-03 01:37:45 -05:00
|
|
|
|
return "anchor-empty";
|
2014-05-22 06:20:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-01-04 23:09:04 +11:00
|
|
|
|
var isDefaultPrevented = function(event) {
|
2019-03-03 01:37:45 -05:00
|
|
|
|
return event.defaultPrevented || event.returnValue === false;
|
|
|
|
|
|
};
|
2016-01-04 23:09:04 +11:00
|
|
|
|
|
2014-05-22 06:20:38 +02:00
|
|
|
|
module.exports = function(el) {
|
2019-03-03 01:37:45 -05:00
|
|
|
|
var that = this;
|
2014-05-22 06:20:38 +02:00
|
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
|
el.setAttribute(attrState, "");
|
2016-01-04 23:09:04 +11:00
|
|
|
|
|
2018-04-09 23:36:32 -04:00
|
|
|
|
on(el, "click", function(event) {
|
2019-03-03 01:37:45 -05:00
|
|
|
|
linkAction.call(that, el, event);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
on(
|
|
|
|
|
|
el,
|
|
|
|
|
|
"keyup",
|
|
|
|
|
|
function(event) {
|
|
|
|
|
|
if (event.keyCode === 13) {
|
|
|
|
|
|
linkAction.call(that, el, event);
|
|
|
|
|
|
}
|
|
|
|
|
|
}.bind(this)
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|