mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-09-08 17:07:03 +03:00
Cleanup Crossroads & Hasher and dropped Signals
This commit is contained in:
parent
32c3f1f059
commit
b26586f2ba
8 changed files with 133 additions and 584 deletions
133
vendors/routes/hasher.js
vendored
133
vendors/routes/hasher.js
vendored
|
|
@ -8,35 +8,24 @@
|
|||
(global => {
|
||||
|
||||
//--------------------------------------------------------------------------------------
|
||||
// Private Vars
|
||||
// Private
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var
|
||||
// local storage for brevity and better compression --------------------------------
|
||||
var _hash, _bindings = [];
|
||||
|
||||
Signal = signals.Signal,
|
||||
|
||||
// local vars ----------------------------------------------------------------------
|
||||
|
||||
_hash,
|
||||
_isActive,
|
||||
_hashValRegexp = /#(.*)$/,
|
||||
_hashRegexp = /^#/;
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------
|
||||
// Private Methods
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
const _trimHash = hash => hash ? hash.replace(new RegExp('^\\/|\\$', 'g'), '') : '',
|
||||
const
|
||||
_hashValRegexp = /#(.*)$/,
|
||||
_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( global.location.href );
|
||||
return (result && result[1])? decodeURIComponent(result[1]) : '';
|
||||
var result = _hashValRegexp.exec( location.href );
|
||||
return (result && result[1]) ? decodeURIComponent(result[1]) : '';
|
||||
},
|
||||
_registerChange = newHash => {
|
||||
if(_hash !== newHash){
|
||||
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.changed.dispatch(_trimHash(newHash), _trimHash(oldHash));
|
||||
|
|
@ -44,13 +33,24 @@
|
|||
},
|
||||
_checkHistory = () => {
|
||||
var windowHash = _getWindowHash();
|
||||
if (windowHash !== _hash){
|
||||
if (windowHash !== _hash) {
|
||||
_registerChange(windowHash);
|
||||
}
|
||||
},
|
||||
_makePath = path => {
|
||||
_setHash = (path, replace) => {
|
||||
path = path.join('/');
|
||||
return path ? '/' + path.replace(_hashRegexp, '') : path;
|
||||
path = path ? '/' + path.replace(_hashRegexp, '') : path;
|
||||
if (path !== _hash){
|
||||
// we should store raw value
|
||||
_registerChange(path);
|
||||
if (path === _hash) {
|
||||
// we check if path is still === _hash to avoid error in
|
||||
// case of multiple consecutive redirects [issue #39]
|
||||
replace
|
||||
? location.replace('#' + encodeURI(path))
|
||||
: (location.hash = '#' + encodeURI(path));
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
|
@ -64,14 +64,13 @@
|
|||
* - pass current hash as 1st parameter to listeners and previous hash value as 2nd parameter.
|
||||
* @type signals.Signal
|
||||
*/
|
||||
changed : new Signal(),
|
||||
|
||||
/**
|
||||
* Signal dispatched when hasher is initialized.
|
||||
* - pass current hash as first parameter to listeners.
|
||||
* @type signals.Signal
|
||||
*/
|
||||
initialized : new Signal(),
|
||||
changed : {
|
||||
active : true,
|
||||
add : callback => _bindings.push(callback),
|
||||
dispatch : function(...args) {
|
||||
this.active && _bindings.forEach(callback => callback(...args));
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Start listening/dispatching changes in the hash/history.
|
||||
|
|
@ -80,32 +79,14 @@
|
|||
* </ul>
|
||||
*/
|
||||
init : () => {
|
||||
if (!_isActive) {
|
||||
hasher.init = ()=>{};
|
||||
_hash = _getWindowHash();
|
||||
|
||||
_hash = _getWindowHash();
|
||||
//thought about branching/overloading hasher.init() to avoid checking multiple times but
|
||||
//don't think worth doing it since it probably won't be called multiple times.
|
||||
addEventListener('hashchange', _checkHistory);
|
||||
|
||||
//thought about branching/overloading hasher.init() to avoid checking multiple times but
|
||||
//don't think worth doing it since it probably won't be called multiple times.
|
||||
global.addEventListener('hashchange', _checkHistory);
|
||||
|
||||
_isActive = true;
|
||||
hasher.initialized.dispatch(_trimHash(_hash));
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Stop listening/dispatching changes in the hash/history.
|
||||
* <ul>
|
||||
* <li>hasher won't dispatch CHANGE events by manually typing a new value or pressing the back/forward buttons after calling this method, unless you call hasher.init() again.</li>
|
||||
* <li>hasher will still dispatch changes made programatically by calling hasher.setHash();</li>
|
||||
* </ul>
|
||||
*/
|
||||
stop : () => {
|
||||
if (_isActive) {
|
||||
global.removeEventListener('hashchange', _checkHistory);
|
||||
|
||||
_isActive = false;
|
||||
}
|
||||
hasher.changed.dispatch(_trimHash(_hash));
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
@ -113,18 +94,7 @@
|
|||
* @param {...string} path Hash value without '#'.
|
||||
* @example hasher.setHash('lorem', 'ipsum', 'dolor') -> '#/lorem/ipsum/dolor'
|
||||
*/
|
||||
setHash : (...path) => {
|
||||
path = _makePath(path);
|
||||
if(path !== _hash){
|
||||
// we should store raw value
|
||||
_registerChange(path);
|
||||
if (path === _hash) {
|
||||
// we check if path is still === _hash to avoid error in
|
||||
// case of multiple consecutive redirects [issue #39]
|
||||
global.location.hash = '#' + encodeURI(path);
|
||||
}
|
||||
}
|
||||
},
|
||||
setHash : (...path) => _setHash(path),
|
||||
|
||||
/**
|
||||
* Set Hash value without keeping previous hash on the history record.
|
||||
|
|
@ -132,33 +102,8 @@
|
|||
* @param {...string} path Hash value without '#'.
|
||||
* @example hasher.replaceHash('lorem', 'ipsum', 'dolor') -> '#/lorem/ipsum/dolor'
|
||||
*/
|
||||
replaceHash : (...path) => {
|
||||
path = _makePath(path);
|
||||
if(path !== _hash){
|
||||
// we should store raw value
|
||||
_registerChange(path, true);
|
||||
if (path === _hash) {
|
||||
// we check if path is still === _hash to avoid error in
|
||||
// case of multiple consecutive redirects [issue #39]
|
||||
global.location.replace('#' + encodeURI(path));
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Removes all event listeners, stops hasher and destroy hasher object.
|
||||
* - IMPORTANT: hasher won't work after calling this method, hasher Object will be deleted.
|
||||
*/
|
||||
dispose : () => {
|
||||
hasher.stop();
|
||||
hasher.initialized.dispose();
|
||||
hasher.changed.dispose();
|
||||
global.hasher = null;
|
||||
}
|
||||
replaceHash : (...path) => _setHash(path, true)
|
||||
};
|
||||
|
||||
hasher.initialized.memorize = true; //see #33
|
||||
|
||||
global.hasher = hasher;
|
||||
|
||||
})(this);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue