84348047 |
import * as R from "ramda";
|
30db14b1 |
|
77ace510 |
function wrapEvent(target, name, obj) {
|
30db14b1 |
target.addEventListener(name, obj);
}
|
f62307c9 |
function runOnceFor(object, flag, cb) {
|
30db14b1 |
if (!object[flag]) {
object[flag] = true;
cb();
}
}
let MISSING_CHANGE_URL = Symbol("missing_change_url");
export default function addChangeUrlEvent(window) {
|
f62307c9 |
runOnceFor(window, MISSING_CHANGE_URL, () => {
|
84348047 |
const changeUrlEventCreator = {
|
30db14b1 |
lastLocation: null,
|
7a0c8d3f |
handleEvent(_) {
|
84348047 |
// interface for EventListener
|
7b1d07fc |
|
84348047 |
let { hash, host, hostname, origin, href, pathname, port, protocol } =
window.location || {};
|
30db14b1 |
// store in object for comparison
|
84348047 |
const pushedLocation = {
hash,
host,
hostname,
origin,
href,
pathname,
port,
protocol
};
|
30db14b1 |
// only dispatch action when url has actually changed so same link can be clicked repeatedly.
if (!R.equals(pushedLocation, this.lastLocation)) {
|
84348047 |
var urlChangeEvent = new CustomEvent("urlchanged", {
detail: pushedLocation
});
|
30db14b1 |
window.dispatchEvent(urlChangeEvent);
this.lastLocation = pushedLocation;
}
}
|
84348047 |
};
|
30db14b1 |
|
27fb05ae |
// / make sure we fire urlchanged for these
|
84348047 |
wrapEvent(window, "popstate", changeUrlEventCreator);
wrapEvent(window, "pushstate", changeUrlEventCreator);
wrapEvent(window, "replacestate", changeUrlEventCreator);
|
30db14b1 |
});
}
|