|
| 1 | +const state = { |
| 2 | + visitedViews: [], |
| 3 | + cachedViews: [] |
| 4 | +}; |
| 5 | +const mutations = { |
| 6 | + ADD_VISITED_VIEW: (state, view) => { |
| 7 | + if (state.visitedViews.some(v => v.path === view.path)) return; |
| 8 | + state.visitedViews.push( |
| 9 | + Object.assign({}, view, { |
| 10 | + title: view.meta.title || 'no-name' |
| 11 | + }) |
| 12 | + ); |
| 13 | + }, |
| 14 | + ADD_CACHED_VIEW: (state, view) => { |
| 15 | + if (state.cachedViews.includes(view.name)) return; |
| 16 | + if (view.meta.cacheFlag) { |
| 17 | + state.cachedViews.push(view.name); |
| 18 | + } |
| 19 | + }, |
| 20 | + DEL_VISITED_VIEW: (state, view) => { |
| 21 | + for (const [i, v] of state.visitedViews.entries()) { |
| 22 | + if (v.path === view.path) { |
| 23 | + state.visitedViews.splice(i, 1); |
| 24 | + break; |
| 25 | + } |
| 26 | + } |
| 27 | + }, |
| 28 | + DEL_CACHED_VIEW: (state, view) => { |
| 29 | + const index = state.cachedViews.indexOf(view.name); |
| 30 | + index > -1 && state.cachedViews.splice(index, 1); |
| 31 | + }, |
| 32 | + DEL_OTHERS_VISITED_VIEWS: (state, view) => { |
| 33 | + state.visitedViews = state.visitedViews.filter(v => { |
| 34 | + return v.meta.affix || v.path === view.path; |
| 35 | + }); |
| 36 | + }, |
| 37 | + DEL_OTHERS_CACHED_VIEWS: (state, view) => { |
| 38 | + const index = state.cachedViews.indexOf(view.name); |
| 39 | + if (index > -1) { |
| 40 | + state.cachedViews = state.cachedViews.slice(index, index + 1); |
| 41 | + } else { |
| 42 | + // if index = -1, there is no cached tags |
| 43 | + state.cachedViews = []; |
| 44 | + } |
| 45 | + }, |
| 46 | + DEL_ALL_VISITED_VIEWS: state => { |
| 47 | + // keep affix tags |
| 48 | + state.visitedViews = state.visitedViews.filter(tag => tag.meta.affix); |
| 49 | + }, |
| 50 | + DEL_ALL_CACHED_VIEWS: state => { |
| 51 | + state.cachedViews = []; |
| 52 | + }, |
| 53 | + UPDATE_VISITED_VIEW: (state, view) => { |
| 54 | + for (let v of state.visitedViews) { |
| 55 | + if (v.path === view.path) { |
| 56 | + v = Object.assign(v, view); |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +const actions = { |
| 64 | + addView({dispatch}, view) { |
| 65 | + dispatch('addVisitedView', view); |
| 66 | + dispatch('addCachedView', view); |
| 67 | + }, |
| 68 | + addVisitedView({commit}, view) { |
| 69 | + commit('ADD_VISITED_VIEW', view); |
| 70 | + }, |
| 71 | + addCachedView({commit}, view) { |
| 72 | + commit('ADD_CACHED_VIEW', view); |
| 73 | + }, |
| 74 | + delView({dispatch, state}, view) { |
| 75 | + return new Promise(resolve => { |
| 76 | + dispatch('delVisitedView', view); |
| 77 | + dispatch('delCachedView', view); |
| 78 | + resolve({ |
| 79 | + visitedViews: [...state.visitedViews], |
| 80 | + cachedViews: [...state.cachedViews] |
| 81 | + }); |
| 82 | + }); |
| 83 | + }, |
| 84 | + delVisitedView({commit, state}, view) { |
| 85 | + return new Promise(resolve => { |
| 86 | + commit('DEL_VISITED_VIEW', view); |
| 87 | + resolve([...state.visitedViews]); |
| 88 | + }); |
| 89 | + }, |
| 90 | + delCachedView({commit, state}, view) { |
| 91 | + return new Promise(resolve => { |
| 92 | + commit('DEL_CACHED_VIEW', view); |
| 93 | + resolve([...state.cachedViews]); |
| 94 | + }); |
| 95 | + }, |
| 96 | + delOthersViews({dispatch, state}, view) { |
| 97 | + return new Promise(resolve => { |
| 98 | + dispatch('delOthersVisitedViews', view); |
| 99 | + dispatch('delOthersCachedViews', view); |
| 100 | + resolve({ |
| 101 | + visitedViews: [...state.visitedViews], |
| 102 | + cachedViews: [...state.cachedViews] |
| 103 | + }); |
| 104 | + }); |
| 105 | + }, |
| 106 | + delOthersVisitedViews({commit, state}, view) { |
| 107 | + return new Promise(resolve => { |
| 108 | + commit('DEL_OTHERS_VISITED_VIEWS', view); |
| 109 | + resolve([...state.visitedViews]); |
| 110 | + }); |
| 111 | + }, |
| 112 | + delOthersCachedViews({commit, state}, view) { |
| 113 | + return new Promise(resolve => { |
| 114 | + commit('DEL_OTHERS_CACHED_VIEWS', view); |
| 115 | + resolve([...state.cachedViews]); |
| 116 | + }); |
| 117 | + }, |
| 118 | + delAllViews({dispatch, state}, view) { |
| 119 | + return new Promise(resolve => { |
| 120 | + dispatch('delAllVisitedViews', view); |
| 121 | + dispatch('delAllCachedViews', view); |
| 122 | + resolve({ |
| 123 | + visitedViews: [...state.visitedViews], |
| 124 | + cachedViews: [...state.cachedViews] |
| 125 | + }); |
| 126 | + }); |
| 127 | + }, |
| 128 | + delAllVisitedViews({commit, state}) { |
| 129 | + return new Promise(resolve => { |
| 130 | + commit('DEL_ALL_VISITED_VIEWS'); |
| 131 | + resolve([...state.visitedViews]); |
| 132 | + }); |
| 133 | + }, |
| 134 | + delAllCachedViews({commit, state}) { |
| 135 | + return new Promise(resolve => { |
| 136 | + commit('DEL_ALL_CACHED_VIEWS'); |
| 137 | + resolve([...state.cachedViews]); |
| 138 | + }); |
| 139 | + }, |
| 140 | + updateVisitedView({commit}, view) { |
| 141 | + commit('UPDATE_VISITED_VIEW', view); |
| 142 | + } |
| 143 | +}; |
| 144 | + |
| 145 | +export default { |
| 146 | + namespaced: true, |
| 147 | + state, |
| 148 | + mutations, |
| 149 | + actions |
| 150 | +}; |
0 commit comments