generateMenu.uts 3.9 KB
Newer Older
1 2 3
import { pages } from '@/target-pages.json'

type PageGroup = {
4 5 6
  id : string,
  name : string,
  index : number | null
7 8 9
}

type Page = {
10 11 12
  path : string,
  style : UTSJSONObject,
  group ?: PageGroup[] | null
13 14 15
}

export type MenuItem = {
16 17 18 19 20
  id : string,
  name : string,
  index : number,
  children : (MenuItem | null)[],
  pages : (Page | null)[]
21 22
}

23 24
export function generateMenu(tabBarType : string) : (MenuItem | null)[] {
  let res : (MenuItem | null)[] = []
25

26
  const tabBarPages = JSON.parse<Page[]>(JSON.stringify(pages))!.filter((page : Page) : boolean => page.path.startsWith(tabBarType) && hasPageGroup(page.group))
27
  tabBarPages.forEach(page => {
28 29
    let menuItemArr : (MenuItem | null)[] = res
    let currentMenu : MenuItem | null = null
30 31 32 33 34 35
    const lastGroup = page.group![page.group!.length - 1]
    let hasPageGroup = false
    if (page.path.endsWith(camelToDash(lastGroup.name)) && page.group!.length > 1) {
      hasPageGroup = true
      page.group!.pop()
    }
36 37 38 39 40
    const groupLength = page.group!.length
    page.group!.forEach((group, groupIndex) => {
      const { index, id, name } = group

      const validIndex = isNumber(index) ? index! : menuItemArr.length
41
      fillMenuArrayWithNull(menuItemArr, validIndex)
42 43 44 45 46 47 48

      if (menuItemArr[validIndex] == null) {
        menuItemArr[validIndex] = {
          id: id.split('.').slice(-1)[0],
          name,
          index: validIndex,
          children: [] as (MenuItem | null)[],
49
          pages: [] as (Page | null)[]
50 51 52 53 54 55 56 57 58
        } as MenuItem
      }


      currentMenu = menuItemArr[validIndex]
      if (groupIndex < groupLength - 1) {
        menuItemArr = menuItemArr[validIndex]!.children as (MenuItem | null)[]
      }
    })
59 60 61 62 63
    if (hasPageGroup) {
      const pageIndex = isNumber(lastGroup.index) ? lastGroup.index! : currentMenu!.pages.length
      fillPageArrayWithNull(currentMenu!.pages, pageIndex)
      if (currentMenu!.pages[pageIndex] == null) {
        currentMenu!.pages[pageIndex] = page
64 65
      } else {
        currentMenu!.pages.push(page)
66
      }
67
    } else {
68 69
      currentMenu!.pages.push(page)
    }
70
  })
71

72 73 74
  return removeNullItem(res)
}

75
function hasPageGroup(value : PageGroup[] | null) : boolean {
76 77 78 79 80 81 82 83
  // #ifdef APP-ANDROID
  return value !== null
  // #endif
  // #ifndef APP-ANDROID
  return !!value
  // #endif
}

84
function isNumber(value : number | null) : boolean {
85 86 87 88 89 90 91 92
  // #ifdef APP-ANDROID
  return value !== null
  // #endif
  // #ifndef APP-ANDROID
  return typeof value === 'number'
  // #endif
}

93
function camelToDash(camelStr : string) : string {
94 95 96
  return camelStr.replace(/([A-Z])/g, '-$1').toLowerCase()
}

97
function fillMenuArrayWithNull(arr : (MenuItem | null)[], index : number) : void {
98
  const len = arr.length
99
  for (let i = 0; i <= index - (len - 1); i++) {
100 101 102 103
    arr.push(null)
  }
}

104
function fillPageArrayWithNull(arr : (Page | null)[], index : number) : void {
105
  const len = arr.length
106
  for (let i = 0; i <= index - (len - 1); i++) {
107 108 109 110
    arr.push(null)
  }
}

111 112
function removeNullItem(arr : (MenuItem | null)[]) : (MenuItem | null)[] {
  const res = arr.filter((item : MenuItem | null) : boolean => item !== null)
113 114 115
  res.forEach(menuItem => {
    addSetTabBarPage(menuItem!)
    // #ifdef APP-ANDROID
116
    menuItem.children = removeNullItem(menuItem.children)
117
    menuItem.pages = menuItem.pages.filter((item : Page | null) : boolean => item !== null)
118 119 120
    // #endif
    // #ifndef APP-ANDROID
    menuItem!.children = removeNullItem(menuItem!.children)
121
    menuItem!.pages = menuItem!.pages.filter((item : Page | null) : boolean => item !== null)
122 123 124 125 126
    // #endif
  })
  return res
}

127
function addSetTabBarPage(menuItem : MenuItem) {
128 129 130 131 132 133 134 135 136
  const { id, name } = menuItem
  if (id == 'page' && name == '页面和路由') {
    menuItem.pages.push({
      path: 'set-tab-bar',
      style: {
        navigationBarTitleText: '设置 TabBar'
      }
    } as Page)
  }
137
}