You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

445 lines
20 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import isEqual from 'lodash.isequal';
import memoizeOne from 'memoize-one';
import hash from 'hash.js';
import { pathToRegexp } from '@qixian.cs/path-to-regexp';
export function stripQueryStringAndHashFromPath(url) {
return url.split('?')[0].split('#')[0];
}
/* eslint no-useless-escape:0 import/prefer-default-export:0 */
var reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
export var isUrl = function isUrl(path) {
return reg.test(path);
};
export var getKeyByPath = function getKeyByPath(item) {
var path = item.path;
if (!path || path === '/') {
// 如果还是没有用对象的hash 生成一个
try {
return "/".concat(hash.sha256().update(JSON.stringify(item)).digest('hex'));
} catch (error) {// dom some thing
}
}
return path ? stripQueryStringAndHashFromPath(path) : path;
};
/**
* 获取locale增加了一个功能如果 locale = false将不使用国际化
* @param item
* @param parentName
*/
var getItemLocaleName = function getItemLocaleName(item, parentName) {
var name = item.name,
locale = item.locale; // 如果配置了 locale 并且 locale 为 false或 ""
if ('locale' in item && locale === false || !name) {
return false;
}
return item.locale || "".concat(parentName, ".").concat(name);
};
/**
* 如果不是 / 开头的和父节点做一下合并
* 如果是 / 开头的不作任何处理
* 如果是 url 也直接返回
* @param path
* @param parentPath
*/
var mergePath = function mergePath() {
var path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var parentPath = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '/';
if ((path || parentPath).startsWith('/')) {
return path;
}
if (isUrl(path)) {
return path;
}
return "/".concat(parentPath, "/").concat(path).replace(/\/\//g, '/').replace(/\/\//g, '/');
}; // bigfish 的兼容准话
var bigfishCompatibleConversions = function bigfishCompatibleConversions(route, props) {
var _route$menu = route.menu,
menu = _route$menu === void 0 ? {} : _route$menu,
indexRoute = route.indexRoute,
_route$path = route.path,
path = _route$path === void 0 ? '' : _route$path,
children = route.children;
var _menu$name = menu.name,
name = _menu$name === void 0 ? route.name : _menu$name,
_menu$icon = menu.icon,
icon = _menu$icon === void 0 ? route.icon : _menu$icon,
_menu$hideChildren = menu.hideChildren,
hideChildren = _menu$hideChildren === void 0 ? route.hideChildren : _menu$hideChildren,
_menu$flatMenu = menu.flatMenu,
flatMenu = _menu$flatMenu === void 0 ? route.flatMenu : _menu$flatMenu; // 兼容平铺式写法
// 拼接 childrenRoutes, 处理存在 indexRoute 时的逻辑
var childrenRoutes = indexRoute && // 如果只有 redirect,不用处理的
Object.keys(indexRoute).join(',') !== 'redirect' ? [_objectSpread({
path: path,
menu: menu
}, indexRoute)].concat(children || []) : children; // 拼接返回的 menu 数据
var result = _objectSpread({}, route);
if (name) {
result.name = name;
}
if (icon) {
result.icon = icon;
}
if (childrenRoutes && childrenRoutes.length) {
/** 在菜单中隐藏子项 */
if (hideChildren) {
delete result.children;
return result;
} // 需要重新进行一次
var routers = formatter(_objectSpread(_objectSpread({}, props), {}, {
data: childrenRoutes
}), route);
/** 在菜单中只隐藏此项,子项往上提,仍旧展示 */
if (flatMenu) {
return routers;
}
result.children = routers;
}
return result;
};
/**
*
* @param props
* @param parent
*/
function formatter(props) {
var parent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
path: '/'
};
var data = props.data,
formatMessage = props.formatMessage,
parentName = props.parentName,
menuLocale = props.locale;
if (!data || !Array.isArray(data)) {
return [];
}
return data.filter(function (item) {
if (!item) return false;
if (item.routes || item.children) return true;
if (item.path) return true;
if (item.layout) return true; // 重定向
if (item.redirect) return false;
return false;
}).filter(function (item) {
var _item$menu, _item$menu2;
// 是否没有权限查看
// 这样就不会显示,是一个兼容性的方式
if (item.unaccessible) {
// eslint-disable-next-line no-param-reassign
delete item.name;
}
if ((item === null || item === void 0 ? void 0 : (_item$menu = item.menu) === null || _item$menu === void 0 ? void 0 : _item$menu.name) || (item === null || item === void 0 ? void 0 : item.flatMenu) || (item === null || item === void 0 ? void 0 : (_item$menu2 = item.menu) === null || _item$menu2 === void 0 ? void 0 : _item$menu2.flatMenu)) {
return true;
} // 显示指定在 menu 中隐藏该项
// layout 插件的功能,其实不应该存在的
if (item.menu === false) {
return false;
}
return true;
}).map(function () {
var item = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
path: '/'
};
var path = mergePath(item.path, parent ? parent.path : '/');
var name = item.name;
var locale = getItemLocaleName(item, parentName || 'menu'); // if enableMenuLocale use item.name,
// close menu international
var localeName = locale !== false && menuLocale !== false && formatMessage && locale ? formatMessage({
id: locale,
defaultMessage: name
}) : name;
var _parent$pro_layout_pa = parent.pro_layout_parentKeys,
pro_layout_parentKeys = _parent$pro_layout_pa === void 0 ? [] : _parent$pro_layout_pa,
children = parent.children,
icon = parent.icon,
flatMenu = parent.flatMenu,
indexRoute = parent.indexRoute,
restParent = _objectWithoutProperties(parent, ["pro_layout_parentKeys", "children", "icon", "flatMenu", "indexRoute"]);
var finallyItem = _objectSpread(_objectSpread(_objectSpread({}, restParent), {}, {
menu: undefined
}, item), {}, {
path: path,
locale: locale,
key: item.key || getKeyByPath(_objectSpread(_objectSpread({}, item), {}, {
path: path
})),
routes: null,
pro_layout_parentKeys: Array.from(new Set([].concat(_toConsumableArray(item.parentKeys || []), _toConsumableArray(pro_layout_parentKeys), ["/".concat(parent.key || '').replace(/\/\//g, '/').replace(/\/\//g, '/')]))).filter(function (key) {
return key && key !== '/';
})
});
if (localeName) {
finallyItem.name = localeName;
} else {
delete finallyItem.name;
}
if (finallyItem.menu === undefined) {
delete finallyItem.menu;
}
if (item.routes || item.children) {
var formatterChildren = formatter(_objectSpread(_objectSpread({}, props), {}, {
data: item.routes || item.children,
parentName: locale || ''
}), finallyItem); // Reduce memory usage
finallyItem.children = formatterChildren && formatterChildren.length > 0 ? formatterChildren : undefined;
if (!finallyItem.children) {
delete finallyItem.children;
}
}
return bigfishCompatibleConversions(finallyItem, props);
}).flat(1);
}
var memoizeOneFormatter = memoizeOne(formatter, isEqual);
/**
* 删除 hideInMenu 和 item.name 不存在的
*/
var defaultFilterMenuData = function defaultFilterMenuData() {
var menuData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
return menuData.filter(function (item) {
return item && (item.name || item.children) && !item.hideInMenu && !item.redirect;
}).map(function (item) {
if (item.children && Array.isArray(item.children) && !item.hideChildrenInMenu && item.children.some(function (child) {
return child && !!child.name;
})) {
var children = defaultFilterMenuData(item.children);
if (children.length) return _objectSpread(_objectSpread({}, item), {}, {
children: children
});
}
return _objectSpread(_objectSpread({}, item), {}, {
children: undefined
});
}).filter(function (item) {
return item;
});
};
/**
* support pathToRegexp get string
*/
var RoutesMap = /*#__PURE__*/function (_Map) {
_inherits(RoutesMap, _Map);
var _super = _createSuper(RoutesMap);
function RoutesMap() {
_classCallCheck(this, RoutesMap);
return _super.apply(this, arguments);
}
_createClass(RoutesMap, [{
key: "get",
value: function get(pathname) {
var routeValue;
try {
// eslint-disable-next-line no-restricted-syntax
var _iterator = _createForOfIteratorHelper(this.entries()),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var _step$value = _slicedToArray(_step.value, 2),
key = _step$value[0],
value = _step$value[1];
var path = stripQueryStringAndHashFromPath(key);
if (!isUrl(key) && pathToRegexp(path, []).test(pathname)) {
routeValue = value;
break;
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
} catch (error) {
routeValue = undefined;
}
return routeValue;
}
}]);
return RoutesMap;
}( /*#__PURE__*/_wrapNativeSuper(Map));
/**
* 获取面包屑映射
* @param MenuDataItem[] menuData 菜单配置
*/
var getBreadcrumbNameMap = function getBreadcrumbNameMap(menuData) {
// Map is used to ensure the order of keys
var routerMap = new RoutesMap();
var flattenMenuData = function flattenMenuData(data, parent) {
data.forEach(function (menuItem) {
if (menuItem && menuItem.children) {
flattenMenuData(menuItem.children, menuItem);
} // Reduce memory usage
var path = mergePath(menuItem.path, parent ? parent.path : '/');
routerMap.set(stripQueryStringAndHashFromPath(path), menuItem);
});
};
flattenMenuData(menuData);
return routerMap;
};
var memoizeOneGetBreadcrumbNameMap = memoizeOne(getBreadcrumbNameMap, isEqual);
var clearChildren = function clearChildren() {
var menuData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
return menuData.map(function (item) {
if (item.children && Array.isArray(item.children) && item.children.length > 0) {
var children = clearChildren(item.children);
if (children.length) return _objectSpread(_objectSpread({}, item), {}, {
children: children
});
}
var finallyItem = _objectSpread({}, item);
delete finallyItem.children;
return finallyItem;
}).filter(function (item) {
return item;
});
};
/**
* @param routes 路由配置
* @param locale 是否使用国际化
* @param formatMessage 国际化的程序
* @param ignoreFilter 是否筛选掉不展示的 menuItem 项plugin-layout需要所有项目来计算布局样式
* @returns { breadcrumb, menuData}
*/
var transformRoute = function transformRoute(routes, locale, formatMessage, ignoreFilter) {
var originalMenuData = memoizeOneFormatter({
data: routes,
formatMessage: formatMessage,
locale: locale
});
var menuData = ignoreFilter ? clearChildren(originalMenuData) : defaultFilterMenuData(originalMenuData); // Map type used for internal logic
var breadcrumb = memoizeOneGetBreadcrumbNameMap(originalMenuData);
return {
breadcrumb: breadcrumb,
menuData: menuData
};
};
export default transformRoute;