2024-05-19 21:20:42 +08:00
|
|
|
import type { MenuRecordRaw } from '@vben-core/typings';
|
|
|
|
|
2024-07-18 21:59:18 +08:00
|
|
|
import { computed, ref, watch } from 'vue';
|
2024-05-21 21:45:48 +08:00
|
|
|
import { useRoute } from 'vue-router';
|
2024-05-19 21:20:42 +08:00
|
|
|
|
2024-06-09 15:39:11 +08:00
|
|
|
import { findRootMenuByPath } from '@vben-core/helpers';
|
2024-06-08 19:49:06 +08:00
|
|
|
import { preferences } from '@vben-core/preferences';
|
2024-07-05 23:15:46 +08:00
|
|
|
import { useCoreAccessStore } from '@vben-core/stores';
|
2024-06-08 19:49:06 +08:00
|
|
|
|
2024-05-21 21:45:48 +08:00
|
|
|
import { useNavigation } from './use-navigation';
|
2024-05-19 21:20:42 +08:00
|
|
|
|
|
|
|
function useExtraMenu() {
|
2024-07-05 23:15:46 +08:00
|
|
|
const accessStore = useCoreAccessStore();
|
2024-05-21 21:45:48 +08:00
|
|
|
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 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
|
|
|
|
*/
|
2024-05-21 21:45:48 +08:00
|
|
|
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) {
|
2024-05-21 21:45:48 +08:00
|
|
|
await navigation(menu.path);
|
2024-05-19 21:20:42 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 选择默认菜单事件
|
|
|
|
* @param menu
|
|
|
|
* @param rootMenu
|
|
|
|
*/
|
|
|
|
const handleDefaultSelect = (
|
|
|
|
menu: MenuRecordRaw,
|
|
|
|
rootMenu?: MenuRecordRaw,
|
|
|
|
) => {
|
|
|
|
extraMenus.value = rootMenu?.children ?? [];
|
|
|
|
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 = () => {
|
2024-06-01 23:15:29 +08:00
|
|
|
if (preferences.sidebar.expandOnHover) {
|
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 { findMenu, rootMenu, rootMenuPath } = findRootMenuByPath(
|
|
|
|
menus.value,
|
|
|
|
route.path,
|
|
|
|
);
|
|
|
|
extraActiveMenu.value = rootMenuPath ?? findMenu?.path ?? '';
|
|
|
|
extraMenus.value = rootMenu?.children ?? [];
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-07-18 21:59:18 +08:00
|
|
|
watch(
|
|
|
|
() => route.path,
|
|
|
|
() => {
|
|
|
|
const { findMenu, rootMenu, rootMenuPath } = findRootMenuByPath(
|
|
|
|
menus.value,
|
|
|
|
route.path,
|
|
|
|
);
|
|
|
|
extraActiveMenu.value = rootMenuPath ?? findMenu?.path ?? '';
|
|
|
|
extraMenus.value = rootMenu?.children ?? [];
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
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 };
|