doum
2026-06-11 d9c657aa78cf0ebe31933a87e63ca92edd8a8da3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
}