|
|
@ -1,5 +1,5 @@ |
|
|
|
import type { WritableComputedRef } from '@vue/reactivity' |
|
|
|
import type { WritableComputedRef } from '@vue/reactivity' |
|
|
|
import { computed, navigateTo, useProject, useRoute, useRouter, useState } from '#imports' |
|
|
|
import { computed, navigateTo, ref, useInjectionState, useProject, useRoute, useRouter, watch } from '#imports' |
|
|
|
import type { TabItem } from '~/lib' |
|
|
|
import type { TabItem } from '~/lib' |
|
|
|
import { TabType } from '~/lib' |
|
|
|
import { TabType } from '~/lib' |
|
|
|
|
|
|
|
|
|
|
@ -10,8 +10,8 @@ function getPredicate(key: Partial<TabItem>) { |
|
|
|
(!('type' in key) || tab.type === key.type) |
|
|
|
(!('type' in key) || tab.type === key.type) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function useTabs() { |
|
|
|
const [setup, use] = useInjectionState(() => { |
|
|
|
const tabs = useState<TabItem[]>('tabs', () => []) |
|
|
|
const tabs = ref<TabItem[]>([]) |
|
|
|
|
|
|
|
|
|
|
|
const route = useRoute() |
|
|
|
const route = useRoute() |
|
|
|
|
|
|
|
|
|
|
@ -21,28 +21,32 @@ export function useTabs() { |
|
|
|
|
|
|
|
|
|
|
|
const projectType = $computed(() => route.params.projectType as string) |
|
|
|
const projectType = $computed(() => route.params.projectType as string) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const previousActiveTabIndex = ref(-1) |
|
|
|
const activeTabIndex: WritableComputedRef<number> = computed({ |
|
|
|
const activeTabIndex: WritableComputedRef<number> = computed({ |
|
|
|
get() { |
|
|
|
get() { |
|
|
|
if ((route.name as string)?.startsWith('projectType-projectId-index-index-type-title-viewTitle') && tables.value?.length) { |
|
|
|
const routeName = route.name as string |
|
|
|
const tab: Partial<TabItem> = { type: route.params.type as TabType, title: route.params.title as string } |
|
|
|
|
|
|
|
|
|
|
|
if (routeName.startsWith('projectType-projectId-index-index-type-title-viewTitle') && tables.value.length) { |
|
|
|
|
|
|
|
const tab: TabItem = { type: route.params.type as TabType, title: route.params.title as string } |
|
|
|
|
|
|
|
|
|
|
|
const id = tables.value?.find((t) => t.title === tab.title)?.id |
|
|
|
const currentTable = tables.value.find((t) => t.title === tab.title) |
|
|
|
|
|
|
|
|
|
|
|
if (!id) return -1 |
|
|
|
if (!currentTable) return -1 |
|
|
|
|
|
|
|
|
|
|
|
tab.id = id as string |
|
|
|
tab.id = currentTable.id |
|
|
|
|
|
|
|
|
|
|
|
let index = tabs.value.findIndex((t) => t.id === tab.id) |
|
|
|
let index = tabs.value.findIndex((t) => t.id === tab.id) |
|
|
|
|
|
|
|
|
|
|
|
if (index === -1) { |
|
|
|
if (index === -1) { |
|
|
|
tabs.value.push(tab as TabItem) |
|
|
|
tabs.value.push(tab) |
|
|
|
index = tabs.value.length - 1 |
|
|
|
index = tabs.value.length - 1 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return index |
|
|
|
return index |
|
|
|
} else if ((route.name as string)?.startsWith('nc-projectId-index-index-auth')) { |
|
|
|
} else if (routeName.startsWith('nc-projectId-index-index-auth')) { |
|
|
|
return tabs.value.findIndex((t) => t.type === 'auth') |
|
|
|
return tabs.value.findIndex((t) => t.type === 'auth') |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// by default, it's showing Team & Auth
|
|
|
|
// by default, it's showing Team & Auth
|
|
|
|
return 0 |
|
|
|
return 0 |
|
|
|
}, |
|
|
|
}, |
|
|
@ -53,11 +57,16 @@ export function useTabs() { |
|
|
|
const tab = tabs.value[index] |
|
|
|
const tab = tabs.value[index] |
|
|
|
|
|
|
|
|
|
|
|
if (!tab) return |
|
|
|
if (!tab) return |
|
|
|
|
|
|
|
|
|
|
|
return navigateToTab(tab) |
|
|
|
return navigateToTab(tab) |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
watch(activeTabIndex, (_, old) => { |
|
|
|
|
|
|
|
previousActiveTabIndex.value = old |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
const activeTab = computed(() => tabs.value?.[activeTabIndex.value]) |
|
|
|
const activeTab = computed(() => tabs.value?.[activeTabIndex.value]) |
|
|
|
|
|
|
|
|
|
|
|
const addTab = (tabMeta: TabItem) => { |
|
|
|
const addTab = (tabMeta: TabItem) => { |
|
|
@ -79,15 +88,19 @@ export function useTabs() { |
|
|
|
|
|
|
|
|
|
|
|
const closeTab = async (key: number | Partial<TabItem>) => { |
|
|
|
const closeTab = async (key: number | Partial<TabItem>) => { |
|
|
|
const index = typeof key === 'number' ? key : tabs.value.findIndex(getPredicate(key)) |
|
|
|
const index = typeof key === 'number' ? key : tabs.value.findIndex(getPredicate(key)) |
|
|
|
|
|
|
|
|
|
|
|
if (activeTabIndex.value === index) { |
|
|
|
if (activeTabIndex.value === index) { |
|
|
|
let newTabIndex = index - 1 |
|
|
|
let newTabIndex = index - 1 |
|
|
|
|
|
|
|
|
|
|
|
if (newTabIndex < 0 && tabs.value?.length > 1) newTabIndex = index + 1 |
|
|
|
if (newTabIndex < 0 && tabs.value?.length > 1) newTabIndex = index + 1 |
|
|
|
|
|
|
|
|
|
|
|
if (newTabIndex === -1) { |
|
|
|
if (newTabIndex === -1) { |
|
|
|
await navigateTo(`/${projectType}/${route.params.projectId}`) |
|
|
|
await navigateTo(`/${projectType}/${route.params.projectId}`) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
await navigateToTab(tabs.value?.[newTabIndex]) |
|
|
|
await navigateToTab(tabs.value?.[newTabIndex]) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
tabs.value.splice(index, 1) |
|
|
|
tabs.value.splice(index, 1) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -108,8 +121,9 @@ export function useTabs() { |
|
|
|
|
|
|
|
|
|
|
|
const updateTab = (key: number | Partial<TabItem>, newTabItemProps: Partial<TabItem>) => { |
|
|
|
const updateTab = (key: number | Partial<TabItem>, newTabItemProps: Partial<TabItem>) => { |
|
|
|
const tab = typeof key === 'number' ? tabs.value[key] : tabs.value.find(getPredicate(key)) |
|
|
|
const tab = typeof key === 'number' ? tabs.value[key] : tabs.value.find(getPredicate(key)) |
|
|
|
|
|
|
|
|
|
|
|
if (tab) { |
|
|
|
if (tab) { |
|
|
|
const isActive = tabs.value.indexOf(tab) === activeTabIndex.value |
|
|
|
const isActive = tabs.value.indexOf(tab) === previousActiveTabIndex.value |
|
|
|
|
|
|
|
|
|
|
|
Object.assign(tab, newTabItemProps) |
|
|
|
Object.assign(tab, newTabItemProps) |
|
|
|
|
|
|
|
|
|
|
@ -123,4 +137,14 @@ export function useTabs() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return { tabs, addTab, activeTabIndex, activeTab, clearTabs, closeTab, updateTab } |
|
|
|
return { tabs, addTab, activeTabIndex, activeTab, clearTabs, closeTab, updateTab } |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function useTabs() { |
|
|
|
|
|
|
|
const state = use() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!state) { |
|
|
|
|
|
|
|
return setup() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return state |
|
|
|
} |
|
|
|
} |
|
|
|