generateMenu.uts 4.6 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1
import { pages, groups } from '@/pages.json'
2
import { state } from '@/store/index.uts'
3 4 5 6 7
type Group = {
  id : string,
  name : string,
  children : (Group | null)[] | null
}
8 9

type PageGroup = {
10 11
  id : string,
  name : string,
12
  index : number
13 14 15
}

type Page = {
16 17
  path : string,
  style : UTSJSONObject,
18
  group ?: string | null,
19 20 21
}

export type MenuItem = {
22 23 24
  id : string,
  name : string,
  index : number,
25 26 27 28
  path : string,
  style : UTSJSONObject,
  group ?: string | null,
  items : (MenuItem | null)[]
29 30
}

31 32 33
export function generateMenu(tabBarType : string) : (MenuItem | null)[] {
  let res : (MenuItem | null)[] = []
  const tabBarPages = JSON.parse<Page[]>(JSON.stringify(pages))!.filter((page : Page) : boolean => page.path.startsWith(tabBarType) && hasPageGroup(page.group))
34
  const groupTree = JSON.parse<(Group | null)[]>(JSON.stringify(groups))
35
  tabBarPages.forEach(page => {
36 37
    let menuItemArr : (MenuItem | null)[] = res
    let currentMenu : MenuItem | null = null
38 39 40 41
    const groupIndexList = page.group!.split(',').map((index : string) : number => parseInt(index))
    let currentGroups : (Group | null)[] | null = groupTree
    const pageGroups : PageGroup[] = []
    groupIndexList.forEach((groupIndex, index) => {
42
      // 跳过第一层 component API CSS
43 44 45 46 47 48
      if (index > 0) {
        pageGroups.push({ id: currentGroups![groupIndex]!.id, name: currentGroups![groupIndex]!.name, index: groupIndex } as PageGroup)
      }
      currentGroups = currentGroups![groupIndex]!.children
    })
    const lastGroup = pageGroups[pageGroups.length - 1]
49
    let hasPageGroup = false
50
    if (page.path.endsWith(camelToDash(lastGroup.name)) && pageGroups.length > 1) {
51
      hasPageGroup = true
52
      pageGroups.pop()
53
    }
54 55
    const groupLength = pageGroups.length
    pageGroups.forEach((group, groupIndex) => {
56 57
      const { index, id, name } = group

58
      const validIndex = index
59
      fillMenuArrayWithNull(menuItemArr, validIndex)
60 61 62 63 64 65

      if (menuItemArr[validIndex] == null) {
        menuItemArr[validIndex] = {
          id: id.split('.').slice(-1)[0],
          name,
          index: validIndex,
66 67 68 69
          path: '',
          style: {},
          group: '',
          items: [] as (MenuItem | null)[],
70 71 72 73 74 75
        } as MenuItem
      }


      currentMenu = menuItemArr[validIndex]
      if (groupIndex < groupLength - 1) {
76
        menuItemArr = menuItemArr[validIndex]!.items as (MenuItem | null)[]
77 78
      }
    })
79 80 81 82 83 84 85 86 87 88

    const pageMenuItem : MenuItem = {
      id: page.path,
      name: page.style["navigationBarTitleText"] as string,
      index: lastGroup.index,
      path: page.path,
      style: page.style,
      group: page.group,
      items: [] as (MenuItem | null)[],
    }
89
    if (hasPageGroup) {
90
      const pageIndex = lastGroup.index
91 92 93
      fillMenuArrayWithNull(currentMenu!.items, pageIndex)
      if (currentMenu!.items[pageIndex] == null) {
        currentMenu!.items[pageIndex] = pageMenuItem
94
      } else {
95
        currentMenu!.items.push(pageMenuItem)
96
      }
97
    } else {
98
      currentMenu!.items.push(pageMenuItem)
99
    }
100
  })
101

102 103 104
  return removeNullItem(res)
}

雪洛's avatar
雪洛 已提交
105
function hasPageGroup(value ?: string | null) : boolean {
106 107 108 109 110 111 112 113
  // #ifdef APP-ANDROID
  return value !== null
  // #endif
  // #ifndef APP-ANDROID
  return !!value
  // #endif
}

114
function isNumber(value : number | null) : boolean {
115 116 117 118 119 120 121 122
  // #ifdef APP-ANDROID
  return value !== null
  // #endif
  // #ifndef APP-ANDROID
  return typeof value === 'number'
  // #endif
}

123
function camelToDash(camelStr : string) : string {
124 125 126
  return camelStr.replace(/([A-Z])/g, '-$1').toLowerCase()
}

127
function fillMenuArrayWithNull(arr : (MenuItem | null)[], index : number) : void {
128
  const len = arr.length
129
  for (let i = 0; i <= index - (len - 1); i++) {
130 131 132 133
    arr.push(null)
  }
}

134 135
function removeNullItem(arr : (MenuItem | null)[]) : (MenuItem | null)[] {
  const res = arr.filter((item : MenuItem | null) : boolean => item !== null)
136 137 138
  res.forEach(menuItem => {
    addSetTabBarPage(menuItem!)
    // #ifdef APP-ANDROID
139
    menuItem.items = removeNullItem(menuItem.items)
140 141
    // #endif
    // #ifndef APP-ANDROID
142
    menuItem!.items = removeNullItem(menuItem!.items)
143 144 145 146 147
    // #endif
  })
  return res
}

148
function addSetTabBarPage(menuItem : MenuItem) {
149
  const { id, name } = menuItem
150
  if (id == 'page' && name == '页面和路由' && !state.noMatchLeftWindow) {
151 152 153 154
    menuItem.items.push({
      id: 'set-tab-bar',
      name: '设置 TabBar',
      index: 0,
155 156 157
      path: 'set-tab-bar',
      style: {
        navigationBarTitleText: '设置 TabBar'
158 159 160 161
      },
      group: null,
      items: []
    } as MenuItem)
162
  }
163
}