UNPKG

react-crossroads

Version:

Client side router for web applications built with React and utilizing the Flux architecture. The backing routing engine is CrossroadsJs.

149 lines (122 loc) • 4.94 kB
var Crossroads, EventEmitter, Logger, RouteStore, RouterConstants, _, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; RouterConstants = require('../constants/RouterConstants'); EventEmitter = require('events').EventEmitter; Crossroads = require('crossroads'); Logger = require('../utils/logger'); _ = require('lodash'); RouteStore = (function(_super) { __extends(RouteStore, _super); function RouteStore(dispatcher, locationStore, routerActions) { this.locationStore = locationStore; this.routerActions = routerActions; this._route = __bind(this._route, this); this._locationChanged = __bind(this._locationChanged, this); this.getCurrentChain = __bind(this.getCurrentChain, this); this.handler = __bind(this.handler, this); this.setMaxListeners(0); this.dispatchToken = dispatcher.register(this.handler); this.router = Crossroads.create(); this.router.routed.add(this._route); this.router.bypassed.add(this._routeNotFound); this.router.ignoreState = true; this.router.shouldTypecast = true; this._routes = {}; this._currentChain = null; } RouteStore.prototype.handler = function(payload) { return null; }; RouteStore.prototype.getCurrentChain = function() { return this._currentChain; }; RouteStore.prototype.isActive = function(to, params) { if (!((this._currentChain != null) && (this._currentChain.isActive != null))) { return false; } return this._currentChain.isActive(to, params); }; RouteStore.prototype.getRoute = function(name) { if (name != null) { return this._routes[name].route; } else { return void 0; } }; RouteStore.prototype.hasRoute = function(name) { if (name != null) { return this._routes[name] != null; } else { return false; } }; RouteStore.prototype.pathTo = function(to, params) { var endpoint, route; if (!this.hasRoute(to)) { throw new Error("No route defined for `" + to + "`"); } route = this.getRoute(to); endpoint = this.getRoute(to).endpoint; return endpoint.makePath(params); }; RouteStore.prototype.hrefTo = function(to, params) { var path; path = this.pathTo(to, params); return this.locationStore.pathToHref(path); }; RouteStore.prototype.start = function() { this.locationStore.addChangeListener(this._locationChanged); return this._locationChanged(); }; RouteStore.prototype._locationChanged = function() { Logger.debug.log("Location store update: " + (this.locationStore.getCurrentPath())); return this.router.parse(this.locationStore.getCurrentPath()); }; RouteStore.prototype._route = function(request, data) { var endpoint, params; endpoint = data.route.endpoint; Logger.debug.log("Route matched `" + request + "` to " + endpoint.name); params = _.zipObject(data.route._paramsIds, data.params); this._currentChain = endpoint.createActiveChain(request, params); return this._emitChange(); }; RouteStore.prototype._routeNotFound = function(request) { return console.error("404 - Not Found " + request); }; RouteStore.prototype.register = function(endpoint) { var route; if (this.hasRoute(endpoint.name)) { throw new Error("Route with duplicate name `" + endpoint.name + "`"); } if (endpoint.path == null) { throw new Error("No path provided for `" + endpoint.name + "`"); } route = this.router.addRoute(endpoint.path, void 0, endpoint.priority || void 0); route.endpoint = endpoint; endpoint.route = route; this._routes[endpoint.name] = endpoint; }; RouteStore.prototype.registerAlias = function(aliasEndpoint, destinationEndpoint) { if (this.hasRoute(aliasEndpoint.name)) { throw new Error("Route with duplicate name `" + aliasEndpoint.name + "`"); } if (destinationEndpoint.route == null) { throw new Error("Endpoint `" + destinationEndpoint.name + "` has not been registered, cannot create alias"); } aliasEndpoint.route = destinationEndpoint.route; this._routes[aliasEndpoint.name] = aliasEndpoint; }; RouteStore.prototype._emitChange = function() { return this.emit(RouterConstants.CHANGE_EVENT); }; RouteStore.prototype.addChangeListener = function(listener) { return this.on(RouterConstants.CHANGE_EVENT, listener); }; RouteStore.prototype.removeChangeListener = function(listener) { return this.removeListener(RouterConstants.CHANGE_EVENT, listener); }; return RouteStore; })(EventEmitter); module.exports = RouteStore;