GlobalTabs.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. activeValue.value = tab.fullPath
  35. activeIndex.value = findActiveIndex(tab.fullPath)
  36. if (tab.name === 'login' || tabs.value.find(item => item.fullPath === tab.fullPath)) {
  37. return
  38. }
  39. tabs.value.push(tab)
  40. }
  41. const menuStore = useMenuStore()
  42. const activeMenu = ref({})
  43. const findActiveMenu = (path: any) => {
  44. const findDeep = (list: any): boolean => {
  45. let flag = false
  46. for (let index = 0; index < list.length; index++) {
  47. const element = list[index]
  48. if (element.path === path) {
  49. flag = true
  50. break
  51. }
  52. if (element.children?.length) {
  53. flag = findDeep(element.children)
  54. }
  55. }
  56. return flag
  57. }
  58. for (let index = 0; index < menuStore.allMenuList.length; index++) {
  59. const element = menuStore.allMenuList[index]
  60. if (element.path === path) {
  61. activeMenu.value = element
  62. break
  63. }
  64. if (element.children?.length && findDeep(element.children)) {
  65. activeMenu.value = element
  66. break
  67. }
  68. }
  69. }
  70. const changeTab = (name: TabPaneName) => {
  71. activeIndex.value = findActiveIndex(name)
  72. const menu = tabs.value.find(item => item.fullPath === name)
  73. router.push(menu)
  74. menuStore.activeRoutePath = name
  75. findActiveMenu(name)
  76. menuStore.setActiveTopMenu(activeMenu.value)
  77. }
  78. const removeTab = (name: TabPaneName) => {
  79. const index = findActiveIndex(name)
  80. activeIndex.value = index < tabs.value.length - 1 ? index + 1 : index - 1
  81. router.push(tabs.value[activeIndex.value])
  82. tabs.value.splice(index, 1)
  83. }
  84. watch(
  85. () => route,
  86. to => {
  87. setTab(routeToTab(to))
  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('-')[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. </script>
  149. <template>
  150. <div class="bg-white" style="border-top: 1px solid var(--el-border-color-light)">
  151. <el-tabs
  152. v-model="activeValue"
  153. type="card"
  154. closable
  155. @tab-change="changeTab"
  156. @tab-remove="removeTab"
  157. @contextmenu.prevent="openContextMenu($event)"
  158. >
  159. <el-tab-pane v-for="item in tabs" :key="item.name" :label="item.title" :name="item.fullPath" :tab="item">
  160. </el-tab-pane>
  161. </el-tabs>
  162. <!--自定义右键菜单html代码-->
  163. <ul v-show="contextMenuVisible" :style="{ left: left + 'px', top: top + 'px' }" class="context-menu">
  164. <li @click="closeAll">关闭所有</li>
  165. <li @click="closeLeft">关闭左侧</li>
  166. <li @click="closeRight">关闭右侧</li>
  167. <li @click="closeOther">关闭其他</li>
  168. </ul>
  169. </div>
  170. </template>
  171. <style lang="scss" scoped>
  172. :deep(.el-tabs__header) {
  173. margin: 0;
  174. // border-top: 1px solid var(--el-border-color-light);
  175. }
  176. :deep(.el-tabs__nav) {
  177. border-top: none !important;
  178. border-radius: 0 !important;
  179. }
  180. :deep(.el-tabs__item.is-active) {
  181. background-color: var(--el-color-primary-light-9);
  182. border-bottom-color: var(--el-border-color) !important;
  183. }
  184. .context-menu {
  185. position: fixed;
  186. width: 100px;
  187. margin: 0;
  188. border-radius: 4px;
  189. background: var(--el-bg-color-overlay);
  190. border: 1px solid var(--el-border-color-light);
  191. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  192. z-index: 3000;
  193. list-style-type: none;
  194. padding: 0;
  195. li {
  196. margin: 0;
  197. padding: 7px 16px;
  198. cursor: pointer;
  199. &:hover {
  200. background: #f2f2f2;
  201. }
  202. }
  203. }
  204. </style>