tab.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. import { toRaw } from 'vue';
  2. import { unref } from 'vue';
  3. import { Action, Module, Mutation, VuexModule, getModule } from 'vuex-module-decorators';
  4. import { hotModuleUnregisterModule } from '/@/utils/helper/vuexHelper';
  5. import { PageEnum } from '/@/enums/pageEnum';
  6. import store from '/@/store';
  7. import router from '/@/router';
  8. import { PAGE_NOT_FOUND_ROUTE, REDIRECT_ROUTE } from '/@/router/constant';
  9. import { RouteLocationNormalized, RouteLocationRaw } from 'vue-router';
  10. import { getRoute } from '/@/router/helper/routeHelper';
  11. import { useGo, useRedo } from '/@/hooks/web/usePage';
  12. import { cloneDeep } from 'lodash-es';
  13. const NAME = 'app-tab';
  14. hotModuleUnregisterModule(NAME);
  15. export const PAGE_LAYOUT_KEY = '__PAGE_LAYOUT__';
  16. function isGotoPage() {
  17. const go = useGo();
  18. go(unref(router.currentRoute).path, true);
  19. }
  20. @Module({ namespaced: true, name: NAME, dynamic: true, store })
  21. class Tab extends VuexModule {
  22. cachedMapState = new Map<string, string[]>();
  23. // tab list
  24. tabsState: RouteLocationNormalized[] = [];
  25. lastDragEndIndexState = 0;
  26. get getTabsState() {
  27. return this.tabsState;
  28. }
  29. get getCurrentTab(): RouteLocationNormalized {
  30. const route = unref(router.currentRoute);
  31. return this.tabsState.find((item) => item.path === route.path)!;
  32. }
  33. get getCachedMapState(): Map<string, string[]> {
  34. return this.cachedMapState;
  35. }
  36. get getLastDragEndIndexState(): number {
  37. return this.lastDragEndIndexState;
  38. }
  39. @Mutation
  40. commitClearCache(): void {
  41. this.cachedMapState = new Map();
  42. }
  43. @Mutation
  44. goToPage() {
  45. const go = useGo();
  46. const len = this.tabsState.length;
  47. const { path } = unref(router.currentRoute);
  48. let toPath: PageEnum | string = PageEnum.BASE_HOME;
  49. if (len > 0) {
  50. const page = this.tabsState[len - 1];
  51. const p = page.fullPath || page.path;
  52. if (p) {
  53. toPath = p;
  54. }
  55. }
  56. // Jump to the current page and report an error
  57. path !== toPath && go(toPath as PageEnum, true);
  58. }
  59. @Mutation
  60. commitCachedMapState(): void {
  61. const cacheMap = new Map<string, string[]>();
  62. const pageCacheSet = new Set<string>();
  63. this.tabsState.forEach((tab) => {
  64. const item = getRoute(tab);
  65. const needCache = !item.meta?.ignoreKeepAlive;
  66. if (!needCache) return;
  67. if (item.meta?.affix) {
  68. const name = item.name as string;
  69. pageCacheSet.add(name);
  70. } else if (item?.matched && needCache) {
  71. const matched = item?.matched;
  72. if (!matched) return;
  73. const len = matched.length;
  74. if (len < 2) return;
  75. for (let i = 0; i < matched.length; i++) {
  76. const key = matched[i].name as string;
  77. if (i < 2) {
  78. pageCacheSet.add(key);
  79. }
  80. if (i < len - 1) {
  81. const { meta, name } = matched[i + 1];
  82. if (meta && (meta.affix || needCache)) {
  83. const mapList = cacheMap.get(key) || [];
  84. if (!mapList.includes(name as string)) {
  85. mapList.push(name as string);
  86. }
  87. cacheMap.set(key, mapList);
  88. }
  89. }
  90. }
  91. }
  92. });
  93. cacheMap.set(PAGE_LAYOUT_KEY, Array.from(pageCacheSet));
  94. this.cachedMapState = cacheMap;
  95. }
  96. @Mutation
  97. commitTabRoutesState(route: RouteLocationNormalized) {
  98. const { path, fullPath, params, query } = route;
  99. let updateIndex = -1;
  100. // Existing pages, do not add tabs repeatedly
  101. const hasTab = this.tabsState.some((tab, index) => {
  102. updateIndex = index;
  103. return (tab.fullPath || tab.path) === (fullPath || path);
  104. });
  105. if (hasTab) {
  106. const curTab = toRaw(this.tabsState)[updateIndex];
  107. if (!curTab) return;
  108. curTab.params = params || curTab.params;
  109. curTab.query = query || curTab.query;
  110. curTab.fullPath = fullPath || curTab.fullPath;
  111. this.tabsState.splice(updateIndex, 1, curTab);
  112. return;
  113. }
  114. this.tabsState = cloneDeep([...this.tabsState, route]);
  115. }
  116. /**
  117. * @description: close tab
  118. */
  119. @Mutation
  120. commitCloseTab(route: RouteLocationNormalized): void {
  121. const { fullPath, meta: { affix } = {} } = route;
  122. if (affix) return;
  123. const index = this.tabsState.findIndex((item) => item.fullPath === fullPath);
  124. index !== -1 && this.tabsState.splice(index, 1);
  125. }
  126. @Mutation
  127. commitCloseAllTab(): void {
  128. this.tabsState = this.tabsState.filter((item) => {
  129. return item.meta && item.meta.affix;
  130. });
  131. }
  132. @Mutation
  133. commitResetState(): void {
  134. this.tabsState = [];
  135. this.cachedMapState = new Map();
  136. }
  137. @Mutation
  138. commitSortTabs({ oldIndex, newIndex }: { oldIndex: number; newIndex: number }): void {
  139. const currentTab = this.tabsState[oldIndex];
  140. this.tabsState.splice(oldIndex, 1);
  141. this.tabsState.splice(newIndex, 0, currentTab);
  142. this.lastDragEndIndexState = this.lastDragEndIndexState + 1;
  143. }
  144. @Mutation
  145. closeMultipleTab({ pathList }: { pathList: string[] }): void {
  146. this.tabsState = toRaw(this.tabsState).filter((item) => !pathList.includes(item.fullPath));
  147. }
  148. @Action
  149. addTabAction(route: RouteLocationNormalized) {
  150. const { path, name } = route;
  151. // 404 The page does not need to add a tab
  152. if (
  153. path === PageEnum.ERROR_PAGE ||
  154. !name ||
  155. [REDIRECT_ROUTE.name, PAGE_NOT_FOUND_ROUTE.name].includes(name as string)
  156. ) {
  157. return;
  158. }
  159. this.commitTabRoutesState(getRoute(route));
  160. this.commitCachedMapState();
  161. }
  162. @Mutation
  163. async commitRedoPage() {
  164. const route = router.currentRoute.value;
  165. for (const [key, value] of this.cachedMapState) {
  166. const index = value.findIndex((item) => item === (route.name as string));
  167. if (index === -1) {
  168. continue;
  169. }
  170. if (value.length === 1) {
  171. this.cachedMapState.delete(key);
  172. continue;
  173. }
  174. value.splice(index, 1);
  175. this.cachedMapState.set(key, value);
  176. }
  177. const redo = useRedo();
  178. await redo();
  179. }
  180. @Action
  181. closeAllTabAction() {
  182. this.commitCloseAllTab();
  183. this.commitClearCache();
  184. this.goToPage();
  185. }
  186. @Action
  187. closeTabAction(tab: RouteLocationNormalized) {
  188. function getObj(tabItem: RouteLocationNormalized) {
  189. const { params, path, query } = tabItem;
  190. return {
  191. params: params || {},
  192. path,
  193. query: query || {},
  194. };
  195. }
  196. const { currentRoute, replace } = router;
  197. const { path } = unref(currentRoute);
  198. if (path !== tab.path) {
  199. // Closed is not the activation tab
  200. this.commitCloseTab(tab);
  201. return;
  202. }
  203. // Closed is activated atb
  204. let toObj: RouteLocationRaw = {};
  205. const index = this.getTabsState.findIndex((item) => item.path === path);
  206. // If the current is the leftmost tab
  207. if (index === 0) {
  208. // There is only one tab, then jump to the homepage, otherwise jump to the right tab
  209. if (this.getTabsState.length === 1) {
  210. toObj = PageEnum.BASE_HOME;
  211. } else {
  212. // Jump to the right tab
  213. const page = this.getTabsState[index + 1];
  214. toObj = getObj(page);
  215. }
  216. } else {
  217. // Close the current tab
  218. const page = this.getTabsState[index - 1];
  219. toObj = getObj(page);
  220. }
  221. this.commitCloseTab(currentRoute.value);
  222. replace(toObj);
  223. }
  224. @Action
  225. closeTabByKeyAction(key: string) {
  226. const index = this.tabsState.findIndex((item) => (item.fullPath || item.path) === key);
  227. index !== -1 && this.closeTabAction(this.tabsState[index]);
  228. }
  229. @Action
  230. closeLeftTabAction(route: RouteLocationNormalized): void {
  231. const index = this.tabsState.findIndex((item) => item.path === route.path);
  232. if (index > 0) {
  233. const leftTabs = this.tabsState.slice(0, index);
  234. const pathList: string[] = [];
  235. for (const item of leftTabs) {
  236. const affix = item.meta ? item.meta.affix : false;
  237. if (!affix) {
  238. pathList.push(item.fullPath);
  239. }
  240. }
  241. this.closeMultipleTab({ pathList });
  242. }
  243. this.commitCachedMapState();
  244. isGotoPage();
  245. }
  246. @Action
  247. closeRightTabAction(route: RouteLocationNormalized): void {
  248. const index = this.tabsState.findIndex((item) => item.fullPath === route.fullPath);
  249. if (index >= 0 && index < this.tabsState.length - 1) {
  250. const rightTabs = this.tabsState.slice(index + 1, this.tabsState.length);
  251. const pathList: string[] = [];
  252. for (const item of rightTabs) {
  253. const affix = item.meta ? item.meta.affix : false;
  254. if (!affix) {
  255. pathList.push(item.fullPath);
  256. }
  257. }
  258. this.closeMultipleTab({ pathList });
  259. }
  260. this.commitCachedMapState();
  261. isGotoPage();
  262. }
  263. @Action
  264. closeOtherTabAction(route: RouteLocationNormalized): void {
  265. const closePathList = this.tabsState.map((item) => item.fullPath);
  266. const pathList: string[] = [];
  267. closePathList.forEach((path) => {
  268. if (path !== route.fullPath) {
  269. const closeItem = this.tabsState.find((item) => item.path === path);
  270. if (!closeItem) return;
  271. const affix = closeItem.meta ? closeItem.meta.affix : false;
  272. if (!affix) {
  273. pathList.push(closeItem.fullPath);
  274. }
  275. }
  276. });
  277. this.closeMultipleTab({ pathList });
  278. this.commitCachedMapState();
  279. isGotoPage();
  280. }
  281. }
  282. export const tabStore = getModule<Tab>(Tab);