mirror of
https://github.com/the-djmaze/snappymail.git
synced 2024-12-25 00:21:29 +08: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
|
@ -314,8 +314,7 @@ export function startScreens(screensClasses) {
|
|||
const cross = new Crossroads();
|
||||
cross.addRoute(/^([a-zA-Z0-9-]*)\/?(.*)$/, screenOnRoute);
|
||||
|
||||
hasher.initialized.add(cross.parse, cross);
|
||||
hasher.changed.add(cross.parse, cross);
|
||||
hasher.changed.add(cross.parse.bind(cross));
|
||||
hasher.init();
|
||||
|
||||
setTimeout(() => $htmlCL.remove('rl-started-trigger'), 100);
|
||||
|
|
|
@ -41,8 +41,7 @@ config.paths.css = {
|
|||
name: 'app.css',
|
||||
src: [
|
||||
'vendors/normalize.css/normalize.css',
|
||||
'vendors/fontastic/styles.css',
|
||||
'vendors/inputosaurus/inputosaurus.css'
|
||||
'vendors/fontastic/styles.css'
|
||||
]
|
||||
},
|
||||
admin: {
|
||||
|
@ -66,10 +65,8 @@ config.paths.js = {
|
|||
'dev/External/ifvisible.js',
|
||||
'dev/dragdropgecko.js',
|
||||
'dev/shortcuts.js',
|
||||
'vendors/inputosaurus/inputosaurus.js',
|
||||
'vendors/routes/signals.min.js',
|
||||
'vendors/routes/hasher.min.js',
|
||||
'vendors/routes/crossroads.min.js',
|
||||
'vendors/routes/hasher.js',
|
||||
'vendors/routes/crossroads.js',
|
||||
'vendors/jua/jua.min.js',
|
||||
'vendors/qr.js/qr.min.js',
|
||||
'vendors/bootstrap/js/bootstrap.native.js',
|
||||
|
|
246
vendors/routes/crossroads.js
vendored
246
vendors/routes/crossroads.js
vendored
|
@ -7,30 +7,7 @@
|
|||
|
||||
(global => {
|
||||
|
||||
// Helpers -----------
|
||||
//====================
|
||||
|
||||
//borrowed from AMD-utils
|
||||
function typecastValue(val) {
|
||||
var r;
|
||||
if (val === null || val === 'null') {
|
||||
r = null;
|
||||
} else if (val === 'true') {
|
||||
r = true;
|
||||
} else if (val === 'false') {
|
||||
r = false;
|
||||
} else if (val === undefined || val === 'undefined') {
|
||||
r = undefined;
|
||||
} else if (val === '' || isNaN(val)) {
|
||||
//isNaN('') returns false
|
||||
r = val;
|
||||
} else {
|
||||
//parseFloat(null || '') returns NaN
|
||||
r = parseFloat(val);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
const isFunction = obj => typeof obj === 'function';
|
||||
|
||||
// Crossroads --------
|
||||
//====================
|
||||
|
@ -38,33 +15,17 @@
|
|||
class Crossroads {
|
||||
|
||||
constructor() {
|
||||
this._routes = [];
|
||||
this.bypassed = new signals.Signal();
|
||||
this.routed = new global.signals.Signal();
|
||||
this.shouldTypecast = false;
|
||||
}
|
||||
this._routes = [];
|
||||
}
|
||||
|
||||
addRoute(pattern, callback, priority) {
|
||||
var route = new Route(pattern, callback, priority, this),
|
||||
routes = this._routes,
|
||||
n = routes.length;
|
||||
do { --n; } while (routes[n] && route._priority <= routes[n]._priority);
|
||||
routes.splice(n+1, 0, route);
|
||||
addRoute(pattern, callback) {
|
||||
var route = new Route(pattern, callback, this);
|
||||
this._routes.push(route);
|
||||
return route;
|
||||
}
|
||||
|
||||
removeRoute(route) {
|
||||
var i = this._routes.indexOf(route);
|
||||
if (i !== -1) {
|
||||
this._routes.splice(i, 1);
|
||||
}
|
||||
route._destroy();
|
||||
}
|
||||
|
||||
parse(request) {
|
||||
request = request || '';
|
||||
|
||||
var routes = this._getMatchedRoutes(request),
|
||||
var routes = this._getMatchedRoutes(request || ''),
|
||||
i = 0,
|
||||
n = routes.length,
|
||||
cur;
|
||||
|
@ -73,13 +34,10 @@
|
|||
//shold be incremental loop, execute routes in order
|
||||
while (i < n) {
|
||||
cur = routes[i];
|
||||
cur.route.matched.dispatch.apply(cur.route.matched, cur.params);
|
||||
cur.route.callback && cur.route.callback(...cur.params);
|
||||
cur.isFirst = !i;
|
||||
this.routed.dispatch(request, cur);
|
||||
i += 1;
|
||||
}
|
||||
} else {
|
||||
this.bypassed.dispatch(request);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -89,7 +47,8 @@
|
|||
n = routes.length,
|
||||
route;
|
||||
//should be decrement loop since higher priorities are added at the end of array
|
||||
while (route = routes[--n]) {
|
||||
while (n) {
|
||||
route = routes[--n];
|
||||
if ((!res.length || route.greedy) && route.match(request)) {
|
||||
res.push({
|
||||
route : route,
|
||||
|
@ -106,21 +65,17 @@
|
|||
|
||||
class Route {
|
||||
|
||||
constructor(pattern, callback, priority, router) {
|
||||
this.greedy = false;
|
||||
this.rules = void(0);
|
||||
var isRegexPattern = pattern instanceof RegExp;
|
||||
this._router = router;
|
||||
this._pattern = pattern;
|
||||
this._paramsIds = isRegexPattern ? null : patternLexer.getParamIds(this._pattern);
|
||||
this._optionalParamsIds = isRegexPattern ? null : patternLexer.getOptionalParamsIds(this._pattern);
|
||||
this._matchRegexp = isRegexPattern ? pattern : patternLexer.compilePattern(pattern);
|
||||
this.matched = new global.signals.Signal();
|
||||
if (callback) {
|
||||
this.matched.add(callback);
|
||||
}
|
||||
this._priority = priority || 0;
|
||||
}
|
||||
constructor(pattern, callback, router) {
|
||||
this.greedy = false;
|
||||
this.rules = {};
|
||||
var isRegexPattern = pattern instanceof RegExp;
|
||||
this._router = router;
|
||||
this._pattern = pattern;
|
||||
this._paramsIds = isRegexPattern ? null : patternLexer.getParamIds(this._pattern);
|
||||
this._optionalParamsIds = isRegexPattern ? null : patternLexer.getOptionalParamsIds(this._pattern);
|
||||
this._matchRegexp = isRegexPattern ? pattern : patternLexer.compilePattern(pattern);
|
||||
this.callback = isFunction(callback) ? callback : null;
|
||||
}
|
||||
|
||||
|
||||
match(request) {
|
||||
|
@ -128,16 +83,10 @@
|
|||
}
|
||||
|
||||
_validateParams(request) {
|
||||
var rules = this.rules,
|
||||
values = this._getParamsObject(request),
|
||||
key;
|
||||
for (key in rules) {
|
||||
// normalize_ isn't a validation rule... (#39)
|
||||
if(key !== 'normalize_' && rules.hasOwnProperty(key) && ! this._isValidParam(request, key, values)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
var values = this._getParamsObject(request);
|
||||
return 0 == Object.keys(this.rules).filter(key =>
|
||||
key !== 'normalize_' && !this._isValidParam(request, key, values)
|
||||
).length;
|
||||
}
|
||||
|
||||
_isValidParam(request, prop, values) {
|
||||
|
@ -154,7 +103,7 @@
|
|||
else if (Array.isArray(validationRule)) {
|
||||
isValid = validationRule.indexOf(val) !== -1;
|
||||
}
|
||||
else if (typeof validationRule === 'function') {
|
||||
else if (isFunction(validationRule)) {
|
||||
isValid = validationRule(val, request, values);
|
||||
}
|
||||
|
||||
|
@ -162,41 +111,29 @@
|
|||
}
|
||||
|
||||
_getParamsObject(request) {
|
||||
var shouldTypecast = this._router.shouldTypecast,
|
||||
values = patternLexer.getParamValues(request, this._matchRegexp, shouldTypecast),
|
||||
var values = patternLexer.getParamValues(request, this._matchRegexp),
|
||||
o = {},
|
||||
n = values.length;
|
||||
while (n--) {
|
||||
o[n] = values[n]; //for RegExp pattern and also alias to normal paths
|
||||
if (this._paramsIds) {
|
||||
o[this._paramsIds[n]] = values[n];
|
||||
}
|
||||
this._paramsIds && (o[this._paramsIds[n]] = values[n]);
|
||||
}
|
||||
o.request_ = shouldTypecast ? typecastValue(request) : request;
|
||||
o.request_ = request;
|
||||
o.vals_ = values;
|
||||
return o;
|
||||
}
|
||||
|
||||
_getParamsArray(request) {
|
||||
var norm = this.rules ? this.rules.normalize_ : null,
|
||||
var norm = this.rules.normalize_,
|
||||
params;
|
||||
if (norm && typeof norm === 'function') {
|
||||
if (isFunction(norm)) {
|
||||
params = norm(request, this._getParamsObject(request));
|
||||
} else {
|
||||
params = patternLexer.getParamValues(request, this._matchRegexp, this._router.shouldTypecast);
|
||||
params = patternLexer.getParamValues(request, this._matchRegexp);
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
dispose() {
|
||||
this._router.removeRoute(this);
|
||||
}
|
||||
|
||||
_destroy() {
|
||||
this.matched.dispose();
|
||||
this.matched = this._pattern = this._matchRegexp = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -204,74 +141,71 @@
|
|||
// Pattern Lexer ------
|
||||
//=====================
|
||||
|
||||
const
|
||||
ESCAPE_CHARS_REGEXP = /[\\.+*?^$[\](){}/'#]/g, //match chars that should be escaped on string regexp
|
||||
UNNECESSARY_SLASHES_REGEXP = /\/$/g, //trailing slash
|
||||
OPTIONAL_SLASHES_REGEXP = /([:}]|\w(?=\/))\/?(:)/g, //slash between `::` or `}:` or `\w:`. $1 = before, $2 = after
|
||||
REQUIRED_SLASHES_REGEXP = /([:}])\/?(\{)/g, //used to insert slash between `:{` and `}{`
|
||||
const
|
||||
ESCAPE_CHARS_REGEXP = /[\\.+*?^$[\](){}/'#]/g, //match chars that should be escaped on string regexp
|
||||
UNNECESSARY_SLASHES_REGEXP = /\/$/g, //trailing slash
|
||||
OPTIONAL_SLASHES_REGEXP = /([:}]|\w(?=\/))\/?(:)/g, //slash between `::` or `}:` or `\w:`. $1 = before, $2 = after
|
||||
REQUIRED_SLASHES_REGEXP = /([:}])\/?(\{)/g, //used to insert slash between `:{` and `}{`
|
||||
|
||||
REQUIRED_PARAMS_REGEXP = /\{([^}]+)\}/g, //match everything between `{ }`
|
||||
OPTIONAL_PARAMS_REGEXP = /:([^:]+):/g, //match everything between `: :`
|
||||
PARAMS_REGEXP = /(?:\{|:)([^}:]+)(?:\}|:)/g, //capture everything between `{ }` or `: :`
|
||||
REQUIRED_PARAMS_REGEXP = /\{([^}]+)\}/g, //match everything between `{ }`
|
||||
OPTIONAL_PARAMS_REGEXP = /:([^:]+):/g, //match everything between `: :`
|
||||
PARAMS_REGEXP = /(?:\{|:)([^}:]+)(?:\}|:)/g, //capture everything between `{ }` or `: :`
|
||||
|
||||
//used to save params during compile (avoid escaping things that
|
||||
//shouldn't be escaped).
|
||||
SAVE_REQUIRED_PARAMS = '__CR_RP__',
|
||||
SAVE_OPTIONAL_PARAMS = '__CR_OP__',
|
||||
SAVE_REQUIRED_SLASHES = '__CR_RS__',
|
||||
SAVE_OPTIONAL_SLASHES = '__CR_OS__',
|
||||
SAVED_REQUIRED_REGEXP = new RegExp(SAVE_REQUIRED_PARAMS, 'g'),
|
||||
SAVED_OPTIONAL_REGEXP = new RegExp(SAVE_OPTIONAL_PARAMS, 'g'),
|
||||
SAVED_OPTIONAL_SLASHES_REGEXP = new RegExp(SAVE_OPTIONAL_SLASHES, 'g'),
|
||||
SAVED_REQUIRED_SLASHES_REGEXP = new RegExp(SAVE_REQUIRED_SLASHES, 'g'),
|
||||
//used to save params during compile (avoid escaping things that
|
||||
//shouldn't be escaped).
|
||||
SAVE_REQUIRED_PARAMS = '__CR_RP__',
|
||||
SAVE_OPTIONAL_PARAMS = '__CR_OP__',
|
||||
SAVE_REQUIRED_SLASHES = '__CR_RS__',
|
||||
SAVE_OPTIONAL_SLASHES = '__CR_OS__',
|
||||
SAVED_REQUIRED_REGEXP = new RegExp(SAVE_REQUIRED_PARAMS, 'g'),
|
||||
SAVED_OPTIONAL_REGEXP = new RegExp(SAVE_OPTIONAL_PARAMS, 'g'),
|
||||
SAVED_OPTIONAL_SLASHES_REGEXP = new RegExp(SAVE_OPTIONAL_SLASHES, 'g'),
|
||||
SAVED_REQUIRED_SLASHES_REGEXP = new RegExp(SAVE_REQUIRED_SLASHES, 'g'),
|
||||
|
||||
|
||||
captureVals = (regex, pattern) => {
|
||||
var vals = [], match;
|
||||
while (match = regex.exec(pattern)) {
|
||||
vals.push(match[1]);
|
||||
}
|
||||
return vals;
|
||||
},
|
||||
captureVals = (regex, pattern) => {
|
||||
var vals = [], match;
|
||||
while (match = regex.exec(pattern)) {
|
||||
vals.push(match[1]);
|
||||
}
|
||||
return vals;
|
||||
},
|
||||
|
||||
tokenize = pattern => {
|
||||
//save chars that shouldn't be escaped
|
||||
return pattern.replace(OPTIONAL_SLASHES_REGEXP, '$1'+ SAVE_OPTIONAL_SLASHES +'$2')
|
||||
.replace(REQUIRED_SLASHES_REGEXP, '$1'+ SAVE_REQUIRED_SLASHES +'$2')
|
||||
.replace(OPTIONAL_PARAMS_REGEXP, SAVE_OPTIONAL_PARAMS)
|
||||
.replace(REQUIRED_PARAMS_REGEXP, SAVE_REQUIRED_PARAMS);
|
||||
},
|
||||
tokenize = pattern => {
|
||||
//save chars that shouldn't be escaped
|
||||
return pattern.replace(OPTIONAL_SLASHES_REGEXP, '$1'+ SAVE_OPTIONAL_SLASHES +'$2')
|
||||
.replace(REQUIRED_SLASHES_REGEXP, '$1'+ SAVE_REQUIRED_SLASHES +'$2')
|
||||
.replace(OPTIONAL_PARAMS_REGEXP, SAVE_OPTIONAL_PARAMS)
|
||||
.replace(REQUIRED_PARAMS_REGEXP, SAVE_REQUIRED_PARAMS);
|
||||
},
|
||||
|
||||
untokenize = pattern => {
|
||||
return pattern.replace(SAVED_OPTIONAL_SLASHES_REGEXP, '\\/?')
|
||||
.replace(SAVED_REQUIRED_SLASHES_REGEXP, '\\/')
|
||||
.replace(SAVED_OPTIONAL_REGEXP, '([^\\/]+)?/?')
|
||||
.replace(SAVED_REQUIRED_REGEXP, '([^\\/]+)');
|
||||
},
|
||||
untokenize = pattern => {
|
||||
return pattern.replace(SAVED_OPTIONAL_SLASHES_REGEXP, '\\/?')
|
||||
.replace(SAVED_REQUIRED_SLASHES_REGEXP, '\\/')
|
||||
.replace(SAVED_OPTIONAL_REGEXP, '([^\\/]+)?/?')
|
||||
.replace(SAVED_REQUIRED_REGEXP, '([^\\/]+)');
|
||||
},
|
||||
|
||||
patternLexer = {
|
||||
getParamIds : pattern => captureVals(PARAMS_REGEXP, pattern),
|
||||
getOptionalParamsIds : pattern => captureVals(OPTIONAL_PARAMS_REGEXP, pattern),
|
||||
getParamValues : (request, regexp, shouldTypecast) => {
|
||||
var vals = regexp.exec(request);
|
||||
if (vals) {
|
||||
vals.shift();
|
||||
if (shouldTypecast) {
|
||||
vals = vals.map(v => typecastValue(v));
|
||||
}
|
||||
}
|
||||
return vals;
|
||||
},
|
||||
compilePattern : pattern => {
|
||||
pattern = pattern || '';
|
||||
if (pattern) {
|
||||
pattern = untokenize(
|
||||
tokenize(pattern.replace(UNNECESSARY_SLASHES_REGEXP, '')).replace(ESCAPE_CHARS_REGEXP, '\\$&')
|
||||
);
|
||||
}
|
||||
return new RegExp('^'+ pattern + '/?$'); //trailing slash is optional
|
||||
}
|
||||
};
|
||||
patternLexer = {
|
||||
getParamIds : pattern => captureVals(PARAMS_REGEXP, pattern),
|
||||
getOptionalParamsIds : pattern => captureVals(OPTIONAL_PARAMS_REGEXP, pattern),
|
||||
getParamValues : (request, regexp) => {
|
||||
var vals = regexp.exec(request);
|
||||
if (vals) {
|
||||
vals.shift();
|
||||
}
|
||||
return vals;
|
||||
},
|
||||
compilePattern : pattern => {
|
||||
pattern = pattern || '';
|
||||
if (pattern) {
|
||||
pattern = untokenize(
|
||||
tokenize(pattern.replace(UNNECESSARY_SLASHES_REGEXP, '')).replace(ESCAPE_CHARS_REGEXP, '\\$&')
|
||||
);
|
||||
}
|
||||
return new RegExp('^'+ pattern + '/?$'); //trailing slash is optional
|
||||
}
|
||||
};
|
||||
|
||||
global.Crossroads = Crossroads;
|
||||
|
||||
|
|
7
vendors/routes/crossroads.min.js
vendored
7
vendors/routes/crossroads.min.js
vendored
|
@ -1,7 +0,0 @@
|
|||
/*
|
||||
Crossroads.js <http://millermedeiros.github.com/crossroads.js>
|
||||
Released under the MIT license
|
||||
Author: Miller Medeiros
|
||||
Version: 0.7.1 - Build: 93 (2012/02/02 09:29 AM)
|
||||
*/
|
||||
(e=>{function t(e){return null===e||"null"===e?null:"true"===e||"false"!==e&&(void 0===e||"undefined"===e?void 0:""===e||isNaN(e)?e:parseFloat(e))}class s{constructor(t,s,a,r){this.greedy=!1,this.rules=void 0;var i=t instanceof RegExp;this._router=r,this._pattern=t,this._paramsIds=i?null:g.getParamIds(this._pattern),this._optionalParamsIds=i?null:g.getOptionalParamsIds(this._pattern),this._matchRegexp=i?t:g.compilePattern(t),this.matched=new e.signals.Signal,s&&this.matched.add(s),this._priority=a||0}match(e){return this._matchRegexp.test(e)&&this._validateParams(e)}_validateParams(e){var t,s=this.rules,a=this._getParamsObject(e);for(t in s)if("normalize_"!==t&&s.hasOwnProperty(t)&&!this._isValidParam(e,t,a))return!1;return!0}_isValidParam(e,t,s){var a=this.rules[t],r=s[t],i=!1;return null==r&&this._optionalParamsIds&&-1!==this._optionalParamsIds.indexOf(t)?i=!0:a instanceof RegExp?i=a.test(r):Array.isArray(a)?i=-1!==a.indexOf(r):"function"==typeof a&&(i=a(r,e,s)),i}_getParamsObject(e){for(var s=this._router.shouldTypecast,a=g.getParamValues(e,this._matchRegexp,s),r={},i=a.length;i--;)r[i]=a[i],this._paramsIds&&(r[this._paramsIds[i]]=a[i]);return r.request_=s?t(e):e,r.vals_=a,r}_getParamsArray(e){var t=this.rules?this.rules.normalize_:null;return t&&"function"==typeof t?t(e,this._getParamsObject(e)):g.getParamValues(e,this._matchRegexp,this._router.shouldTypecast)}dispose(){this._router.removeRoute(this)}_destroy(){this.matched.dispose(),this.matched=this._pattern=this._matchRegexp=null}}const a=/[\\.+*?^$[\](){}\/'#]/g,r=/\/$/g,i=/([:}]|\w(?=\/))\/?(:)/g,_=/([:}])\/?(\{)/g,h=/\{([^}]+)\}/g,n=/:([^:]+):/g,l=/(?:\{|:)([^}:]+)(?:\}|:)/g,o=new RegExp("__CR_RP__","g"),p=new RegExp("__CR_OP__","g"),u=new RegExp("__CR_OS__","g"),c=new RegExp("__CR_RS__","g"),d=(e,t)=>{for(var s,a=[];s=e.exec(t);)a.push(s[1]);return a},g={getParamIds:e=>d(l,e),getOptionalParamsIds:e=>d(n,e),getParamValues:(e,s,a)=>{var r=s.exec(e);return r&&(r.shift(),a&&(r=r.map(e=>t(e)))),r},compilePattern:e=>((e=e||"")&&(e=(e=>e.replace(u,"\\/?").replace(c,"\\/").replace(p,"([^\\/]+)?/?").replace(o,"([^\\/]+)"))((e=>e.replace(i,"$1__CR_OS__$2").replace(_,"$1__CR_RS__$2").replace(n,"__CR_OP__").replace(h,"__CR_RP__"))(e.replace(r,"")).replace(a,"\\$&"))),new RegExp("^"+e+"/?$"))};e.Crossroads=class{constructor(){this._routes=[],this.bypassed=new signals.Signal,this.routed=new e.signals.Signal,this.shouldTypecast=!1}addRoute(e,t,a){var r=new s(e,t,a,this),i=this._routes,_=i.length;do{--_}while(i[_]&&r._priority<=i[_]._priority);return i.splice(_+1,0,r),r}removeRoute(e){var t=this._routes.indexOf(e);-1!==t&&this._routes.splice(t,1),e._destroy()}parse(e){e=e||"";var t,s=this._getMatchedRoutes(e),a=0,r=s.length;if(r)for(;a<r;)(t=s[a]).route.matched.dispatch.apply(t.route.matched,t.params),t.isFirst=!a,this.routed.dispatch(e,t),a+=1;else this.bypassed.dispatch(e)}_getMatchedRoutes(e){for(var t,s=[],a=this._routes,r=a.length;t=a[--r];)s.length&&!t.greedy||!t.match(e)||s.push({route:t,params:t._getParamsArray(e)});return s}}})(this);
|
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);
|
||||
|
|
7
vendors/routes/hasher.min.js
vendored
7
vendors/routes/hasher.min.js
vendored
|
@ -1,7 +0,0 @@
|
|||
/*!
|
||||
* Hasher <http://github.com/millermedeiros/hasher>
|
||||
* @author Miller Medeiros
|
||||
* @version 1.1.2 (2012/10/31 03:19 PM)
|
||||
* Released under the MIT License
|
||||
*/
|
||||
(e=>{var i,a,n=signals.Signal,s=/#(.*)$/,h=/^#/;const t=e=>e?e.replace(new RegExp("^\\/|\\$","g"),""):"",o=()=>{var i=s.exec(e.location.href);return i&&i[1]?decodeURIComponent(i[1]):""},c=e=>{if(i!==e){var a=i;i=e,l.changed.dispatch(t(e),t(a))}},d=()=>{var e=o();e!==i&&c(e)},r=e=>(e=e.join("/"))?"/"+e.replace(h,""):e,l={changed:new n,initialized:new n,init:()=>{a||(i=o(),e.addEventListener("hashchange",d),a=!0,l.initialized.dispatch(t(i)))},stop:()=>{a&&(e.removeEventListener("hashchange",d),a=!1)},setHash:(...a)=>{(a=r(a))!==i&&(c(a),a===i&&(e.location.hash="#"+encodeURI(a)))},replaceHash:(...a)=>{(a=r(a))!==i&&(c(a),a===i&&e.location.replace("#"+encodeURI(a)))},dispose:()=>{l.stop(),l.initialized.dispose(),l.changed.dispose(),e.hasher=null}};l.initialized.memorize=!0,e.hasher=l})(this);
|
305
vendors/routes/signals.js
vendored
305
vendors/routes/signals.js
vendored
|
@ -1,305 +0,0 @@
|
|||
/** @license
|
||||
* JS Signals <http://millermedeiros.github.com/js-signals/>
|
||||
* Released under the MIT license
|
||||
* Author: Miller Medeiros
|
||||
* Version: 1.0.0 - Build: 268 (2012/11/29 05:48 PM)
|
||||
*/
|
||||
|
||||
(global=>{
|
||||
|
||||
// SignalBinding -------------------------------------------------
|
||||
//================================================================
|
||||
|
||||
class SignalBinding {
|
||||
|
||||
/**
|
||||
* Object that represents a binding between a Signal and a listener function.
|
||||
* <br />- <strong>This is an internal constructor and shouldn't be called by regular users.</strong>
|
||||
* <br />- inspired by Joa Ebert AS3 SignalBinding and Robert Penner's Slot classes.
|
||||
* @author Miller Medeiros
|
||||
* @constructor
|
||||
* @internal
|
||||
* @name SignalBinding
|
||||
* @param {Signal} signal Reference to Signal object that listener is currently bound to.
|
||||
* @param {Function} listener Handler function bound to the signal.
|
||||
* @param {boolean} isOnce If binding should be executed just once.
|
||||
* @param {Object} [listenerContext] Context on which listener will be executed (object that should represent the `this` variable inside listener function).
|
||||
* @param {Number} [priority] The priority level of the event listener. (default = 0).
|
||||
*/
|
||||
constructor (signal, listener, isOnce, listenerContext, priority) {
|
||||
|
||||
/**
|
||||
* If binding is active and should be executed.
|
||||
* @type boolean
|
||||
*/
|
||||
this.active = true;
|
||||
|
||||
/**
|
||||
* Default parameters passed to listener during `Signal.dispatch` and `SignalBinding.execute`. (curried parameters)
|
||||
* @type Array|null
|
||||
*/
|
||||
this.params = null;
|
||||
|
||||
/**
|
||||
* Handler function bound to the signal.
|
||||
* @type Function
|
||||
* @private
|
||||
*/
|
||||
this._listener = listener;
|
||||
|
||||
/**
|
||||
* If binding should be executed just once.
|
||||
* @type boolean
|
||||
* @private
|
||||
*/
|
||||
this._isOnce = isOnce;
|
||||
|
||||
/**
|
||||
* Context on which listener will be executed (object that should represent the `this` variable inside listener function).
|
||||
* @memberOf SignalBinding.prototype
|
||||
* @name context
|
||||
* @type Object|undefined|null
|
||||
*/
|
||||
this.context = listenerContext;
|
||||
|
||||
/**
|
||||
* Reference to Signal object that listener is currently bound to.
|
||||
* @type Signal
|
||||
* @private
|
||||
*/
|
||||
this._signal = signal;
|
||||
|
||||
/**
|
||||
* Listener priority
|
||||
* @type Number
|
||||
* @private
|
||||
*/
|
||||
this._priority = priority || 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call listener passing arbitrary parameters.
|
||||
* <p>If binding was added using `Signal.addOnce()` it will be automatically removed from signal dispatch queue, this method is used internally for the signal dispatch.</p>
|
||||
* @param {Array} [paramsArr] Array of parameters that should be passed to the listener
|
||||
* @return {*} Value returned by the listener.
|
||||
*/
|
||||
execute (paramsArr) {
|
||||
var handlerReturn, params;
|
||||
if (this.active && !!this._listener) {
|
||||
params = this.params? this.params.concat(paramsArr) : paramsArr;
|
||||
handlerReturn = this._listener.apply(this.context, params);
|
||||
if (this._isOnce) {
|
||||
this.detach();
|
||||
}
|
||||
}
|
||||
return handlerReturn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detach binding from signal.
|
||||
* - alias to: mySignal.remove(myBinding.getListener());
|
||||
* @return {Function|null} Handler function bound to the signal or `null` if binding was previously detached.
|
||||
*/
|
||||
detach () {
|
||||
return (this._signal && this._listener) ? this._signal.remove(this._listener, this.context) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {boolean} If SignalBinding will only be executed once.
|
||||
*/
|
||||
isOnce () {
|
||||
return this._isOnce;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete instance properties
|
||||
* @private
|
||||
*/
|
||||
_destroy () {
|
||||
delete this._signal;
|
||||
delete this._listener;
|
||||
delete this.context;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Signal --------------------------------------------------------
|
||||
//================================================================
|
||||
|
||||
function validateListener(listener, fnName) {
|
||||
if (typeof listener !== 'function') {
|
||||
throw new Error( 'listener is a required param of {fn}() and should be a Function.'.replace('{fn}', fnName) );
|
||||
}
|
||||
}
|
||||
|
||||
class Signal {
|
||||
|
||||
/**
|
||||
* Custom event broadcaster
|
||||
* <br />- inspired by Robert Penner's AS3 Signals.
|
||||
* @name Signal
|
||||
* @author Miller Medeiros
|
||||
* @constructor
|
||||
*/
|
||||
constructor () {
|
||||
/**
|
||||
* If Signal should keep record of previously dispatched parameters and
|
||||
* automatically execute listener during `add()`/`addOnce()` if Signal was
|
||||
* already dispatched before.
|
||||
* @type boolean
|
||||
*/
|
||||
this.memorize = false;
|
||||
|
||||
/**
|
||||
* If Signal is active and should broadcast events.
|
||||
* <p><strong>IMPORTANT:</strong> Setting this property during a dispatch will only affect the next dispatch, if you want to stop the propagation of a signal use `halt()` instead.</p>
|
||||
* @type boolean
|
||||
*/
|
||||
this.active = true;
|
||||
|
||||
/**
|
||||
* @type Array.<SignalBinding>
|
||||
* @private
|
||||
*/
|
||||
this._bindings = [];
|
||||
this._prevParams = null;
|
||||
|
||||
// enforce dispatch to aways work on same context (#47)
|
||||
var self = this;
|
||||
this.dispatch = (...args) => Signal.prototype.dispatch.apply(self, args);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Function} listener
|
||||
* @return {number}
|
||||
* @private
|
||||
*/
|
||||
_indexOfListener (listener, context) {
|
||||
var n = this._bindings.length,
|
||||
cur;
|
||||
while (n--) {
|
||||
cur = this._bindings[n];
|
||||
if (cur._listener === listener && cur.context === context) {
|
||||
return n;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a listener to the signal.
|
||||
* @param {Function} listener Signal handler function.
|
||||
* @param {Object} [listenerContext] Context on which listener will be executed (object that should represent the `this` variable inside listener function).
|
||||
* @param {Number} [priority] The priority level of the event listener. Listeners with higher priority will be executed before listeners with lower priority. Listeners with same priority level will be executed at the same order as they were added. (default = 0)
|
||||
* @return {SignalBinding} An Object representing the binding between the Signal and listener.
|
||||
*/
|
||||
add (listener, listenerContext, priority) {
|
||||
validateListener(listener, 'add');
|
||||
|
||||
var prevIndex = this._indexOfListener(listener, listenerContext),
|
||||
binding;
|
||||
|
||||
if (prevIndex !== -1) {
|
||||
binding = this._bindings[prevIndex];
|
||||
if (binding.isOnce() !== false) {
|
||||
throw new Error('You cannot addOnce() then add() the same listener without removing the relationship first.');
|
||||
}
|
||||
} else {
|
||||
binding = new SignalBinding(this, listener, false, listenerContext, priority);
|
||||
//simplified insertion sort
|
||||
var n = this._bindings.length;
|
||||
do { --n; } while (this._bindings[n] && binding._priority <= this._bindings[n]._priority);
|
||||
this._bindings.splice(n + 1, 0, binding);
|
||||
}
|
||||
|
||||
if(this.memorize && this._prevParams){
|
||||
binding.execute(this._prevParams);
|
||||
}
|
||||
|
||||
return binding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a single listener from the dispatch queue.
|
||||
* @param {Function} listener Handler function that should be removed.
|
||||
* @param {Object} [context] Execution context (since you can add the same handler multiple times if executing in a different context).
|
||||
* @return {Function} Listener handler function.
|
||||
*/
|
||||
remove (listener, context) {
|
||||
validateListener(listener, 'remove');
|
||||
|
||||
var i = this._indexOfListener(listener, context);
|
||||
if (i !== -1) {
|
||||
this._bindings[i]._destroy(); //no reason to a SignalBinding exist if it isn't attached to a signal
|
||||
this._bindings.splice(i, 1);
|
||||
}
|
||||
return listener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all listeners from the Signal.
|
||||
*/
|
||||
removeAll () {
|
||||
var n = this._bindings.length;
|
||||
while (n--) {
|
||||
this._bindings[n]._destroy();
|
||||
}
|
||||
this._bindings.length = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch/Broadcast Signal to all listeners added to the queue.
|
||||
* @param {...*} [params] Parameters that should be passed to each handler.
|
||||
*/
|
||||
dispatch (...paramsArr) {
|
||||
if (! this.active) {
|
||||
return;
|
||||
}
|
||||
|
||||
var n = this._bindings.length,
|
||||
bindings;
|
||||
|
||||
if (this.memorize) {
|
||||
this._prevParams = paramsArr;
|
||||
}
|
||||
|
||||
if (! n) {
|
||||
//should come after memorize
|
||||
return;
|
||||
}
|
||||
|
||||
bindings = this._bindings.slice(); //clone array in case add/remove items during dispatch
|
||||
|
||||
//execute all callbacks until end of the list or until a callback returns `false` or stops propagation
|
||||
//reverse loop since listeners with higher priority will be added at the end of the list
|
||||
do { n--; } while (bindings[n] && bindings[n].execute(paramsArr) !== false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all bindings from signal and destroy any reference to external objects (destroy Signal object).
|
||||
* <p><strong>IMPORTANT:</strong> calling any method on the signal instance after calling dispose will throw errors.</p>
|
||||
*/
|
||||
dispose () {
|
||||
this.removeAll();
|
||||
delete this._bindings;
|
||||
delete this._prevParams;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Namespace -----------------------------------------------------
|
||||
//================================================================
|
||||
|
||||
var signals = Signal;
|
||||
|
||||
/**
|
||||
* Custom event broadcaster
|
||||
* @see Signal
|
||||
*/
|
||||
// alias for backwards compatibility (see #gh-44)
|
||||
signals.Signal = Signal;
|
||||
|
||||
global.signals = signals;
|
||||
|
||||
})(this);
|
7
vendors/routes/signals.min.js
vendored
7
vendors/routes/signals.min.js
vendored
|
@ -1,7 +0,0 @@
|
|||
/*
|
||||
JS Signals <http://millermedeiros.github.com/js-signals/>
|
||||
Released under the MIT license
|
||||
Author: Miller Medeiros
|
||||
Version: 1.0.0 - Build: 268 (2012/11/29 05:48 PM)
|
||||
*/
|
||||
(i=>{class t{constructor(i,t,e,s,n){this.active=!0,this.params=null,this._listener=t,this._isOnce=e,this.context=s,this._signal=i,this._priority=n||0}execute(i){var t,e;return this.active&&this._listener&&(e=this.params?this.params.concat(i):i,t=this._listener.apply(this.context,e),this._isOnce&&this.detach()),t}detach(){return this._signal&&this._listener?this._signal.remove(this._listener,this.context):null}isOnce(){return this._isOnce}_destroy(){delete this._signal,delete this._listener,delete this.context}}function e(i,t){if("function"!=typeof i)throw new Error("listener is a required param of {fn}() and should be a Function.".replace("{fn}",t))}class s{constructor(){this.memorize=!1,this.active=!0,this._bindings=[],this._prevParams=null;var i=this;this.dispatch=((...t)=>s.prototype.dispatch.apply(i,t))}_indexOfListener(i,t){for(var e,s=this._bindings.length;s--;)if((e=this._bindings[s])._listener===i&&e.context===t)return s;return-1}add(i,s,n){e(i,"add");var r,h=this._indexOfListener(i,s);if(-1!==h){if(!1!==(r=this._bindings[h]).isOnce())throw new Error("You cannot addOnce() then add() the same listener without removing the relationship first.")}else{r=new t(this,i,!1,s,n);var a=this._bindings.length;do{--a}while(this._bindings[a]&&r._priority<=this._bindings[a]._priority);this._bindings.splice(a+1,0,r)}return this.memorize&&this._prevParams&&r.execute(this._prevParams),r}remove(i,t){e(i,"remove");var s=this._indexOfListener(i,t);return-1!==s&&(this._bindings[s]._destroy(),this._bindings.splice(s,1)),i}removeAll(){for(var i=this._bindings.length;i--;)this._bindings[i]._destroy();this._bindings.length=0}dispatch(...i){if(this.active){var t,e=this._bindings.length;if(this.memorize&&(this._prevParams=i),e){t=this._bindings.slice();do{e--}while(t[e]&&!1!==t[e].execute(i))}}}dispose(){this.removeAll(),delete this._bindings,delete this._prevParams}}var n=s;n.Signal=s,i.signals=n})(this);
|
Loading…
Reference in a new issue