feat(router): add sortRoutesByOrder function
This commit is contained in:
parent
748cfa2c62
commit
0cf09baef8
@ -18,6 +18,7 @@ import {
|
||||
getGlobalMenusByAuthRoutes,
|
||||
getSelectedMenuKeyPathByKey,
|
||||
isRouteExistByRouteName,
|
||||
sortRoutesByOrder,
|
||||
updateLocaleOfGlobalMenus
|
||||
} from './shared';
|
||||
|
||||
@ -185,11 +186,13 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
|
||||
* @param routes Auth routes
|
||||
*/
|
||||
function handleAuthRoutes(routes: ElegantConstRoute[]) {
|
||||
const vueRoutes = getAuthVueRoutes(routes);
|
||||
const sortRoutes = sortRoutesByOrder(routes);
|
||||
|
||||
const vueRoutes = getAuthVueRoutes(sortRoutes);
|
||||
|
||||
addRoutesToVueRouter(vueRoutes);
|
||||
|
||||
getGlobalMenus(routes);
|
||||
getGlobalMenus(sortRoutes);
|
||||
|
||||
getCacheRoutes(vueRoutes);
|
||||
}
|
||||
|
@ -44,6 +44,22 @@ function filterAuthRouteByRoles(route: ElegantConstRoute, roles: string[]) {
|
||||
return hasPermission ? [filterRoute] : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort routes by order
|
||||
*
|
||||
* @param routes An array of routes
|
||||
* @returns A new array of routes sorted by order
|
||||
*
|
||||
* This function sorts the routes by their order property, which is a number. If the order property is missing or
|
||||
* invalid, it is treated as 0. The routes with lower order values are placed before the routes with higher order
|
||||
* values. The function also sorts the children routes recursively, if any.
|
||||
*/
|
||||
export const sortRoutesByOrder = (routes: ElegantConstRoute[]): ElegantConstRoute[] => {
|
||||
return routes
|
||||
.sort((next, prev) => (Number(next.meta?.order) || 0) - (Number(prev.meta?.order) || 0))
|
||||
.map(route => (route.children ? { ...route, children: sortRoutesByOrder(route.children) } : route));
|
||||
};
|
||||
|
||||
/**
|
||||
* Get global menus by auth routes
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user