import { defineStore } from 'pinia';
|
import { ref } from 'vue';
|
|
|
export const useTagsViewStore = defineStore('tagsView', () => {
|
const visitedViews = ref([]);
|
const cachedViews = ref([]);
|
|
function addView(view) {
|
addVisitedView(view);
|
addCachedView(view);
|
}
|
|
function addVisitedView(view) {
|
if (visitedViews.value.some(v => v.path === view.path)) return;
|
visitedViews.value.push(
|
Object.assign({}, view, {
|
title: view.meta.title || 'no-name'
|
})
|
);
|
}
|
|
function addCachedView(view) {
|
if (cachedViews.value.includes(view.name)) return;
|
if (!view.meta.noCache) {
|
cachedViews.value.push(view.name);
|
}
|
}
|
|
function delView(view) {
|
delVisitedView(view);
|
delCachedView(view);
|
}
|
|
function delVisitedView(view) {
|
for (const [i, v] of visitedViews.value.entries()) {
|
if (v.path === view.path) {
|
visitedViews.value.splice(i, 1);
|
break;
|
}
|
}
|
}
|
|
function delCachedView(view) {
|
if (view.name) {
|
const index = cachedViews.value.indexOf(view.name);
|
index > -1 && cachedViews.value.splice(index, 1);
|
}
|
}
|
|
function delOthersViews(view) {
|
delOthersVisitedViews(view);
|
delOthersCachedViews(view);
|
}
|
|
function delOthersVisitedViews(view) {
|
visitedViews.value = visitedViews.value.filter(v => {
|
return v.meta.affix || v.path === view.path;
|
});
|
}
|
|
function delOthersCachedViews(view) {
|
const index = cachedViews.value.indexOf(view.name);
|
if (index > -1) {
|
cachedViews.value = cachedViews.value.slice(index, index + 1);
|
} else {
|
// if index = -1, there is no cached tags
|
cachedViews.value = [];
|
}
|
}
|
|
function delAllViews() {
|
delAllVisitedViews();
|
delAllCachedViews();
|
}
|
|
function delAllVisitedViews() {
|
const affixTags = visitedViews.value.filter(tag => tag.meta.affix);
|
visitedViews.value = affixTags;
|
}
|
|
function delAllCachedViews() {
|
cachedViews.value = [];
|
}
|
|
function updateTitle(view) {
|
for (let v of visitedViews.value) {
|
if (v.path === view.path) {
|
v = Object.assign(v, view);
|
break;
|
}
|
}
|
}
|
|
return { visitedViews, cachedViews, addView, addVisitedView, addCachedView, delView, delOthersViews, delAllViews, updateTitle };
|
});
|