提交 cb41b6db 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

types: 优化 generate menu 类型

上级 eeb22317
...@@ -12,9 +12,9 @@ ...@@ -12,9 +12,9 @@
:inWhiteList="true"></u-link> :inWhiteList="true"></u-link>
</view> </view>
<uni-collapse> <uni-collapse>
<uni-collapse-item ref="category" v-for="menuItem in menu" :key="menuItem!.id" :title="menuItem.name" <uni-collapse-item ref="category" v-for="menuItem in menu" :key="menuItem.id" :title="menuItem.name"
class="item"> class="item">
<template v-for="childMenuItem in menuItem.items" :key="childMenuItem!.id"> <template v-for="childMenuItem in menuItem.items" :key="childMenuItem.id">
<view v-if="childMenuItem.items.length==0" style="padding-left: 18px;" <view v-if="childMenuItem.items.length==0" style="padding-left: 18px;"
:class="{'uni-navigate-item':childMenuItem.path !== 'set-tab-bar' || windowWidth <= 768}" :class="{'uni-navigate-item':childMenuItem.path !== 'set-tab-bar' || windowWidth <= 768}"
hover-class="is--active" hover-class="is--active"
...@@ -62,7 +62,7 @@ ...@@ -62,7 +62,7 @@
export default { export default {
data() { data() {
return { return {
menu: menu as (MenuItem | null)[], menu: menu as MenuItem[],
arrowRightIcon: '/static/icons/arrow-right.png', arrowRightIcon: '/static/icons/arrow-right.png',
} }
}, },
......
...@@ -12,9 +12,9 @@ ...@@ -12,9 +12,9 @@
:inWhiteList="true"></u-link> :inWhiteList="true"></u-link>
</view> </view>
<uni-collapse> <uni-collapse>
<uni-collapse-item ref="category" v-for="menuItem in menu" :key="menuItem!.id" :title="menuItem.name" <uni-collapse-item ref="category" v-for="menuItem in menu" :key="menuItem.id" :title="menuItem.name"
class="item"> class="item">
<template v-for="childMenuItem in menuItem.items" :key="childMenuItem!.id"> <template v-for="childMenuItem in menuItem.items" :key="childMenuItem.id">
<view v-if="childMenuItem.items.length==0" style="padding-left: 18px" class="uni-navigate-item" <view v-if="childMenuItem.items.length==0" style="padding-left: 18px" class="uni-navigate-item"
hover-class="is--active" @click="goPage(`/${childMenuItem.path}`)"> hover-class="is--active" @click="goPage(`/${childMenuItem.path}`)">
<text class="uni-navigate-text" :class="{ <text class="uni-navigate-text" :class="{
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
<uni-collapse-item :title="childMenuItem.name" class="item" <uni-collapse-item :title="childMenuItem.name" class="item"
style="margin-bottom: 0; padding-left: 5px; padding-right: 5px"> style="margin-bottom: 0; padding-left: 5px; padding-right: 5px">
<view style="padding-left: 18px" class="uni-navigate-item" hover-class="is--active" <view style="padding-left: 18px" class="uni-navigate-item" hover-class="is--active"
v-for="grandChildMenuItem in childMenuItem.items" :key="grandChildMenuItem!.path" v-for="grandChildMenuItem in childMenuItem.items" :key="grandChildMenuItem.path"
@click="goPage(`/${grandChildMenuItem.path}`)"> @click="goPage(`/${grandChildMenuItem.path}`)">
<text class="uni-navigate-text" :class="{ <text class="uni-navigate-text" :class="{
'left-win-active': 'left-win-active':
...@@ -53,7 +53,7 @@ ...@@ -53,7 +53,7 @@
export default { export default {
data() { data() {
return { return {
menu: menu as (MenuItem | null)[], menu: menu as MenuItem[],
arrowRightIcon: '/static/icons/arrow-right.png', arrowRightIcon: '/static/icons/arrow-right.png',
} }
}, },
...@@ -66,11 +66,11 @@ ...@@ -66,11 +66,11 @@
} }
}, },
methods: { methods: {
goPage(url : string) { goPage(url : string) {
if(this.hasLeftWin) { if(this.hasLeftWin) {
uni.reLaunch({ url }) uni.reLaunch({ url })
} else { } else {
uni.navigateTo({ url }) uni.navigateTo({ url })
} }
}, },
}, },
...@@ -98,4 +98,4 @@ ...@@ -98,4 +98,4 @@
.item { .item {
margin-bottom: 12px; margin-bottom: 12px;
} }
</style> </style>
...@@ -12,9 +12,9 @@ ...@@ -12,9 +12,9 @@
:text="'https://uniapp.dcloud.io/uni-app-x/component/'" :inWhiteList="true"></u-link> :text="'https://uniapp.dcloud.io/uni-app-x/component/'" :inWhiteList="true"></u-link>
</view> </view>
<uni-collapse> <uni-collapse>
<uni-collapse-item ref="category" v-for="menuItem in menu" :key="menuItem!.id" :title="menuItem.name" <uni-collapse-item ref="category" v-for="menuItem in menu" :key="menuItem.id" :title="menuItem.name"
class="item"> class="item">
<template v-for="childMenuItem in menuItem.items" :key="childMenuItem!.id"> <template v-for="childMenuItem in menuItem.items" :key="childMenuItem.id">
<view v-if="childMenuItem.items.length==0" style="padding-left: 18px" class="uni-navigate-item" <view v-if="childMenuItem.items.length==0" style="padding-left: 18px" class="uni-navigate-item"
hover-class="is--active" @click="goPage(`/${childMenuItem.path}`)"> hover-class="is--active" @click="goPage(`/${childMenuItem.path}`)">
<text class="uni-navigate-text" :class="{ <text class="uni-navigate-text" :class="{
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
<uni-collapse-item :title="childMenuItem.name" class="item" <uni-collapse-item :title="childMenuItem.name" class="item"
style="margin-bottom: 0; padding-left: 5px; padding-right: 5px"> style="margin-bottom: 0; padding-left: 5px; padding-right: 5px">
<view style="padding-left: 18px" class="uni-navigate-item" hover-class="is--active" <view style="padding-left: 18px" class="uni-navigate-item" hover-class="is--active"
v-for="grandChildMenuItem in childMenuItem.items" :key="grandChildMenuItem!.path" v-for="grandChildMenuItem in childMenuItem.items" :key="grandChildMenuItem.path"
@click="goPage(`/${grandChildMenuItem.path}`)"> @click="goPage(`/${grandChildMenuItem.path}`)">
<text class="uni-navigate-text" :class="{ <text class="uni-navigate-text" :class="{
'left-win-active': 'left-win-active':
...@@ -60,7 +60,7 @@ ...@@ -60,7 +60,7 @@
export default { export default {
data() { data() {
return { return {
menu: menu as (MenuItem | null)[], menu: menu as MenuItem[],
arrowRightIcon: '/static/icons/arrow-right.png' as string.ImageURIString, arrowRightIcon: '/static/icons/arrow-right.png' as string.ImageURIString,
pageHiden: false pageHiden: false
} }
...@@ -74,11 +74,11 @@ ...@@ -74,11 +74,11 @@
} }
}, },
methods: { methods: {
goPage(url : string) { goPage(url : string) {
if(this.hasLeftWin) { if(this.hasLeftWin) {
uni.reLaunch({ url }) uni.reLaunch({ url })
} else { } else {
uni.navigateTo({ url }) uni.navigateTo({ url })
} }
}, },
// #ifdef UNI-APP-X && APP // #ifdef UNI-APP-X && APP
...@@ -132,4 +132,4 @@ ...@@ -132,4 +132,4 @@
.item { .item {
margin-bottom: 12px; margin-bottom: 12px;
} }
</style> </style>
...@@ -25,15 +25,15 @@ export type MenuItem = { ...@@ -25,15 +25,15 @@ export type MenuItem = {
path : string, path : string,
style : UTSJSONObject, style : UTSJSONObject,
group ?: string | null, group ?: string | null,
items : (MenuItem | null)[] items: MenuItem[]
} }
export function generateMenu(tabBarType : string) : (MenuItem | null)[] { export function generateMenu(tabBarType: string): MenuItem[] {
let res : (MenuItem | null)[] = [] let res: MenuItem[] = []
const tabBarPages = JSON.parse<Page[]>(JSON.stringify(pages))!.filter((page : Page) : boolean => page.path.startsWith(tabBarType) && hasPageGroup(page.group)) const tabBarPages = JSON.parse<Page[]>(JSON.stringify(pages))!.filter((page : Page) : boolean => page.path.startsWith(tabBarType) && hasPageGroup(page.group))
const groupTree = JSON.parse<(Group | null)[]>(JSON.stringify(groups)) const groupTree = JSON.parse<(Group | null)[]>(JSON.stringify(groups))
tabBarPages.forEach(page => { tabBarPages.forEach(page => {
let menuItemArr : (MenuItem | null)[] = res let menuItemArr: MenuItem[] = res
let currentMenu : MenuItem | null = null let currentMenu : MenuItem | null = null
const groupIndexList = page.group!.split(',').map((index : string) : number => parseInt(index)) const groupIndexList = page.group!.split(',').map((index : string) : number => parseInt(index))
let currentGroups : (Group | null)[] | null = groupTree let currentGroups : (Group | null)[] | null = groupTree
...@@ -56,9 +56,9 @@ export function generateMenu(tabBarType : string) : (MenuItem | null)[] { ...@@ -56,9 +56,9 @@ export function generateMenu(tabBarType : string) : (MenuItem | null)[] {
const { index, id, name } = group const { index, id, name } = group
const validIndex = index const validIndex = index
fillMenuArrayWithNull(menuItemArr, validIndex) fillMenuArrayWithEmptyMenuItem(menuItemArr, validIndex)
if (menuItemArr[validIndex] == null) { if (menuItemArr[validIndex].name == 'empty') {
menuItemArr[validIndex] = { menuItemArr[validIndex] = {
id: id.split('.').slice(-1)[0], id: id.split('.').slice(-1)[0],
name, name,
...@@ -66,14 +66,14 @@ export function generateMenu(tabBarType : string) : (MenuItem | null)[] { ...@@ -66,14 +66,14 @@ export function generateMenu(tabBarType : string) : (MenuItem | null)[] {
path: '', path: '',
style: {}, style: {},
group: '', group: '',
items: [] as (MenuItem | null)[], items: [] as MenuItem[],
} as MenuItem } as MenuItem
} }
currentMenu = menuItemArr[validIndex] currentMenu = menuItemArr[validIndex]
if (groupIndex < groupLength - 1) { if (groupIndex < groupLength - 1) {
menuItemArr = menuItemArr[validIndex]!.items as (MenuItem | null)[] menuItemArr = menuItemArr[validIndex].items
} }
}) })
...@@ -84,12 +84,12 @@ export function generateMenu(tabBarType : string) : (MenuItem | null)[] { ...@@ -84,12 +84,12 @@ export function generateMenu(tabBarType : string) : (MenuItem | null)[] {
path: page.path, path: page.path,
style: page.style, style: page.style,
group: page.group, group: page.group,
items: [] as (MenuItem | null)[], items: [] as MenuItem[],
} }
if (hasPageGroup) { if (hasPageGroup) {
const pageIndex = lastGroup.index const pageIndex = lastGroup.index
fillMenuArrayWithNull(currentMenu!.items, pageIndex) fillMenuArrayWithEmptyMenuItem(currentMenu!.items, pageIndex)
if (currentMenu!.items[pageIndex] == null) { if (currentMenu!.items[pageIndex].name == 'empty') {
currentMenu!.items[pageIndex] = pageMenuItem currentMenu!.items[pageIndex] = pageMenuItem
} else { } else {
currentMenu!.items.push(pageMenuItem) currentMenu!.items.push(pageMenuItem)
...@@ -99,7 +99,7 @@ export function generateMenu(tabBarType : string) : (MenuItem | null)[] { ...@@ -99,7 +99,7 @@ export function generateMenu(tabBarType : string) : (MenuItem | null)[] {
} }
}) })
return removeNullItem(res) return removeEmptyItem(res)
} }
function hasPageGroup(value ?: string | null) : boolean { function hasPageGroup(value ?: string | null) : boolean {
...@@ -124,29 +124,37 @@ function camelToDash(camelStr : string) : string { ...@@ -124,29 +124,37 @@ function camelToDash(camelStr : string) : string {
return camelStr.replace(/([A-Z])/g, '-$1').toLowerCase() return camelStr.replace(/([A-Z])/g, '-$1').toLowerCase()
} }
function fillMenuArrayWithNull(arr : (MenuItem | null)[], index : number) : void { function fillMenuArrayWithEmptyMenuItem(arr: MenuItem[], index: number): void {
const len = arr.length const len = arr.length
for (let i = 0; i <= index - (len - 1); i++) { for (let i = 0; i <= index - (len - 1); i++) {
arr.push(null) arr.push({
id: '',
name: 'empty',
index: i,
path: '',
style: {},
group: '',
items: [] as MenuItem[],
})
} }
} }
function removeNullItem(arr : (MenuItem | null)[]) : (MenuItem | null)[] { function removeEmptyItem(arr: MenuItem[]): MenuItem[] {
const res = arr.filter((item : MenuItem | null) : boolean => item !== null) const res = arr.filter((item: MenuItem): boolean => item.name !== 'empty')
res.forEach(menuItem => { res.forEach(menuItem => {
addSetTabBarPage(menuItem!) addSetTabBarPage(menuItem)
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
menuItem.items = removeNullItem(menuItem.items) menuItem.items = removeEmptyItem(menuItem.items)
// #endif // #endif
// #ifndef APP-ANDROID // #ifndef APP-ANDROID
menuItem!.items = removeNullItem(menuItem!.items) menuItem!.items = removeEmptyItem(menuItem!.items)
// #endif // #endif
}) })
return res return res
} }
function addSetTabBarPage(menuItem : MenuItem) { function addSetTabBarPage(menuItem : MenuItem) {
const { id, name } = menuItem const { id, name } = menuItem
if (id == 'page' && name == '页面和路由') { if (id == 'page' && name == '页面和路由') {
menuItem.items.push({ menuItem.items.push({
id: 'set-tab-bar', id: 'set-tab-bar',
...@@ -160,4 +168,4 @@ function addSetTabBarPage(menuItem : MenuItem) { ...@@ -160,4 +168,4 @@ function addSetTabBarPage(menuItem : MenuItem) {
items: [] items: []
} as MenuItem) } as MenuItem)
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册