Blame view

src/layouts/default/menu/useLayoutMenu.ts 2.75 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

      const parentPath = await getCurrentParentPath(path);
      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) {
70
    if (unref(getSplitLeft) || unref(getIsMobile)) return;
vben authored
71
72
73
74

    // spilt mode left
    const children = await getChildrenMenus(parentPath);
    if (!children) {
75
      setMenuSetting({ hidden: true });
vben authored
76
77
78
79
      menusRef.value = [];
      return;
    }
80
    setMenuSetting({ hidden: false });
vben authored
81
82
83
84
85
86
    menusRef.value = children;
  }

  // get menus
  async function genMenus() {
    // normal mode
87
    if (unref(normalType) || unref(getIsMobile)) {
vben authored
88
89
90
91
92
      menusRef.value = await getMenus();
      return;
    }

    // split-top
vben authored
93
    if (unref(getSpiltTop)) {
vben authored
94
95
96
97
98
99
      const shallowMenus = await getShallowMenus();

      menusRef.value = shallowMenus;
      return;
    }
  }
vben authored
100
vben authored
101
  return { menusRef };
vben authored
102
}