@pmwcs/base
Version:
PMWCS base module
47 lines (39 loc) • 1.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.onLocationChange = onLocationChange;
var _isBrowserEnv = require("./isBrowserEnv");
/**
* wrapper around history functions to create custom event
* 'locationchange'
*/
function onLocationChange() {
if (onLocationChange.wrapped || !(0, _isBrowserEnv.isBrowserEnv)()) return;
onLocationChange.wrapped = true;
history.pushState = function (f) {
return function pushState() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var ret = f.apply(this, args);
window.dispatchEvent(new Event('pushstate'));
window.dispatchEvent(new Event('locationchange'));
return ret;
};
}(history.pushState);
history.replaceState = function (f) {
return function replaceState() {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
var ret = f.apply(this, args);
window.dispatchEvent(new Event('replacestate'));
window.dispatchEvent(new Event('locationchange'));
return ret;
};
}(history.replaceState);
window.addEventListener('popstate', function () {
window.dispatchEvent(new Event('locationchange'));
});
}