2025-08-22 14:01:30 +09:00
|
|
|
import { defineStore } from "pinia";
|
|
|
|
|
|
2025-09-24 16:25:30 +09:00
|
|
|
export interface Tab {
|
2025-08-28 16:59:15 +09:00
|
|
|
key: number; // 1~10
|
2025-08-22 14:01:30 +09:00
|
|
|
label: string;
|
2025-08-28 16:59:15 +09:00
|
|
|
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[],
|
2025-08-28 16:59:15 +09:00
|
|
|
activeTab: 1,
|
2025-08-14 11:00:48 +09:00
|
|
|
}),
|
|
|
|
|
actions: {
|
2025-09-24 16:52:48 +09:00
|
|
|
// 서브메뉴 클릭 시 새 탭 생성
|
2025-09-24 16:35:52 +09:00
|
|
|
async updateActiveTab(sub: {
|
|
|
|
|
label: string;
|
|
|
|
|
to: string;
|
|
|
|
|
componentName: string;
|
|
|
|
|
}) {
|
2025-09-24 16:52:48 +09:00
|
|
|
if (this.tabs.length > 10) {
|
|
|
|
|
alert("탭은 최대 10개까지 열 수 있습니다.");
|
|
|
|
|
return;
|
2025-08-14 11:00:48 +09:00
|
|
|
}
|
2025-09-24 16:52: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
|
|
|
|
2025-09-24 16:52:48 +09:00
|
|
|
// 활성 탭 제거 (HOME 탭 보호)
|
2025-08-22 14:01:30 +09:00
|
|
|
removeTab(key: number) {
|
2025-09-24 16:52:48 +09:00
|
|
|
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) {
|
2025-08-28 16:59:15 +09:00
|
|
|
this.activeTab = this.tabs.length
|
|
|
|
|
? this.tabs[this.tabs.length - 1].key
|
|
|
|
|
: 0;
|
2025-08-22 14:01:30 +09:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2025-09-23 16:01:23 +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}`);
|
2025-08-28 16:59:15 +09:00
|
|
|
},
|
2025-09-23 16:01:23 +09:00
|
|
|
|
|
|
|
|
// 탭 초기화
|
|
|
|
|
resetTabs() {
|
2025-09-24 16:25:30 +09:00
|
|
|
this.tabs = [{ ...defaultTab }];
|
2025-09-23 16:01:23 +09:00
|
|
|
this.activeTab = 1;
|
|
|
|
|
},
|
2025-08-28 16:59:15 +09:00
|
|
|
},
|
|
|
|
|
persist: true,
|
2025-08-22 14:01:30 +09:00
|
|
|
});
|