generateMenu.uts 4.5 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1
import { pages, groups } from '@/pages.json'
2

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
  path : string,
  style : UTSJSONObject,
  group ?: string | null,
DCloud-WZF's avatar
DCloud-WZF 已提交
28
  items : MenuItem[]
29 30
}

DCloud-WZF's avatar
DCloud-WZF 已提交
31 32
export function generateMenu(tabBarType : string) : MenuItem[] {
  let res : MenuItem[] = []
33
  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 => {
DCloud-WZF's avatar
DCloud-WZF 已提交
36
    let menuItemArr : MenuItem[] = res
37
    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
DCloud-WZF's avatar
DCloud-WZF 已提交
59
      fillMenuArrayWithEmptyMenuItem(menuItemArr, validIndex)
60

DCloud-WZF's avatar
DCloud-WZF 已提交
61
      if (menuItemArr[validIndex].name == 'empty') {
62 63 64 65
        menuItemArr[validIndex] = {
          id: id.split('.').slice(-1)[0],
          name,
          index: validIndex,
66 67 68
          path: '',
          style: {},
          group: '',
DCloud-WZF's avatar
DCloud-WZF 已提交
69
          items: [] as MenuItem[],
70 71 72 73 74 75
        } as MenuItem
      }


      currentMenu = menuItemArr[validIndex]
      if (groupIndex < groupLength - 1) {
DCloud-WZF's avatar
DCloud-WZF 已提交
76
        menuItemArr = menuItemArr[validIndex].items
77 78
      }
    })
79 80 81 82 83 84 85 86

    const pageMenuItem : MenuItem = {
      id: page.path,
      name: page.style["navigationBarTitleText"] as string,
      index: lastGroup.index,
      path: page.path,
      style: page.style,
      group: page.group,
DCloud-WZF's avatar
DCloud-WZF 已提交
87
      items: [] as MenuItem[],
88
    }
89
    if (hasPageGroup) {
90
      const pageIndex = lastGroup.index
DCloud-WZF's avatar
DCloud-WZF 已提交
91 92
      fillMenuArrayWithEmptyMenuItem(currentMenu!.items, pageIndex)
      if (currentMenu!.items[pageIndex].name == 'empty') {
93
        currentMenu!.items[pageIndex] = pageMenuItem
94
      } else {
95
        currentMenu!.items.push(pageMenuItem)
96
      }
97
    } else {
98
      currentMenu!.items.push(pageMenuItem)
99
    }
100
  })
101

DCloud-WZF's avatar
DCloud-WZF 已提交
102
  return removeEmptyItem(res)
103 104
}

雪洛'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 camelToDash(camelStr : string) : string {
115 116 117
  return camelStr.replace(/([A-Z])/g, '-$1').toLowerCase()
}

DCloud-WZF's avatar
DCloud-WZF 已提交
118
function fillMenuArrayWithEmptyMenuItem(arr : MenuItem[], index : number) : void {
119
  const len = arr.length
120
  for (let i = 0; i <= index - (len - 1); i++) {
DCloud-WZF's avatar
DCloud-WZF 已提交
121 122 123 124 125 126 127 128
    arr.push({
      id: '',
      name: 'empty',
      index: i,
      path: '',
      style: {},
      group: '',
      items: [] as MenuItem[],
129
    } as MenuItem)
130 131 132
  }
}

DCloud-WZF's avatar
DCloud-WZF 已提交
133 134
function removeEmptyItem(arr : MenuItem[]) : MenuItem[] {
  const res = arr.filter((item : MenuItem) : boolean => item.name !== 'empty')
135
  res.forEach(menuItem => {
DCloud-WZF's avatar
DCloud-WZF 已提交
136
    addSetTabBarPage(menuItem)
137
    // #ifdef APP-ANDROID
DCloud-WZF's avatar
DCloud-WZF 已提交
138
    menuItem.items = removeEmptyItem(menuItem.items)
139 140
    // #endif
    // #ifndef APP-ANDROID
DCloud-WZF's avatar
DCloud-WZF 已提交
141
    menuItem!.items = removeEmptyItem(menuItem!.items)
142 143 144 145 146
    // #endif
  })
  return res
}

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