generateMenu.uts 2.8 KB
Newer Older
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
import { pages } from '@/target-pages.json'

type PageGroup = {
  id: string,
  name: string,
  index: number | null
}

type Page = {
  path: string,
  style: UTSJSONObject,
  group?: PageGroup[] | null
}

export type MenuItem = {
  id: string,
  name: string,
  index: number,
  children: (MenuItem | null)[],
  pages: Page[]
}

export function generateMenu(tabBarType: string): (MenuItem | null)[] {
  const res: (MenuItem | null)[] = []

  const tabBarPages = JSON.parse<Page[]>(JSON.stringify(pages))!.filter((page: Page): boolean => page.path.startsWith(tabBarType) && hasPageGroup(page.group))
  tabBarPages.forEach(page => {
    let menuItemArr: (MenuItem | null)[] = res
    let currentMenu: MenuItem | null = null
    const groupLength = page.group!.length
    page.group!.forEach((group, groupIndex) => {
      const { index, id, name } = group

      const validIndex = isNumber(index) ? index! : menuItemArr.length
      fillArrayWithNull(menuItemArr, validIndex)

      if (menuItemArr[validIndex] == null) {
        menuItemArr[validIndex] = {
          id: id.split('.').slice(-1)[0],
          name,
          index: validIndex,
          children: [] as (MenuItem | null)[],
          pages: [] as Page[]
        } as MenuItem
      }


      currentMenu = menuItemArr[validIndex]
      if (groupIndex < groupLength - 1) {
        menuItemArr = menuItemArr[validIndex]!.children as (MenuItem | null)[]
      }
    })

    currentMenu!.pages.push(page)
  })
  return removeNullItem(res)
}

function hasPageGroup(value: PageGroup[] | null): boolean {
  // #ifdef APP-ANDROID
  return value !== null
  // #endif
  // #ifndef APP-ANDROID
  return !!value
  // #endif
}

function isNumber(value: number | null): boolean {
  // #ifdef APP-ANDROID
  return value !== null
  // #endif
  // #ifndef APP-ANDROID
  return typeof value === 'number'
  // #endif
}

function fillArrayWithNull(arr: (MenuItem | null)[], index: number): void {
  const len = arr.length
  for (let i = 0;i <= index - (len - 1);i++) {
    arr.push(null)
  }
}

function removeNullItem(arr: (MenuItem | null)[]): (MenuItem | null)[] {
  const res = arr.filter((item: MenuItem | null): boolean => item !== null)
  res.forEach(menuItem => {
    addSetTabBarPage(menuItem!)
    // #ifdef APP-ANDROID
    menuItem!.children = removeNullItem(menuItem.children)
    // #endif
    // #ifndef APP-ANDROID
    menuItem!.children = removeNullItem(menuItem!.children)
    // #endif
  })
  return res
}

function addSetTabBarPage(menuItem: MenuItem) {
  const { id, name } = menuItem
  if (id == 'page' && name == '页面和路由') {
    menuItem.pages.push({
      path: 'set-tab-bar',
      style: {
        navigationBarTitleText: '设置 TabBar'
      }
    } as Page)
  }
}