UNPKG

reactant-router

Version:

A router plugin for Reactant

234 lines (219 loc) 10.5 kB
'use strict'; var React = require('react'); var reactantModule = require('reactant-module'); var connectedReactRouter = require('connected-react-router'); var history = require('history'); /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */ var extendStatics = function(d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; function __extends(d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); } function __decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } function __param(paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } } function __metadata(metadataKey, metadataValue) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue); } function __read(o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; } function __spreadArray(to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; var RouterOptions = Symbol('RouterOptions'); // TODO: support ssr and router config var ReactantRouter = /** @class */ (function (_super) { __extends(ReactantRouter, _super); function ReactantRouter(options) { var _a, _b; var _this = _super.call(this) || this; _this.options = options; _this.onLocationChanged = connectedReactRouter.onLocationChanged; _this.routerActions = connectedReactRouter.routerActions; _this.stateKey = 'router'; _this.ConnectedRouter = function (props) { return (React.createElement(connectedReactRouter.ConnectedRouter, { history: _this.history }, props.children)); }; _this.provider = function (props) { if (!_this.autoProvide) return props.children; return React.createElement(_this.ConnectedRouter, null, props.children); }; var _c = (_this.options || {}).autoProvide, autoProvide = _c === void 0 ? true : _c; _this.autoProvide = autoProvide; _this.autoCreateHistory = (_b = (_a = _this.options) === null || _a === void 0 ? void 0 : _a.autoCreateHistory) !== null && _b !== void 0 ? _b : true; if (_this.autoCreateHistory) { _this.history = _this.options.createHistory(); _this.middleware = function (store) { return function (next) { return function (action) { if (action.type !== connectedReactRouter.CALL_HISTORY_METHOD) { return next(action); } // Just call `history` method and `history` will change routing, then trigger `history.listen`, and dispatch LOCATION_CHANGE action var _a = action.payload, method = _a.method, _b = _a.args, args = _b === void 0 ? [] : _b; var history = _this.history; history[method].apply(history, __spreadArray([], __read(args), false)); }; }; }; } return _this; } Object.defineProperty(ReactantRouter.prototype, "store", { get: function () { return this[reactantModule.storeKey]; }, enumerable: false, configurable: true }); ReactantRouter.prototype.beforeCombineRootReducers = function (reducers) { var _a, _b, _c; var _d, _e, _f; if (Object.prototype.hasOwnProperty.call(reducers, this.stateKey)) { throw new Error("The identifier '".concat(this.stateKey, "' has a duplicate name, please reset the option 'stateKey' of 'ReactantRouter' module.")); } if (this[reactantModule.enableAutoComputedKey]) { var signalMap = (_d = this[reactantModule.signalMapKey]) !== null && _d !== void 0 ? _d : {}; signalMap[this.stateKey] = reactantModule.signal({}); var current_1 = signalMap[this.stateKey]; if (!this[reactantModule.signalMapKey]) { Object.defineProperties(this, (_a = {}, _a[reactantModule.signalMapKey] = { enumerable: false, configurable: false, writable: false, value: signalMap, }, _a)); } var reducer_1 = connectedReactRouter.connectRouter((_e = this.history) !== null && _e !== void 0 ? _e : this.defaultHistory); return Object.assign(reducers, (_b = {}, // TODO: fix type _b[this.stateKey] = function (state, action) { var result = reducer_1(state, action); if (result !== state) { // update signal value for auto-computed current_1.value = result; } return result; }, _b)); } return Object.assign(reducers, (_c = {}, _c[this.stateKey] = connectedReactRouter.connectRouter((_f = this.history) !== null && _f !== void 0 ? _f : this.defaultHistory), _c)); }; Object.defineProperty(ReactantRouter.prototype, "router", { get: function () { var _a, _b; if (this[reactantModule.enableAutoComputedKey]) { var currentRouter = (_a = this[reactantModule.signalMapKey]) === null || _a === void 0 ? void 0 : _a[this.stateKey]; if (currentRouter) { return currentRouter.value; } } return (_b = this.store) === null || _b === void 0 ? void 0 : _b.getState()[this.stateKey]; }, enumerable: false, configurable: true }); Object.defineProperty(ReactantRouter.prototype, "currentPath", { get: function () { var _a; return (_a = this.router) === null || _a === void 0 ? void 0 : _a.location.pathname; }, enumerable: false, configurable: true }); ReactantRouter.prototype.push = function (path, state) { var _a; (_a = this.store) === null || _a === void 0 ? void 0 : _a.dispatch(this.routerActions.push(path, state)); }; ReactantRouter.prototype.replace = function (path, state) { var _a; (_a = this.store) === null || _a === void 0 ? void 0 : _a.dispatch(this.routerActions.replace(path, state)); }; ReactantRouter.prototype.go = function (n) { var _a; (_a = this.store) === null || _a === void 0 ? void 0 : _a.dispatch(this.routerActions.go(n)); }; ReactantRouter.prototype.goBack = function () { var _a; (_a = this.store) === null || _a === void 0 ? void 0 : _a.dispatch(this.routerActions.goBack()); }; ReactantRouter.prototype.goForward = function () { var _a; (_a = this.store) === null || _a === void 0 ? void 0 : _a.dispatch(this.routerActions.goForward()); }; ReactantRouter = __decorate([ reactantModule.injectable({ name: 'Router', }), __param(0, reactantModule.inject(RouterOptions)), __metadata("design:paramtypes", [Object]) ], ReactantRouter); return ReactantRouter; }(reactantModule.PluginModule)); Object.defineProperty(exports, "LOCATION_CHANGE", { enumerable: true, get: function () { return connectedReactRouter.LOCATION_CHANGE; } }); Object.defineProperty(exports, "createBrowserHistory", { enumerable: true, get: function () { return history.createBrowserHistory; } }); Object.defineProperty(exports, "createHashHistory", { enumerable: true, get: function () { return history.createHashHistory; } }); Object.defineProperty(exports, "createMemoryHistory", { enumerable: true, get: function () { return history.createMemoryHistory; } }); exports.Router = ReactantRouter; exports.RouterOptions = RouterOptions;