Files
bio_frontend/stores/tab.ts

81 lines
2.1 KiB
TypeScript
Raw Normal View History

2025-08-22 14:01:30 +09:00
import { defineStore } from "pinia";
export interface Tab {
key: number; // 1~10
2025-08-22 14:01:30 +09:00
label: string;
to: string; // 페이지 라우트
2025-08-22 14:01:30 +09:00
componentName: string;
2025-08-14 11:00:48 +09:00
}
2025-08-22 14:01:30 +09:00
const defaultTab = { key: 1, label: "홈", to: "/", componentName: "home" };
export const useTabsStore = defineStore("tabs", {
2025-08-14 11:00:48 +09:00
state: () => ({
2025-08-22 14:01:30 +09:00
tabs: [defaultTab] as Tab[],
activeTab: 1,
2025-08-14 11:00:48 +09:00
}),
actions: {
2025-09-24 16:35:52 +09:00
async updateActiveTab(sub: {
label: string;
to: string;
componentName: string;
}) {
// 이미 동일한 페이지가 열려있는지 확인
const existingTab = this.tabs.find(
tab => tab.to === sub.to && tab.componentName === sub.componentName
);
if (existingTab) {
// 이미 동일한 페이지가 열려있으면 해당 탭으로 이동
this.activeTab = existingTab.key;
await navigateTo(`/${existingTab.key}${existingTab.to}`);
return;
}
if (this.tabs.length > 10) {
alert("탭은 최대 10개까지 열 수 있습니다.");
return;
2025-08-14 11:00:48 +09:00
}
// 빈 key 찾기
let newKey = 1;
while (this.tabs.find(t => t.key === newKey)) newKey++;
this.tabs.push({
key: newKey,
label: sub.label,
to: sub.to,
componentName: sub.componentName,
});
this.activeTab = newKey;
await navigateTo(`/${newKey}${sub.to}`);
2025-08-14 11:00:48 +09:00
},
2025-08-22 14:01:30 +09:00
// 활성 탭 제거 (HOME 탭 보호)
2025-08-22 14:01:30 +09:00
removeTab(key: number) {
if (key === 1) return; // HOME 탭은 제거 금지
2025-08-22 14:01:30 +09:00
this.tabs = this.tabs.filter(t => t.key !== key);
if (this.activeTab === key) {
this.activeTab = this.tabs.length
? this.tabs[this.tabs.length - 1].key
: 0;
2025-08-22 14:01:30 +09:00
}
},
// 활성 탭 변경
2025-09-24 16:35:52 +09:00
async setActiveTab(key: number) {
2025-08-22 14:01:30 +09:00
this.activeTab = key;
const tab = this.tabs.find(t => t.key === this.activeTab);
2025-09-24 16:35:52 +09:00
await navigateTo(`/${tab?.key}${tab?.to}`);
},
// 탭 초기화
resetTabs() {
this.tabs = [{ ...defaultTab }];
this.activeTab = 1;
},
},
persist: true,
2025-08-22 14:01:30 +09:00
});