55 * https://opensource.org/licenses/MIT
66 */
77
8- import type { UnwrapRef } from "vue"
8+ import type { Ref } from "vue"
99import type ElementIcon from "@src/element-ui/icon"
1010import type { MenuItemRegistered } from "element-plus"
11- import type { RouteLocationNormalizedLoaded , Router } from "vue-router"
11+ import type { Router } from "vue-router"
1212import type { I18nKey } from "@app/locale"
1313import type { MenuMessage } from "@i18n/message/app/menu"
1414
15- import { defineComponent , h , onMounted , reactive } from "vue"
15+ import { defineComponent , h , onMounted , ref , watch } from "vue"
1616import { ElIcon , ElMenu , ElMenuItem , ElMenuItemGroup } from "element-plus"
17- import { useRoute , useRouter } from "vue-router"
17+ import { useRouter } from "vue-router"
1818import { t } from "@app/locale"
1919import { HOME_PAGE , FEEDBACK_QUESTIONNAIRE , getGuidePageUrl } from "@util/constant/url"
2020import { Aim , Calendar , ChatSquare , Folder , HelpFilled , HotWater , Memo , Rank , SetUp , Stopwatch , Sugar , Tickets , Timer } from "@element-plus/icons-vue"
2121import { locale } from "@i18n"
2222import TrendIcon from "./icon/trend-icon"
23- import { createTab } from "@api/chrome/tab"
23+ import { createTabAfterCurrent } from "@api/chrome/tab"
2424import { ANALYSIS_ROUTE , MERGE_ROUTE } from "@app/router/constants"
2525import { START_ROUTE } from "@guide/router/constants"
2626
@@ -37,11 +37,6 @@ type _MenuGroup = {
3737 children : _MenuItem [ ]
3838}
3939
40- type _RouteProps = {
41- router : Router
42- current : RouteLocationNormalizedLoaded
43- }
44-
4540/**
4641 * Generate menu items after locale initialized
4742 */
@@ -126,23 +121,24 @@ function generateMenus(): _MenuGroup[] {
126121 } ]
127122}
128123
129- function openMenu ( route : string , title : I18nKey , routeProps : UnwrapRef < _RouteProps > ) {
130- const routerVal = routeProps . router
131- const currentRouteVal = routeProps . current
132- if ( currentRouteVal && currentRouteVal . path !== route ) {
133- routerVal && routerVal . push ( route )
124+ function openMenu ( route : string , title : I18nKey , router : Router ) {
125+ const currentPath = router . currentRoute . value ?. path
126+ if ( currentPath !== route ) {
127+ router ?. push ( route )
134128 document . title = t ( title )
135129 }
136130}
137131
138- const openHref = ( href : string ) => createTab ( href )
132+ const openHref = ( href : string ) => createTabAfterCurrent ( href )
139133
140- function handleClick ( menuItem : _MenuItem , routeProps : UnwrapRef < _RouteProps > ) {
134+ function handleClick ( menuItem : _MenuItem , router : Router , currentActive : Ref < string > ) {
141135 const { route, title, href } = menuItem
142136 if ( route ) {
143- openMenu ( route , msg => msg . menu [ title ] , routeProps )
137+ openMenu ( route , msg => msg . menu [ title ] , router )
138+ currentActive . value = '/data/dashboard' //route
144139 } else {
145140 openHref ( href )
141+ currentActive . value = router . currentRoute ?. value ?. path
146142 }
147143}
148144
@@ -153,10 +149,10 @@ const iconStyle: Partial<CSSStyleDeclaration> = {
153149 lineHeight : '0.83em'
154150}
155151
156- function renderMenuLeaf ( menu : _MenuItem , routeProps : UnwrapRef < _RouteProps > ) {
152+ function renderMenuLeaf ( menu : _MenuItem , router : Router , currentActive : Ref < string > ) {
157153 const { route, title, icon, index } = menu
158154 const props : { onClick : ( item : MenuItemRegistered ) => void ; index ?: string } = {
159- onClick : ( _item ) => handleClick ( menu , routeProps )
155+ onClick : ( _item ) => handleClick ( menu , router , currentActive )
160156 }
161157 const realIndex = index || route
162158 realIndex && ( props . index = realIndex )
@@ -166,9 +162,9 @@ function renderMenuLeaf(menu: _MenuItem, routeProps: UnwrapRef<_RouteProps>) {
166162 } )
167163}
168164
169- function renderMenu ( menu : _MenuGroup , props : UnwrapRef < _RouteProps > ) {
165+ function renderMenu ( menu : _MenuGroup , router : Router , currentActive : Ref < string > ) {
170166 const title = t ( msg => msg . menu [ menu . title ] )
171- return h ( ElMenuItemGroup , { title } , ( ) => menu . children . map ( item => renderMenuLeaf ( item , props ) ) )
167+ return h ( ElMenuItemGroup , { title } , ( ) => menu . children . map ( item => renderMenuLeaf ( item , router , currentActive ) ) )
172168}
173169
174170async function initTitle ( allMenus : _MenuGroup [ ] , router : Router ) {
@@ -185,22 +181,22 @@ async function initTitle(allMenus: _MenuGroup[], router: Router) {
185181 }
186182}
187183
188- const _default = defineComponent ( {
189- name : "LayoutMenu" ,
190- setup ( ) {
191- const routeProps : UnwrapRef < _RouteProps > = reactive ( {
192- router : useRouter ( ) ,
193- current : useRoute ( )
194- } )
195-
196- const allMenus = generateMenus ( )
197- onMounted ( ( ) => initTitle ( allMenus , useRouter ( ) ) )
198-
199- return ( ) => h ( ElMenu ,
200- { defaultActive : routeProps . current . path } ,
201- ( ) => allMenus . map ( menu => renderMenu ( menu , routeProps ) )
202- )
184+ const _default = defineComponent ( ( ) => {
185+ const router = useRouter ( )
186+ const currentActive : Ref < string > = ref ( )
187+ const syncRouter = ( ) => {
188+ const route = router . currentRoute . value
189+ route && ( currentActive . value = route . path )
203190 }
191+
192+ watch ( router . currentRoute , syncRouter )
193+
194+ const allMenus = generateMenus ( )
195+ onMounted ( ( ) => initTitle ( allMenus , router ) )
196+
197+ return ( ) => h ( 'div' , { class : 'menu-container' } , h ( ElMenu , { defaultActive : currentActive . value } ,
198+ ( ) => allMenus . map ( menu => renderMenu ( menu , router , currentActive ) )
199+ ) )
204200} )
205201
206202export default _default
0 commit comments