UserTaskbarTabRemove.vue 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { storeToRefs } from 'pinia'
  4. import { useRouter } from 'vue-router'
  5. import { useConfirmation } from '#shared/composables/useConfirmation.ts'
  6. import { useTouchDevice } from '#shared/composables/useTouchDevice.ts'
  7. import CommonButton from '#desktop/components/CommonButton/CommonButton.vue'
  8. import { useUserCurrentTaskbarTabsStore } from '#desktop/entities/user/current/stores/taskbarTabs.ts'
  9. import type { UserTaskbarTab, UserTaskbarTabPlugin } from './types.ts'
  10. interface Props {
  11. taskbarTab: UserTaskbarTab
  12. dirty?: boolean
  13. plugin?: UserTaskbarTabPlugin
  14. }
  15. const props = defineProps<Props>()
  16. const taskbarTabStore = useUserCurrentTaskbarTabsStore()
  17. const { activeTaskbarTabEntityKey } = storeToRefs(taskbarTabStore)
  18. const { isTouchDevice } = useTouchDevice()
  19. const router = useRouter()
  20. const confirmRemoveUserTaskbarTab = async () => {
  21. if (!props.taskbarTab.taskbarTabId) return
  22. if (props.plugin?.confirmTabRemove) {
  23. // Redirect to taskbar tab that is to be closed, if:
  24. // * it has a dirty state
  25. // * it's not the currently active tab
  26. // * the tab link can be computed
  27. if (
  28. props.dirty &&
  29. props.taskbarTab.tabEntityKey !== activeTaskbarTabEntityKey.value &&
  30. typeof props.plugin?.buildTaskbarTabLink === 'function'
  31. ) {
  32. const link = props.plugin.buildTaskbarTabLink(
  33. props.taskbarTab.entity,
  34. props.taskbarTab.tabEntityKey,
  35. )
  36. if (link) await router.push(link)
  37. }
  38. if (props.dirty) {
  39. const { waitForVariantConfirmation } = useConfirmation()
  40. const confirmed = await waitForVariantConfirmation(
  41. 'unsaved',
  42. undefined,
  43. `ticket-unsaved-${props.taskbarTab.tabEntityKey}`,
  44. )
  45. if (!confirmed) return
  46. }
  47. }
  48. // Redirection to a historical route will be handled by the store.
  49. taskbarTabStore.deleteTaskbarTab(props.taskbarTab.taskbarTabId)
  50. }
  51. </script>
  52. <template>
  53. <CommonButton
  54. v-if="props.taskbarTab.taskbarTabId"
  55. v-tooltip="$t('Close this tab')"
  56. :class="{ 'opacity-0 transition-opacity': !isTouchDevice }"
  57. class="absolute end-2 top-3 focus:opacity-100 group-hover/tab:opacity-100"
  58. icon="x-lg"
  59. size="small"
  60. variant="remove"
  61. @click.stop="confirmRemoveUserTaskbarTab"
  62. />
  63. </template>
  64. <style scoped>
  65. .dragging-active button {
  66. @apply invisible;
  67. }
  68. </style>