123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- import { isEqual } from 'lodash-es'
- import { storeToRefs } from 'pinia'
- import {
- computed,
- inject,
- provide,
- watch,
- type ComputedRef,
- type InjectionKey,
- type Ref,
- } from 'vue'
- import type { UserTaskbarTab } from '#desktop/components/UserTaskbarTabs/types.ts'
- import { useUserCurrentTaskbarTabsStore } from '../stores/taskbarTabs.ts'
- import type { TaskbarTabContext } from '../types.ts'
- interface CurrentTaskbarTabData {
- currentTaskbarTab: ComputedRef<UserTaskbarTab | undefined>
- currentTaskbarTabId: ComputedRef<string | undefined>
- currentTaskbarEntityKey: string | undefined
- currentTaskbarTabFormId: ComputedRef<string | undefined>
- currentTaskbarTabNewArticlePresent: ComputedRef<boolean>
- }
- export const CURRENT_TASKBAR_TAB_KEY = Symbol(
- 'current-taskbar-tab',
- ) as InjectionKey<CurrentTaskbarTabData>
- export const initializeCurrentTaskbarTab = (taskbarEntityKey?: string) => {
- const { taskbarTabListByTabEntityKey } = storeToRefs(
- useUserCurrentTaskbarTabsStore(),
- )
- const currentTaskbarTab = computed<UserTaskbarTab | undefined>(
- (existingTaskbarTab) => {
- if (!taskbarEntityKey) return
- if (
- existingTaskbarTab &&
- isEqual(
- existingTaskbarTab,
- taskbarTabListByTabEntityKey.value[taskbarEntityKey],
- )
- ) {
- return existingTaskbarTab
- }
- return taskbarTabListByTabEntityKey.value[taskbarEntityKey]
- },
- )
- const currentTaskbarTabEntityAccess = computed(
- () => currentTaskbarTab.value?.entityAccess,
- )
- const currentTaskbarTabId = computed(
- () => currentTaskbarTab.value?.taskbarTabId,
- )
- const currentTaskbarTabFormId = computed(
- () => currentTaskbarTab.value?.formId || undefined,
- )
- const currentTaskbarTabNewArticlePresent = computed(
- () => !!currentTaskbarTab.value?.formNewArticlePresent,
- )
- return {
- currentTaskbarTab,
- currentTaskbarTabEntityAccess,
- currentTaskbarTabId,
- currentTaskbarTabFormId,
- currentTaskbarTabNewArticlePresent,
- }
- }
- export const provideCurrentTaskbarTab = (data: CurrentTaskbarTabData) => {
- provide(CURRENT_TASKBAR_TAB_KEY, data)
- }
- export const useTaskbarTab = (context?: Ref<TaskbarTabContext>) => {
- const { taskbarTabContexts } = storeToRefs(useUserCurrentTaskbarTabsStore())
- const {
- currentTaskbarTab,
- currentTaskbarTabId,
- currentTaskbarTabFormId,
- currentTaskbarEntityKey,
- currentTaskbarTabNewArticlePresent,
- } = inject(CURRENT_TASKBAR_TAB_KEY) as CurrentTaskbarTabData
- const { updateTaskbarTab, deleteTaskbarTab } =
- useUserCurrentTaskbarTabsStore()
- // Keep track of the passed context and update the store state accordingly.
- if (context) {
- watch(
- context,
- (newValue) => {
- if (!currentTaskbarTab.value?.tabEntityKey) return
- taskbarTabContexts.value[currentTaskbarTab.value.tabEntityKey] =
- newValue
- },
- { immediate: true },
- )
- }
- watch(
- () =>
- currentTaskbarTab.value &&
- taskbarTabContexts.value[currentTaskbarTab.value.tabEntityKey]
- ?.formIsDirty,
- (isDirty) => {
- if (isDirty === undefined || !currentTaskbarTab.value?.taskbarTabId)
- return
- if (currentTaskbarTab.value.dirty === isDirty) return
- updateTaskbarTab(currentTaskbarTab.value.taskbarTabId, {
- ...currentTaskbarTab.value,
- dirty: isDirty,
- })
- },
- )
- const currentTaskbarTabDelete = () => {
- if (!currentTaskbarTabId.value) return
- deleteTaskbarTab(currentTaskbarTabId.value)
- }
- return {
- currentTaskbarTab,
- currentTaskbarEntityKey,
- currentTaskbarTabId,
- currentTaskbarTabFormId,
- currentTaskbarTabNewArticlePresent,
- currentTaskbarTabDelete,
- }
- }
|