mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
4.0 KiB
135 lines
4.0 KiB
2 years ago
|
import type { WritableComputedRef } from '@vue/reactivity'
|
||
2 years ago
|
import { navigateTo, useProject, useRoute, useState } from '#imports'
|
||
2 years ago
|
|
||
2 years ago
|
export enum TabType {
|
||
2 years ago
|
TABLE = 'table',
|
||
|
VIEW = 'view',
|
||
|
AUTH = 'auth',
|
||
|
}
|
||
|
|
||
3 years ago
|
export interface TabItem {
|
||
2 years ago
|
type: TabType
|
||
3 years ago
|
title: string
|
||
2 years ago
|
id?: string
|
||
2 years ago
|
viewTitle?: string
|
||
|
viewId?: string
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
function getPredicate(key: Partial<TabItem>) {
|
||
|
return (tab: TabItem) =>
|
||
|
(!('id' in key) || tab.id === key.id) &&
|
||
2 years ago
|
(!('title' in key) || tab.title === key.title) &&
|
||
|
(!('type' in key) || tab.type === key.type)
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
export function useTabs() {
|
||
2 years ago
|
const tabs = useState<TabItem[]>('tabs', () => [])
|
||
2 years ago
|
|
||
|
const route = useRoute()
|
||
2 years ago
|
const router = useRouter()
|
||
2 years ago
|
const { tables } = useProject()
|
||
|
|
||
2 years ago
|
const projectType = $computed(() => route.params.projectType as string)
|
||
|
|
||
2 years ago
|
const activeTabIndex: WritableComputedRef<number> = computed({
|
||
2 years ago
|
get() {
|
||
2 years ago
|
if ((route.name as string)?.startsWith('projectType-projectId-index-index-type-title-viewTitle') && tables.value?.length) {
|
||
2 years ago
|
const tab: Partial<TabItem> = { type: route.params.type as TabType, title: route.params.title as string }
|
||
2 years ago
|
|
||
|
const id = tables.value?.find((t) => t.title === tab.title)?.id
|
||
|
|
||
2 years ago
|
if (!id) return -1
|
||
|
|
||
2 years ago
|
tab.id = id as string
|
||
2 years ago
|
|
||
2 years ago
|
let index = tabs.value.findIndex((t) => t.id === tab.id)
|
||
2 years ago
|
|
||
2 years ago
|
if (index === -1) {
|
||
|
tabs.value.push(tab as TabItem)
|
||
|
index = tabs.value.length - 1
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return index
|
||
2 years ago
|
} else if ((route.name as string)?.startsWith('nc-projectId-index-index-auth')) {
|
||
2 years ago
|
return tabs.value.findIndex((t) => t.type === 'auth')
|
||
2 years ago
|
}
|
||
2 years ago
|
// by default, it's showing Team & Auth
|
||
|
return 0
|
||
2 years ago
|
},
|
||
|
set(index: number) {
|
||
|
if (index === -1) {
|
||
2 years ago
|
navigateTo(`/${projectType}/${route.params.projectId}`)
|
||
2 years ago
|
} else {
|
||
2 years ago
|
const tab = tabs.value[index]
|
||
2 years ago
|
|
||
2 years ago
|
if (!tab) return
|
||
2 years ago
|
return navigateToTab(tab)
|
||
2 years ago
|
}
|
||
|
},
|
||
|
})
|
||
3 years ago
|
|
||
2 years ago
|
const activeTab = computed(() => tabs.value?.[activeTabIndex.value])
|
||
|
|
||
3 years ago
|
const addTab = (tabMeta: TabItem) => {
|
||
3 years ago
|
const tabIndex = tabs.value.findIndex((tab) => tab.id === tabMeta.id)
|
||
3 years ago
|
// if tab already found make it active
|
||
3 years ago
|
if (tabIndex > -1) {
|
||
2 years ago
|
activeTabIndex.value = tabIndex
|
||
3 years ago
|
}
|
||
|
// if tab not found add it
|
||
|
else {
|
||
|
tabs.value = [...(tabs.value || []), tabMeta]
|
||
2 years ago
|
activeTabIndex.value = tabs.value.length - 1
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|
||
2 years ago
|
|
||
3 years ago
|
const clearTabs = () => {
|
||
|
tabs.value = []
|
||
3 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
const closeTab = async (key: number | Partial<TabItem>) => {
|
||
|
const index = typeof key === 'number' ? key : tabs.value.findIndex(getPredicate(key))
|
||
2 years ago
|
if (activeTabIndex.value === index) {
|
||
2 years ago
|
let newTabIndex = index - 1
|
||
|
if (newTabIndex < 0 && tabs.value?.length > 1) newTabIndex = index + 1
|
||
|
if (newTabIndex === -1) {
|
||
2 years ago
|
await navigateTo(`/${projectType}/${route.params.projectId}`)
|
||
2 years ago
|
} else {
|
||
2 years ago
|
await navigateToTab(tabs.value?.[newTabIndex])
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
tabs.value.splice(index, 1)
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
2 years ago
|
function navigateToTab(tab: TabItem) {
|
||
|
switch (tab.type) {
|
||
|
case TabType.TABLE:
|
||
2 years ago
|
return navigateTo(
|
||
|
`/${projectType}/${route.params.projectId}/table/${tab?.title}${tab.viewTitle ? `/${tab.viewTitle}` : ''}`,
|
||
|
)
|
||
2 years ago
|
case TabType.VIEW:
|
||
2 years ago
|
return navigateTo(
|
||
|
`/${projectType}/${route.params.projectId}/view/${tab?.title}${tab.viewTitle ? `/${tab.viewTitle}` : ''}`,
|
||
|
)
|
||
2 years ago
|
case TabType.AUTH:
|
||
2 years ago
|
return navigateTo(`/${projectType}/${route.params.projectId}/auth`)
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
const updateTab = (key: number | Partial<TabItem>, newTabItemProps: Partial<TabItem>) => {
|
||
|
const tab = typeof key === 'number' ? tabs.value[key] : tabs.value.find(getPredicate(key))
|
||
|
if (tab) {
|
||
2 years ago
|
const isActive = tabs.value.indexOf(tab) === activeTabIndex.value
|
||
2 years ago
|
Object.assign(tab, newTabItemProps)
|
||
2 years ago
|
if (isActive && tab.title)
|
||
|
router.replace({
|
||
|
params: {
|
||
|
title: tab.title,
|
||
|
},
|
||
|
})
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
return { tabs, addTab, activeTabIndex, activeTab, clearTabs, closeTab, updateTab }
|
||
3 years ago
|
}
|