Files
pjax/lib/proto/attach-link.js

75 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2014-10-14 08:23:56 +02:00
require("../polyfills/Function.prototype.bind")
2014-05-22 06:20:38 +02:00
var on = require("../events/on")
var clone = require("../clone")
var attrClick = "data-pjax-click-state"
var attrKey = "data-pjax-keyup-state"
var linkAction = function(el, event) {
// Dont 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()
// dont do "nothing" if user try to reload the page by clicking the same link twice
if (el.href === window.location.href.split("#")[0]) {
el.setAttribute(attrClick, "reload")
this.reload()
2014-05-22 06:20:38 +02:00
return
}
el.setAttribute(attrClick, "load")
this.loadUrl(el.href, clone(this.options))
}
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) {
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) {
// Dont 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
}
2014-10-14 11:42:36 +02:00
if (event.keyCode == 13) {
linkAction.call(that, el, event)
2014-05-22 06:20:38 +02:00
}
}.bind(this))
}