UNPKG

react-cm-ui

Version:
228 lines (186 loc) 7.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPathNameBreadcrumbs = exports.routesToArray = exports.transformPathsWithOptionalParams = exports.parsePath = void 0; var _lodash = require("lodash"); 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; } var extractRoute = function extractRoute(route, prefix) { var shouldAddRoute = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; var path = route.props && route.props.path ? route.props.path : route.path; var paths = []; if (!path) { if (Array.isArray(route)) { route.forEach(function (r) { paths = paths.concat(extractRoute(r, prefix, shouldAddRoute)); }); return paths; } // eslint-disable-next-line no-use-before-define return extractChildRoutes(route, prefix); } var currentPath = "".concat(prefix || '').concat(path).replace(/\/+/g, '/'); var pathString = "".concat(currentPath.startsWith('/') ? '' : '/').concat(currentPath); var title = (0, _lodash.get)(route, 'title'); var couldItBeHome = path === '/' && !title; if (couldItBeHome) { title = 'Home'; } if (!route.childRoutes) { paths.push({ path: pathString, title: title || pathString.split('/').filter(function (subPath) { return subPath !== ''; }).reverse()[0] }); } // eslint-disable-next-line no-use-before-define paths = paths.concat(extractChildRoutes(route, "".concat(currentPath, "/"))); return paths; }; var extractChildRoutes = function extractChildRoutes(route, prefix) { var paths = []; var shouldAddIndexRoute = route.indexRoute && route.path; if (shouldAddIndexRoute) { var indexRouteString = "".concat(prefix).replace('//', '/'); var title = (0, _lodash.get)(route, 'indexRoute.title'); var couldItBeHome = indexRouteString === '/' && !title; if (couldItBeHome) { title = 'Home'; } paths.push({ path: indexRouteString, title: title || indexRouteString.split('/').filter(function (subPath) { return subPath !== ''; }).reverse()[0] }); } var childRoutes = route.props && route.props.children ? route.props.children : route.childRoutes; if (childRoutes) { if (Array.isArray(childRoutes)) { childRoutes.forEach(function (r) { paths = paths.concat(extractRoute(r, prefix, shouldAddIndexRoute)); }); } else { paths = paths.concat(extractRoute(childRoutes, prefix, shouldAddIndexRoute)); } } return paths; }; var parsePath = function parsePath(path, params) { var inversed = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; var paramsKeys = Object.keys(params); var formattedPath = path; for (var i = 0; i < paramsKeys.length; i += 1) { var currentKey = paramsKeys[i]; var currentValue = params[currentKey]; formattedPath = inversed ? formattedPath.replace(currentValue, ":".concat(currentKey)) : formattedPath.replace(":".concat(currentKey), currentValue); } return formattedPath || ''; }; exports.parsePath = parsePath; var transformPathsWithOptionalParams = function transformPathsWithOptionalParams(paths) { var optionalPaths = []; var replacements = { '(': '', ')': '' }; for (var i = 0; i < paths.length; i += 1) { var auxPath = paths[i]; if (auxPath.path.includes('(')) { var subPaths = auxPath.path.split('('); var pathPrefix = subPaths[0]; optionalPaths.push({ path: pathPrefix, title: auxPath.title }); for (var j = 1; j < subPaths.length; j += 1) { var pathSufix = subPaths[j]; var optionalPath = void 0; var isNestedOptionalPath = pathSufix.includes('))'); if (isNestedOptionalPath) { var previousSufix = subPaths[j - 1]; pathPrefix = "".concat(pathPrefix).concat(previousSufix); optionalPath = "".concat(pathPrefix).concat(pathSufix); } else { optionalPath = "".concat(pathPrefix).concat(pathSufix); } optionalPaths.push({ path: optionalPath, title: auxPath.title }); } } else { optionalPaths.push(auxPath); } } optionalPaths = optionalPaths.map(function (auxPath) { return _objectSpread(_objectSpread({}, auxPath), {}, { path: auxPath.path.replace(/[()]/g, function (_char) { return replacements[_char]; }) }); }).map(function (auxPath) { if (auxPath.path.length > 2) { return _objectSpread(_objectSpread({}, auxPath), {}, { path: auxPath.path.replace(/\/$/, '') }); } return auxPath; }); return optionalPaths; }; exports.transformPathsWithOptionalParams = transformPathsWithOptionalParams; var routesToArray = function routesToArray(route) { var paths = []; if (Array.isArray(route)) { route.forEach(function (r) { paths = paths.concat(extractRoute(r)); }); } else { paths = paths.concat(extractRoute(route)); } paths = transformPathsWithOptionalParams(paths); paths = (0, _lodash.uniqBy)(paths, 'path'); return paths; }; exports.routesToArray = routesToArray; var getPathNameBreadcrumbs = function getPathNameBreadcrumbs(pathName, pathParams, routerPushFunction, existentRoutes, routesNameMapper) { var originalPath = parsePath(pathName, pathParams); var pathNames = pathName.split('/').filter(function (x) { return x; }); var originalPathNames = originalPath.split('/').filter(function (x) { return x; }); var pathBreadcrumbs = pathNames.reduce(function (currentBreadcrumbs, path, index) { var reverseParsedPath = "/".concat(originalPathNames.slice(0, index + 1).join('/')); var existentPath = (0, _lodash.find)(existentRoutes, { path: reverseParsedPath }); if (existentPath) { var to = "/".concat(pathNames.slice(0, index + 1).join('/')); var customRouteNameMapper = (0, _lodash.find)(routesNameMapper, { route: existentPath.path }); currentBreadcrumbs.push({ to: to, originalPath: reverseParsedPath, title: (0, _lodash.get)(customRouteNameMapper, 'title') || existentPath.title, onBreadcrumbClick: function onBreadcrumbClick() { return routerPushFunction(to); } }); } return currentBreadcrumbs; }, [{ to: '/', originalPath: '/', title: 'Home', onBreadcrumbClick: function onBreadcrumbClick() { return routerPushFunction('/'); } }]); return pathBreadcrumbs; }; exports.getPathNameBreadcrumbs = getPathNameBreadcrumbs;