2014-05-22 06:20:38 +02:00
|
|
|
|
var on = require("../events/on")
|
2018-03-06 10:06:38 +00:00
|
|
|
|
var clone = require("../util/clone")
|
2014-05-22 06:20:38 +02:00
|
|
|
|
|
|
|
|
|
|
var attrClick = "data-pjax-click-state"
|
|
|
|
|
|
var attrKey = "data-pjax-keyup-state"
|
|
|
|
|
|
|
|
|
|
|
|
var linkAction = function(el, event) {
|
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
|
2018-02-02 09:52:44 -05:00
|
|
|
|
var options = clone(this.options)
|
2018-01-23 13:22:31 -05:00
|
|
|
|
|
2014-05-22 06:20:38 +02:00
|
|
|
|
// Don’t break browser special behavior on links (like page in new window)
|
|
|
|
|
|
if (event.which > 1 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
|
|
|
|
|
|
el.setAttribute(attrClick, "modifier")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
if (el.protocol !== window.location.protocol || el.host !== window.location.host) {
|
|
|
|
|
|
el.setAttribute(attrClick, "external")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Ignore click if we are on an anchor on the same page
|
|
|
|
|
|
if (el.pathname === window.location.pathname && el.hash.length > 0) {
|
|
|
|
|
|
el.setAttribute(attrClick, "anchor-present")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Ignore anchors on the same page (keep native behavior)
|
|
|
|
|
|
if (el.hash && el.href.replace(el.hash, "") === window.location.href.replace(location.hash, "")) {
|
|
|
|
|
|
el.setAttribute(attrClick, "anchor")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Ignore empty anchor "foo.html#"
|
|
|
|
|
|
if (el.href === window.location.href.split("#")[0] + "#") {
|
|
|
|
|
|
el.setAttribute(attrClick, "anchor-empty")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
|
|
|
|
|
|
// don’t do "nothing" if user try to reload the page by clicking the same link twice
|
2016-03-24 12:38:15 +01:00
|
|
|
|
if (
|
|
|
|
|
|
this.options.currentUrlFullReload &&
|
|
|
|
|
|
el.href === window.location.href.split("#")[0]
|
|
|
|
|
|
) {
|
2015-01-28 18:27:16 -08:00
|
|
|
|
el.setAttribute(attrClick, "reload")
|
|
|
|
|
|
this.reload()
|
2014-05-22 06:20:38 +02:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2018-01-23 13:22:31 -05:00
|
|
|
|
|
2014-05-22 06:20:38 +02:00
|
|
|
|
el.setAttribute(attrClick, "load")
|
2017-12-21 00:12:09 -05:00
|
|
|
|
|
|
|
|
|
|
options.triggerElement = el
|
|
|
|
|
|
this.loadUrl(el.href, options)
|
2014-05-22 06:20:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-01-04 23:09:04 +11:00
|
|
|
|
var isDefaultPrevented = function(event) {
|
2018-02-02 09:52:44 -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) {
|
2014-10-14 11:42:36 +02:00
|
|
|
|
var that = this
|
2014-05-22 06:20:38 +02:00
|
|
|
|
|
|
|
|
|
|
on(el, "click", function(event) {
|
2016-01-04 23:09:04 +11:00
|
|
|
|
if (isDefaultPrevented(event)) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-10-14 11:42:36 +02:00
|
|
|
|
linkAction.call(that, el, event)
|
2014-05-22 06:20:38 +02:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
on(el, "keyup", function(event) {
|
2016-01-04 23:09:04 +11:00
|
|
|
|
if (isDefaultPrevented(event)) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-22 06:20:38 +02:00
|
|
|
|
// Don’t break browser special behavior on links (like page in new window)
|
|
|
|
|
|
if (event.which > 1 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
|
|
|
|
|
|
el.setAttribute(attrKey, "modifier")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-02 09:52:44 -05:00
|
|
|
|
if (event.keyCode === 13) {
|
2014-10-14 11:42:36 +02:00
|
|
|
|
linkAction.call(that, el, event)
|
2014-05-22 06:20:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
}.bind(this))
|
|
|
|
|
|
}
|