use-tabs.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // @ts-nocheck
  2. import {computed} from "vue";
  3. import {useLocalStorage} from "@vueuse/core";
  4. import {secondLevelRouterKey} from "@/router";
  5. import XEUtils from "xe-utils";
  6. import {RouteLocationNormalizedLoaded} from "vue-router";
  7. // @ts-ignore
  8. import {stringIsBlank} from "@/utils/blank-utils";
  9. import router from "@/router";
  10. // 定义Tabs类型
  11. export interface Tab {
  12. name: string;
  13. query: any;
  14. title: string;
  15. path: string
  16. params: string | null
  17. }
  18. // 导出tabs对象
  19. export const tabs = useLocalStorage<Tab[]>("tabs", [])
  20. export const tabsKeys = computed(() => {
  21. return XEUtils.eachAndReturnList(tabs, (item) => {
  22. return item.name
  23. })
  24. })
  25. export const getKeepAliveNames = computed(() => {
  26. const tmp = []
  27. secondLevelRouterKey.forEach((item, key) => {
  28. if (item.includes(router.currentRoute.value.name)) {
  29. tmp.push(key)
  30. }
  31. })
  32. return [...tmp, ...tabsKeys.value]
  33. })
  34. function useTabs() {
  35. function routerPush(index: number) {
  36. if (XEUtils.has(tabs.value, index)) {
  37. const pushInfo = tabs.value[index]
  38. router.push({
  39. name: pushInfo.name,
  40. query: pushInfo.query,
  41. params: pushInfo.params
  42. }).then(r => {
  43. })
  44. } else {
  45. router.push({
  46. name: 'dashboard'
  47. }).then(() => {
  48. })
  49. }
  50. }
  51. function removeByIndex(index: number) {
  52. const closeInfo = tabs.value[index]
  53. const currentRouter = router.currentRoute.value
  54. if (closeInfo.name === currentRouter.name) {
  55. if (index > 0) {
  56. routerPush(index - 1)
  57. } else {
  58. routerPush(index + 1)
  59. }
  60. }
  61. tabs.value.splice(index, 1)
  62. }
  63. function tabsAdd(info: RouteLocationNormalizedLoaded) {
  64. const hasIndex = tabsKeys.value.indexOf(info.name)
  65. const tmp: Tab = {
  66. path: info.path,
  67. name: info.name,
  68. query: info.query,
  69. title: info.meta.title,
  70. params: info.params
  71. }
  72. if (XEUtils.get(info, 'meta.hideTabs', true)) {
  73. return
  74. }
  75. if (stringIsBlank(info.name)) {
  76. return;
  77. }
  78. if (hasIndex > -1) {
  79. tabs.value[hasIndex] = tmp
  80. } else {
  81. tabs.value.push(tmp)
  82. }
  83. }
  84. function closeRight(index: number) {
  85. tabs.value.splice(index + 1, tabsKeys.value.length - 1);
  86. router.push(tabs.value[tabs.value.length - 1].path)
  87. }
  88. function closeLeft(index: number) {
  89. tabs.value.splice(0, index);
  90. router.push(tabs.value[0].path)
  91. }
  92. function closeAll() {
  93. router.push({
  94. name: 'dashboard'
  95. }).then(() => {
  96. })
  97. tabs.value = []
  98. }
  99. function isActive(name: string) {
  100. return name === router.currentRoute.value.name
  101. }
  102. function closeOther(index: number) {
  103. routerPush(index)
  104. tabs.value = tabs.value.splice(index, 1)
  105. }
  106. return {
  107. tabs: tabs,
  108. removeByIndex,
  109. routerPush,
  110. closeRight,
  111. closeLeft,
  112. closeAll,
  113. isActive,
  114. closeOther,
  115. tabsAdd
  116. }
  117. }
  118. export const useTab = useTabs()