Skip to content

Commit

Permalink
Latest dist
Browse files Browse the repository at this point in the history
  • Loading branch information
Gary committed Sep 4, 2014
1 parent eecbc72 commit 76f8cd6
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 121 deletions.
243 changes: 123 additions & 120 deletions dist/lightrouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,22 @@
*/
this.hash = null;

options = options || {};

if (options.type)
{
this.setType(options.type);
}

if (options.pathRoot)
{
this.setPathRoot(options.pathRoot);
}
/**
* Named param replace and matching regex
* @type {Object}
*/
var namedParam = '([\\w-]+)';
this.namedParam = {
replace: new RegExp(':' + namedParam, 'g'),
match: namedParam,
};

if (options.path)
{
this.setPath(options.path);
}
options = options || {};

if (options.hash)
{
this.setHash(options.hash);
}
if (options.type) this.setType(options.type);
if (options.path) this.setPath(options.path);
if (options.pathRoot) this.setPathRoot(options.pathRoot);
if (options.hash) this.setHash(options.hash);

if (options.routes)
{
Expand All @@ -92,123 +87,131 @@
}
}

function routeToRegex(route)
{
if (typeof route === 'string')
{
return new RegExp('^' + route.replace(/\//g, '\\/').replace(/:(\w*)/g, '([^\\\\]*)') + '$');
}
return route;
}
LightRouter.prototype = {

/**
* Add a route
* @param string|RegExp route
* @param function callback
* @return self
*/
LightRouter.prototype.add = function(route, callback) {
this.routes.push({
route: route,
callback: callback
});
return this;
};
/**
* Add a route
* @param string|RegExp route
* @param function callback
* @return self
*/
add: function(route, callback) {
this.routes.push({
route: route,
callback: callback
});
return this;
},

/**
* Empty/clear all the routes
* @return self
*/
LightRouter.prototype.empty = function() {
this.routes = [];
return this;
};

/**
* Set's the routing type
* @param self
*/
LightRouter.prototype.setType = function(type) {
this.type = type;
return this;
};
/**
* Empty/clear all the routes
* @return self
*/
empty: function() {
this.routes = [];
return this;
},

/**
* Set the path root url
* @param string url
* @return self
*/
LightRouter.prototype.setPathRoot = function(url) {
this.pathRoot = url;
return this;
};
/**
* Set's the routing type
* @param self
*/
setType: function(type) {
this.type = type;
return this;
},

/**
* Sets the custom path to test routes against
* @param string path
* @return self
*/
LightRouter.prototype.setPath = function(path) {
this.path = path;
return this;
};
/**
* Set the path root url
* @param string url
* @return self
*/
setPathRoot: function(url) {
this.pathRoot = url;
return this;
},

/**
* Sets the custom hash to test routes against
* @param string hash
* @return self
*/
LightRouter.prototype.setHash = function(hash) {
this.hash = hash;
return this;
};
/**
* Sets the custom path to test routes against
* @param string path
* @return self
*/
setPath: function(path) {
this.path = path;
return this;
},

/**
* Gets the url to test the routes against
* @return self
*/
LightRouter.prototype.getUrl = function(routeType) {
/**
* Sets the custom hash to test routes against
* @param string hash
* @return self
*/
setHash: function(hash) {
this.hash = hash;
return this;
},

var url;
/**
* Converts the given route string to a regex, suitable for matching against.
* @param string route
* @return RegExp
*/
regexRoute: function(route) {
if (typeof route === 'string')
{
return new RegExp('^' + route.replace(/\//g, '\\/').replace(this.namedParam.replace, this.namedParam.match) + '$');
}
return route;
},

if (routeType === undefined)
{
routeType = this.type;
}
/**
* Gets the url to test the routes against
* @return self
*/
getUrl: function(routeType) {

if (routeType == 'path')
{
var rootRegex = new RegExp('^' + this.pathRoot + '/?');
url = this.path || window.location.pathname.substring(1);
url = url.replace(rootRegex, '');
}
else if (routeType == 'hash')
{
url = this.hash || window.location.hash.substring(1);
}

return url;
};
var url;

/**
* Run the router
* @return self
*/
LightRouter.prototype.run = function() {
var url = this.getUrl(), route, i, matched, routeOptions, routeRegex;
if (routeType === undefined)
{
routeType = this.type;
}

for (i in this.routes)
{
routeOptions = this.routes[i];
routeRegex = routeToRegex(routeOptions.route);
matched = url.match(routeRegex);
if (routeType == 'path')
{
var rootRegex = new RegExp('^' + this.pathRoot + '/?');
url = this.path || window.location.pathname.substring(1);
url = url.replace(rootRegex, '');
}
else if (routeType == 'hash')
{
url = this.hash || window.location.hash.substring(1);
}

return decodeURI(url);
},

/**
* Run the router
* @return self
*/
run: function() {
var url = this.getUrl(), i, matched, routeOptions, routeRegex;

if (matched)
for (i in this.routes)
{
routeOptions.callback.apply(undefined, matched.slice(1));
routeOptions = this.routes[i];
routeRegex = this.regexRoute(routeOptions.route);
matched = url.match(routeRegex);

if (matched)
{
routeOptions.callback.apply(undefined, matched.slice(1));
}
}
return this;
}
return this;
};

return LightRouter;
Expand Down
2 changes: 1 addition & 1 deletion dist/lightrouter.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 76f8cd6

Please sign in to comment.