File tree Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ import {
18
18
getGlobalMenusByAuthRoutes ,
19
19
getSelectedMenuKeyPathByKey ,
20
20
isRouteExistByRouteName ,
21
+ sortRoutesByOrder ,
21
22
updateLocaleOfGlobalMenus
22
23
} from './shared' ;
23
24
@@ -185,11 +186,13 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
185
186
* @param routes Auth routes
186
187
*/
187
188
function handleAuthRoutes ( routes : ElegantConstRoute [ ] ) {
188
- const vueRoutes = getAuthVueRoutes ( routes ) ;
189
+ const sortRoutes = sortRoutesByOrder ( routes ) ;
190
+
191
+ const vueRoutes = getAuthVueRoutes ( sortRoutes ) ;
189
192
190
193
addRoutesToVueRouter ( vueRoutes ) ;
191
194
192
- getGlobalMenus ( routes ) ;
195
+ getGlobalMenus ( sortRoutes ) ;
193
196
194
197
getCacheRoutes ( vueRoutes ) ;
195
198
}
Original file line number Diff line number Diff line change @@ -44,6 +44,22 @@ function filterAuthRouteByRoles(route: ElegantConstRoute, roles: string[]) {
44
44
return hasPermission ? [ filterRoute ] : [ ] ;
45
45
}
46
46
47
+ /**
48
+ * Sort routes by order
49
+ *
50
+ * @param routes An array of routes
51
+ * @returns A new array of routes sorted by order
52
+ *
53
+ * This function sorts the routes by their order property, which is a number. If the order property is missing or
54
+ * invalid, it is treated as 0. The routes with lower order values are placed before the routes with higher order
55
+ * values. The function also sorts the children routes recursively, if any.
56
+ */
57
+ export const sortRoutesByOrder = ( routes : ElegantConstRoute [ ] ) : ElegantConstRoute [ ] => {
58
+ return routes
59
+ . sort ( ( next , prev ) => ( Number ( next . meta ?. order ) || 0 ) - ( Number ( prev . meta ?. order ) || 0 ) )
60
+ . map ( route => ( route . children ? { ...route , children : sortRoutesByOrder ( route . children ) } : route ) ) ;
61
+ } ;
62
+
47
63
/**
48
64
* Get global menus by auth routes
49
65
*
You can’t perform that action at this time.
0 commit comments