提交 | 用户 | 时间
|
fd2207
|
1 |
const state = { |
懒 |
2 |
visitedViews: [], |
|
3 |
cachedViews: [], |
|
4 |
iframeViews: [] |
|
5 |
} |
|
6 |
|
|
7 |
const mutations = { |
|
8 |
ADD_IFRAME_VIEW: (state, view) => { |
|
9 |
if (state.iframeViews.some(v => v.path === view.path)) return |
|
10 |
state.iframeViews.push( |
|
11 |
Object.assign({}, view, { |
|
12 |
title: view.meta.title || 'no-name' |
|
13 |
}) |
|
14 |
) |
|
15 |
}, |
|
16 |
ADD_VISITED_VIEW: (state, view) => { |
|
17 |
if (state.visitedViews.some(v => v.path === view.path)) return |
|
18 |
state.visitedViews.push( |
|
19 |
Object.assign({}, view, { |
|
20 |
title: view.meta.title || 'no-name' |
|
21 |
}) |
|
22 |
) |
|
23 |
}, |
|
24 |
ADD_CACHED_VIEW: (state, view) => { |
|
25 |
if (state.cachedViews.includes(view.name)) return |
|
26 |
if (view.meta && !view.meta.noCache) { |
|
27 |
state.cachedViews.push(view.name) |
|
28 |
} |
|
29 |
}, |
|
30 |
DEL_VISITED_VIEW: (state, view) => { |
|
31 |
for (const [i, v] of state.visitedViews.entries()) { |
|
32 |
if (v.path === view.path) { |
|
33 |
state.visitedViews.splice(i, 1) |
|
34 |
break |
|
35 |
} |
|
36 |
} |
|
37 |
state.iframeViews = state.iframeViews.filter(item => item.path !== view.path) |
|
38 |
}, |
|
39 |
DEL_IFRAME_VIEW: (state, view) => { |
|
40 |
state.iframeViews = state.iframeViews.filter(item => item.path !== view.path) |
|
41 |
}, |
|
42 |
DEL_CACHED_VIEW: (state, view) => { |
|
43 |
const index = state.cachedViews.indexOf(view.name) |
|
44 |
index > -1 && state.cachedViews.splice(index, 1) |
|
45 |
}, |
|
46 |
|
|
47 |
DEL_OTHERS_VISITED_VIEWS: (state, view) => { |
|
48 |
state.visitedViews = state.visitedViews.filter(v => { |
|
49 |
return v.meta.affix || v.path === view.path |
|
50 |
}) |
|
51 |
state.iframeViews = state.iframeViews.filter(item => item.path === view.path) |
|
52 |
}, |
|
53 |
DEL_OTHERS_CACHED_VIEWS: (state, view) => { |
|
54 |
const index = state.cachedViews.indexOf(view.name) |
|
55 |
if (index > -1) { |
|
56 |
state.cachedViews = state.cachedViews.slice(index, index + 1) |
|
57 |
} else { |
|
58 |
state.cachedViews = [] |
|
59 |
} |
|
60 |
}, |
|
61 |
DEL_ALL_VISITED_VIEWS: state => { |
|
62 |
// keep affix tags |
|
63 |
const affixTags = state.visitedViews.filter(tag => tag.meta.affix) |
|
64 |
state.visitedViews = affixTags |
|
65 |
state.iframeViews = [] |
|
66 |
}, |
|
67 |
DEL_ALL_CACHED_VIEWS: state => { |
|
68 |
state.cachedViews = [] |
|
69 |
}, |
|
70 |
UPDATE_VISITED_VIEW: (state, view) => { |
|
71 |
for (let v of state.visitedViews) { |
|
72 |
if (v.path === view.path) { |
|
73 |
v = Object.assign(v, view) |
|
74 |
break |
|
75 |
} |
|
76 |
} |
|
77 |
}, |
|
78 |
DEL_RIGHT_VIEWS: (state, view) => { |
|
79 |
const index = state.visitedViews.findIndex(v => v.path === view.path) |
|
80 |
if (index === -1) { |
|
81 |
return |
|
82 |
} |
|
83 |
state.visitedViews = state.visitedViews.filter((item, idx) => { |
|
84 |
if (idx <= index || (item.meta && item.meta.affix)) { |
|
85 |
return true |
|
86 |
} |
|
87 |
const i = state.cachedViews.indexOf(item.name) |
|
88 |
if (i > -1) { |
|
89 |
state.cachedViews.splice(i, 1) |
|
90 |
} |
|
91 |
if(item.meta.link) { |
|
92 |
const fi = state.iframeViews.findIndex(v => v.path === item.path) |
|
93 |
state.iframeViews.splice(fi, 1) |
|
94 |
} |
|
95 |
return false |
|
96 |
}) |
|
97 |
}, |
|
98 |
DEL_LEFT_VIEWS: (state, view) => { |
|
99 |
const index = state.visitedViews.findIndex(v => v.path === view.path) |
|
100 |
if (index === -1) { |
|
101 |
return |
|
102 |
} |
|
103 |
state.visitedViews = state.visitedViews.filter((item, idx) => { |
|
104 |
if (idx >= index || (item.meta && item.meta.affix)) { |
|
105 |
return true |
|
106 |
} |
|
107 |
const i = state.cachedViews.indexOf(item.name) |
|
108 |
if (i > -1) { |
|
109 |
state.cachedViews.splice(i, 1) |
|
110 |
} |
|
111 |
if(item.meta.link) { |
|
112 |
const fi = state.iframeViews.findIndex(v => v.path === item.path) |
|
113 |
state.iframeViews.splice(fi, 1) |
|
114 |
} |
|
115 |
return false |
|
116 |
}) |
|
117 |
} |
|
118 |
} |
|
119 |
|
|
120 |
const actions = { |
|
121 |
addView({ dispatch }, view) { |
|
122 |
dispatch('addVisitedView', view) |
|
123 |
dispatch('addCachedView', view) |
|
124 |
}, |
|
125 |
addIframeView({ commit }, view) { |
|
126 |
commit('ADD_IFRAME_VIEW', view) |
|
127 |
}, |
|
128 |
addVisitedView({ commit }, view) { |
|
129 |
commit('ADD_VISITED_VIEW', view) |
|
130 |
}, |
|
131 |
addCachedView({ commit }, view) { |
|
132 |
commit('ADD_CACHED_VIEW', view) |
|
133 |
}, |
|
134 |
delView({ dispatch, state }, view) { |
|
135 |
return new Promise(resolve => { |
|
136 |
dispatch('delVisitedView', view) |
|
137 |
dispatch('delCachedView', view) |
|
138 |
resolve({ |
|
139 |
visitedViews: [...state.visitedViews], |
|
140 |
cachedViews: [...state.cachedViews] |
|
141 |
}) |
|
142 |
}) |
|
143 |
}, |
|
144 |
delVisitedView({ commit, state }, view) { |
|
145 |
return new Promise(resolve => { |
|
146 |
commit('DEL_VISITED_VIEW', view) |
|
147 |
resolve([...state.visitedViews]) |
|
148 |
}) |
|
149 |
}, |
|
150 |
delIframeView({ commit, state }, view) { |
|
151 |
return new Promise(resolve => { |
|
152 |
commit('DEL_IFRAME_VIEW', view) |
|
153 |
resolve([...state.iframeViews]) |
|
154 |
}) |
|
155 |
}, |
|
156 |
delCachedView({ commit, state }, view) { |
|
157 |
return new Promise(resolve => { |
|
158 |
commit('DEL_CACHED_VIEW', view) |
|
159 |
resolve([...state.cachedViews]) |
|
160 |
}) |
|
161 |
}, |
|
162 |
delOthersViews({ dispatch, state }, view) { |
|
163 |
return new Promise(resolve => { |
|
164 |
dispatch('delOthersVisitedViews', view) |
|
165 |
dispatch('delOthersCachedViews', view) |
|
166 |
resolve({ |
|
167 |
visitedViews: [...state.visitedViews], |
|
168 |
cachedViews: [...state.cachedViews] |
|
169 |
}) |
|
170 |
}) |
|
171 |
}, |
|
172 |
delOthersVisitedViews({ commit, state }, view) { |
|
173 |
return new Promise(resolve => { |
|
174 |
commit('DEL_OTHERS_VISITED_VIEWS', view) |
|
175 |
resolve([...state.visitedViews]) |
|
176 |
}) |
|
177 |
}, |
|
178 |
delOthersCachedViews({ commit, state }, view) { |
|
179 |
return new Promise(resolve => { |
|
180 |
commit('DEL_OTHERS_CACHED_VIEWS', view) |
|
181 |
resolve([...state.cachedViews]) |
|
182 |
}) |
|
183 |
}, |
|
184 |
delAllViews({ dispatch, state }, view) { |
|
185 |
return new Promise(resolve => { |
|
186 |
dispatch('delAllVisitedViews', view) |
|
187 |
dispatch('delAllCachedViews', view) |
|
188 |
resolve({ |
|
189 |
visitedViews: [...state.visitedViews], |
|
190 |
cachedViews: [...state.cachedViews] |
|
191 |
}) |
|
192 |
}) |
|
193 |
}, |
|
194 |
delAllVisitedViews({ commit, state }) { |
|
195 |
return new Promise(resolve => { |
|
196 |
commit('DEL_ALL_VISITED_VIEWS') |
|
197 |
resolve([...state.visitedViews]) |
|
198 |
}) |
|
199 |
}, |
|
200 |
delAllCachedViews({ commit, state }) { |
|
201 |
return new Promise(resolve => { |
|
202 |
commit('DEL_ALL_CACHED_VIEWS') |
|
203 |
resolve([...state.cachedViews]) |
|
204 |
}) |
|
205 |
}, |
|
206 |
updateVisitedView({ commit }, view) { |
|
207 |
commit('UPDATE_VISITED_VIEW', view) |
|
208 |
}, |
|
209 |
delRightTags({ commit }, view) { |
|
210 |
return new Promise(resolve => { |
|
211 |
commit('DEL_RIGHT_VIEWS', view) |
|
212 |
resolve([...state.visitedViews]) |
|
213 |
}) |
|
214 |
}, |
|
215 |
delLeftTags({ commit }, view) { |
|
216 |
return new Promise(resolve => { |
|
217 |
commit('DEL_LEFT_VIEWS', view) |
|
218 |
resolve([...state.visitedViews]) |
|
219 |
}) |
|
220 |
}, |
|
221 |
} |
|
222 |
|
|
223 |
export default { |
|
224 |
namespaced: true, |
|
225 |
state, |
|
226 |
mutations, |
|
227 |
actions |
|
228 |
} |