Skip to content

Commit

Permalink
optimize(core): optimize menu selectedKey
Browse files Browse the repository at this point in the history
  • Loading branch information
loicduong committed Sep 7, 2024
1 parent 8870ebf commit 66ab892
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 56 deletions.
25 changes: 19 additions & 6 deletions src/layouts/context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const { setupStore: setupMixMenuContext, useStore: useMixMenuContext } =
function useMixMenu() {
const route = useRoute()
const routeStore = useRouteStore()
const { selectedKey } = useMenu()

const activeFirstLevelMenuKey = ref('')

Expand All @@ -16,12 +17,7 @@ function useMixMenu() {
}

function getActiveFirstLevelMenuKey() {
const { hideInMenu, activeMenu } = route.meta
const name = route.name as string

const routeName = (hideInMenu ? activeMenu : name) || name

const [firstLevelRouteName] = routeName.split('_')
const [firstLevelRouteName] = selectedKey.value.split('_')

setActiveFirstLevelMenuKey(firstLevelRouteName)
}
Expand Down Expand Up @@ -68,3 +64,20 @@ function useMixMenu() {
getActiveFirstLevelMenuKey,
}
}

export function useMenu() {
const route = useRoute()

const selectedKey = computed(() => {
const { hideInMenu, activeMenu } = route.meta
const name = route.name as string

const routeName = (hideInMenu ? activeMenu : name) || name

return routeName
})

return {
selectedKey,
}
}
11 changes: 2 additions & 9 deletions src/layouts/modules/global-menu/modules/horizontal-menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@
import { GLOBAL_HEADER_MENU_ID } from '@/constants/app'
import { useRouterPush } from '@/hooks/common/router'
import { useRouteStore } from '@/store/modules/route'
import { useMenu } from '../../../context'
defineOptions({
name: 'HorizontalMenu',
})
const route = useRoute()
const routeStore = useRouteStore()
const { routerPushByKeyWithMetaQuery } = useRouterPush()
const selectedKey = computed(() => {
const { hideInMenu, activeMenu } = route.meta
const name = route.name as string
const routeName = (hideInMenu ? activeMenu : name) || name
return routeName
})
const { selectedKey } = useMenu()
</script>

<template>
Expand Down
14 changes: 3 additions & 11 deletions src/layouts/modules/global-menu/modules/horizontal-mix-menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,19 @@ import { GLOBAL_HEADER_MENU_ID, GLOBAL_SIDER_MENU_ID } from '@/constants/app'
import { useRouterPush } from '@/hooks/common/router'
import { useAppStore } from '@/store/modules/app'
import { useThemeStore } from '@/store/modules/theme'
import { useMixMenuContext } from '../../../context'
import { useMenu, useMixMenuContext } from '../../../context'
import FirstLevelMenu from '../components/first-level-menu.vue'
defineOptions({
name: 'HorizontalMixMenu',
})
const route = useRoute()
const appStore = useAppStore()
const themeStore = useThemeStore()
const { allMenus, childLevelMenus, activeFirstLevelMenuKey, setActiveFirstLevelMenuKey } = useMixMenuContext()
const { routerPushByKeyWithMetaQuery } = useRouterPush()
const selectedKey = computed(() => {
const { hideInMenu, activeMenu } = route.meta
const name = route.name as string
const routeName = (hideInMenu ? activeMenu : name) || name
return routeName
})
const { allMenus, childLevelMenus, activeFirstLevelMenuKey, setActiveFirstLevelMenuKey } = useMixMenuContext()
const { selectedKey } = useMenu()
function handleSelectMixMenu(menu: App.Global.Menu) {
setActiveFirstLevelMenuKey(menu.key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useRouteStore } from '@/store/modules/route'
import { useThemeStore } from '@/store/modules/theme'
import { SimpleScrollbar } from '@sa/materials'
import type { RouteKey } from '@elegant-router/types'
import { useMixMenuContext } from '../../../context'
import { useMenu, useMixMenuContext } from '../../../context'
defineOptions({
name: 'ReversedHorizontalMixMenu',
Expand All @@ -16,23 +16,15 @@ const route = useRoute()
const appStore = useAppStore()
const themeStore = useThemeStore()
const routeStore = useRouteStore()
const { routerPushByKeyWithMetaQuery } = useRouterPush()
const {
firstLevelMenus,
childLevelMenus,
activeFirstLevelMenuKey,
setActiveFirstLevelMenuKey,
isActiveFirstLevelMenuHasChildren,
} = useMixMenuContext()
const { routerPushByKeyWithMetaQuery } = useRouterPush()
const selectedKey = computed(() => {
const { hideInMenu, activeMenu } = route.meta
const name = route.name as string
const routeName = (hideInMenu ? activeMenu : name) || name
return routeName
})
const { selectedKey } = useMenu()
function handleSelectMixMenu(key: RouteKey) {
setActiveFirstLevelMenuKey(key)
Expand Down
11 changes: 2 additions & 9 deletions src/layouts/modules/global-menu/modules/vertical-menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useAppStore } from '@/store/modules/app'
import { useRouteStore } from '@/store/modules/route'
import { useThemeStore } from '@/store/modules/theme'
import { SimpleScrollbar } from '@sa/materials'
import { useMenu } from '../../../context'
defineOptions({
name: 'VerticalMenu',
Expand All @@ -15,18 +16,10 @@ const appStore = useAppStore()
const themeStore = useThemeStore()
const routeStore = useRouteStore()
const { routerPushByKeyWithMetaQuery } = useRouterPush()
const { selectedKey } = useMenu()
const inverted = computed(() => !themeStore.darkMode && themeStore.sider.inverted)
const selectedKey = computed(() => {
const { hideInMenu, activeMenu } = route.meta
const name = route.name as string
const routeName = (hideInMenu ? activeMenu : name) || name
return routeName
})
const expandedKeys = ref<string[]>([])
function updateExpandedKeys() {
Expand Down
12 changes: 2 additions & 10 deletions src/layouts/modules/global-menu/modules/vertical-mix-menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useRouteStore } from '@/store/modules/route'
import { useThemeStore } from '@/store/modules/theme'
import { useBoolean } from '@sa/hooks'
import { SimpleScrollbar } from '@sa/materials'
import { useMixMenuContext } from '../../../context'
import { useMenu, useMixMenuContext } from '../../../context'
import GlobalLogo from '../../global-logo/index.vue'
import FirstLevelMenu from '../components/first-level-menu.vue'
Expand All @@ -29,6 +29,7 @@ const {
getActiveFirstLevelMenuKey,
//
} = useMixMenuContext()
const { selectedKey } = useMenu()
const inverted = computed(() => !themeStore.darkMode && themeStore.sider.inverted)
Expand All @@ -55,15 +56,6 @@ function handleResetActiveMenu() {
}
}
const selectedKey = computed(() => {
const { hideInMenu, activeMenu } = route.meta
const name = route.name as string
const routeName = (hideInMenu ? activeMenu : name) || name
return routeName
})
const expandedKeys = ref<string[]>([])
function updateExpandedKeys() {
Expand Down

0 comments on commit 66ab892

Please sign in to comment.