import { defineStore } from "pinia";
|
|
export const useTagsViewStore = defineStore("tagsView", {
|
state() {
|
return { visitedViews: [], cachedViews: [] };
|
},
|
|
actions: {
|
ADD_VISITED_VIEW(view) {
|
if (this.visitedViews.some((v) => v.path === view.path)) return;
|
this.visitedViews.push(
|
Object.assign({}, view, {
|
title: view.meta.title || "no-name",
|
})
|
);
|
},
|
ADD_CACHED_VIEW(view) {
|
if (this.cachedViews.includes(view.name)) return;
|
if (!view.meta.noCache) {
|
this.cachedViews.push(view.name);
|
}
|
},
|
|
DEL_VISITED_VIEW(view) {
|
for (const [i, v] of this.visitedViews.entries()) {
|
if (v.path === view.path) {
|
this.visitedViews.splice(i, 1);
|
break;
|
}
|
}
|
},
|
DEL_CACHED_VIEW(view) {
|
const index = this.cachedViews.indexOf(view.name);
|
index > -1 && this.cachedViews.splice(index, 1);
|
},
|
|
DEL_OTHERS_VISITED_VIEWS(view) {
|
this.visitedViews = this.visitedViews.filter((v) => {
|
return v.meta.affix || v.path === view.path;
|
});
|
},
|
DEL_OTHERS_CACHED_VIEWS(view) {
|
const index = this.cachedViews.indexOf(view.name);
|
if (index > -1) {
|
let list = this.cachedViews.slice(index, index + 1);
|
this.cachedViews.length = 0;
|
this.cachedViews.push(...list);
|
} else {
|
// if index = -1, there is no cached tags
|
this.cachedViews.length = 0;
|
}
|
},
|
|
DEL_ALL_VISITED_VIEWS() {
|
// keep affix tags
|
const affixTags = this.visitedViews.filter((tag) => tag.meta.affix);
|
this.visitedViews.length = 0;
|
this.visitedViews.push(...affixTags);
|
},
|
DEL_ALL_CACHED_VIEWS() {
|
this.cachedViews.length = 0;
|
},
|
|
UPDATE_VISITED_VIEW(view) {
|
for (let v of this.visitedViews) {
|
if (v.path === view.path) {
|
v = Object.assign(v, view);
|
break;
|
}
|
}
|
},
|
UPDATE_VISITED_VIEW_NAME(view) {
|
for (let v of this.visitedViews) {
|
if (v.name === view.name) {
|
v = Object.assign(v, view);
|
break;
|
}
|
}
|
},
|
|
addView(view) {
|
this.addVisitedView(view);
|
this.addCachedView(view);
|
},
|
addVisitedView(view) {
|
this.ADD_VISITED_VIEW(view);
|
},
|
addCachedView(view) {
|
this.ADD_CACHED_VIEW(view);
|
},
|
|
delView(view) {
|
return new Promise((resolve) => {
|
this.delVisitedView(view);
|
this.delCachedView(view);
|
resolve({
|
visitedViews: [...this.visitedViews],
|
cachedViews: [...this.cachedViews],
|
});
|
});
|
},
|
delVisitedView(view) {
|
return new Promise((resolve) => {
|
this.DEL_VISITED_VIEW(view);
|
resolve([...this.visitedViews]);
|
});
|
},
|
delCachedView(view) {
|
return new Promise((resolve) => {
|
this.DEL_CACHED_VIEW(view);
|
resolve([...this.cachedViews]);
|
});
|
},
|
|
delOthersViews(view) {
|
return new Promise((resolve) => {
|
this.delOthersVisitedViews(view);
|
this.delOthersCachedViews(view);
|
resolve({
|
visitedViews: [...this.visitedViews],
|
cachedViews: [...this.cachedViews],
|
});
|
});
|
},
|
delOthersVisitedViews(view) {
|
return new Promise((resolve) => {
|
this.DEL_OTHERS_VISITED_VIEWS(view);
|
resolve([...this.visitedViews]);
|
});
|
},
|
delOthersCachedViews(view) {
|
return new Promise((resolve) => {
|
this.DEL_OTHERS_CACHED_VIEWS(view);
|
resolve([...this.cachedViews]);
|
});
|
},
|
|
delAllViews(view) {
|
return new Promise((resolve) => {
|
this.delAllVisitedViews(view);
|
this.delAllCachedViews(view);
|
resolve({
|
visitedViews: [...this.visitedViews],
|
cachedViews: [...this.cachedViews],
|
});
|
});
|
},
|
delAllVisitedViews() {
|
return new Promise((resolve) => {
|
this.DEL_ALL_VISITED_VIEWS();
|
resolve([...this.visitedViews]);
|
});
|
},
|
delAllCachedViews() {
|
return new Promise((resolve) => {
|
this.DEL_ALL_CACHED_VIEWS();
|
resolve([...this.cachedViews]);
|
});
|
},
|
updateVisitedView(view) {
|
this.UPDATE_VISITED_VIEW(view);
|
},
|
updateVisitedViewName(view) {
|
this.UPDATE_VISITED_VIEW_NAME(view);
|
},
|
},
|
});
|