Files
2026-01-16 02:20:32 +00:00

394 lines
20 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
var _excluded = ["pro_layout_parentKeys", "children", "icon", "flatMenu", "indexRoute", "routes"];
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, 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 o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _wrapNativeSuper(t) { var r = "function" == typeof Map ? new Map() : void 0; return _wrapNativeSuper = function _wrapNativeSuper(t) { if (null === t || !_isNativeFunction(t)) return t; if ("function" != typeof t) throw new TypeError("Super expression must either be null or a function"); if (void 0 !== r) { if (r.has(t)) return r.get(t); r.set(t, Wrapper); } function Wrapper() { return _construct(t, arguments, _getPrototypeOf(this).constructor); } return Wrapper.prototype = Object.create(t.prototype, { constructor: { value: Wrapper, enumerable: !1, writable: !0, configurable: !0 } }), _setPrototypeOf(Wrapper, t); }, _wrapNativeSuper(t); }
function _construct(t, e, r) { if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments); var o = [null]; o.push.apply(o, e); var p = new (t.bind.apply(t, o))(); return r && _setPrototypeOf(p, r.prototype), p; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _isNativeFunction(t) { try { return -1 !== Function.toString.call(t).indexOf("[native code]"); } catch (n) { return "function" == typeof t; } }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
//@ts-ignore
import { pathToRegexp } from '../path-to-regexp';
import sha265 from '../sha265';
export var childrenPropsName = 'routes';
export function stripQueryStringAndHashFromPath(url) {
return url.split('?')[0].split('#')[0];
}
export var isUrl = function isUrl(path) {
if (!path.startsWith('http')) {
return false;
}
try {
var url = new URL(path);
return !!url;
} catch (error) {
return false;
}
};
export var getKeyByPath = function getKeyByPath(item) {
var path = item.path;
if (!path || path === '/') {
// 如果还是没有用对象的hash 生成一个
try {
return "/".concat(sha265(JSON.stringify(item)));
} 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.endsWith('/*')) {
return path.replace('/*', '/');
}
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;
var routerChildren = 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 childrenList = indexRoute &&
// 如果只有 redirect,不用处理的
Object.keys(indexRoute).join(',') !== 'redirect' ? [_objectSpread({
path: path,
menu: menu
}, indexRoute)].concat(routerChildren || []) : routerChildren;
// 拼接返回的 menu 数据
var result = _objectSpread({}, route);
if (name) {
result.name = name;
}
if (icon) {
result.icon = icon;
}
if (childrenList && childrenList.length) {
/** 在菜单中隐藏子项 */
if (hideChildren) {
delete result.children;
return result;
}
// 需要重新进行一次
var finalChildren = formatter(_objectSpread(_objectSpread({}, props), {}, {
data: childrenList
}), route);
/** 在菜单中只隐藏此项,子项往上提,仍旧展示 */
if (flatMenu) {
return finalChildren;
}
delete result[childrenPropsName];
}
return result;
};
var notNullArray = function notNullArray(value) {
return Array.isArray(value) && value.length > 0;
};
/**
*
* @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 (notNullArray(item.children)) return true;
if (item.path) return true;
if (item.originPath) return true;
if (item.layout) return true;
// 重定向
if (item.redirect) return false;
if (item.unaccessible) return false;
return false;
}).filter(function (item) {
var _item$menu, _item$menu2;
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 (finallyItem) {
var item = _objectSpread(_objectSpread({}, finallyItem), {}, {
path: finallyItem.path || finallyItem.originPath
});
if (!item.children && item[childrenPropsName]) {
item.children = item[childrenPropsName];
delete item[childrenPropsName];
}
// 是否没有权限查看
// 这样就不会显示,是一个兼容性的方式
if (item.unaccessible) {
// eslint-disable-next-line no-param-reassign
delete item.name;
}
if (item.path === '*') {
item.path = '.';
}
if (item.path === '/*') {
item.path = '.';
}
if (!item.path && item.originPath) {
item.path = item.originPath;
}
return item;
}).map(function () {
var item = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
path: '/'
};
var routerChildren = item.children || item[childrenPropsName] || [];
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,
routes = parent.routes,
restParent = _objectWithoutProperties(parent, _excluded);
var item_pro_layout_parentKeys = new Set([].concat(_toConsumableArray(pro_layout_parentKeys), _toConsumableArray(item.parentKeys || [])));
if (parent.key) {
item_pro_layout_parentKeys.add(parent.key);
}
var finallyItem = _objectSpread(_objectSpread(_objectSpread({}, restParent), {}, {
menu: undefined
}, item), {}, {
path: path,
locale: locale,
key: item.key || getKeyByPath(_objectSpread(_objectSpread({}, item), {}, {
path: path
})),
pro_layout_parentKeys: Array.from(item_pro_layout_parentKeys).filter(function (key) {
return key && key !== '/';
})
});
if (localeName) {
finallyItem.name = localeName;
} else {
delete finallyItem.name;
}
if (finallyItem.menu === undefined) {
delete finallyItem.menu;
}
if (notNullArray(routerChildren)) {
var formatterChildren = formatter(_objectSpread(_objectSpread({}, props), {}, {
data: routerChildren,
parentName: locale || ''
}), finallyItem);
if (notNullArray(formatterChildren)) {
finallyItem.children = formatterChildren;
}
}
return bigfishCompatibleConversions(finallyItem, props);
}).flat(1);
}
/**
* 删除 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 || notNullArray(item.children)) && !item.hideInMenu && !item.redirect;
}).map(function (item) {
var newItem = _objectSpread({}, item);
var routerChildren = newItem.children || item[childrenPropsName] || [];
delete newItem[childrenPropsName];
if (notNullArray(routerChildren) && !newItem.hideChildrenInMenu && routerChildren.some(function (child) {
return child && !!child.name;
})) {
var newChildren = _defaultFilterMenuData(routerChildren);
if (newChildren.length) return _objectSpread(_objectSpread({}, newItem), {}, {
children: newChildren
});
}
return _objectSpread({}, item);
}).filter(function (item) {
return item;
});
};
/**
* support pathToRegexp get string
*/
var RouteListMap = /*#__PURE__*/function (_Map) {
function RouteListMap() {
_classCallCheck(this, RouteListMap);
return _callSuper(this, RouteListMap, arguments);
}
_inherits(RouteListMap, _Map);
return _createClass(RouteListMap, [{
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;
}
}]);
}(/*#__PURE__*/_wrapNativeSuper(Map));
/**
* 获取面包屑映射
* @param MenuDataItem[] menuData 菜单配置
*/
var getBreadcrumbNameMap = function getBreadcrumbNameMap(menuData) {
// Map is used to ensure the order of keys
var routerMap = new RouteListMap();
var _flattenMenuData = function flattenMenuData(data, parent) {
data.forEach(function (menuItem) {
var routerChildren = menuItem.children || menuItem[childrenPropsName] || [];
if (notNullArray(routerChildren)) {
_flattenMenuData(routerChildren, menuItem);
}
// Reduce memory usage
var path = mergePath(menuItem.path, parent ? parent.path : '/');
routerMap.set(stripQueryStringAndHashFromPath(path), menuItem);
});
};
_flattenMenuData(menuData);
return routerMap;
};
var _clearChildren = function clearChildren() {
var menuData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
return menuData.map(function (item) {
var routerChildren = item.children || item[childrenPropsName];
if (notNullArray(routerChildren)) {
var newChildren = _clearChildren(routerChildren);
if (newChildren.length) return _objectSpread({}, item);
}
var finallyItem = _objectSpread({}, item);
delete finallyItem[childrenPropsName];
delete finallyItem.children;
return finallyItem;
}).filter(function (item) {
return item;
});
};
/**
* @param routeList 路由配置
* @param locale 是否使用国际化
* @param formatMessage 国际化的程序
* @param ignoreFilter 是否筛选掉不展示的 menuItem 项plugin-layout需要所有项目来计算布局样式
* @returns { breadcrumb, menuData}
*/
var transformRoute = function transformRoute(routeList, locale, formatMessage, ignoreFilter) {
var originalMenuData = formatter({
data: _toConsumableArray(routeList).map(function (item) {
return _objectSpread({}, item);
}),
formatMessage: formatMessage,
locale: locale
});
var menuData = ignoreFilter ? _clearChildren(originalMenuData) : _defaultFilterMenuData(originalMenuData);
// Map type used for internal logic
var breadcrumb = getBreadcrumbNameMap(originalMenuData);
return {
breadcrumb: breadcrumb,
menuData: menuData
};
};
export default transformRoute;