use-extra-menu.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import type { MenuRecordRaw } from '@vben-core/typings';
  2. import { computed, ref } from 'vue';
  3. import { useRoute } from 'vue-router';
  4. import { findRootMenuByPath } from '@vben-core/helpers';
  5. import { preferences } from '@vben-core/preferences';
  6. import { useAccessStore } from '@vben-core/stores';
  7. import { useNavigation } from './use-navigation';
  8. function useExtraMenu() {
  9. const accessStore = useAccessStore();
  10. const { navigation } = useNavigation();
  11. const menus = computed(() => accessStore.getAccessMenus);
  12. const route = useRoute();
  13. const extraMenus = ref<MenuRecordRaw[]>([]);
  14. const sidebarExtraVisible = ref<boolean>(false);
  15. const extraActiveMenu = ref('');
  16. /**
  17. * 选择混合菜单事件
  18. * @param menu
  19. */
  20. const handleMixedMenuSelect = async (menu: MenuRecordRaw) => {
  21. extraMenus.value = menu?.children ?? [];
  22. extraActiveMenu.value = menu.parents?.[0] ?? menu.path;
  23. const hasChildren = extraMenus.value.length > 0;
  24. sidebarExtraVisible.value = hasChildren;
  25. if (!hasChildren) {
  26. await navigation(menu.path);
  27. }
  28. };
  29. /**
  30. * 选择默认菜单事件
  31. * @param menu
  32. * @param rootMenu
  33. */
  34. const handleDefaultSelect = (
  35. menu: MenuRecordRaw,
  36. rootMenu?: MenuRecordRaw,
  37. ) => {
  38. extraMenus.value = rootMenu?.children ?? [];
  39. extraActiveMenu.value = menu.parents?.[0] ?? menu.path;
  40. if (preferences.sidebar.expandOnHover) {
  41. sidebarExtraVisible.value = extraMenus.value.length > 0;
  42. }
  43. };
  44. /**
  45. * 侧边菜单鼠标移出事件
  46. */
  47. const handleSideMouseLeave = () => {
  48. if (preferences.sidebar.expandOnHover) {
  49. return;
  50. }
  51. sidebarExtraVisible.value = false;
  52. const { findMenu, rootMenu, rootMenuPath } = findRootMenuByPath(
  53. menus.value,
  54. route.path,
  55. );
  56. extraActiveMenu.value = rootMenuPath ?? findMenu?.path ?? '';
  57. extraMenus.value = rootMenu?.children ?? [];
  58. };
  59. const handleMenuMouseEnter = (menu: MenuRecordRaw) => {
  60. if (!preferences.sidebar.expandOnHover) {
  61. const { findMenu } = findRootMenuByPath(menus.value, menu.path);
  62. extraMenus.value = findMenu?.children ?? [];
  63. extraActiveMenu.value = menu.parents?.[0] ?? menu.path;
  64. sidebarExtraVisible.value = extraMenus.value.length > 0;
  65. }
  66. };
  67. return {
  68. extraActiveMenu,
  69. extraMenus,
  70. handleDefaultSelect,
  71. handleMenuMouseEnter,
  72. handleMixedMenuSelect,
  73. handleSideMouseLeave,
  74. sidebarExtraVisible,
  75. };
  76. }
  77. export { useExtraMenu };