dom-router
Advanced tools
Comparing version 3.1.6 to 4.0.1
@@ -5,5 +5,5 @@ /** | ||
* @author Jason Mulligan <jason.mulligan@avoidwork.com> | ||
* @copyright 2021 | ||
* @copyright 2022 | ||
* @license BSD-3-Clause | ||
* @version 3.1.6 | ||
* @version 4.0.0 | ||
*/ | ||
@@ -46,23 +46,9 @@ | ||
current () { | ||
return this.history[0]; | ||
return this.history[this.history.length - 1]; | ||
} | ||
hashchange (ev) { | ||
if (this.stop) { | ||
if ("stopPropagation" in ev && typeof ev.stopPropagation === "function") { | ||
ev.stopPropagation(); | ||
} | ||
handler () { | ||
const oldHash = this.history.length > 0 ? (this.current().hash || "").replace(not_hash, "") || null : null, | ||
newHash = includes(location.hash, "#") ? location.hash.replace(not_hash, "") : null; | ||
if ("preventDefault" in ev && typeof ev.preventDefault === "function") { | ||
ev.preventDefault(); | ||
} | ||
} | ||
this.handler(ev); | ||
} | ||
handler (ev) { | ||
const oldHash = includes(ev.oldURL, "#") ? ev.oldURL.replace(not_hash, "") : null, | ||
newHash = includes(ev.newURL, "#") ? ev.newURL.replace(not_hash, "") : null; | ||
if (this.active && this.valid(newHash)) { | ||
@@ -138,5 +124,3 @@ if (!includes(this.routes, newHash)) { | ||
log (arg) { | ||
if (this.logging) { | ||
this.history.unshift(arg); | ||
} | ||
this.history.push(this.logging ? arg : {hash: arg.hash}); | ||
@@ -146,2 +130,6 @@ return this; | ||
popstate (ev) { | ||
this.handler(ev); | ||
} | ||
process () { | ||
@@ -154,3 +142,3 @@ const hash = document.location.hash.replace("#", ""); | ||
if (hash.length > 0 && includes(this.routes, hash)) { | ||
this.handler({oldURL: "", newURL: document.location.hash}); | ||
this.handler(); | ||
} else { | ||
@@ -172,4 +160,10 @@ this.route(this.start); | ||
scan (arg) { | ||
scan (arg = "") { | ||
this.routes = Array.from(new Set(this.select("a").filter(i => includes(i.href, "#")).map(i => i.href.replace(not_hash, "")).filter(i => i !== ""))); | ||
if (arg.length > 0) { | ||
this.routes.push(arg); | ||
this.routes = Array.from(new Set(this.routes)); | ||
} | ||
this.start = arg || this.routes[0] || null; | ||
@@ -195,8 +189,8 @@ | ||
obj.hashchange = obj.hashchange.bind(obj); | ||
obj.popstate = obj.popstate.bind(obj); | ||
if ("addEventListener" in window) { | ||
window.addEventListener("hashchange", obj.hashchange, false); | ||
window.addEventListener("popstate", obj.popstate, false); | ||
} else { | ||
window.onhashchange = obj.hashchange; | ||
window.onpopstate = obj.popstate; | ||
} | ||
@@ -211,3 +205,3 @@ | ||
factory.version = "3.1.6"; | ||
factory.version = "4.0.0"; | ||
@@ -214,0 +208,0 @@ // CJS, AMD & window supported |
{ | ||
"name": "dom-router", | ||
"description": "URL hash DOM router", | ||
"version": "3.1.6", | ||
"version": "4.0.1", | ||
"homepage": "https://github.com/avoidwork/dom-router", | ||
@@ -6,0 +6,0 @@ "author": "Jason Mulligan <jason.mulligan@avoidwork.com>", |
@@ -6,3 +6,3 @@ # dom-router | ||
Imagine you didn't have to write a bunch of JavaScript to get a slick, progressively enhanced interface! `dom-router` | ||
is a URL hash to DOM router which automatically, & intelligently toggles visibility of `Elements` based on `hashchange` | ||
is a URL hash to DOM router which automatically, & intelligently toggles visibility of `Elements` based on `popstate` | ||
events. | ||
@@ -98,6 +98,6 @@ | ||
#### current() | ||
Returns the current `Route`, if logging is enabled | ||
Returns the current `Route`; if logging is enabled the trigger `Element` will be present | ||
#### hashchange(ev) | ||
Event handler, expects `{oldURL: "", newURL: ""}` | ||
#### popstate() | ||
Event handler | ||
@@ -113,5 +113,6 @@ #### scan(default) | ||
- `Element.classList` API, or shim | ||
- `popstate` Event | ||
## License | ||
Copyright (c) 2021 Jason Mulligan | ||
Copyright (c) 2022 Jason Mulligan | ||
Licensed under the BSD-3 license |
Sorry, the diff of this file is not supported yet
116
17226
167