use-extra-menu.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import type { MenuRecordRaw } from '@vben-core/typings';
  2. import { preference } from '@vben/preference';
  3. import { useAccessStore } from '@vben/stores';
  4. import { computed, ref } from 'vue';
  5. import { useRoute } from 'vue-router';
  6. import { findRootMenuByPath } from './helper';
  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 extraVisible = 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. extraVisible.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 (preference.sideExpandOnHover) {
  41. extraVisible.value = extraMenus.value.length > 0;
  42. }
  43. };
  44. /**
  45. * 侧边菜单鼠标移出事件
  46. */
  47. const handleSideMouseLeave = () => {
  48. if (preference.sideExpandOnHover) {
  49. return;
  50. }
  51. extraVisible.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 (!preference.sideExpandOnHover) {
  61. const { findMenu } = findRootMenuByPath(menus.value, menu.path);
  62. extraMenus.value = findMenu?.children ?? [];
  63. extraActiveMenu.value = menu.parents?.[0] ?? menu.path;
  64. extraVisible.value = extraMenus.value.length > 0;
  65. }
  66. };
  67. return {
  68. extraActiveMenu,
  69. extraMenus,
  70. extraVisible,
  71. handleDefaultSelect,
  72. handleMenuMouseEnter,
  73. handleMixedMenuSelect,
  74. handleSideMouseLeave,
  75. };
  76. }
  77. export { useExtraMenu };