UNPKG

react-web-router-flux

Version:

adapter react native-router-flux for web

231 lines (193 loc) 10.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /** * 名称:适配react-native-router-flux 风格Actions * 作者:Beven * 日期:2016-11-14 * 描述:提供api方式进行页面路由跳转 */ //>>引入依赖 var _Scene = require("./Scene"); var _Scene2 = _interopRequireDefault(_Scene); var _reactRouter = require("react-router"); var _utils = require("./utils"); var _utils2 = _interopRequireDefault(_utils); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var _router = null; var Actions = function () { /** * Actions构造函数 */ function Actions() { _classCallCheck(this, Actions); this.create = this.create.bind(this); this.iterate = this.iterate.bind(this); this.pop = this.pop.bind(this); this.refresh = this.refresh.bind(this); this.focus = this.focus.bind(this); } /** * 获取router对象 */ _createClass(Actions, [{ key: "create", /** * 创建一组路由 * @param scenes {Scene} 根路由 */ value: function create(scenes) { this.iterate(scenes); return scenes; } /** * 依次遍历所有创建的route,并且使用search进行递归创建scene对应api函数 * @param root {Scene} 根scene */ }, { key: "iterate", value: function iterate(root) { this.root = root; this.assert(root.props, 'props should be defined for stack'); this.build(root); this.search(root); } /** * 递遍历指定scene路由 * @param scene {Scene} 路由 * @param parentKey {String} 当前路由的所有父路由组合名称 * @param parentUrl {String} 当前节点的所有父路由组合url */ }, { key: "search", value: function search(scene, parentKey, parentUrl) { var path = scene.props.path; var children = scene.props.children; if (!children || Object.prototype.toString.call(children) != "[object Array]") { return; } parentUrl = parentUrl != null ? parentUrl + "/" + path : path; parentKey = _utils2.default.nameJoin(parentKey, parentKey != null ? scene.key : ""); var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = children[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var child = _step.value; this.build(child, parentKey, parentUrl); this.search(child, parentKey, parentUrl); } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } } /** * 生成scene对应的api函数 * @param scene {Scene} 路由 * @param parentKey {String} 当前路由的所有父路由组合名称 * @param parentUrl {String} 当前节点的所有父路由组合url */ }, { key: "build", value: function build(scene, parentKey, parentUrl) { var _this = this; var props = scene.props; var key = scene.key; var url = (parentUrl + "/" + scene.props.path).replace(/\/\//g, "/"); if (!this.isEmpty(key) && (props.component || props.getComponent)) { key = _utils2.default.nameJoin(key, parentKey); this[key] = function () { var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; return _this.go(url, props); }; } } }, { key: "current", value: function current() { if (global.location) { return global.location.href; } } /** * 跳转到指定url * @param url {String} url地址 * @param props {Object} 属性参数 */ }, { key: "go", value: function go(url) { var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var absUrl = _utils2.default.param(url, props); this.router.push(absUrl); } }, { key: "popTo", value: function popTo() { var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; } }, { key: "pop", value: function pop(num) { _reactRouter.browserHistory.goBack(); } }, { key: "jump", value: function jump() { var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; _reactRouter.browserHistory.go(props); } }, { key: "refresh", value: function refresh() { var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; if (global.location) { return global.location.reload(); } } }, { key: "focus", value: function focus() { var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; } }, { key: "assert", value: function assert(v, message) { if (this.isEmpty(v)) { throw new Error(message); } } }, { key: "isEmpty", value: function isEmpty(v) { return v == null || v == undefined || v.toString().replace(/\s/g, '') == ''; } }, { key: "router", get: function get() { return _router; } /** * 设置router对象 */ , set: function set(value) { _router = value; } }]); return Actions; }(); exports.default = new Actions();