UNPKG

umi-plugin-abp-feature

Version:

feature

72 lines (53 loc) 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.traverseModifyRoutes = traverseModifyRoutes; function _react() { const data = _interopRequireDefault(require("react")); _react = function _react() { return data; }; return data; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function traverseModifyRoutes(routes, access) { const resultRoutes = [].concat(routes); const notHandledRoutes = []; notHandledRoutes.push(...resultRoutes); for (let i = 0; i < notHandledRoutes.length; i++) { const currentRoute = notHandledRoutes[i]; let currentRouteAccessible = typeof currentRoute.unaccessible === 'boolean' ? !currentRoute.unaccessible : true; if (currentRoute && currentRoute.access) { if (typeof currentRoute.access !== 'string') { throw new Error('[plugin-access]: "access" field set in "' + currentRoute.path + '" route should be a string.'); } const accessProp = access[currentRoute.access]; if (typeof accessProp === 'function') { currentRouteAccessible = accessProp(currentRoute); } else if (typeof accessProp === 'boolean') { currentRouteAccessible = accessProp; } currentRoute.unaccessible = !currentRouteAccessible; } if (currentRoute.routes || currentRoute.childRoutes) { const childRoutes = currentRoute.routes || currentRoute.childRoutes; if (!Array.isArray(childRoutes)) { continue; } childRoutes.forEach(childRoute => { childRoute.unaccessible = !currentRouteAccessible; }); // Default inherit from parent route notHandledRoutes.push(...childRoutes); } } // Make parent route unaccessible if child routes exist and all of child routes are unaccessible for (let i = 0; i < notHandledRoutes.length; i++) { const currentRoute = notHandledRoutes[i]; const childRoutes = currentRoute.routes || currentRoute.childRoutes; const isAllChildRoutesUnaccessible = Array.isArray(childRoutes) && childRoutes.every(route => route.unaccessible); if (!currentRoute.unaccessible && isAllChildRoutesUnaccessible) { currentRoute.unaccessible = true; } } return resultRoutes; }