forked from ramiel/router.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.min.js
17 lines (17 loc) · 4.83 KB
/
router.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
Router.js
@version 0.8.4
@author: Fabrizio Ruggeri
@website: http://ramielcreations.com/projects/router-js/
@license GPL-v2
*/
(function(m,l){"undefined"!=typeof module?module.exports=l():"function"==typeof define&&"object"==typeof define.amd?define(l):this[m]=l()})("Router",function(){function m(a,b){a.addEventListener?a.addEventListener("hashchange",b,!1):a.attachEvent&&a.attachEvent("hashchange",b)}function l(){for(var a=1;a<arguments.length;a++)for(var b in arguments[a])arguments[a].hasOwnProperty(b)&&(arguments[0][b]=arguments[a][b]);return arguments[0]}Function.prototype.bind||(Function.prototype.bind=function(a){var b=
this,d=Array.prototype.slice.call(arguments);a=d.shift();return function(){return b.apply(a,d.concat(Array.prototype.slice.call(arguments)))}});var p=/:([\w\d]+)/g,r=/\/\*(?!\*)/,s=/\*{2}/,n=/\/*$/,q=function(a){this.href=a;this.params;this.query;this.splat};q.prototype.get=function(a,b){return this.params&&void 0!==this.params[a]?this.params[a]:this.query&&void 0!==this.query[a]?this.query[a]:void 0!==b?b:void 0};var c=function(a){this._options=l({ignorecase:!0},a);this._routes=[];this._befores=
[];this._errors={_:function(a,d,e){console&&console.warn&&console.warn("Router.js : "+e)},_404:function(a,d){console&&console.warn&&console.warn("404! Unmatched route for url "+d)},_500:function(a,d){if(console&&console.error)console.error("500! Internal error route for url "+d);else throw Error("500");}};this._paused=!1;this._hasChangeHandler=this._onHashChange.bind(this);m(window,this._hasChangeHandler)};c.prototype._onHashChange=function(a){this._paused||this._route(this._extractFragment(window.location.href));
return!0};c.prototype._extractFragment=function(a){var b=a.indexOf("#");return 0<=b?a.substring(b):"#/"};c.prototype._throwsRouteError=function(a,b,d){if(this._errors["_"+a]instanceof Function)this._errors["_"+a](b,d,a);else this._errors._(b,d,a);return!1};c.prototype._buildRequestObject=function(a,b,d){if(!a)throw Error("Unable to compile request object");var e=new q(a);b&&(e.params=b);b=a.split("?");if(2==b.length){a=null;b=b[1].split("&");e.query={};for(var c=0,f=b.length;c<f;c++)a=b[c].split("="),
e.query[decodeURI(a[0])]=decodeURI(a[1].replace(/\+/g,"%20"));e.query}d&&0<d.length&&(e.splats=d);return e};c.prototype._followRoute=function(a,b,d){var e=d.splice(0,1),e=this._routes[e],c=b.match(e.path),f={},h=[];if(!e)return this._throwsRouteError(500,Error("Internal error"),a);for(var g=0,l=e.paramNames.length;g<l;g++)f[e.paramNames[g]]=c[g+1];g+=1;if(c&&g<c.length)for(;g<c.length;g++)h.push(c[g]);b=0===d.length?null:function(b,d,c,e){return function(e,g){if(e)return this._throwsRouteError(g||
500,e,a);this._followRoute(b,d,c)}.bind(this)}.call(this,a,b,d);d=this._buildRequestObject(a,f,h);e.routeAction(d,b)};c.prototype._routeBefores=function(a,b,d,c,k){var f;if(0<a.length){var h=a.splice(0,1),h=h[0];f=function(b,f){if(b)return this._throwsRouteError(f||500,b,d);this._routeBefores(a,h,d,c,k)}.bind(this)}else f=function(a,b){if(a)return this._throwsRouteError(b||500,a,d);this._followRoute(d,c,k)}.bind(this);b(this._buildRequestObject(d),f)};c.prototype._route=function(a){var b="",c=this._befores.slice(),
e=[],k,f=a;if(0===f.length)return!0;f=f.replace(n,"");k=f.split("?")[0].replace(n,"");for(var h in this._routes)this._routes.hasOwnProperty(h)&&(b=this._routes[h],b.path.test(k)&&e.push(h));if(0<e.length)0<c.length?(b=c.splice(0,1),b=b[0],this._routeBefores(c,b,a,f,e)):this._followRoute(a,f,e);else return this._throwsRouteError(404,null,a)};c.prototype.pause=function(){this._paused=!0;return this};c.prototype.play=function(a){this._paused=!1;("undefined"==typeof a?0:a)&&this._route(this._extractFragment(window.location.href));
return this};c.prototype.setLocation=function(a){window.history.pushState(null,"",a);return this};c.prototype.redirect=function(a){this.setLocation(a);this._paused||this._route(this._extractFragment(a));return this};c.prototype.addRoute=c.prototype.add=c.prototype.route=c.prototype.get=function(a,b){var c,e=this._options.ignorecase?"i":"",k=[];if("string"==typeof a){for(a=a.replace(n,"");null!==(c=p.exec(a));)k.push(c[1]);a=RegExp(a.replace(p,"([^/\\?]+)").replace(r,"/([^/\\?]+)").replace(s,"(.*?)\\??")+
"(?:\\?.+)?$",e)}this._routes.push({path:a,paramNames:k,routeAction:b});return this};c.prototype.before=function(a){this._befores.push(a);return this};c.prototype.errors=function(a,b){if(isNaN(a))throw Error("Invalid code for routes error handling");if(!(b instanceof Function))throw Error("Invalid callback for routes error handling");this._errors["_"+a]=b;return this};c.prototype.run=function(a){a||(a=this._extractFragment(window.location.href));a=0===a.indexOf("#")?a:"#"+a;this.redirect(a);return this};
c.prototype.destroy=function(){var a=window,b=this._hasChangeHandler;a.removeEventListener?a.removeEventListener("hashchange",b,!1):a.detachEvent&&a.detachEvent("hashchange",b);return this};return c});