var Router; var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; Router = (function() { function Router(pages) { this.pages = pages; this.routes = this.build(); $(window).bind('hashchange', __bind(function() { return this.draw(); }, this)); } Router.prototype.build = function() { var f, fragments, map, page, pattern, route, routes, _ref; routes = []; _ref = this.pages; for (pattern in _ref) { page = _ref[pattern]; routes.push(route = { args: [], page: page, re: null }); if (pattern === 'default') { route.re = pattern; continue; } fragments = (function() { var _i, _len, _ref2, _results; _ref2 = pattern.split('/'); _results = []; for (_i = 0, _len = _ref2.length; _i < _len; _i++) { f = _ref2[_i]; if (f.length > 0) { _results.push(f); } } return _results; })(); map = function(fragment) { if (fragment[0] === ':') { route.args.push(fragment.replace(':', '')); return '(/[^/]+)?'; } else { return '/' + fragment; } }; route.re = new RegExp('#' + ((function() { var _i, _len, _results; _results = []; for (_i = 0, _len = fragments.length; _i < _len; _i++) { f = fragments[_i]; _results.push(map(f)); } return _results; })()).join('')); } return routes; }; Router.prototype.draw = function() { var args, ix, name, opts, route, _i, _len, _ref, _ref2, _ref3; _ref = this.match(), route = _ref[0], args = _ref[1]; route || (route = this.defaultRoute()); if (!route) { return; } _ref2 = [{}, 0], opts = _ref2[0], ix = _ref2[1]; _ref3 = route.args; for (_i = 0, _len = _ref3.length; _i < _len; _i++) { name = _ref3[_i]; opts[name] = args[ix++]; } return route.page.draw(opts); }; Router.prototype.match = function() { var arg, args, match, route, _i, _len, _ref; _ref = this.routes; for (_i = 0, _len = _ref.length; _i < _len; _i++) { route = _ref[_i]; if (match = window.location.hash.match(route.re)) { args = (function() { var _j, _len2, _ref2, _results; _ref2 = match.slice(1); _results = []; for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) { arg = _ref2[_j]; _results.push(arg.replace('/', '')); } return _results; })(); return [route, args]; } } return []; }; Router.prototype.defaultRoute = function() { var route, _i, _len, _ref; _ref = this.routes; for (_i = 0, _len = _ref.length; _i < _len; _i++) { route = _ref[_i]; if (route.re === 'default') { return route; } } }; return Router; })();