useTabDropdown.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import type { TabContentProps } from './types';
  2. import type { DropMenu } from '/@/components/Dropdown';
  3. import type { ComputedRef } from 'vue';
  4. import { computed, unref, reactive } from 'vue';
  5. import { MenuEventEnum } from './types';
  6. import { useMultipleTabStore } from '/@/store/modules/multipleTab';
  7. import { RouteLocationNormalized, useRouter } from 'vue-router';
  8. import { useTabs } from '/@/hooks/web/useTabs';
  9. import { useI18n } from '/@/hooks/web/useI18n';
  10. export function useTabDropdown(tabContentProps: TabContentProps, getIsTabs: ComputedRef<boolean>) {
  11. const state = reactive({
  12. current: null as Nullable<RouteLocationNormalized>,
  13. currentIndex: 0,
  14. });
  15. const { t } = useI18n();
  16. const tabStore = useMultipleTabStore();
  17. const { currentRoute } = useRouter();
  18. const { refreshPage, closeAll, close, closeLeft, closeOther, closeRight } = useTabs();
  19. const getTargetTab = computed(
  20. (): RouteLocationNormalized => {
  21. return unref(getIsTabs) ? tabContentProps.tabItem : unref(currentRoute);
  22. }
  23. );
  24. /**
  25. * @description: drop-down list
  26. */
  27. const getDropMenuList = computed(() => {
  28. if (!unref(getTargetTab)) {
  29. return;
  30. }
  31. const { meta } = unref(getTargetTab);
  32. const { path } = unref(currentRoute);
  33. // Refresh button
  34. const curItem = state.current;
  35. const index = state.currentIndex;
  36. const refreshDisabled = curItem ? curItem.path !== path : true;
  37. // Close left
  38. const closeLeftDisabled = index === 0;
  39. const disabled = tabStore.getTabList.length === 1;
  40. // Close right
  41. const closeRightDisabled =
  42. index === tabStore.getTabList.length - 1 && tabStore.getLastDragEndIndex >= 0;
  43. const dropMenuList: DropMenu[] = [
  44. {
  45. icon: 'ion:reload-sharp',
  46. event: MenuEventEnum.REFRESH_PAGE,
  47. text: t('layout.multipleTab.reload'),
  48. disabled: refreshDisabled,
  49. },
  50. {
  51. icon: 'clarity:close-line',
  52. event: MenuEventEnum.CLOSE_CURRENT,
  53. text: t('layout.multipleTab.close'),
  54. disabled: !!meta?.affix || disabled,
  55. divider: true,
  56. },
  57. {
  58. icon: 'line-md:arrow-close-left',
  59. event: MenuEventEnum.CLOSE_LEFT,
  60. text: t('layout.multipleTab.closeLeft'),
  61. disabled: closeLeftDisabled,
  62. divider: false,
  63. },
  64. {
  65. icon: 'line-md:arrow-close-right',
  66. event: MenuEventEnum.CLOSE_RIGHT,
  67. text: t('layout.multipleTab.closeRight'),
  68. disabled: closeRightDisabled,
  69. divider: true,
  70. },
  71. {
  72. icon: 'dashicons:align-center',
  73. event: MenuEventEnum.CLOSE_OTHER,
  74. text: t('layout.multipleTab.closeOther'),
  75. disabled: disabled,
  76. },
  77. {
  78. icon: 'clarity:minus-line',
  79. event: MenuEventEnum.CLOSE_ALL,
  80. text: t('layout.multipleTab.closeAll'),
  81. disabled: disabled,
  82. },
  83. ];
  84. return dropMenuList;
  85. });
  86. function handleContextMenu(tabItem: RouteLocationNormalized) {
  87. return (e: Event) => {
  88. if (!tabItem) {
  89. return;
  90. }
  91. e?.preventDefault();
  92. const index = tabStore.getTabList.findIndex((tab) => tab.path === tabItem.path);
  93. state.current = tabItem;
  94. state.currentIndex = index;
  95. };
  96. }
  97. // Handle right click event
  98. function handleMenuEvent(menu: DropMenu): void {
  99. const { event } = menu;
  100. switch (event) {
  101. case MenuEventEnum.REFRESH_PAGE:
  102. // refresh page
  103. refreshPage();
  104. break;
  105. // Close current
  106. case MenuEventEnum.CLOSE_CURRENT:
  107. close(tabContentProps.tabItem);
  108. break;
  109. // Close left
  110. case MenuEventEnum.CLOSE_LEFT:
  111. closeLeft();
  112. break;
  113. // Close right
  114. case MenuEventEnum.CLOSE_RIGHT:
  115. closeRight();
  116. break;
  117. // Close other
  118. case MenuEventEnum.CLOSE_OTHER:
  119. closeOther();
  120. break;
  121. // Close all
  122. case MenuEventEnum.CLOSE_ALL:
  123. closeAll();
  124. break;
  125. }
  126. }
  127. return { getDropMenuList, handleMenuEvent, handleContextMenu };
  128. }