提交 | 用户 | 时间
|
820397
|
1 |
import router from '@/router' |
H |
2 |
import type { RouteLocationNormalizedLoaded } from 'vue-router' |
|
3 |
import { getRawRoute } from '@/utils/routerHelper' |
|
4 |
import { defineStore } from 'pinia' |
|
5 |
import { store } from '../index' |
|
6 |
import { findIndex } from '@/utils' |
|
7 |
|
|
8 |
export interface TagsViewState { |
|
9 |
visitedViews: RouteLocationNormalizedLoaded[] |
|
10 |
cachedViews: Set<string> |
|
11 |
} |
|
12 |
|
|
13 |
export const useTagsViewStore = defineStore('tagsView', { |
|
14 |
state: (): TagsViewState => ({ |
|
15 |
visitedViews: [], |
|
16 |
cachedViews: new Set() |
|
17 |
}), |
|
18 |
getters: { |
|
19 |
getVisitedViews(): RouteLocationNormalizedLoaded[] { |
|
20 |
return this.visitedViews |
|
21 |
}, |
|
22 |
getCachedViews(): string[] { |
|
23 |
return Array.from(this.cachedViews) |
|
24 |
} |
|
25 |
}, |
|
26 |
actions: { |
|
27 |
// 新增缓存和tag |
|
28 |
addView(view: RouteLocationNormalizedLoaded): void { |
|
29 |
this.addVisitedView(view) |
|
30 |
this.addCachedView() |
|
31 |
}, |
|
32 |
// 新增tag |
|
33 |
addVisitedView(view: RouteLocationNormalizedLoaded) { |
3e359e
|
34 |
if (this.visitedViews.some((v) => v.fullPath === view.fullPath)) return |
820397
|
35 |
if (view.meta?.noTagsView) return |
3e359e
|
36 |
const visitedView = Object.assign({}, view, { title: view.meta?.title || 'no-name' }) |
H |
37 |
|
|
38 |
if (visitedView.meta) { |
|
39 |
const titleSuffixList: string[] = [] |
|
40 |
this.visitedViews.forEach((v) => { |
|
41 |
if (v.path === visitedView.path && v.meta?.title === visitedView.meta?.title) { |
|
42 |
titleSuffixList.push(v.meta?.titleSuffix || '1') |
|
43 |
} |
820397
|
44 |
}) |
3e359e
|
45 |
if (titleSuffixList.length) { |
H |
46 |
let titleSuffix = 1 |
|
47 |
while (titleSuffixList.includes(`${titleSuffix}`)) { |
|
48 |
titleSuffix += 1 |
|
49 |
} |
|
50 |
visitedView.meta.titleSuffix = titleSuffix === 1 ? undefined : `${titleSuffix}` |
|
51 |
} |
|
52 |
} |
|
53 |
|
|
54 |
this.visitedViews.push(visitedView) |
820397
|
55 |
}, |
H |
56 |
// 新增缓存 |
|
57 |
addCachedView() { |
|
58 |
const cacheMap: Set<string> = new Set() |
|
59 |
for (const v of this.visitedViews) { |
|
60 |
const item = getRawRoute(v) |
|
61 |
const needCache = !item.meta?.noCache |
|
62 |
if (!needCache) { |
|
63 |
continue |
|
64 |
} |
|
65 |
const name = item.name as string |
|
66 |
cacheMap.add(name) |
|
67 |
} |
|
68 |
if (Array.from(this.cachedViews).sort().toString() === Array.from(cacheMap).sort().toString()) |
|
69 |
return |
|
70 |
this.cachedViews = cacheMap |
|
71 |
}, |
|
72 |
// 删除某个 |
|
73 |
delView(view: RouteLocationNormalizedLoaded) { |
|
74 |
this.delVisitedView(view) |
|
75 |
this.delCachedView() |
|
76 |
}, |
|
77 |
// 删除tag |
|
78 |
delVisitedView(view: RouteLocationNormalizedLoaded) { |
|
79 |
for (const [i, v] of this.visitedViews.entries()) { |
3e359e
|
80 |
if (v.fullPath === view.fullPath) { |
820397
|
81 |
this.visitedViews.splice(i, 1) |
H |
82 |
break |
|
83 |
} |
|
84 |
} |
|
85 |
}, |
|
86 |
// 删除缓存 |
|
87 |
delCachedView() { |
|
88 |
const route = router.currentRoute.value |
|
89 |
const index = findIndex<string>(this.getCachedViews, (v) => v === route.name) |
|
90 |
if (index > -1) { |
|
91 |
this.cachedViews.delete(this.getCachedViews[index]) |
|
92 |
} |
|
93 |
}, |
|
94 |
// 删除所有缓存和tag |
|
95 |
delAllViews() { |
|
96 |
this.delAllVisitedViews() |
|
97 |
this.delCachedView() |
|
98 |
}, |
|
99 |
// 删除所有tag |
|
100 |
delAllVisitedViews() { |
|
101 |
// const affixTags = this.visitedViews.filter((tag) => tag.meta.affix) |
|
102 |
this.visitedViews = [] |
|
103 |
}, |
|
104 |
// 删除其他 |
|
105 |
delOthersViews(view: RouteLocationNormalizedLoaded) { |
|
106 |
this.delOthersVisitedViews(view) |
|
107 |
this.addCachedView() |
|
108 |
}, |
|
109 |
// 删除其他tag |
|
110 |
delOthersVisitedViews(view: RouteLocationNormalizedLoaded) { |
|
111 |
this.visitedViews = this.visitedViews.filter((v) => { |
3e359e
|
112 |
return v?.meta?.affix || v.fullPath === view.fullPath |
820397
|
113 |
}) |
H |
114 |
}, |
|
115 |
// 删除左侧 |
|
116 |
delLeftViews(view: RouteLocationNormalizedLoaded) { |
|
117 |
const index = findIndex<RouteLocationNormalizedLoaded>( |
|
118 |
this.visitedViews, |
3e359e
|
119 |
(v) => v.fullPath === view.fullPath |
820397
|
120 |
) |
H |
121 |
if (index > -1) { |
|
122 |
this.visitedViews = this.visitedViews.filter((v, i) => { |
3e359e
|
123 |
return v?.meta?.affix || v.fullPath === view.fullPath || i > index |
820397
|
124 |
}) |
H |
125 |
this.addCachedView() |
|
126 |
} |
|
127 |
}, |
|
128 |
// 删除右侧 |
|
129 |
delRightViews(view: RouteLocationNormalizedLoaded) { |
|
130 |
const index = findIndex<RouteLocationNormalizedLoaded>( |
|
131 |
this.visitedViews, |
3e359e
|
132 |
(v) => v.fullPath === view.fullPath |
820397
|
133 |
) |
H |
134 |
if (index > -1) { |
|
135 |
this.visitedViews = this.visitedViews.filter((v, i) => { |
3e359e
|
136 |
return v?.meta?.affix || v.fullPath === view.fullPath || i < index |
820397
|
137 |
}) |
H |
138 |
this.addCachedView() |
|
139 |
} |
|
140 |
}, |
|
141 |
updateVisitedView(view: RouteLocationNormalizedLoaded) { |
|
142 |
for (let v of this.visitedViews) { |
3e359e
|
143 |
if (v.fullPath === view.fullPath) { |
820397
|
144 |
v = Object.assign(v, view) |
H |
145 |
break |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
}, |
|
150 |
persist: false |
|
151 |
}) |
|
152 |
|
|
153 |
export const useTagsViewStoreWithOut = () => { |
|
154 |
return useTagsViewStore(store) |
|
155 |
} |