function normalizePath (path) {
|
if (!path) {
|
return ''
|
}
|
let normalized = path
|
if (normalized.endsWith('/')) {
|
normalized = normalized.substring(0, normalized.length - 1)
|
}
|
return normalized
|
}
|
|
export function findMenuByUrl (url, menus) {
|
const target = normalizePath(url)
|
for (const menu of menus || []) {
|
if (menu.url && normalizePath(menu.url) === target) {
|
return menu
|
}
|
if (menu.children && menu.children.length > 0) {
|
const child = findMenuByUrl(target, menu.children)
|
if (child) {
|
return child
|
}
|
}
|
}
|
return null
|
}
|
|
export function findMenuPathByUrl (url, menus, ancestors = []) {
|
const target = normalizePath(url)
|
for (const menu of menus || []) {
|
const chain = [...ancestors, menu]
|
if (menu.url && normalizePath(menu.url) === target) {
|
return chain
|
}
|
if (menu.children && menu.children.length > 0) {
|
const childChain = findMenuPathByUrl(target, menu.children, chain)
|
if (childChain) {
|
return childChain
|
}
|
}
|
}
|
return null
|
}
|
|
export function getOpenMenuIndexes (url, menus) {
|
const chain = findMenuPathByUrl(url, menus)
|
if (!chain || chain.length <= 1) {
|
return []
|
}
|
return chain.slice(0, -1).map(menu => menu.index)
|
}
|
|
export function navigateByMenu (router, store, path, menus) {
|
const menuConfig = findMenuByUrl(path, menus)
|
if (menuConfig) {
|
if (menuConfig.params) {
|
router.push({
|
path: menuConfig.url,
|
query: { index: menuConfig.index, param: menuConfig.params }
|
})
|
} else {
|
router.push(menuConfig.url)
|
}
|
store.commit('pushtags', menuConfig)
|
return
|
}
|
router.push({ path })
|
}
|