UNPKG

react-crossroads

Version:

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

96 lines (79 loc) 2.53 kB
var Logger, RouterActions, RouterConstants, isAbsoluteUrl, pattern; RouterConstants = require('../constants/RouterConstants'); Logger = require('../utils/logger'); pattern = require('crossroads').patternLexer; isAbsoluteUrl = function(url) { return typeof url === 'string' && /^https?:\/\//.test(url); }; RouterActions = (function() { function RouterActions(dispatcher, stores) { this.dispatcher = dispatcher; this.stores = stores; } RouterActions.prototype._dispatch = function(action) { if (this.stores.location.isBlocked()) { action = { actionType: RouterConstants.LOCATION_ATTEMPT, originalAction: action }; } return this.dispatcher.handleRouteAction(action); }; RouterActions.prototype._resolvePath = function(to, params) { switch (false) { case !isAbsoluteUrl(to): return to; case !this.stores.route.hasRoute(to): return this.stores.route.pathTo(to, params); default: return pattern.interpolate(to, params); } }; RouterActions.prototype.transition = function(to, params) { var path; path = this._resolvePath(to, params); Logger.debug.log("RouterActions.transition(" + path + ")"); return this._dispatch({ actionType: RouterConstants.LOCATION_CHANGE, path: path, fromLocationEvent: false }); }; RouterActions.prototype.replace = function(to, params) { var path; path = this._resolvePath(to, params); Logger.debug.log("RouterActions.replace(" + path + ")"); return this._dispatch({ actionType: RouterConstants.LOCATION_REPLACE, path: path }); }; RouterActions.prototype.back = function() { Logger.debug.log("RouterActions.back()"); return this._dispatch({ actionType: RouterConstants.LOCATION_GOBACK }); }; RouterActions.prototype.block = function(id) { return this.dispatcher.handleRouteAction({ actionType: RouterConstants.LOCATION_BLOCK, blockId: id }); }; RouterActions.prototype.unblock = function(id) { return this.dispatcher.handleRouteAction({ actionType: RouterConstants.LOCATION_UNBLOCK, blockId: id }); }; RouterActions.prototype.updateLocation = function(path) { Logger.debug.log("RouterActions.updateLocation(" + path + ")"); return this._dispatch({ actionType: RouterConstants.LOCATION_CHANGE, path: path, fromLocationEvent: true }); }; return RouterActions; })(); module.exports = RouterActions;