mirror of
https://github.com/the-djmaze/snappymail.git
synced 2025-01-06 23:07:56 +08:00
7 lines
3.1 KiB
JavaScript
7 lines
3.1 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,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);
|