GlobalTabs.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <script lang="ts" setup>
  2. import type { TabPaneName } from 'element-plus'
  3. import config from '@/config/defaultSetting'
  4. import { useMenuStore } from '@/stores/menu'
  5. interface Tab {
  6. name: string
  7. fullPath: string
  8. params: object
  9. query: object
  10. title: string
  11. }
  12. const router = useRouter()
  13. const route = useRoute()
  14. const routeToTab = (route: any) => {
  15. return (
  16. route && {
  17. name: route.name,
  18. fullPath: route.fullPath || route.path,
  19. params: route.params,
  20. query: route.query,
  21. title: route.meta?.title
  22. }
  23. )
  24. }
  25. const defaultTabs = [routeToTab(router.getRoutes().find(item => item.name === config.homeRouteName))]
  26. route.name !== config.homeRouteName && defaultTabs.push(routeToTab(route))
  27. const tabs = useStorage<any[]>('globalTabs', defaultTabs, sessionStorage)
  28. const activeValue = ref(route.fullPath)
  29. const activeIndex = ref(0)
  30. const findActiveIndex = (fullPath: string | number) => {
  31. return tabs.value.findIndex(item => item.fullPath === fullPath)
  32. }
  33. const setTab = (tab: Tab) => {
  34. if (tab.name === 'login' || tabs.value.find(item => item.name === tab.name)) {
  35. return
  36. }
  37. tabs.value.push(tab)
  38. }
  39. const menuStore = useMenuStore()
  40. const activeMenu = ref({})
  41. const findActiveMenu = (path: any) => {
  42. const findDeep = (list: any): boolean => {
  43. let flag = false
  44. for (let index = 0; index < list.length; index++) {
  45. const element = list[index]
  46. if (element.path === path) {
  47. flag = true
  48. break
  49. }
  50. if (element.children?.length) {
  51. flag = findDeep(element.children)
  52. }
  53. }
  54. return flag
  55. }
  56. for (let index = 0; index < menuStore.allMenuList.length; index++) {
  57. const element = menuStore.allMenuList[index]
  58. if (element.path === path) {
  59. activeMenu.value = element
  60. break
  61. }
  62. if (element.children?.length && findDeep(element.children)) {
  63. activeMenu.value = element
  64. break
  65. }
  66. }
  67. }
  68. const changeTab = (name: TabPaneName) => {
  69. activeIndex.value = findActiveIndex(name)
  70. const menu = tabs.value.find(item => item.fullPath === name)
  71. router.push(menu)
  72. menuStore.activeRoutePath = name
  73. findActiveMenu(name)
  74. menuStore.setActiveTopMenu(activeMenu.value)
  75. }
  76. const removeTab = (name: TabPaneName) => {
  77. const index = findActiveIndex(name)
  78. activeIndex.value = index < tabs.value.length - 1 ? index + 1 : index - 1
  79. router.push(tabs.value[activeIndex.value])
  80. tabs.value.splice(index, 1)
  81. }
  82. watch(
  83. () => route,
  84. to => {
  85. setTab(routeToTab(to))
  86. activeValue.value = to.fullPath
  87. activeIndex.value = findActiveIndex(to.fullPath)
  88. },
  89. {
  90. deep: true
  91. }
  92. )
  93. // 右键菜单相关
  94. const contextMenuVisible = ref(false)
  95. const left = ref(0)
  96. const top = ref(0)
  97. const contextMenuIndex = ref(0)
  98. const contextMenuItem = computed(() => tabs.value[contextMenuIndex.value]) as any
  99. const findContextMenuItemIndex = (fullPath: string) => tabs.value.findIndex(item => item.fullPath === fullPath)
  100. const openContextMenu = (e: any) => {
  101. const id = e.target?.offsetParent?.id || e.target?.id
  102. if (id) {
  103. const fullPath = id.split('tab-')[1]
  104. contextMenuIndex.value = findContextMenuItemIndex(fullPath)
  105. left.value = e.clientX + 1
  106. top.value = e.clientY + 1
  107. contextMenuVisible.value = true
  108. }
  109. }
  110. const closeContextMenu = () => {
  111. contextMenuVisible.value = false
  112. }
  113. const closeAll = () => {
  114. router.push(tabs.value[0])
  115. tabs.value.splice(1)
  116. closeContextMenu()
  117. }
  118. const closeLeft = () => {
  119. if (activeIndex.value < contextMenuIndex.value) {
  120. router.push(contextMenuItem.value)
  121. }
  122. tabs.value.splice(1, contextMenuIndex.value - 1)
  123. closeContextMenu()
  124. }
  125. const closeRight = () => {
  126. if (activeIndex.value > contextMenuIndex.value) {
  127. router.push(contextMenuItem.value)
  128. }
  129. tabs.value.splice(contextMenuIndex.value + 1)
  130. closeContextMenu()
  131. }
  132. const closeOther = () => {
  133. router.push(contextMenuItem.value)
  134. if (contextMenuIndex.value) {
  135. tabs.value = [tabs.value[0], contextMenuItem.value]
  136. } else {
  137. tabs.value = [tabs.value[0]]
  138. }
  139. contextMenuVisible.value = false
  140. }
  141. watch(contextMenuVisible, val => {
  142. if (val) {
  143. document.body.addEventListener('click', closeContextMenu)
  144. } else {
  145. document.body.removeEventListener('click', closeContextMenu)
  146. }
  147. })
  148. // 刷新页面
  149. const refresh = () => {
  150. router.go(0)
  151. }
  152. </script>
  153. <template>
  154. <div class="bg-white" style="border-top: 1px solid var(--el-border-color-light)">
  155. <el-tabs
  156. v-model="activeValue"
  157. type="card"
  158. @tab-change="changeTab"
  159. @tab-remove="removeTab"
  160. @contextmenu.prevent="openContextMenu($event)"
  161. @dblclick="refresh"
  162. >
  163. <el-tab-pane
  164. v-for="item in tabs"
  165. :key="item.name"
  166. :label="item.title"
  167. :name="item.fullPath"
  168. :tab="item"
  169. :closable="item.name !== config.homeRouteName"
  170. >
  171. </el-tab-pane>
  172. </el-tabs>
  173. <!--自定义右键菜单html代码-->
  174. <ul v-show="contextMenuVisible" :style="{ left: left + 'px', top: top + 'px' }" class="context-menu">
  175. <li @click="closeAll">关闭所有</li>
  176. <li @click="closeLeft">关闭左侧</li>
  177. <li @click="closeRight">关闭右侧</li>
  178. <li @click="closeOther">关闭其他</li>
  179. </ul>
  180. </div>
  181. </template>
  182. <style lang="scss" scoped>
  183. :deep(.el-tabs__header) {
  184. margin: 0;
  185. // border-top: 1px solid var(--el-border-color-light);
  186. }
  187. :deep(.el-tabs__nav) {
  188. border-top: none !important;
  189. border-radius: 0 !important;
  190. }
  191. :deep(.el-tabs__item.is-active) {
  192. background-color: var(--el-color-primary-light-9);
  193. border-bottom-color: var(--el-border-color) !important;
  194. }
  195. .context-menu {
  196. position: fixed;
  197. width: 100px;
  198. margin: 0;
  199. border-radius: 4px;
  200. background: var(--el-bg-color-overlay);
  201. border: 1px solid var(--el-border-color-light);
  202. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  203. z-index: 3000;
  204. list-style-type: none;
  205. padding: 0;
  206. li {
  207. margin: 0;
  208. padding: 7px 16px;
  209. cursor: pointer;
  210. &:hover {
  211. background: #f2f2f2;
  212. }
  213. }
  214. }
  215. </style>