7c819b1ea8e8794999588fd6001f3c3efd9c96bb.svn-base 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.noCache) {
  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. for (const i of state.cachedViews) {
  30. if (i === view.name) {
  31. const index = state.cachedViews.indexOf(i)
  32. state.cachedViews.splice(index, 1)
  33. break
  34. }
  35. }
  36. },
  37. DEL_OTHERS_VISITED_VIEWS: (state, view) => {
  38. state.visitedViews = state.visitedViews.filter(v => {
  39. return v.meta.affix || v.path === view.path
  40. })
  41. },
  42. DEL_OTHERS_CACHED_VIEWS: (state, view) => {
  43. for (const i of state.cachedViews) {
  44. if (i === view.name) {
  45. const index = state.cachedViews.indexOf(i)
  46. state.cachedViews = state.cachedViews.slice(index, index + 1)
  47. break
  48. }
  49. }
  50. },
  51. DEL_ALL_VISITED_VIEWS: state => {
  52. // keep affix tags
  53. const affixTags = state.visitedViews.filter(tag => tag.meta.affix)
  54. state.visitedViews = affixTags
  55. },
  56. DEL_ALL_CACHED_VIEWS: state => {
  57. state.cachedViews = []
  58. },
  59. UPDATE_VISITED_VIEW: (state, view) => {
  60. for (let v of state.visitedViews) {
  61. if (v.path === view.path) {
  62. v = Object.assign(v, view)
  63. break
  64. }
  65. }
  66. }
  67. }
  68. const actions = {
  69. addView({ dispatch }, view) {
  70. dispatch('addVisitedView', view)
  71. dispatch('addCachedView', view)
  72. },
  73. addVisitedView({ commit }, view) {
  74. commit('ADD_VISITED_VIEW', view)
  75. },
  76. addCachedView({ commit }, view) {
  77. commit('ADD_CACHED_VIEW', view)
  78. },
  79. delView({ dispatch, state }, view) {
  80. return new Promise(resolve => {
  81. dispatch('delVisitedView', view)
  82. dispatch('delCachedView', view)
  83. resolve({
  84. visitedViews: [...state.visitedViews],
  85. cachedViews: [...state.cachedViews]
  86. })
  87. })
  88. },
  89. delVisitedView({ commit, state }, view) {
  90. return new Promise(resolve => {
  91. commit('DEL_VISITED_VIEW', view)
  92. resolve([...state.visitedViews])
  93. })
  94. },
  95. delCachedView({ commit, state }, view) {
  96. return new Promise(resolve => {
  97. commit('DEL_CACHED_VIEW', view)
  98. resolve([...state.cachedViews])
  99. })
  100. },
  101. delOthersViews({ dispatch, state }, view) {
  102. return new Promise(resolve => {
  103. dispatch('delOthersVisitedViews', view)
  104. dispatch('delOthersCachedViews', view)
  105. resolve({
  106. visitedViews: [...state.visitedViews],
  107. cachedViews: [...state.cachedViews]
  108. })
  109. })
  110. },
  111. delOthersVisitedViews({ commit, state }, view) {
  112. return new Promise(resolve => {
  113. commit('DEL_OTHERS_VISITED_VIEWS', view)
  114. resolve([...state.visitedViews])
  115. })
  116. },
  117. delOthersCachedViews({ commit, state }, view) {
  118. return new Promise(resolve => {
  119. commit('DEL_OTHERS_CACHED_VIEWS', view)
  120. resolve([...state.cachedViews])
  121. })
  122. },
  123. delAllViews({ dispatch, state }, view) {
  124. return new Promise(resolve => {
  125. dispatch('delAllVisitedViews', view)
  126. dispatch('delAllCachedViews', view)
  127. resolve({
  128. visitedViews: [...state.visitedViews],
  129. cachedViews: [...state.cachedViews]
  130. })
  131. })
  132. },
  133. delAllVisitedViews({ commit, state }) {
  134. return new Promise(resolve => {
  135. commit('DEL_ALL_VISITED_VIEWS')
  136. resolve([...state.visitedViews])
  137. })
  138. },
  139. delAllCachedViews({ commit, state }) {
  140. return new Promise(resolve => {
  141. commit('DEL_ALL_CACHED_VIEWS')
  142. resolve([...state.cachedViews])
  143. })
  144. },
  145. updateVisitedView({ commit }, view) {
  146. commit('UPDATE_VISITED_VIEW', view)
  147. }
  148. }
  149. export default {
  150. namespaced: true,
  151. state,
  152. mutations,
  153. actions
  154. }