cloudroam
2024-12-31 2eeea7a6431f0b5fb25b338e2512c48deab8652e
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
const state = () => ({
  visitedViews: [],
})
 
const mutations = {
  ADD_VISITED_VIEW: (state, view) => {
    const title =
      view.matched.slice(-1)[0]?.instances?.default?.$metaInfo?.title
    if (title) {
      const sameIndex = state.visitedViews.findIndex(
        (v) => v.name === view.name
      )
      if (sameIndex !== -1) {
        if (state.visitedViews[sameIndex].fullPath !== view.fullPath) {
          state.visitedViews.splice(sameIndex, 1, { ...view, title })
        }
      } else {
        state.visitedViews.push(
          Object.assign({}, view, {
            title,
          })
        )
      }
    }
  },
 
  DEL_VISITED_VIEW: (state, view) => {
    for (const [i, v] of state.visitedViews.entries()) {
      if (v.name === view.name) {
        state.visitedViews.splice(i, 1)
        break
      }
    }
  },
 
  DEL_OTHERS_VISITED_VIEWS: (state, view) => {
    state.visitedViews = state.visitedViews.filter((v) => {
      return v.name === view.name
    })
  },
 
  DEL_RIGHT_VISITED_VIEWS: (state, view) => {
    const selectedIndex = state.visitedViews.findIndex(
      (item) => item.name === view.name
    )
    if (selectedIndex !== -1) {
      state.visitedViews.splice(selectedIndex + 1)
    }
  },
}
 
const actions = {
  addVisitedView({ commit }, view) {
    commit('ADD_VISITED_VIEW', view)
  },
 
  delVisitedView({ commit, state }, view) {
    return new Promise((resolve) => {
      commit('DEL_VISITED_VIEW', view)
      resolve([...state.visitedViews])
    })
  },
 
  delOthersVisitedViews({ commit, state }, view) {
    return new Promise((resolve) => {
      commit('DEL_OTHERS_VISITED_VIEWS', view)
      resolve([...state.visitedViews])
    })
  },
 
  delRightVisitedViews({ commit }, view) {
    return new Promise((resolve) => {
      commit('DEL_RIGHT_VISITED_VIEWS', view)
      resolve([...state.visitedViews])
    })
  },
}
 
export default {
  namespaced: true,
  state,
  mutations,
  actions,
}