Ben Lin
2024-06-18 ebbd788fbb2c0b45d4473798efc57eec8ba74a25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import type { MenuSetting } from '#/config';
 
import { computed, unref, ref } from 'vue';
 
import { useAppStore } from '@/store/modules/app';
 
import { SIDE_BAR_MINI_WIDTH, SIDE_BAR_SHOW_TIT_MINI_WIDTH } from '@/enums/appEnum';
import { MenuModeEnum, MenuTypeEnum, TriggerEnum } from '@/enums/menuEnum';
import { useFullContent } from '@/hooks/web/useFullContent';
 
const mixSideHasChildren = ref(false);
 
export function useMenuSetting() {
  const { getFullContent: fullContent } = useFullContent();
  const appStore = useAppStore();
 
  const getShowSidebar = computed(() => {
    return (
      unref(getSplit) ||
      (unref(getShowMenu) && unref(getMenuMode) !== MenuModeEnum.HORIZONTAL && !unref(fullContent))
    );
  });
 
  const getCollapsed = computed(() => appStore.getMenuSetting.collapsed);
 
  const getMenuType = computed(() => appStore.getMenuSetting.type);
 
  const getMenuMode = computed(() => appStore.getMenuSetting.mode);
 
  const getMenuFixed = computed(() => appStore.getMenuSetting.fixed);
 
  const getShowMenu = computed(() => appStore.getMenuSetting.show);
 
  const getMenuHidden = computed(() => appStore.getMenuSetting.hidden);
 
  const getMenuWidth = computed(() => appStore.getMenuSetting.menuWidth);
 
  const getTrigger = computed(() => appStore.getMenuSetting.trigger);
 
  const getMenuTheme = computed(() => appStore.getMenuSetting.theme);
 
  const getSplit = computed(() => appStore.getMenuSetting.split);
 
  const getMenuBgColor = computed(() => appStore.getMenuSetting.bgColor);
 
  const getMixSideTrigger = computed(() => appStore.getMenuSetting.mixSideTrigger);
 
  const getCanDrag = computed(() => appStore.getMenuSetting.canDrag);
 
  const getAccordion = computed(() => appStore.getMenuSetting.accordion);
 
  const getMixSideFixed = computed(() => appStore.getMenuSetting.mixSideFixed);
 
  const getTopMenuAlign = computed(() => appStore.getMenuSetting.topMenuAlign);
 
  const getCloseMixSidebarOnChange = computed(
    () => appStore.getMenuSetting.closeMixSidebarOnChange,
  );
 
  const getIsSidebarType = computed(() => unref(getMenuType) === MenuTypeEnum.SIDEBAR);
 
  const getIsTopMenu = computed(() => unref(getMenuType) === MenuTypeEnum.TOP_MENU);
 
  const getCollapsedShowTitle = computed(() => appStore.getMenuSetting.collapsedShowTitle);
 
  const getShowTopMenu = computed(() => {
    return unref(getMenuMode) === MenuModeEnum.HORIZONTAL || unref(getSplit);
  });
 
  const getShowHeaderTrigger = computed(() => {
    if (
      unref(getMenuType) === MenuTypeEnum.TOP_MENU ||
      !unref(getShowMenu) ||
      unref(getMenuHidden)
    ) {
      return false;
    }
 
    return unref(getTrigger) === TriggerEnum.HEADER;
  });
 
  const getIsHorizontal = computed(() => {
    return unref(getMenuMode) === MenuModeEnum.HORIZONTAL;
  });
 
  const getIsMixSidebar = computed(() => {
    return unref(getMenuType) === MenuTypeEnum.MIX_SIDEBAR;
  });
 
  const getIsMixMode = computed(() => {
    return unref(getMenuMode) === MenuModeEnum.INLINE && unref(getMenuType) === MenuTypeEnum.MIX;
  });
 
  const getRealWidth = computed(() => {
    if (unref(getIsMixSidebar)) {
      return unref(getCollapsed) && !unref(getMixSideFixed)
        ? unref(getMiniWidthNumber)
        : unref(getMenuWidth);
    }
    return unref(getCollapsed) ? unref(getMiniWidthNumber) : unref(getMenuWidth);
  });
 
  const getMiniWidthNumber = computed(() => {
    const { collapsedShowTitle, siderHidden } = appStore.getMenuSetting;
    return siderHidden
      ? 0
      : collapsedShowTitle
        ? SIDE_BAR_SHOW_TIT_MINI_WIDTH
        : SIDE_BAR_MINI_WIDTH;
  });
 
  const getCalcContentWidth = computed(() => {
    const width =
      unref(getIsTopMenu) || !unref(getShowMenu) || (unref(getSplit) && unref(getMenuHidden))
        ? 0
        : unref(getIsMixSidebar)
          ? (unref(getCollapsed) ? SIDE_BAR_MINI_WIDTH : SIDE_BAR_SHOW_TIT_MINI_WIDTH) +
            (unref(getMixSideFixed) && unref(mixSideHasChildren) ? unref(getRealWidth) : 0)
          : unref(getRealWidth);
 
    return `calc(100% - ${unref(width)}px)`;
  });
 
  // Set menu configuration
  function setMenuSetting(menuSetting: Partial<MenuSetting>): void {
    appStore.setMenuSetting(menuSetting);
  }
 
  function toggleCollapsed() {
    setMenuSetting({
      collapsed: !unref(getCollapsed),
    });
  }
  return {
    setMenuSetting,
    toggleCollapsed,
    getMenuFixed,
    getRealWidth,
    getMenuType,
    getMenuMode,
    getShowMenu,
    getCollapsed,
    getMiniWidthNumber,
    getCalcContentWidth,
    getMenuWidth,
    getTrigger,
    getSplit,
    getMenuTheme,
    getCanDrag,
    getCollapsedShowTitle,
    getIsHorizontal,
    getIsSidebarType,
    getAccordion,
    getShowTopMenu,
    getShowHeaderTrigger,
    getTopMenuAlign,
    getMenuHidden,
    getIsTopMenu,
    getMenuBgColor,
    getShowSidebar,
    getIsMixMode,
    getIsMixSidebar,
    getCloseMixSidebarOnChange,
    getMixSideTrigger,
    getMixSideFixed,
    mixSideHasChildren,
  };
}