initializeGlobalDirectives.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { type App, type Directive } from 'vue'
  3. import type { ImportGlobEagerDefault } from '#shared/types/utils.ts'
  4. import vTooltip from '#desktop/plugins/directives/tooltip.ts'
  5. interface DirectiveModule {
  6. name: string
  7. directive: Record<string, unknown> | ((...args: unknown[]) => unknown)
  8. }
  9. const directiveModules: ImportGlobEagerDefault<DirectiveModule>[] =
  10. Object.values(
  11. import.meta.glob('../plugins/directives/*', {
  12. eager: true,
  13. }),
  14. )
  15. export const directives: Record<string, Directive> = directiveModules.reduce(
  16. (directiveRecord: Record<string, Directive>, module) => {
  17. const { name, directive } = module.default
  18. directiveRecord[name] = directive
  19. return directiveRecord
  20. },
  21. {},
  22. )
  23. const initializeGlobalDirectives = (app: App) => {
  24. directiveModules.forEach(
  25. (module: ImportGlobEagerDefault<DirectiveModule>) => {
  26. const { name, directive } = module.default
  27. app.directive(name, directive)
  28. },
  29. )
  30. }
  31. export default initializeGlobalDirectives
  32. // :TODO improve DX by adding type definitions for global directives
  33. declare module '@vue/runtime-core' {
  34. export interface GlobalDirectives {
  35. vTooltip: typeof vTooltip
  36. }
  37. // export interface ComponentCustomProperties {
  38. // vTooltip: typeof vTooltip.directive
  39. // }
  40. }