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

140 lines
3.9 KiB
TypeScript
Raw Normal View History

2020-12-03 21:49:32 +08:00
import type { TabContentProps } from './types';
2020-09-28 20:19:10 +08:00
import type { DropMenu } from '/@/components/Dropdown';
2021-04-10 19:25:49 +08:00
import type { ComputedRef } from 'vue';
2020-09-28 20:19:10 +08:00
2020-12-03 21:49:32 +08:00
import { computed, unref, reactive } from 'vue';
2021-04-10 19:25:49 +08:00
import { MenuEventEnum } from './types';
import { useMultipleTabStore } from '/@/store/modules/multipleTab';
import { RouteLocationNormalized, useRouter } from 'vue-router';
2020-12-03 21:49:32 +08:00
import { useTabs } from '/@/hooks/web/useTabs';
import { useI18n } from '/@/hooks/web/useI18n';
2020-09-28 20:19:10 +08:00
2021-04-10 19:25:49 +08:00
export function useTabDropdown(tabContentProps: TabContentProps, getIsTabs: ComputedRef<boolean>) {
2020-12-03 21:49:32 +08:00
const state = reactive({
current: null as Nullable<RouteLocationNormalized>,
currentIndex: 0,
2020-09-28 20:19:10 +08:00
});
2021-04-10 19:25:49 +08:00
const { t } = useI18n();
const tabStore = useMultipleTabStore();
const { currentRoute } = useRouter();
const { refreshPage, closeAll, close, closeLeft, closeOther, closeRight } = useTabs();
2020-12-03 21:49:32 +08:00
2021-04-10 19:25:49 +08:00
const getTargetTab = computed(
2020-12-03 21:49:32 +08:00
(): RouteLocationNormalized => {
2021-04-10 19:25:49 +08:00
return unref(getIsTabs) ? tabContentProps.tabItem : unref(currentRoute);
2020-12-03 21:49:32 +08:00
}
);
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(() => {
2021-04-10 19:25:49 +08:00
if (!unref(getTargetTab)) {
return;
}
const { meta } = unref(getTargetTab);
2020-12-03 21:49:32 +08:00
const { path } = unref(currentRoute);
2020-09-28 20:19:10 +08:00
2020-11-25 22:28:58 +08:00
// Refresh button
2020-12-03 21:49:32 +08:00
const curItem = state.current;
const index = state.currentIndex;
2020-09-28 20:19:10 +08:00
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;
2021-04-10 19:25:49 +08:00
const disabled = tabStore.getTabList.length === 1;
2020-12-03 21:49:32 +08:00
2020-11-25 22:28:58 +08:00
// Close right
2020-12-03 21:49:32 +08:00
const closeRightDisabled =
2021-04-10 19:25:49 +08:00
index === tabStore.getTabList.length - 1 && tabStore.getLastDragEndIndex >= 0;
2020-12-03 21:49:32 +08:00
const dropMenuList: DropMenu[] = [
{
2020-12-08 00:22:55 +08:00
icon: 'ion:reload-sharp',
2020-12-03 21:49:32 +08:00
event: MenuEventEnum.REFRESH_PAGE,
2021-01-12 21:10:27 +08:00
text: t('layout.multipleTab.reload'),
2020-12-03 21:49:32 +08:00
disabled: refreshDisabled,
},
{
2020-12-08 00:22:55 +08:00
icon: 'clarity:close-line',
2020-12-03 21:49:32 +08:00
event: MenuEventEnum.CLOSE_CURRENT,
text: t('layout.multipleTab.close'),
2021-04-10 19:25:49 +08:00
disabled: !!meta?.affix || disabled,
2020-12-03 21:49:32 +08:00
divider: true,
},
{
2020-12-08 00:22:55 +08:00
icon: 'line-md:arrow-close-left',
2020-12-03 21:49:32 +08:00
event: MenuEventEnum.CLOSE_LEFT,
text: t('layout.multipleTab.closeLeft'),
disabled: closeLeftDisabled,
divider: false,
},
{
2020-12-08 00:22:55 +08:00
icon: 'line-md:arrow-close-right',
2020-12-03 21:49:32 +08:00
event: MenuEventEnum.CLOSE_RIGHT,
text: t('layout.multipleTab.closeRight'),
disabled: closeRightDisabled,
divider: true,
},
{
2020-12-08 00:22:55 +08:00
icon: 'dashicons:align-center',
2020-12-03 21:49:32 +08:00
event: MenuEventEnum.CLOSE_OTHER,
text: t('layout.multipleTab.closeOther'),
disabled: disabled,
},
{
2020-12-08 00:22:55 +08:00
icon: 'clarity:minus-line',
2020-12-03 21:49:32 +08:00
event: MenuEventEnum.CLOSE_ALL,
text: t('layout.multipleTab.closeAll'),
disabled: disabled,
},
];
2020-09-28 20:19:10 +08:00
return dropMenuList;
});
2020-12-03 21:49:32 +08:00
function handleContextMenu(tabItem: RouteLocationNormalized) {
return (e: Event) => {
2021-04-10 19:25:49 +08:00
if (!tabItem) {
return;
}
2020-12-03 21:49:32 +08:00
e?.preventDefault();
2021-04-10 19:25:49 +08:00
const index = tabStore.getTabList.findIndex((tab) => tab.path === tabItem.path);
2020-12-03 21:49:32 +08:00
state.current = tabItem;
state.currentIndex = index;
};
2020-09-28 20:19:10 +08:00
}
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.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:
2021-01-06 20:10:16 +08:00
close(tabContentProps.tabItem);
2020-09-28 20:19:10 +08:00
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;
}
}
2021-04-10 19:25:49 +08:00
return { getDropMenuList, handleMenuEvent, handleContextMenu };
2020-09-28 20:19:10 +08:00
}