function normalizePath (path) { if (!path) { return '' } return path.endsWith('/') ? path.substring(0, path.length - 1) : path } function matchMenuRoute (menu, path, index) { if (!menu || !menu.url) { return false } if (normalizePath(menu.url) !== path) { return false } if (index != null && index !== '' && menu.index !== index) { return false } return true } function findMenuInTree (menus, path, index, parents = []) { if (!menus || menus.length === 0) { return null } for (const menu of menus) { if (matchMenuRoute(menu, path, index)) { return { menu, parents } } if (menu.children && menu.children.length > 0) { const found = findMenuInTree(menu.children, path, index, parents.concat(menu)) if (found) { return found } } } return null } export function findMenuByRoute (topMenuList, path, index) { const normalizedPath = normalizePath(path) if (!normalizedPath || !topMenuList || topMenuList.length === 0) { return null } for (let topIndex = 0; topIndex < topMenuList.length; topIndex++) { const topMenu = topMenuList[topIndex] if (topMenu.linkType !== 0) { continue } let found = null if (index != null && index !== '') { found = findMenuInTree(topMenu.children || [], normalizedPath, index) } if (!found) { found = findMenuInTree(topMenu.children || [], normalizedPath, null) } if (found) { return { topMenu, topIndex, menu: found.menu, parents: found.parents } } } return null }