generateMenu.uts 4.4 KB
Newer Older
1 2 3 4 5 6 7
import { pages, groups } from '@/target-pages.json'

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 25 26
  id : string,
  name : string,
  index : number,
  children : (MenuItem | null)[],
  pages : (Page | null)[]
27 28
}

29 30 31
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))
32
  const groupTree = JSON.parse<(Group | null)[]>(JSON.stringify(groups))
33
  tabBarPages.forEach(page => {
34 35
    let menuItemArr : (MenuItem | null)[] = res
    let currentMenu : MenuItem | null = null
36 37 38 39 40 41 42 43 44 45
    const groupIndexList = page.group!.split(',').map((index : string) : number => parseInt(index))
    let currentGroups : (Group | null)[] | null = groupTree
    const pageGroups : PageGroup[] = []
    groupIndexList.forEach((groupIndex, index) => {
      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]
46
    let hasPageGroup = false
47
    if (page.path.endsWith(camelToDash(lastGroup.name)) && pageGroups.length > 1) {
48
      hasPageGroup = true
49
      pageGroups.pop()
50
    }
51 52
    const groupLength = pageGroups.length
    pageGroups.forEach((group, groupIndex) => {
53 54
      const { index, id, name } = group

55
      const validIndex = index
56
      fillMenuArrayWithNull(menuItemArr, validIndex)
57 58 59 60 61 62 63

      if (menuItemArr[validIndex] == null) {
        menuItemArr[validIndex] = {
          id: id.split('.').slice(-1)[0],
          name,
          index: validIndex,
          children: [] as (MenuItem | null)[],
64
          pages: [] as (Page | null)[]
65 66 67 68 69 70 71 72 73
        } as MenuItem
      }


      currentMenu = menuItemArr[validIndex]
      if (groupIndex < groupLength - 1) {
        menuItemArr = menuItemArr[validIndex]!.children as (MenuItem | null)[]
      }
    })
74
    if (hasPageGroup) {
75
      const pageIndex = lastGroup.index
76 77 78
      fillPageArrayWithNull(currentMenu!.pages, pageIndex)
      if (currentMenu!.pages[pageIndex] == null) {
        currentMenu!.pages[pageIndex] = page
79 80
      } else {
        currentMenu!.pages.push(page)
81
      }
82
    } else {
83 84
      currentMenu!.pages.push(page)
    }
85
  })
86

87 88 89
  return removeNullItem(res)
}

90
function hasPageGroup(value : string | null) : boolean {
91 92 93 94 95 96 97 98
  // #ifdef APP-ANDROID
  return value !== null
  // #endif
  // #ifndef APP-ANDROID
  return !!value
  // #endif
}

99
function isNumber(value : number | null) : boolean {
100 101 102 103 104 105 106 107
  // #ifdef APP-ANDROID
  return value !== null
  // #endif
  // #ifndef APP-ANDROID
  return typeof value === 'number'
  // #endif
}

108
function camelToDash(camelStr : string) : string {
109 110 111
  return camelStr.replace(/([A-Z])/g, '-$1').toLowerCase()
}

112
function fillMenuArrayWithNull(arr : (MenuItem | null)[], index : number) : void {
113
  const len = arr.length
114
  for (let i = 0; i <= index - (len - 1); i++) {
115 116 117 118
    arr.push(null)
  }
}

119
function fillPageArrayWithNull(arr : (Page | null)[], index : number) : void {
120
  const len = arr.length
121
  for (let i = 0; i <= index - (len - 1); i++) {
122 123 124 125
    arr.push(null)
  }
}

126 127
function removeNullItem(arr : (MenuItem | null)[]) : (MenuItem | null)[] {
  const res = arr.filter((item : MenuItem | null) : boolean => item !== null)
128 129 130
  res.forEach(menuItem => {
    addSetTabBarPage(menuItem!)
    // #ifdef APP-ANDROID
131
    menuItem.children = removeNullItem(menuItem.children)
132
    menuItem.pages = menuItem.pages.filter((item : Page | null) : boolean => item !== null)
133 134 135
    // #endif
    // #ifndef APP-ANDROID
    menuItem!.children = removeNullItem(menuItem!.children)
136
    menuItem!.pages = menuItem!.pages.filter((item : Page | null) : boolean => item !== null)
137 138 139 140 141
    // #endif
  })
  return res
}

142
function addSetTabBarPage(menuItem : MenuItem) {
143 144 145 146 147 148 149 150 151
  const { id, name } = menuItem
  if (id == 'page' && name == '页面和路由') {
    menuItem.pages.push({
      path: 'set-tab-bar',
      style: {
        navigationBarTitleText: '设置 TabBar'
      }
    } as Page)
  }
152
}