implement choo v5 API (#425)
* implement choo v5 API * fix tests * 5.0.0-3 * 4kb now * bump nanomorph * 5.0.0-4 * fix missing deps stuff * 5.0.0-5 * fix travis * fixup! oops standard * chooexpose * fix state passing * fixup! update example * fixup! incorporate feedback * fixup! more docs * fixup! even more docs
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
var document = require('global/document')
|
||||
var window = require('global/window')
|
||||
var assert = require('assert')
|
||||
|
||||
module.exports = history
|
||||
|
||||
// listen to html5 pushstate events
|
||||
// and update router accordingly
|
||||
// fn(str) -> null
|
||||
function history (cb) {
|
||||
assert.equal(typeof cb, 'function', 'sheet-router/history: cb must be a function')
|
||||
window.onpopstate = function () {
|
||||
cb({
|
||||
pathname: document.location.pathname,
|
||||
search: document.location.search,
|
||||
href: document.location.href,
|
||||
hash: document.location.hash
|
||||
})
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
var window = require('global/window')
|
||||
var assert = require('assert')
|
||||
|
||||
module.exports = href
|
||||
|
||||
var noRoutingAttrName = 'data-no-routing'
|
||||
|
||||
// handle a click if is anchor tag with an href
|
||||
// and url lives on the same domain. Replaces
|
||||
// trailing '#' so empty links work as expected.
|
||||
// (fn(str), obj?) -> undefined
|
||||
function href (cb, root) {
|
||||
assert.equal(typeof cb, 'function', 'sheet-router/href: cb must be a function')
|
||||
|
||||
window.onclick = function (e) {
|
||||
if ((e.button && e.button !== 0) || e.ctrlKey || e.metaKey || e.altKey || e.shiftKey) return
|
||||
|
||||
var node = (function traverse (node) {
|
||||
if (!node || node === root) return
|
||||
if (node.localName !== 'a') return traverse(node.parentNode)
|
||||
if (node.href === undefined) return traverse(node.parentNode)
|
||||
if (window.location.host !== node.host) return traverse(node.parentNode)
|
||||
return node
|
||||
})(e.target)
|
||||
|
||||
if (!node) return
|
||||
|
||||
var isRoutingDisabled = node.hasAttribute(noRoutingAttrName)
|
||||
if (isRoutingDisabled) return
|
||||
|
||||
e.preventDefault()
|
||||
cb(node)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user