From 33d325999497bfdd382ee1410839d86a5b5c37dd Mon Sep 17 00:00:00 2001 From: Gary Date: Fri, 8 May 2015 14:13:41 +0100 Subject: [PATCH] Dist --- dist/lightrouter.js | 48 ++++++++++++++++++++++++++--------------- dist/lightrouter.min.js | 2 +- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/dist/lightrouter.js b/dist/lightrouter.js index ca1c901..bb876d5 100644 --- a/dist/lightrouter.js +++ b/dist/lightrouter.js @@ -114,10 +114,7 @@ * @return self */ add: function(route, callback) { - this.routes.push(new this.Route({ - route: route, - callback: callback - }, this)); + this.routes.push(new this.Route(route, callback, this)); return this; }, @@ -213,6 +210,21 @@ return decodeURI(url); }, + /** + * Attempt to match a one-time route and callback + * + * @param {string} path + * @param {closure|string} callback + * @return {mixed} + */ + match: function(path, callback) { + var route = new this.Route(path, callback, this); + if (route.test(this.getUrl())) + { + return route.run(); + } + }, + /** * Run the router * @return Route|undefined @@ -238,12 +250,14 @@ /** * Route object - * @param {object} options Options passed to the route + * @param {string} path + * @param {string} closure * @param {LightRouter} router Instance of the light router the route belongs to. */ - function Route(options, router) + function Route(path, callback, router) { - this.options = options; + this.path = path; + this.callback = callback; this.router = router; this.values = []; } @@ -257,13 +271,13 @@ */ regex: function() { - var route = this.options.route; + var path = this.path; - if (typeof route === 'string') + if (typeof path === 'string') { - return new RegExp('^' + route.replace(/\//g, '\\/').replace(this.router.namedParam.match, this.router.namedParam.replace) + '$'); + return new RegExp('^' + path.replace(/\//g, '\\/').replace(this.router.namedParam.match, this.router.namedParam.replace) + '$'); } - return route; + return path; }, /** @@ -272,12 +286,12 @@ */ params: function() { - var obj = {}, name, values = this.values, params = values, i, t = 0, route = this.options.route; + var obj = {}, name, values = this.values, params = values, i, t = 0, path = this.path; - if (typeof route === 'string') + if (typeof path === 'string') { t = 1; - params = route.match(this.router.namedParam.match); + params = path.match(this.router.namedParam.match); } for (i in params) @@ -309,11 +323,11 @@ * @return {mixed} */ run: function() { - if (typeof this.options.callback === 'string') + if (typeof this.callback === 'string') { - return this.router.handler[this.options.callback](this.params()); + return this.router.handler[this.callback](this.params()); } - return this.options.callback.apply(this.router.context, [this.params()]); + return this.callback.apply(this.router.context, [this.params()]); } }; diff --git a/dist/lightrouter.min.js b/dist/lightrouter.min.js index 7b60fef..5d881fa 100644 --- a/dist/lightrouter.min.js +++ b/dist/lightrouter.min.js @@ -1 +1 @@ -/* lightrouter.js - Copyright 2014 Gary Green. Licensed under the Apache License, Version 2.0 */!function(t){"undefined"!=typeof exports?module.exports=t():window.LightRouter=t(window)}(function(t){function e(e){this.pathRoot="",this.routes=[],this.type="path",this.path=null,this.hash=null,this.context=this,this.handler=t;var s="([\\w-]+)";if(this.namedParam={match:new RegExp("{("+s+")}","g"),replace:s},e=e||{},e.type&&this.setType(e.type),e.path&&this.setPath(e.path),e.pathRoot&&this.setPathRoot(e.pathRoot),e.hash&&this.setHash(e.hash),e.context&&this.setContext(e.context),e.handler&&this.setHandler(e.handler),e.routes){var h;for(h in e.routes)this.add(h,e.routes[h])}}function s(t,e){this.options=t,this.router=e,this.values=[]}return e.prototype={Route:s,add:function(t,e){return this.routes.push(new this.Route({route:t,callback:e},this)),this},empty:function(){return this.routes=[],this},setType:function(t){return this.type=t,this},setPathRoot:function(t){return this.pathRoot=t,this},setPath:function(t){return this.path=t,this},setHash:function(t){return this.hash=t,this},setContext:function(t){return this.context=t,this},setHandler:function(t){return this.handler=t,this},getUrl:function(e){var s;if(e=e||this.type,"path"==e){var h=new RegExp("^"+this.pathRoot+"/?");s=this.path||t.location.pathname.substring(1),s=s.replace(h,"")}else"hash"==e&&(s=this.hash||t.location.hash.substring(1));return decodeURI(s)},run:function(){var t,e=this.getUrl();for(var s in this.routes)if(t=this.routes[s],t.test(e))return t.run(),t}},s.prototype={regex:function(){var t=this.options.route;return"string"==typeof t?new RegExp("^"+t.replace(/\//g,"\\/").replace(this.router.namedParam.match,this.router.namedParam.replace)+"$"):t},params:function(){var t,e,s={},h=this.values,r=h,i=0,n=this.options.route;"string"==typeof n&&(i=1,r=n.match(this.router.namedParam.match));for(e in r)t=i?r[e].replace(this.router.namedParam.match,"$1"):e,s[t]=h[e];return s},test:function(t){var e;return(e=t.match(this.regex()))?(this.values=e.slice(1),!0):!1},run:function(){return"string"==typeof this.options.callback?this.router.handler[this.options.callback](this.params()):this.options.callback.apply(this.router.context,[this.params()])}},e}); \ No newline at end of file +/* lightrouter.js - Copyright 2014 Gary Green. Licensed under the Apache License, Version 2.0 */!function(t){"undefined"!=typeof exports?module.exports=t():window.LightRouter=t(window)}(function(t){function e(e){this.pathRoot="",this.routes=[],this.type="path",this.path=null,this.hash=null,this.context=this,this.handler=t;var h="([\\w-]+)";if(this.namedParam={match:new RegExp("{("+h+")}","g"),replace:h},e=e||{},e.type&&this.setType(e.type),e.path&&this.setPath(e.path),e.pathRoot&&this.setPathRoot(e.pathRoot),e.hash&&this.setHash(e.hash),e.context&&this.setContext(e.context),e.handler&&this.setHandler(e.handler),e.routes){var s;for(s in e.routes)this.add(s,e.routes[s])}}function h(t,e,h){this.path=t,this.callback=e,this.router=h,this.values=[]}return e.prototype={Route:h,add:function(t,e){return this.routes.push(new this.Route(t,e,this)),this},empty:function(){return this.routes=[],this},setType:function(t){return this.type=t,this},setPathRoot:function(t){return this.pathRoot=t,this},setPath:function(t){return this.path=t,this},setHash:function(t){return this.hash=t,this},setContext:function(t){return this.context=t,this},setHandler:function(t){return this.handler=t,this},getUrl:function(e){var h;if(e=e||this.type,"path"==e){var s=new RegExp("^"+this.pathRoot+"/?");h=this.path||t.location.pathname.substring(1),h=h.replace(s,"")}else"hash"==e&&(h=this.hash||t.location.hash.substring(1));return decodeURI(h)},match:function(t,e){var h=new this.Route(t,e,this);return h.test(this.getUrl())?h.run():void 0},run:function(){var t,e=this.getUrl();for(var h in this.routes)if(t=this.routes[h],t.test(e))return t.run(),t}},h.prototype={regex:function(){var t=this.path;return"string"==typeof t?new RegExp("^"+t.replace(/\//g,"\\/").replace(this.router.namedParam.match,this.router.namedParam.replace)+"$"):t},params:function(){var t,e,h={},s=this.values,r=s,i=0,n=this.path;"string"==typeof n&&(i=1,r=n.match(this.router.namedParam.match));for(e in r)t=i?r[e].replace(this.router.namedParam.match,"$1"):e,h[t]=s[e];return h},test:function(t){var e;return(e=t.match(this.regex()))?(this.values=e.slice(1),!0):!1},run:function(){return"string"==typeof this.callback?this.router.handler[this.callback](this.params()):this.callback.apply(this.router.context,[this.params()])}},e}); \ No newline at end of file