Files
aiot-platform-ui/packages/effects/layouts/src/basic/menu/use-extra-menu.ts

137 lines
3.9 KiB
TypeScript
Raw Normal View History

import type { MenuRecordRaw } from '@vben/types';
2024-05-19 21:20:42 +08:00
import { computed, nextTick, ref, watch } from 'vue';
import { useRoute } from 'vue-router';
2024-05-19 21:20:42 +08:00
import { preferences } from '@vben/preferences';
2024-07-30 21:10:28 +08:00
import { useAccessStore } from '@vben/stores';
import { findRootMenuByPath } from '@vben/utils';
2024-06-08 19:49:06 +08:00
import { useNavigation } from './use-navigation';
2024-05-19 21:20:42 +08:00
function useExtraMenu() {
2024-07-30 21:10:28 +08:00
const accessStore = useAccessStore();
const { navigation } = useNavigation();
2024-05-19 21:20:42 +08:00
2024-07-10 00:50:41 +08:00
const menus = computed(() => accessStore.accessMenus);
2024-05-19 21:20:42 +08:00
/** 记录当前顶级菜单下哪个子菜单最后激活 */
const defaultSubMap = new Map<string, string>();
const extraRootMenus = ref<MenuRecordRaw[]>([]);
2024-05-19 21:20:42 +08:00
const route = useRoute();
const extraMenus = ref<MenuRecordRaw[]>([]);
2024-06-09 15:39:11 +08:00
const sidebarExtraVisible = ref<boolean>(false);
2024-05-19 21:20:42 +08:00
const extraActiveMenu = ref('');
/**
*
* @param menu
*/
const handleMixedMenuSelect = async (menu: MenuRecordRaw) => {
2024-05-19 21:20:42 +08:00
extraMenus.value = menu?.children ?? [];
extraActiveMenu.value = menu.parents?.[0] ?? menu.path;
const hasChildren = extraMenus.value.length > 0;
2024-06-09 15:39:11 +08:00
sidebarExtraVisible.value = hasChildren;
2024-05-19 21:20:42 +08:00
if (!hasChildren) {
await navigation(menu.path);
} else if (preferences.sidebar.autoActivateChild) {
await navigation(
defaultSubMap.has(menu.path)
? (defaultSubMap.get(menu.path) as string)
: menu.path,
);
2024-05-19 21:20:42 +08:00
}
};
/**
*
* @param menu
* @param rootMenu
*/
const handleDefaultSelect = async (
2024-05-19 21:20:42 +08:00
menu: MenuRecordRaw,
rootMenu?: MenuRecordRaw,
) => {
await nextTick();
extraMenus.value = rootMenu?.children ?? extraRootMenus.value ?? [];
2024-05-19 21:20:42 +08:00
extraActiveMenu.value = menu.parents?.[0] ?? menu.path;
2024-06-01 23:15:29 +08:00
if (preferences.sidebar.expandOnHover) {
2024-06-09 15:39:11 +08:00
sidebarExtraVisible.value = extraMenus.value.length > 0;
2024-05-19 21:20:42 +08:00
}
};
/**
*
*/
const handleSideMouseLeave = () => {
// const { findMenu, rootMenu, rootMenuPath } = findRootMenuByPath(
// menus.value,
// route.path,
// );
calcExtraMenus(route.path);
2024-06-01 23:15:29 +08:00
if (preferences.sidebar.expandOnHover) {
sidebarExtraVisible.value = extraMenus.value.length > 0;
2024-05-19 21:20:42 +08:00
return;
}
2024-06-09 15:39:11 +08:00
sidebarExtraVisible.value = false;
2024-05-19 21:20:42 +08:00
};
const handleMenuMouseEnter = (menu: MenuRecordRaw) => {
2024-06-01 23:15:29 +08:00
if (!preferences.sidebar.expandOnHover) {
2024-05-19 21:20:42 +08:00
const { findMenu } = findRootMenuByPath(menus.value, menu.path);
extraMenus.value = findMenu?.children ?? [];
extraActiveMenu.value = menu.parents?.[0] ?? menu.path;
2024-06-09 15:39:11 +08:00
sidebarExtraVisible.value = extraMenus.value.length > 0;
2024-05-19 21:20:42 +08:00
}
};
function calcExtraMenus(path: string) {
const currentPath = route.meta?.activePath || path;
const { findMenu, rootMenu, rootMenuPath } = findRootMenuByPath(
menus.value,
currentPath,
);
if (preferences.app.layout === 'header-mixed-nav') {
const subExtra = findRootMenuByPath(
rootMenu?.children ?? [],
currentPath,
1,
);
extraRootMenus.value = subExtra.rootMenu?.children ?? [];
extraActiveMenu.value = subExtra.rootMenuPath ?? '';
extraMenus.value = subExtra.rootMenu?.children ?? [];
} else {
extraRootMenus.value = rootMenu?.children ?? [];
if (rootMenuPath) defaultSubMap.set(rootMenuPath, currentPath);
extraActiveMenu.value = rootMenuPath ?? findMenu?.path ?? '';
extraMenus.value = rootMenu?.children ?? [];
}
if (preferences.sidebar.expandOnHover) {
sidebarExtraVisible.value = extraMenus.value.length > 0;
}
}
watch(
() => [route.path, preferences.app.layout],
([path]) => {
calcExtraMenus(path || '');
},
{ immediate: true },
);
2024-05-19 21:20:42 +08:00
return {
extraActiveMenu,
extraMenus,
handleDefaultSelect,
handleMenuMouseEnter,
handleMixedMenuSelect,
handleSideMouseLeave,
2024-06-09 15:39:11 +08:00
sidebarExtraVisible,
2024-05-19 21:20:42 +08:00
};
}
export { useExtraMenu };