Skip to content

Commit

Permalink
feat(projects): add sort routes
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Jan 15, 2024
1 parent 230027c commit 637c64a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/store/modules/route/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getGlobalMenusByAuthRoutes,
getSelectedMenuKeyPathByKey,
isRouteExistByRouteName,
sortRoutesByOrder,
updateLocaleOfGlobalMenus
} from './shared';

Expand Down Expand Up @@ -187,7 +188,9 @@ 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);

Expand Down
26 changes: 26 additions & 0 deletions src/store/modules/route/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,32 @@ function filterAuthRouteByRoles(route: ElegantConstRoute, roles: string[]) {
return hasPermission ? [filterRoute] : [];
}

/**
* sort route by order
*
* @param route route
*/
function sortRouteByOrder(route: ElegantConstRoute) {
if (route.children?.length) {
route.children.sort((next, prev) => (Number(next.meta?.order) || 0) - (Number(prev.meta?.order) || 0));
route.children.forEach(sortRouteByOrder);
}

return route;
}

/**
* sort routes by order
*
* @param routes routes
*/
export function sortRoutesByOrder(routes: ElegantConstRoute[]) {
routes.sort((next, prev) => (Number(next.meta?.order) || 0) - (Number(prev.meta?.order) || 0));
routes.forEach(sortRouteByOrder);

return routes;
}

/**
* Get global menus by auth routes
*
Expand Down

0 comments on commit 637c64a

Please sign in to comment.