Fix and improved hasher routing

This commit is contained in:
the-djmaze 2022-03-03 16:28:05 +01:00
parent 06e2dc722d
commit fafe56ecb4
16 changed files with 41 additions and 83 deletions

View file

@ -13,12 +13,11 @@
const
_hashValRegexp = /#(.*)$/,
_hashRegexp = /^#/,
_hashRegexp = /^[#/]+/,
_hashTrim = /^\/|\$/g,
_trimHash = hash => hash ? hash.replace(_hashTrim, '') : '',
_getWindowHash = () => {
//parsed full URL instead of getting window.location.hash because Firefox decode hash value (and all the other browsers don't)
//also because of IE8 bug with hash query in local file [issue #6]
var result = _hashValRegexp.exec( location.href );
return (result && result[1]) ? decodeURIComponent(result[1]) : '';
},
@ -26,17 +25,10 @@
if (_hash !== newHash) {
var oldHash = _hash;
_hash = newHash; //should come before event dispatch to make sure user can get proper value inside event handler
hasher.dispatch(_trimHash(newHash), _trimHash(oldHash));
}
},
_checkHistory = () => {
var windowHash = _getWindowHash();
if (windowHash !== _hash) {
_registerChange(windowHash);
_dispatch(_trimHash(newHash), _trimHash(oldHash));
}
},
_setHash = (path, replace) => {
path = path.join('/');
path = path ? '/' + path.replace(_hashRegexp, '') : path;
if (path !== _hash){
// we should store raw value
@ -51,6 +43,7 @@
}
}
},
_dispatch = (...args) => hasher.active && _bindings.forEach(callback => callback(...args)),
//--------------------------------------------------------------------------------------
// Public (API)
@ -69,7 +62,6 @@
*/
active : true,
add : callback => _bindings.push(callback),
dispatch : (...args) => hasher.active && _bindings.forEach(callback => callback(...args)),
/**
* Start listening/dispatching changes in the hash/history.
@ -77,28 +69,27 @@
* <li>hasher won't dispatch CHANGE events by manually typing a new value or pressing the back/forward buttons before calling this method.</li>
* </ul>
*/
init : () => hasher.dispatch(_trimHash(_hash)),
init : () => _dispatch(_trimHash(_hash)),
/**
* Set Hash value, generating a new history record.
* @param {...string} path Hash value without '#'.
* @example hasher.setHash('lorem', 'ipsum', 'dolor') -> '#/lorem/ipsum/dolor'
* @example hasher.setHash('lorem/ipsum/dolor') -> '#/lorem/ipsum/dolor'
*/
setHash : (...path) => _setHash(path),
setHash : path => _setHash(path),
/**
* Set Hash value without keeping previous hash on the history record.
* Similar to calling `window.location.replace("#/hash")` but will also work on IE6-7.
* @param {...string} path Hash value without '#'.
* @example hasher.replaceHash('lorem', 'ipsum', 'dolor') -> '#/lorem/ipsum/dolor'
* @example hasher.replaceHash('lorem/ipsum/dolor') -> '#/lorem/ipsum/dolor'
*/
replaceHash : (...path) => _setHash(path, true)
replaceHash : path => _setHash(path, true)
};
var _hash = _getWindowHash(),
_bindings = [];
addEventListener('hashchange', _checkHistory);
addEventListener('hashchange', () => _registerChange(_getWindowHash()));
global.hasher = hasher;
})(this);