vue-vben-admin/src/layouts/default/multitabs/useTabDropdown.ts

244 lines
6.8 KiB
TypeScript
Raw Normal View History

2020-09-28 20:19:10 +08:00
import type { AppRouteRecordRaw } from '/@/router/types';
2020-11-25 22:28:58 +08:00
import type { TabContentProps } from './data';
2020-09-28 20:19:10 +08:00
import type { Ref } from 'vue';
import type { TabItem } from '/@/store/modules/tab';
import type { DropMenu } from '/@/components/Dropdown';
import { computed, unref } from 'vue';
2020-11-25 22:28:58 +08:00
import { TabContentEnum, MenuEventEnum, getActions } from './data';
2020-09-28 20:19:10 +08:00
import { tabStore } from '/@/store/modules/tab';
import { appStore } from '/@/store/modules/app';
import { PageEnum } from '/@/enums/pageEnum';
import { useGo, useRedo } from '/@/hooks/web/usePage';
import router from '/@/router';
import { useTabs, isInitUseTab } from '/@/hooks/web/useTabs';
import { RouteLocationRaw } from 'vue-router';
2020-09-28 20:19:10 +08:00
const { initTabFn } = useTabs();
2020-11-25 22:28:58 +08:00
2020-09-28 20:19:10 +08:00
export function useTabDropdown(tabContentProps: TabContentProps) {
const { currentRoute } = router;
const redo = useRedo();
const go = useGo();
const isTabsRef = computed(() => tabContentProps.type === TabContentEnum.TAB_TYPE);
const getCurrentTab: Ref<TabItem | AppRouteRecordRaw> = computed(() => {
return unref(isTabsRef)
? tabContentProps.tabItem
: ((unref(currentRoute) as any) as AppRouteRecordRaw);
});
2020-11-25 22:28:58 +08:00
// Current tab list
const getTabsState = computed(() => tabStore.getTabsState);
2020-09-28 20:19:10 +08:00
/**
2020-11-25 22:28:58 +08:00
* @description: drop-down list
2020-09-28 20:19:10 +08:00
*/
const getDropMenuList = computed(() => {
const dropMenuList = getActions();
2020-11-25 22:28:58 +08:00
// Reset to initial state
2020-09-28 20:19:10 +08:00
for (const item of dropMenuList) {
item.disabled = false;
}
2020-11-25 22:28:58 +08:00
// No tab
2020-09-28 20:19:10 +08:00
if (!unref(getTabsState) || unref(getTabsState).length <= 0) {
return dropMenuList;
} else if (unref(getTabsState).length === 1) {
2020-11-25 22:28:58 +08:00
// Only one tab
2020-09-28 20:19:10 +08:00
for (const item of dropMenuList) {
if (item.event !== MenuEventEnum.REFRESH_PAGE) {
item.disabled = true;
}
}
return dropMenuList;
}
2020-11-25 22:28:58 +08:00
if (!unref(getCurrentTab)) return;
2020-09-28 20:19:10 +08:00
const { meta, path } = unref(getCurrentTab);
2020-11-25 22:28:58 +08:00
// Refresh button
2020-09-28 20:19:10 +08:00
const curItem = tabStore.getCurrentContextMenuState;
const index = tabStore.getCurrentContextMenuIndexState;
const refreshDisabled = curItem ? curItem.path !== path : true;
2020-11-25 22:28:58 +08:00
// Close left
2020-09-28 20:19:10 +08:00
const closeLeftDisabled = index === 0;
2020-11-25 22:28:58 +08:00
// Close right
2020-09-28 20:19:10 +08:00
const closeRightDisabled = index === unref(getTabsState).length - 1;
2020-11-25 22:28:58 +08:00
// Currently fixed tab
// TODO PERf
2020-09-28 20:19:10 +08:00
dropMenuList[0].disabled = unref(isTabsRef) ? refreshDisabled : false;
if (meta && meta.affix) {
dropMenuList[1].disabled = true;
}
dropMenuList[2].disabled = closeLeftDisabled;
dropMenuList[3].disabled = closeRightDisabled;
return dropMenuList;
});
/**
2020-11-25 22:28:58 +08:00
* @description: Jump to page when closing all pages
2020-09-28 20:19:10 +08:00
*/
function gotoPage() {
const len = unref(getTabsState).length;
const { path } = unref(currentRoute);
let toPath: PageEnum | string = PageEnum.BASE_HOME;
if (len > 0) {
const page = unref(getTabsState)[len - 1];
const p = page.fullPath || page.path;
if (p) {
toPath = p;
}
2020-09-28 20:19:10 +08:00
}
2020-11-25 22:28:58 +08:00
// Jump to the current page and report an error
2020-09-28 20:19:10 +08:00
path !== toPath && go(toPath as PageEnum, true);
}
function isGotoPage(currentTab?: TabItem) {
const { path } = unref(currentRoute);
const currentPath = (currentTab || unref(getCurrentTab)).path;
2020-11-25 22:28:58 +08:00
// Not the current tab, when you close the left/right side, you need to jump to the page
2020-09-28 20:19:10 +08:00
if (path !== currentPath) {
go(currentPath as PageEnum, true);
}
}
function refreshPage(tabItem?: TabItem) {
try {
tabStore.commitCloseTabKeepAlive(tabItem || unref(getCurrentTab));
} catch (error) {}
redo();
}
2020-11-25 22:28:58 +08:00
2020-09-28 20:19:10 +08:00
function closeAll() {
tabStore.commitCloseAllTab();
gotoPage();
}
2020-11-25 22:28:58 +08:00
2020-09-28 20:19:10 +08:00
function closeLeft(tabItem?: TabItem) {
tabStore.closeLeftTabAction(tabItem || unref(getCurrentTab));
isGotoPage(tabItem);
}
2020-11-25 22:28:58 +08:00
2020-09-28 20:19:10 +08:00
function closeRight(tabItem?: TabItem) {
tabStore.closeRightTabAction(tabItem || unref(getCurrentTab));
isGotoPage(tabItem);
}
2020-11-25 22:28:58 +08:00
2020-09-28 20:19:10 +08:00
function closeOther(tabItem?: TabItem) {
tabStore.closeOtherTabAction(tabItem || unref(getCurrentTab));
isGotoPage(tabItem);
}
2020-11-25 22:28:58 +08:00
2020-09-28 20:19:10 +08:00
function closeCurrent(tabItem?: TabItem) {
closeTab(unref(tabItem || unref(getCurrentTab)));
}
2020-11-25 22:28:58 +08:00
2020-09-28 20:19:10 +08:00
function scaleScreen() {
const {
headerSetting: { show: showHeader },
menuSetting: { show: showMenu },
} = appStore.getProjectConfig;
const isScale = !showHeader && !showMenu;
appStore.commitProjectConfigState({
headerSetting: { show: isScale },
menuSetting: { show: isScale },
});
}
if (!isInitUseTab) {
initTabFn({
refreshPageFn: refreshPage,
closeAllFn: closeAll,
closeCurrentFn: closeCurrent,
closeLeftFn: closeLeft,
closeOtherFn: closeOther,
closeRightFn: closeRight,
});
}
2020-11-25 22:28:58 +08:00
// Handle right click event
2020-09-28 20:19:10 +08:00
function handleMenuEvent(menu: DropMenu): void {
const { event } = menu;
switch (event) {
case MenuEventEnum.SCALE:
scaleScreen();
break;
case MenuEventEnum.REFRESH_PAGE:
2020-11-25 22:28:58 +08:00
// refresh page
2020-09-28 20:19:10 +08:00
refreshPage();
break;
2020-11-25 22:28:58 +08:00
// Close current
2020-09-28 20:19:10 +08:00
case MenuEventEnum.CLOSE_CURRENT:
closeCurrent();
break;
2020-11-25 22:28:58 +08:00
// Close left
2020-09-28 20:19:10 +08:00
case MenuEventEnum.CLOSE_LEFT:
closeLeft();
break;
2020-11-25 22:28:58 +08:00
// Close right
2020-09-28 20:19:10 +08:00
case MenuEventEnum.CLOSE_RIGHT:
closeRight();
break;
2020-11-25 22:28:58 +08:00
// Close other
2020-09-28 20:19:10 +08:00
case MenuEventEnum.CLOSE_OTHER:
closeOther();
break;
2020-11-25 22:28:58 +08:00
// Close all
2020-09-28 20:19:10 +08:00
case MenuEventEnum.CLOSE_ALL:
closeAll();
break;
}
}
return { getDropMenuList, handleMenuEvent };
}
2020-11-25 22:28:58 +08:00
export function getObj(tabItem: TabItem) {
const { params, path, query } = tabItem;
return {
params: params || {},
path,
query: query || {},
};
}
export function closeTab(closedTab: TabItem | AppRouteRecordRaw) {
2020-09-28 20:19:10 +08:00
const { currentRoute, replace } = router;
2020-11-25 22:28:58 +08:00
// Current tab list
const getTabsState = computed(() => tabStore.getTabsState);
2020-11-25 21:26:10 +08:00
2020-09-28 20:19:10 +08:00
const { path } = unref(currentRoute);
if (path !== closedTab.path) {
2020-11-25 22:28:58 +08:00
// Closed is not the activation tab
2020-09-28 20:19:10 +08:00
tabStore.commitCloseTab(closedTab);
return;
}
2020-11-25 22:28:58 +08:00
// Closed is activated atb
let toObj: RouteLocationRaw = {};
2020-11-25 22:28:58 +08:00
2020-09-28 20:19:10 +08:00
const index = unref(getTabsState).findIndex((item) => item.path === path);
2020-11-25 22:28:58 +08:00
// If the current is the leftmost tab
2020-09-28 20:19:10 +08:00
if (index === 0) {
2020-11-25 22:28:58 +08:00
// There is only one tab, then jump to the homepage, otherwise jump to the right tab
2020-09-28 20:19:10 +08:00
if (unref(getTabsState).length === 1) {
toObj = PageEnum.BASE_HOME;
2020-09-28 20:19:10 +08:00
} else {
2020-11-25 22:28:58 +08:00
// Jump to the right tab
const page = unref(getTabsState)[index + 1];
2020-11-25 22:28:58 +08:00
toObj = getObj(page);
2020-09-28 20:19:10 +08:00
}
} else {
2020-11-25 22:28:58 +08:00
// Close the current tab
const page = unref(getTabsState)[index - 1];
2020-11-25 22:28:58 +08:00
toObj = getObj(page);
2020-09-28 20:19:10 +08:00
}
const route = (unref(currentRoute) as unknown) as AppRouteRecordRaw;
tabStore.commitCloseTab(route);
replace(toObj);
2020-09-28 20:19:10 +08:00
}