|
import {defineStore} from "pinia";
|
|
export const useTagsViewStore = defineStore('tagsView', {
|
state: () => ({
|
visitedViews: [] as any[], // 访问过的视图
|
cachedViews: [] as string[], // 缓存的视图
|
}),
|
|
actions: {
|
// 添加访问过的视图
|
addVisitedView(view: any) {
|
if (this.visitedViews.some(v => v.path === view.path)) return;
|
this.visitedViews.push({
|
...view,
|
title: view.meta.title || 'no-name',
|
});
|
},
|
delView( view:any) {
|
return new Promise(resolve => {
|
this.delVisitedView(view);
|
this.delCachedView(view);
|
resolve({
|
visitedViews: [...this.visitedViews],
|
cachedViews: [...this.cachedViews]
|
})
|
})
|
},
|
// 添加缓存视图
|
addCachedView(view: any) {
|
if (this.cachedViews.includes(view.name)) return;
|
if (!view.meta.noCache) {
|
this.cachedViews.push(view.name);
|
}
|
},
|
|
// 删除访问过的视图
|
delVisitedView(view: any) {
|
this.visitedViews = this.visitedViews.filter(v => v.path !== view.path);
|
},
|
|
// 删除缓存视图
|
delCachedView(view: any) {
|
return new Promise((resolve, reject) => {
|
const index = this.cachedViews.indexOf(view.name);
|
if (index > -1) {
|
this.cachedViews.splice(index, 1);
|
}
|
return resolve(index);
|
})
|
},
|
delAllViews( ) {
|
return new Promise(resolve => {
|
this.delAllVisitedViews()
|
this.delAllCachedViews()
|
resolve({
|
visitedViews: [...this.visitedViews],
|
cachedViews: [...this.cachedViews]
|
})
|
})
|
},
|
delOthersViews( view:any) {
|
return new Promise(resolve => {
|
this.delOthersVisitedViews( view)
|
this.delOthersCachedViews( view)
|
resolve({
|
visitedViews: [...this.visitedViews],
|
cachedViews: [...this.cachedViews]
|
})
|
})
|
},
|
// 删除其他访问过的视图
|
delOthersVisitedViews(view: any) {
|
this.visitedViews = this.visitedViews.filter(v => v.meta.affix || v.path === view.path);
|
},
|
|
// 删除其他缓存视图
|
delOthersCachedViews(view: any) {
|
const index = this.cachedViews.indexOf(view.name);
|
if (index > -1) {
|
this.cachedViews = this.cachedViews.slice(index, index + 1);
|
} else {
|
this.cachedViews = [];
|
}
|
},
|
|
// 删除所有访问过的视图
|
delAllVisitedViews() {
|
this.visitedViews = this.visitedViews.filter(tag => tag.meta.affix);
|
},
|
|
// 删除所有缓存视图
|
delAllCachedViews() {
|
this.cachedViews = [];
|
},
|
|
// 更新访问过的视图
|
updateVisitedView(view: any) {
|
for (const v of this.visitedViews) {
|
if (v.path === view.path) {
|
Object.assign(v, view);
|
break;
|
}
|
}
|
},
|
|
// 删除右侧标签
|
delRightViews(view: any) {
|
const index = this.visitedViews.findIndex(v => v.path === view.path);
|
if (index === -1) return;
|
|
this.visitedViews = this.visitedViews.filter((item, idx) => {
|
if (idx <= index || (item.meta && item.meta.affix)) {
|
return true;
|
}
|
const i = this.cachedViews.indexOf(item.name);
|
if (i > -1) {
|
this.cachedViews.splice(i, 1);
|
}
|
return false;
|
});
|
},
|
|
// 删除左侧标签
|
delLeftViews(view: any) {
|
const index = this.visitedViews.findIndex(v => v.path === view.path);
|
if (index === -1) return;
|
|
this.visitedViews = this.visitedViews.filter((item, idx) => {
|
if (idx >= index || (item.meta && item.meta.affix)) {
|
return true;
|
}
|
const i = this.cachedViews.indexOf(item.name);
|
if (i > -1) {
|
this.cachedViews.splice(i, 1);
|
}
|
return false;
|
});
|
},
|
},
|
});
|