wiz-frameworks
Version:
wizlong react framework
99 lines (89 loc) • 3.27 kB
JavaScript
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
import React from 'react';
import dynamic from 'dva/dynamic';
import { Route, Switch, Redirect } from 'dva/router';
import { CacheSwitch, CacheRoute } from 'react-router-cache-route';
import DocumentTitle from 'react-document-title';
import assign from 'object-assign';
import { utils_tool } from '../tool';
var $$ = utils_tool.$$;
/**
* 生成动态组件
* @param {*} app
* @param {*} models
* @param {*} component
*/
export var dynamicWrapper = function dynamicWrapper(app, _models, component) {
return dynamic({
app: app,
models: function models() {
return _models;
},
component: component
});
};
/**
* 生成一组路由
* @param {*} app
* @param {*} routesConfig
*/
export var createRoutes = function createRoutes(app, routesConfig) {
return React.createElement(
Switch,
null,
routesConfig(app).map(function (config) {
return createRoute(app, function () {
return config;
});
})
);
};
// 路由映射表
window.dva_router_pathMap = {};
/**
* 生成单个路由
* @param {*} app
* @param {*} routesConfig
*/
var createRoute = function createRoute(app, routesConfig) {
var _routesConfig = routesConfig(app),
Comp = _routesConfig.component,
path = _routesConfig.path,
indexRoute = _routesConfig.indexRoute,
title = _routesConfig.title,
cached = _routesConfig.cached,
otherProps = _objectWithoutProperties(_routesConfig, ['component', 'path', 'indexRoute', 'title', 'cached']);
if (path && path !== '/') {
window.dva_router_pathMap[path] = _extends({ path: path, title: title }, otherProps);
// 为子路由增加parentPath
if (otherProps.childRoutes && otherProps.childRoutes.length) {
otherProps.childRoutes.forEach(function (item) {
if (window.dva_router_pathMap[item.key]) {
window.dva_router_pathMap[item.key].parentPath = path;
}
});
}
}
var htmlTitle = global.constants && global.constants.env.app_html_title ? global.constants.env.app_html_title : title;
var routeProps = assign({
key: path || $$.randomStr(4),
render: function render(props) {
return React.createElement(
DocumentTitle,
{
title: htmlTitle ? htmlTitle.replace(/{.*}/gi, title) : title
},
React.createElement(Comp, _extends({ routerData: otherProps }, props))
);
},
cached: cached
}, path && {
path: path
});
if (indexRoute) {
return [React.createElement(Redirect, { key: path + '_redirect', exact: true, from: path, to: indexRoute }), React.createElement(Route, routeProps)];
}
return cached ? React.createElement(CacheRoute, routeProps) : React.createElement(Route, _extends({}, routeProps, { exact: true }));
};
export { createRoute };