vben
2021-02-22 be3a3ed699f73d352d49623ef07288093a3332c4
提交 | 用户 | age
ff2b12 1 import type { Menu as MenuType } from '/@/router/types';
V 2 import type { MenuState } from './types';
3
ca4f1a 4 import { computed, Ref, toRaw } from 'vue';
ff2b12 5
V 6 import { unref } from 'vue';
be3a3e 7 import { uniq } from 'lodash-es';
ff2b12 8 import { getAllParentPath } from '/@/router/helper/menuHelper';
be3a3e 9
ff2b12 10 import { useTimeoutFn } from '/@/hooks/core/useTimeout';
V 11
12 export function useOpenKeys(
13   menuState: MenuState,
14   menus: Ref<MenuType[]>,
15   accordion: Ref<boolean>,
ca4f1a 16   mixSider: Ref<boolean>,
V 17   collapse: Ref<boolean>
ff2b12 18   // mode: Ref<MenuModeEnum>,
V 19 ) {
20   async function setOpenKeys(path: string) {
21     // if (mode.value === MenuModeEnum.HORIZONTAL) {
22     //   return;
23     // }
24     const native = !mixSider.value;
25     useTimeoutFn(
26       () => {
27         const menuList = toRaw(menus.value);
28         if (menuList?.length === 0) {
29           menuState.activeSubMenuNames = [];
30           menuState.openNames = [];
31           return;
32         }
33         const keys = getAllParentPath(menuList, path);
34         if (!unref(accordion)) {
be3a3e 35           menuState.openNames = uniq([...menuState.openNames, ...keys]);
ff2b12 36         } else {
V 37           menuState.openNames = keys;
38         }
39         menuState.activeSubMenuNames = menuState.openNames;
40       },
41       16,
42       native
43     );
44   }
45
ca4f1a 46   const getOpenKeys = computed(() => {
V 47     return unref(collapse) ? [] : menuState.openNames;
48   });
49
50   return { setOpenKeys, getOpenKeys };
ff2b12 51 }