firstLevelRoutes.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { RouteRecordRaw } from 'vue-router'
  3. interface RouteModule {
  4. default: RouteRecordRaw[]
  5. }
  6. interface PageRoute {
  7. path: string
  8. name: string
  9. meta: {
  10. title: string
  11. order: number
  12. level: number
  13. icon: string
  14. requiredPermission: string[]
  15. requiresAuth: boolean
  16. [key: string]: unknown
  17. }
  18. }
  19. const routeFiles = import.meta.glob<RouteModule>('../../pages/**/routes.ts', {
  20. eager: true,
  21. import: 'default',
  22. })
  23. const routeValues = Object.values(routeFiles)
  24. const firstLevelRoutes: PageRoute[] = []
  25. let permanentItemCount = 0
  26. routeValues.forEach((routeModule) => {
  27. if (Array.isArray(routeModule)) {
  28. permanentItemCount += routeModule.filter(
  29. (route: RouteRecordRaw) => route.meta?.permanentItem,
  30. ).length
  31. const routes = routeModule.filter((route: RouteRecordRaw) => {
  32. return route.meta?.level === 1
  33. })
  34. if (!routes?.length) return
  35. const mappedRoutes = routes.map((route) => ({
  36. path: route.path,
  37. name: route.name,
  38. meta: route.meta,
  39. }))
  40. firstLevelRoutes.push(...mappedRoutes)
  41. }
  42. })
  43. const sortedFirstLevelRoutes = firstLevelRoutes.sort(
  44. (a, b) => a.meta.order - b.meta.order,
  45. )
  46. const numberOfPermanentItems = permanentItemCount
  47. export { firstLevelRoutes, sortedFirstLevelRoutes, numberOfPermanentItems }