UserTaskbarTabRemove.vue 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 { useWalker } from '#shared/router/walker.ts'
  8. import CommonButton from '#desktop/components/CommonButton/CommonButton.vue'
  9. import { useUserCurrentTaskbarTabsStore } from '#desktop/entities/user/current/stores/taskbarTabs.ts'
  10. import type { UserTaskbarTab, UserTaskbarTabPlugin } from './types.ts'
  11. interface Props {
  12. taskbarTab: UserTaskbarTab
  13. dirty?: boolean
  14. plugin?: UserTaskbarTabPlugin
  15. }
  16. const props = defineProps<Props>()
  17. const taskbarTabStore = useUserCurrentTaskbarTabsStore()
  18. const { activeTaskbarTabEntityKey } = storeToRefs(taskbarTabStore)
  19. const { isTouchDevice } = useTouchDevice()
  20. const router = useRouter()
  21. const walker = useWalker()
  22. const confirmRemoveUserTaskbarTab = async () => {
  23. if (!props.taskbarTab.taskbarTabId) return
  24. if (props.plugin?.confirmTabRemove) {
  25. // Redirect to taskbar tab that is to be closed, if:
  26. // * it has a dirty state
  27. // * it's not the currently active tab
  28. // * the tab link can be computed
  29. if (
  30. props.dirty &&
  31. props.taskbarTab.tabEntityKey !== activeTaskbarTabEntityKey.value &&
  32. typeof props.plugin?.buildTaskbarTabLink === 'function'
  33. ) {
  34. const link = props.plugin.buildTaskbarTabLink(
  35. props.taskbarTab.entity,
  36. props.taskbarTab.tabEntityKey,
  37. )
  38. if (link) await router.push(link)
  39. }
  40. if (props.dirty) {
  41. const { waitForVariantConfirmation } = useConfirmation()
  42. const confirmed = await waitForVariantConfirmation(
  43. 'unsaved',
  44. undefined,
  45. `ticket-unsaved-${props.taskbarTab.tabEntityKey}`,
  46. )
  47. if (!confirmed) return
  48. }
  49. }
  50. // In case the tab is currently active, go back to previous route in the history stack.
  51. if (props.taskbarTab.tabEntityKey === activeTaskbarTabEntityKey.value)
  52. // TODO: Adjust the following redirect fallback to Overviews page instead, when ready.
  53. walker.back('/')
  54. taskbarTabStore.deleteTaskbarTab(props.taskbarTab.taskbarTabId)
  55. }
  56. </script>
  57. <template>
  58. <CommonButton
  59. v-if="props.taskbarTab.taskbarTabId"
  60. v-tooltip="$t('Close this tab')"
  61. :class="{ 'opacity-0 transition-opacity': !isTouchDevice }"
  62. class="absolute end-2 top-3 focus:opacity-100 group-hover/tab:opacity-100"
  63. icon="x-lg"
  64. size="small"
  65. variant="remove"
  66. @click.stop="confirmRemoveUserTaskbarTab"
  67. />
  68. </template>
  69. <style scoped>
  70. .dragging-active button {
  71. @apply invisible;
  72. }
  73. </style>