manage.ts 990 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { nextTick, reactive } from 'vue'
  3. import type { DestroyComponentData, PushComponentData } from './types.ts'
  4. import type { Component } from 'vue'
  5. export enum Events {
  6. Push = 'dynamic-component.push',
  7. Destroy = 'dynamic-component.destroy',
  8. }
  9. const createEvent = <T = PushComponentData | DestroyComponentData>(
  10. title: string,
  11. detail: T,
  12. ) => {
  13. return new CustomEvent<T>(title, { detail })
  14. }
  15. export const pushComponent = async (
  16. name: string,
  17. id: string,
  18. cmp: Component,
  19. props = {},
  20. ) => {
  21. const event = createEvent(Events.Push, {
  22. name,
  23. id,
  24. cmp,
  25. props: reactive(props),
  26. })
  27. window.dispatchEvent(event)
  28. await nextTick()
  29. }
  30. // if no id is passed down, destroys all named components
  31. export const destroyComponent = async (name: string, id?: string) => {
  32. const event = createEvent(Events.Destroy, { name, id })
  33. window.dispatchEvent(event)
  34. await nextTick()
  35. }