Blame view

src/layouts/default/menu/useLayoutMenu.ts 2.96 KB
vben authored
1
2
3
4
5
6
7
8
9
10
import type { Menu } from '/@/router/types';
import type { Ref } from 'vue';

import { watch, unref, ref, computed } from 'vue';
import { useRouter } from 'vue-router';

import { MenuSplitTyeEnum } from '/@/enums/menuEnum';
import { useThrottle } from '/@/hooks/core/useThrottle';
import { useMenuSetting } from '/@/hooks/setting/useMenuSetting';
vben authored
11
import { getChildrenMenus, getCurrentParentPath, getMenus, getShallowMenus } from '/@/router/menus';
vben authored
12
import { permissionStore } from '/@/store/modules/permission';
13
import { useAppInject } from '/@/hooks/web/useAppInject';
vben authored
14
15
16
17
18

export function useSplitMenu(splitType: Ref<MenuSplitTyeEnum>) {
  // Menu array
  const menusRef = ref<Menu[]>([]);
  const { currentRoute } = useRouter();
19
  const { getIsMobile } = useAppInject();
vben authored
20
21
22
23
24
25
26
27
  const { setMenuSetting, getIsHorizontal, getSplit } = useMenuSetting();

  const [throttleHandleSplitLeftMenu] = useThrottle(handleSplitLeftMenu, 50);

  const splitNotLeft = computed(
    () => unref(splitType) !== MenuSplitTyeEnum.LEFT && !unref(getIsHorizontal)
  );
vben authored
28
29
30
  const getSplitLeft = computed(
    () => !unref(getSplit) || unref(splitType) !== MenuSplitTyeEnum.LEFT
  );
vben authored
31
vben authored
32
  const getSpiltTop = computed(() => unref(splitType) === MenuSplitTyeEnum.TOP);
vben authored
33
34
35
36
37
38
39
40

  const normalType = computed(() => {
    return unref(splitType) === MenuSplitTyeEnum.NONE || !unref(getSplit);
  });

  watch(
    [() => unref(currentRoute).path, () => unref(splitType)],
    async ([path]: [string, MenuSplitTyeEnum]) => {
41
      if (unref(splitNotLeft) || unref(getIsMobile)) return;
vben authored
42
43
44
45
46
47
48
      const { meta } = unref(currentRoute);
      const currentActiveMenu = meta.currentActiveMenu;
      let parentPath = await getCurrentParentPath(path);
      if (!parentPath) {
        parentPath = await getCurrentParentPath(currentActiveMenu);
      }
vben authored
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
      parentPath && throttleHandleSplitLeftMenu(parentPath);
    },
    {
      immediate: true,
    }
  );

  // Menu changes
  watch(
    [() => permissionStore.getLastBuildMenuTimeState, () => permissionStore.getBackMenuListState],
    () => {
      genMenus();
    },
    {
      immediate: true,
    }
  );

  // split Menu changes
  watch([() => getSplit.value], () => {
    if (unref(splitNotLeft)) return;
    genMenus();
  });

  // Handle left menu split
  async function handleSplitLeftMenu(parentPath: string) {
75
    if (unref(getSplitLeft) || unref(getIsMobile)) return;
vben authored
76
77
78

    // spilt mode left
    const children = await getChildrenMenus(parentPath);
79
80

    if (!children || !children.length) {
81
      setMenuSetting({ hidden: true });
vben authored
82
83
84
85
      menusRef.value = [];
      return;
    }
86
    setMenuSetting({ hidden: false });
vben authored
87
88
89
90
91
92
    menusRef.value = children;
  }

  // get menus
  async function genMenus() {
    // normal mode
93
    if (unref(normalType) || unref(getIsMobile)) {
vben authored
94
95
96
97
98
      menusRef.value = await getMenus();
      return;
    }

    // split-top
vben authored
99
    if (unref(getSpiltTop)) {
vben authored
100
101
102
103
104
105
      const shallowMenus = await getShallowMenus();

      menusRef.value = shallowMenus;
      return;
    }
  }
vben authored
106
vben authored
107
  return { menusRef };
vben authored
108
}