snappymail/vendors/routes/crossroads.min.js
djmaze e7180a86ce Made eslint using 'browser' environment and added globals, because RainLoop is used in browsers.
This also allowed to remove all webpack 'externals' overhead.
2020-08-12 00:25:36 +02:00

7 lines
3.3 KiB
JavaScript

/*
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,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<a;)(t=s[r]).route.matched.dispatch.apply(t.route.matched,t.params),t.isFirst=!r,this.routed.dispatch(e,t),r+=1;else this.bypassed.dispatch(e)}getNumRoutes(){return this._routes.length}_getMatchedRoutes(e){for(var t,s=[],r=this._routes,a=r.length;t=r[--a];)s.length&&!t.greedy||!t.match(e)||s.push({route:t,params:t._getParamsArray(e)});return s}}})(this);