useTabDropdown.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import type { AppRouteRecordRaw } from '/@/router/types';
  2. import type { TabContentProps } from './tab.data';
  3. import type { Ref } from 'vue';
  4. import type { TabItem } from '/@/store/modules/tab';
  5. import type { DropMenu } from '/@/components/Dropdown';
  6. import { computed, unref } from 'vue';
  7. import { TabContentEnum, MenuEventEnum, getActions } from './tab.data';
  8. import { tabStore } from '/@/store/modules/tab';
  9. import { appStore } from '/@/store/modules/app';
  10. import { PageEnum } from '/@/enums/pageEnum';
  11. import { useGo, useRedo } from '/@/hooks/web/usePage';
  12. import router from '/@/router';
  13. import { useTabs, isInitUseTab } from '/@/hooks/web/useTabs';
  14. const { initTabFn } = useTabs();
  15. /**
  16. * @description: 右键下拉
  17. */
  18. export function useTabDropdown(tabContentProps: TabContentProps) {
  19. const { currentRoute } = router;
  20. const redo = useRedo();
  21. const go = useGo();
  22. const isTabsRef = computed(() => tabContentProps.type === TabContentEnum.TAB_TYPE);
  23. const getCurrentTab: Ref<TabItem | AppRouteRecordRaw> = computed(() => {
  24. return unref(isTabsRef)
  25. ? tabContentProps.tabItem
  26. : ((unref(currentRoute) as any) as AppRouteRecordRaw);
  27. });
  28. // 当前tab列表
  29. const getTabsState = computed(() => {
  30. return tabStore.getTabsState;
  31. });
  32. /**
  33. * @description: 下拉列表
  34. */
  35. const getDropMenuList = computed(() => {
  36. const dropMenuList = getActions();
  37. // 重置为初始状态
  38. for (const item of dropMenuList) {
  39. item.disabled = false;
  40. }
  41. // 没有tab
  42. if (!unref(getTabsState) || unref(getTabsState).length <= 0) {
  43. return dropMenuList;
  44. } else if (unref(getTabsState).length === 1) {
  45. // 只有一个tab
  46. for (const item of dropMenuList) {
  47. if (item.event !== MenuEventEnum.REFRESH_PAGE) {
  48. item.disabled = true;
  49. }
  50. }
  51. return dropMenuList;
  52. }
  53. if (!unref(getCurrentTab)) {
  54. return;
  55. }
  56. const { meta, path } = unref(getCurrentTab);
  57. // console.log(unref(getCurrentTab));
  58. // 刷新按钮
  59. const curItem = tabStore.getCurrentContextMenuState;
  60. const index = tabStore.getCurrentContextMenuIndexState;
  61. const refreshDisabled = curItem ? curItem.path !== path : true;
  62. // 关闭左侧
  63. const closeLeftDisabled = index === 0;
  64. // 关闭右侧
  65. const closeRightDisabled = index === unref(getTabsState).length - 1;
  66. // 当前为固定tab
  67. dropMenuList[0].disabled = unref(isTabsRef) ? refreshDisabled : false;
  68. if (meta && meta.affix) {
  69. dropMenuList[1].disabled = true;
  70. }
  71. dropMenuList[2].disabled = closeLeftDisabled;
  72. dropMenuList[3].disabled = closeRightDisabled;
  73. return dropMenuList;
  74. });
  75. /**
  76. * @description: 关闭所有页面时,跳转页面
  77. */
  78. function gotoPage() {
  79. const len = unref(getTabsState).length;
  80. const { path } = unref(currentRoute);
  81. let toPath: PageEnum | string = PageEnum.BASE_HOME;
  82. if (len > 0) {
  83. toPath = unref(getTabsState)[len - 1].path;
  84. }
  85. // 跳到当前页面报错
  86. path !== toPath && go(toPath as PageEnum, true);
  87. }
  88. function isGotoPage(currentTab?: TabItem) {
  89. const { path } = unref(currentRoute);
  90. const currentPath = (currentTab || unref(getCurrentTab)).path;
  91. // 不是当前tab,关闭左侧/右侧时,需跳转页面
  92. if (path !== currentPath) {
  93. go(currentPath as PageEnum, true);
  94. }
  95. }
  96. function refreshPage(tabItem?: TabItem) {
  97. try {
  98. tabStore.commitCloseTabKeepAlive(tabItem || unref(getCurrentTab));
  99. } catch (error) {}
  100. redo();
  101. }
  102. function closeAll() {
  103. tabStore.commitCloseAllTab();
  104. gotoPage();
  105. }
  106. function closeLeft(tabItem?: TabItem) {
  107. tabStore.closeLeftTabAction(tabItem || unref(getCurrentTab));
  108. isGotoPage(tabItem);
  109. }
  110. function closeRight(tabItem?: TabItem) {
  111. tabStore.closeRightTabAction(tabItem || unref(getCurrentTab));
  112. isGotoPage(tabItem);
  113. }
  114. function closeOther(tabItem?: TabItem) {
  115. tabStore.closeOtherTabAction(tabItem || unref(getCurrentTab));
  116. isGotoPage(tabItem);
  117. }
  118. function closeCurrent(tabItem?: TabItem) {
  119. closeTab(unref(tabItem || unref(getCurrentTab)));
  120. }
  121. function scaleScreen() {
  122. const {
  123. headerSetting: { show: showHeader },
  124. menuSetting: { show: showMenu },
  125. } = appStore.getProjectConfig;
  126. const isScale = !showHeader && !showMenu;
  127. appStore.commitProjectConfigState({
  128. headerSetting: { show: isScale },
  129. menuSetting: { show: isScale },
  130. });
  131. }
  132. if (!isInitUseTab) {
  133. initTabFn({
  134. refreshPageFn: refreshPage,
  135. closeAllFn: closeAll,
  136. closeCurrentFn: closeCurrent,
  137. closeLeftFn: closeLeft,
  138. closeOtherFn: closeOther,
  139. closeRightFn: closeRight,
  140. });
  141. }
  142. // 处理右键事件
  143. function handleMenuEvent(menu: DropMenu): void {
  144. const { event } = menu;
  145. switch (event) {
  146. case MenuEventEnum.SCALE:
  147. scaleScreen();
  148. break;
  149. case MenuEventEnum.REFRESH_PAGE:
  150. // 刷新页面
  151. refreshPage();
  152. break;
  153. // 关闭当前
  154. case MenuEventEnum.CLOSE_CURRENT:
  155. closeCurrent();
  156. break;
  157. // 关闭左侧
  158. case MenuEventEnum.CLOSE_LEFT:
  159. closeLeft();
  160. break;
  161. // 关闭右侧
  162. case MenuEventEnum.CLOSE_RIGHT:
  163. closeRight();
  164. break;
  165. // 关闭其他
  166. case MenuEventEnum.CLOSE_OTHER:
  167. closeOther();
  168. break;
  169. // 关闭其他
  170. case MenuEventEnum.CLOSE_ALL:
  171. closeAll();
  172. break;
  173. default:
  174. break;
  175. }
  176. }
  177. return { getDropMenuList, handleMenuEvent };
  178. }
  179. export function closeTab(closedTab: TabItem) {
  180. const { currentRoute, replace } = router;
  181. // 当前tab列表
  182. const getTabsState = computed(() => {
  183. return tabStore.getTabsState;
  184. });
  185. const { path } = unref(currentRoute);
  186. if (path !== closedTab.path) {
  187. // 关闭的不是激活tab
  188. tabStore.commitCloseTab(closedTab);
  189. return;
  190. }
  191. // 关闭的为激活atb
  192. let toPath: PageEnum | string;
  193. const index = unref(getTabsState).findIndex((item) => item.path === path);
  194. // 如果当前为最左边tab
  195. if (index === 0) {
  196. // 只有一个tab,则跳转至首页,否则跳转至右tab
  197. if (unref(getTabsState).length === 1) {
  198. toPath = PageEnum.BASE_HOME;
  199. } else {
  200. // 跳转至右边tab
  201. toPath = unref(getTabsState)[index + 1].path;
  202. }
  203. } else {
  204. // 跳转至左边tab
  205. toPath = unref(getTabsState)[index - 1].path;
  206. }
  207. const route = (unref(currentRoute) as unknown) as AppRouteRecordRaw;
  208. tabStore.commitCloseTab(route);
  209. replace(toPath);
  210. }