he wei
2025-04-23 b9bd29a1a81f6f7de479e3cc3fdfe3d85fc660bf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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 updateVisitedView(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, updateVisitedView, delAllVisitedViews, delAllCachedViews, delCachedView };
});