hooks.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { ref, unref, watch } from 'vue'
  2. import { useRouter } from 'vue-router'
  3. const findViewIndex = (viewList, view) => {
  4. return viewList.findIndex(v => v.fullPath === view.fullPath)
  5. }
  6. export const useCacheView = (updateCache) => {
  7. const cacheViewList = ref([])
  8. // 添加
  9. const addCacheView = (path) => {
  10. // 判断是否需要添加
  11. if (!cacheViewList.value.includes(path)) {
  12. cacheViewList.value.push(path)
  13. updateCache(cacheViewList.value)
  14. }
  15. }
  16. // 删除
  17. const removeCacheView = (path) => {
  18. const index = cacheViewList.value.indexOf(path)
  19. if (index > -1) {
  20. cacheViewList.value.splice(index, 1)
  21. updateCache(cacheViewList.value)
  22. }
  23. }
  24. // 重置
  25. const resetCacheList = () => {
  26. cacheViewList.value = []
  27. updateCache(cacheViewList.value)
  28. }
  29. return {
  30. cacheViewList,
  31. addCacheView,
  32. removeCacheView,
  33. resetCacheList
  34. }
  35. }
  36. export const useActiveOrder = () => {
  37. const activeOrderList = ref([])
  38. const updateActiveOrder = (view) => {
  39. const activeIndex = findViewIndex(activeOrderList.value, view)
  40. if (activeIndex > -1) activeOrderList.value.splice(activeIndex, 1)
  41. activeOrderList.value.push(view)
  42. }
  43. const removeActiveOrder = (view) => {
  44. const activeIndex = findViewIndex(activeOrderList.value, view)
  45. if (activeIndex > -1) activeOrderList.value.splice(activeIndex, 1)
  46. }
  47. const resetActiveOrderList = () => {
  48. activeOrderList.value = []
  49. }
  50. const getNextActiveOrder = () => {
  51. const activeOrderListLength = activeOrderList.value.length
  52. if (activeOrderListLength > 2) {
  53. return activeOrderList.value[activeOrderListLength - 2]
  54. }
  55. }
  56. return {
  57. activeOrderList,
  58. updateActiveOrder,
  59. removeActiveOrder,
  60. resetActiveOrderList,
  61. getNextActiveOrder
  62. }
  63. }
  64. // activeView - ref({}) 当前激活的路由
  65. export const useOpenedView = (homeView) => {
  66. const router = useRouter()
  67. const activeView = ref({})
  68. const { updateActiveOrder, removeActiveOrder, resetActiveOrderList, getNextActiveOrder } = useActiveOrder()
  69. const isActiveView = (view) => {
  70. return unref(activeView).fullPath === view?.fullPath
  71. }
  72. const setActiveView = (view) => {
  73. activeView.value = view
  74. router.push(view.fullPath)
  75. }
  76. // TODO: 考虑固定打开的ViewList
  77. const openedViewList = ref(homeView ? [homeView] : [])
  78. const getNextActiveView = () => {
  79. return getNextActiveOrder() || homeView
  80. }
  81. watch(activeView, (val) => {
  82. updateActiveOrder(val)
  83. }, { immediate: true })
  84. // 添加一个视图
  85. const addOpenedView = (view) => {
  86. openedViewList.value.push(view)
  87. setActiveView(view)
  88. }
  89. // 修改一个视图
  90. const updateOpenedView = (index, view) => {
  91. openedViewList.value.splice(index, 1, view)
  92. setActiveView(view)
  93. }
  94. // 关闭一个视图
  95. const removeOpenedView = (view) => {
  96. if (isActiveView(view)) setActiveView(getNextActiveView()) // 获取上一个激活的
  97. // 删除openedView时需要同时删除opened和active
  98. const index = findViewIndex(openedViewList.value, view)
  99. if (index > -1)openedViewList.value.splice(index, 1)
  100. removeActiveOrder(view)
  101. }
  102. // 关闭其他视图 view允许为空
  103. const removeOtherView = (view) => {
  104. if (!isActiveView(view)) {
  105. setActiveView(view)
  106. }
  107. openedViewList.value = [unref(homeView), activeView.value]
  108. }
  109. const findViewByOpenedViewList = (fullPath) => {
  110. return openedViewList.value.find(openedView => openedView.fullPath === fullPath)
  111. }
  112. // 关闭所有
  113. const resetOpenedList = () => {
  114. resetActiveOrderList()
  115. if (homeView) {
  116. openedViewList.value = [unref(homeView)]
  117. setActiveView(homeView)
  118. } else {
  119. // 默认方案
  120. openedViewList.value = [{ fullPath: '/', title: '首页' }]
  121. setActiveView({ fullPath: '/', title: '首页' })
  122. }
  123. }
  124. return {
  125. activeView,
  126. openedViewList,
  127. addOpenedView,
  128. updateOpenedView,
  129. removeOpenedView,
  130. removeOtherView,
  131. resetOpenedList,
  132. setActiveView,
  133. findViewByOpenedViewList
  134. }
  135. }