doum
7 天以前 e46bfa3ff94a8a1b4daf37c7fcb79c2fab22a72c
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
67
68
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 })
}