index.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import mainInitializeRouter from '#shared/router/index.ts'
  3. import type { InitializeAppRouter, RoutesModule } from '#shared/types/router.ts'
  4. import activeTaskbarTab from './guards/before/activeTaskbarTab.ts'
  5. import systemSetupInfo from './guards/before/systemSetupInfo.ts'
  6. import type { App } from 'vue'
  7. import type { RouteRecordRaw } from 'vue-router'
  8. const routeModules: Record<string, RoutesModule> = import.meta.glob(
  9. ['../pages/*/routes.ts', '../pages/*/routes/*.ts'],
  10. { eager: true },
  11. )
  12. const mainRoutes: Array<RouteRecordRaw> = []
  13. const childRoutes: Array<RouteRecordRaw> = []
  14. const names = new Set<string | symbol>()
  15. const handleRoutes = (routes: Array<RouteRecordRaw>, isMainRoute = false) => {
  16. if (isMainRoute) {
  17. mainRoutes.push(...routes)
  18. } else {
  19. childRoutes.push(...routes)
  20. }
  21. if (import.meta.env.PROD) return
  22. // for debugging routes, vue-router doesn't do this automatically
  23. routes.forEach((route) => {
  24. if (!route.name) return
  25. if (names.has(route.name)) {
  26. console.error(
  27. `Duplicate route name: ${String(route.name)} for ${route.path}`,
  28. )
  29. } else {
  30. names.add(route.name)
  31. }
  32. })
  33. }
  34. Object.values(routeModules).forEach((module: RoutesModule) => {
  35. const defaultExport = module.default
  36. const { isMainRoute } = module
  37. handleRoutes(
  38. Array.isArray(defaultExport) ? defaultExport : [defaultExport],
  39. isMainRoute,
  40. )
  41. })
  42. export const routes: Array<RouteRecordRaw> = [
  43. ...mainRoutes,
  44. {
  45. path: '/',
  46. name: 'LayoutPage',
  47. component: () => import('#desktop/components/layout/LayoutPage.vue'),
  48. children: childRoutes,
  49. },
  50. ]
  51. const initializeRouter: InitializeAppRouter = (app: App) => {
  52. return mainInitializeRouter(
  53. app,
  54. routes,
  55. [systemSetupInfo, activeTaskbarTab],
  56. [],
  57. 'desktop',
  58. )
  59. }
  60. export default initializeRouter