useTabDropdown.ts 4.1 KB

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