提交 | 用户 | 时间
|
314507
|
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) { |
|
34 |
if (this.visitedViews.some((v) => v.path === view.path)) return |
|
35 |
if (view.meta?.noTagsView) return |
|
36 |
this.visitedViews.push( |
|
37 |
Object.assign({}, view, { |
|
38 |
title: view.meta?.title || 'no-name' |
|
39 |
}) |
|
40 |
) |
|
41 |
}, |
|
42 |
// 新增缓存 |
|
43 |
addCachedView() { |
|
44 |
const cacheMap: Set<string> = new Set() |
|
45 |
for (const v of this.visitedViews) { |
|
46 |
const item = getRawRoute(v) |
|
47 |
const needCache = !item.meta?.noCache |
|
48 |
if (!needCache) { |
|
49 |
continue |
|
50 |
} |
|
51 |
const name = item.name as string |
|
52 |
cacheMap.add(name) |
|
53 |
} |
|
54 |
if (Array.from(this.cachedViews).sort().toString() === Array.from(cacheMap).sort().toString()) |
|
55 |
return |
|
56 |
this.cachedViews = cacheMap |
|
57 |
}, |
|
58 |
// 删除某个 |
|
59 |
delView(view: RouteLocationNormalizedLoaded) { |
|
60 |
this.delVisitedView(view) |
|
61 |
this.delCachedView() |
|
62 |
}, |
|
63 |
// 删除tag |
|
64 |
delVisitedView(view: RouteLocationNormalizedLoaded) { |
|
65 |
for (const [i, v] of this.visitedViews.entries()) { |
|
66 |
if (v.path === view.path) { |
|
67 |
this.visitedViews.splice(i, 1) |
|
68 |
break |
|
69 |
} |
|
70 |
} |
|
71 |
}, |
|
72 |
// 删除缓存 |
|
73 |
delCachedView() { |
|
74 |
const route = router.currentRoute.value |
|
75 |
const index = findIndex<string>(this.getCachedViews, (v) => v === route.name) |
|
76 |
if (index > -1) { |
|
77 |
this.cachedViews.delete(this.getCachedViews[index]) |
|
78 |
} |
|
79 |
}, |
|
80 |
// 删除所有缓存和tag |
|
81 |
delAllViews() { |
|
82 |
this.delAllVisitedViews() |
|
83 |
this.delCachedView() |
|
84 |
}, |
|
85 |
// 删除所有tag |
|
86 |
delAllVisitedViews() { |
|
87 |
// const affixTags = this.visitedViews.filter((tag) => tag.meta.affix) |
|
88 |
this.visitedViews = [] |
|
89 |
}, |
|
90 |
// 删除其他 |
|
91 |
delOthersViews(view: RouteLocationNormalizedLoaded) { |
|
92 |
this.delOthersVisitedViews(view) |
|
93 |
this.addCachedView() |
|
94 |
}, |
|
95 |
// 删除其他tag |
|
96 |
delOthersVisitedViews(view: RouteLocationNormalizedLoaded) { |
|
97 |
this.visitedViews = this.visitedViews.filter((v) => { |
|
98 |
return v?.meta?.affix || v.path === view.path |
|
99 |
}) |
|
100 |
}, |
|
101 |
// 删除左侧 |
|
102 |
delLeftViews(view: RouteLocationNormalizedLoaded) { |
|
103 |
const index = findIndex<RouteLocationNormalizedLoaded>( |
|
104 |
this.visitedViews, |
|
105 |
(v) => v.path === view.path |
|
106 |
) |
|
107 |
if (index > -1) { |
|
108 |
this.visitedViews = this.visitedViews.filter((v, i) => { |
|
109 |
return v?.meta?.affix || v.path === view.path || i > index |
|
110 |
}) |
|
111 |
this.addCachedView() |
|
112 |
} |
|
113 |
}, |
|
114 |
// 删除右侧 |
|
115 |
delRightViews(view: RouteLocationNormalizedLoaded) { |
|
116 |
const index = findIndex<RouteLocationNormalizedLoaded>( |
|
117 |
this.visitedViews, |
|
118 |
(v) => v.path === view.path |
|
119 |
) |
|
120 |
if (index > -1) { |
|
121 |
this.visitedViews = this.visitedViews.filter((v, i) => { |
|
122 |
return v?.meta?.affix || v.path === view.path || i < index |
|
123 |
}) |
|
124 |
this.addCachedView() |
|
125 |
} |
|
126 |
}, |
|
127 |
updateVisitedView(view: RouteLocationNormalizedLoaded) { |
|
128 |
for (let v of this.visitedViews) { |
|
129 |
if (v.path === view.path) { |
|
130 |
v = Object.assign(v, view) |
|
131 |
break |
|
132 |
} |
|
133 |
} |
|
134 |
} |
|
135 |
}, |
|
136 |
persist: false |
|
137 |
}) |
|
138 |
|
|
139 |
export const useTagsViewStoreWithOut = () => { |
|
140 |
return useTagsViewStore(store) |
|
141 |
} |