From 3757324e32e32a69b7245385c5d26f5648528c02 Mon Sep 17 00:00:00 2001 From: djmaze Date: Wed, 23 Sep 2020 14:44:46 +0200 Subject: [PATCH] Cleanup hash routing --- dev/Knoin/AbstractScreen.js | 13 +++---------- vendors/routes/crossroads.js | 12 ------------ vendors/routes/crossroads.min.js | 2 +- vendors/routes/hasher.js | 6 +----- vendors/routes/hasher.min.js | 2 +- vendors/routes/signals.js | 23 +---------------------- vendors/routes/signals.min.js | 2 +- 7 files changed, 8 insertions(+), 52 deletions(-) diff --git a/dev/Knoin/AbstractScreen.js b/dev/Knoin/AbstractScreen.js index e3c82bdce..3740b64e2 100644 --- a/dev/Knoin/AbstractScreen.js +++ b/dev/Knoin/AbstractScreen.js @@ -40,19 +40,12 @@ export class AbstractScreen { * @returns {void} */ __start() { - let route = null, - fMatcher = null; const routes = this.routes(); - if (Array.isNotEmpty(routes)) { - fMatcher = (this.onRoute || (()=>{})).bind(this); - route = new Crossroads(); + let route = new Crossroads(), + fMatcher = (this.onRoute || (()=>{})).bind(this); - routes.forEach((item) => { - if (item && route) { - route.addRoute(item[0], fMatcher).rules = item[1]; - } - }); + routes.forEach(item => item && route && (route.addRoute(item[0], fMatcher).rules = item[1])); this.oCross = route; } diff --git a/vendors/routes/crossroads.js b/vendors/routes/crossroads.js index c38cb687a..a625577f0 100644 --- a/vendors/routes/crossroads.js +++ b/vendors/routes/crossroads.js @@ -61,14 +61,6 @@ route._destroy(); } - removeAllRoutes() { - var n = this.getNumRoutes(); - while (n--) { - this._routes[n]._destroy(); - } - this._routes.length = 0; - } - parse(request) { request = request || ''; @@ -91,10 +83,6 @@ } } - getNumRoutes() { - return this._routes.length; - } - _getMatchedRoutes(request) { var res = [], routes = this._routes, diff --git a/vendors/routes/crossroads.min.js b/vendors/routes/crossroads.min.js index e14e939f0..f74f14d5e 100644 --- a/vendors/routes/crossroads.min.js +++ b/vendors/routes/crossroads.min.js @@ -4,4 +4,4 @@ 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,r,a){this.greedy=!1,this.rules=void 0;var i=t instanceof RegExp;this._router=a,this._pattern=t,this._paramsIds=i?null:c.getParamIds(this._pattern),this._optionalParamsIds=i?null:c.getOptionalParamsIds(this._pattern),this._matchRegexp=i?t:c.compilePattern(t),this.matched=new e.signals.Signal,s&&this.matched.add(s),this._priority=r||0}match(e){return this._matchRegexp.test(e)&&this._validateParams(e)}_validateParams(e){var t,s=this.rules,r=this._getParamsObject(e);for(t in s)if("normalize_"!==t&&s.hasOwnProperty(t)&&!this._isValidParam(e,t,r))return!1;return!0}_isValidParam(e,t,s){var r=this.rules[t],a=s[t],i=!1;return null==a&&this._optionalParamsIds&&-1!==this._optionalParamsIds.indexOf(t)?i=!0:r instanceof RegExp?i=r.test(a):Array.isArray(r)?i=-1!==r.indexOf(a):"function"==typeof r&&(i=r(a,e,s)),i}_getParamsObject(e){for(var s=this._router.shouldTypecast,r=c.getParamValues(e,this._matchRegexp,s),a={},i=r.length;i--;)a[i]=r[i],this._paramsIds&&(a[this._paramsIds[i]]=r[i]);return a.request_=s?t(e):e,a.vals_=r,a}_getParamsArray(e){var t=this.rules?this.rules.normalize_:null;return t&&"function"==typeof t?t(e,this._getParamsObject(e)):c.getParamValues(e,this._matchRegexp,this._router.shouldTypecast)}dispose(){this._router.removeRoute(this)}_destroy(){this.matched.dispose(),this.matched=this._pattern=this._matchRegexp=null}}const r=/[\\.+*?^$[\](){}\/'#]/g,a=/\/$/g,i=/([:}]|\w(?=\/))\/?(:)/g,_=/([:}])\/?(\{)/g,h=/\{([^}]+)\}/g,o=/:([^:]+):/g,l=/(?:\{|:)([^}:]+)(?:\}|:)/g,n=new RegExp("__CR_RP__","g"),u=new RegExp("__CR_OP__","g"),p=new RegExp("__CR_OS__","g"),g=new RegExp("__CR_RS__","g"),d=(e,t)=>{for(var s,r=[];s=e.exec(t);)r.push(s[1]);return r},c={getParamIds:e=>d(l,e),getOptionalParamsIds:e=>d(o,e),getParamValues:(e,s,r)=>{var a=s.exec(e);return a&&(a.shift(),r&&(a=a.map(e=>t(e)))),a},compilePattern:e=>((e=e||"")&&(e=(e=>e.replace(p,"\\/?").replace(g,"\\/").replace(u,"([^\\/]+)?/?").replace(n,"([^\\/]+)"))((e=>e.replace(i,"$1__CR_OS__$2").replace(_,"$1__CR_RS__$2").replace(o,"__CR_OP__").replace(h,"__CR_RP__"))(e.replace(a,"")).replace(r,"\\$&"))),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,r){var a=new s(e,t,r,this),i=this._routes,_=i.length;do{--_}while(i[_]&&a._priority<=i[_]._priority);return i.splice(_+1,0,a),a}removeRoute(e){var t=this._routes.indexOf(e);-1!==t&&this._routes.splice(t,1),e._destroy()}removeAllRoutes(){for(var e=this.getNumRoutes();e--;)this._routes[e]._destroy();this._routes.length=0}parse(e){e=e||"";var t,s=this._getMatchedRoutes(e),r=0,a=s.length;if(a)for(;r{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 { - if(! hash) return ''; - var regexp = new RegExp('^\\/|\\$', 'g'); - return hash.replace(regexp, ''); - }, + const _trimHash = hash => hash ? hash.replace(new RegExp('^\\/|\\$', 'g'), '') : '', _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] diff --git a/vendors/routes/hasher.min.js b/vendors/routes/hasher.min.js index 547ba6a12..791767df4 100644 --- a/vendors/routes/hasher.min.js +++ b/vendors/routes/hasher.min.js @@ -4,4 +4,4 @@ * @version 1.1.2 (2012/10/31 03:19 PM) * Released under the MIT License */ -(e=>{var i,a,n=signals.Signal,s=/#(.*)$/,t=/^#/;const r=e=>{if(!e)return"";var i=new RegExp("^\\/|\\$","g");return e.replace(i,"")},h=()=>{var i=s.exec(e.location.href);return i&&i[1]?decodeURIComponent(i[1]):""},o=e=>{if(i!==e){var a=i;i=e,l.changed.dispatch(r(e),r(a))}},c=()=>{var e=h();e!==i&&o(e)},d=e=>(e=e.join("/"))?"/"+e.replace(t,""):e,l={changed:new n,initialized:new n,init:()=>{a||(i=h(),e.addEventListener("hashchange",c),a=!0,l.initialized.dispatch(r(i)))},stop:()=>{a&&(e.removeEventListener("hashchange",c),a=!1)},setHash:(...a)=>{(a=d(a))!==i&&(o(a),a===i&&(e.location.hash="#"+encodeURI(a)))},replaceHash:(...a)=>{(a=d(a))!==i&&(o(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); +(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); diff --git a/vendors/routes/signals.js b/vendors/routes/signals.js index aa1c034fa..5a6a43196 100644 --- a/vendors/routes/signals.js +++ b/vendors/routes/signals.js @@ -101,14 +101,7 @@ * @return {Function|null} Handler function bound to the signal or `null` if binding was previously detached. */ detach () { - return this.isBound()? this._signal.remove(this._listener, this.context) : null; - } - - /** - * @return {Boolean} `true` if binding is still bound to the signal and have a listener. - */ - isBound () { - return (!!this._signal && !!this._listener); + return (this._signal && this._listener) ? this._signal.remove(this._listener, this.context) : null; } /** @@ -118,20 +111,6 @@ return this._isOnce; } - /** - * @return {Function} Handler function bound to the signal. - */ - getListener () { - return this._listener; - } - - /** - * @return {Signal} Signal that listener is currently bound to. - */ - getSignal () { - return this._signal; - } - /** * Delete instance properties * @private diff --git a/vendors/routes/signals.min.js b/vendors/routes/signals.min.js index 06df42ec6..86486af44 100644 --- a/vendors/routes/signals.min.js +++ b/vendors/routes/signals.min.js @@ -4,4 +4,4 @@ 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.isBound()?this._signal.remove(this._listener,this.context):null}isBound(){return!!this._signal&&!!this._listener}isOnce(){return this._isOnce}getListener(){return this._listener}getSignal(){return this._signal}_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); +(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);