useMenuSetting.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import type { MenuSetting } from '/#/config';
  2. import { computed, unref, ref } from 'vue';
  3. import { useAppStore } from '/@/store/modules/app';
  4. import { SIDE_BAR_MINI_WIDTH, SIDE_BAR_SHOW_TIT_MINI_WIDTH } from '/@/enums/appEnum';
  5. import { MenuModeEnum, MenuTypeEnum, TriggerEnum } from '/@/enums/menuEnum';
  6. import { useFullContent } from '/@/hooks/web/useFullContent';
  7. const mixSideHasChildren = ref(false);
  8. export function useMenuSetting() {
  9. const { getFullContent: fullContent } = useFullContent();
  10. const appStore = useAppStore();
  11. const getShowSidebar = computed(() => {
  12. return unref(getSplit) || (unref(getShowMenu) && unref(getMenuMode) !== MenuModeEnum.HORIZONTAL && !unref(fullContent));
  13. });
  14. const getCollapsed = computed(() => appStore.getMenuSetting.collapsed);
  15. const getMenuType = computed(() => appStore.getMenuSetting.type);
  16. const getMenuMode = computed(() => appStore.getMenuSetting.mode);
  17. const getMenuFixed = computed(() => appStore.getMenuSetting.fixed);
  18. const getShowMenu = computed(() => appStore.getMenuSetting.show);
  19. const getMenuHidden = computed(() => appStore.getMenuSetting.hidden);
  20. const getMenuWidth = computed(() => {
  21. const scale = document.body.clientWidth / 1920;
  22. const menuWidth = appStore.getMenuSetting.menuWidth;
  23. return menuWidth * scale;
  24. });
  25. const getTrigger = computed(() => appStore.getMenuSetting.trigger);
  26. const getMenuTheme = computed(() => appStore.getMenuSetting.theme);
  27. const getSplit = computed(() => appStore.getMenuSetting.split);
  28. const getMenuBgColor = computed(() => appStore.getMenuSetting.bgColor);
  29. const getMixSideTrigger = computed(() => appStore.getMenuSetting.mixSideTrigger);
  30. const getCanDrag = computed(() => appStore.getMenuSetting.canDrag);
  31. const getAccordion = computed(() => appStore.getMenuSetting.accordion);
  32. const getMixSideFixed = computed(() => appStore.getMenuSetting.mixSideFixed);
  33. const getTopMenuAlign = computed(() => appStore.getMenuSetting.topMenuAlign);
  34. const getCloseMixSidebarOnChange = computed(() => appStore.getMenuSetting.closeMixSidebarOnChange);
  35. const getIsSidebarType = computed(() => unref(getMenuType) === MenuTypeEnum.SIDEBAR);
  36. const getIsTopMenu = computed(() => unref(getMenuType) === MenuTypeEnum.TOP_MENU);
  37. const getIsBottomMenu = computed(() => unref(getMenuType) === MenuTypeEnum.BOTTOM_MENU);
  38. const getIsBottomMenuH = computed(() => unref(getMenuType) === MenuTypeEnum.BOTTOM_MENU_H);
  39. const getCollapsedShowTitle = computed(() => appStore.getMenuSetting.collapsedShowTitle);
  40. const getShowTopMenu = computed(() => {
  41. return unref(getMenuMode) === MenuModeEnum.HORIZONTAL || unref(getSplit);
  42. });
  43. const getShowHeaderTrigger = computed(() => {
  44. if (unref(getMenuType) === MenuTypeEnum.TOP_MENU || !unref(getShowMenu) || unref(getMenuHidden)) {
  45. return false;
  46. }
  47. return unref(getTrigger) === TriggerEnum.HEADER;
  48. });
  49. const getIsHorizontal = computed(() => {
  50. return unref(getMenuMode) === MenuModeEnum.HORIZONTAL;
  51. });
  52. const getIsMixSidebar = computed(() => {
  53. return unref(getMenuType) === MenuTypeEnum.MIX_SIDEBAR;
  54. });
  55. const getIsMixMode = computed(() => {
  56. return unref(getMenuMode) === MenuModeEnum.INLINE && unref(getMenuType) === MenuTypeEnum.MIX;
  57. });
  58. const getRealWidth = computed(() => {
  59. if (unref(getIsMixSidebar)) {
  60. return unref(getCollapsed) && !unref(getMixSideFixed) ? unref(getMiniWidthNumber) : unref(getMenuWidth);
  61. }
  62. return unref(getCollapsed) ? unref(getMiniWidthNumber) : unref(getMenuWidth);
  63. });
  64. const getMiniWidthNumber = computed(() => {
  65. const { collapsedShowTitle } = appStore.getMenuSetting;
  66. return collapsedShowTitle ? SIDE_BAR_SHOW_TIT_MINI_WIDTH : SIDE_BAR_MINI_WIDTH;
  67. });
  68. const getCalcContentWidth = computed(() => {
  69. const width =
  70. unref(getIsTopMenu) || !unref(getShowMenu) || (unref(getSplit) && unref(getMenuHidden))
  71. ? 0
  72. : unref(getIsMixSidebar)
  73. ? (unref(getCollapsed) ? SIDE_BAR_MINI_WIDTH : SIDE_BAR_SHOW_TIT_MINI_WIDTH) +
  74. (unref(getMixSideFixed) && unref(mixSideHasChildren) ? unref(getRealWidth) : 0)
  75. : unref(getRealWidth);
  76. return `calc(100% - ${unref(width)}px)`;
  77. });
  78. // Set menu configuration
  79. function setMenuSetting(menuSetting: Partial<MenuSetting>): void {
  80. appStore.setProjectConfig({ menuSetting });
  81. }
  82. function toggleCollapsed() {
  83. setMenuSetting({
  84. collapsed: !unref(getCollapsed),
  85. });
  86. // const event = document.createEvent('HTMLEvents');
  87. // event.initEvent('resize', true, true);
  88. // window.dispatchEvent(event);
  89. }
  90. return {
  91. setMenuSetting,
  92. toggleCollapsed,
  93. getMenuFixed,
  94. getRealWidth,
  95. getMenuType,
  96. getMenuMode,
  97. getShowMenu,
  98. getCollapsed,
  99. getMiniWidthNumber,
  100. getCalcContentWidth,
  101. getMenuWidth,
  102. getTrigger,
  103. getSplit,
  104. getMenuTheme,
  105. getCanDrag,
  106. getCollapsedShowTitle,
  107. getIsHorizontal,
  108. getIsSidebarType,
  109. getAccordion,
  110. getShowTopMenu,
  111. getShowHeaderTrigger,
  112. getTopMenuAlign,
  113. getMenuHidden,
  114. getIsTopMenu,
  115. getIsBottomMenu,
  116. getIsBottomMenuH,
  117. getMenuBgColor,
  118. getShowSidebar,
  119. getIsMixMode,
  120. getIsMixSidebar,
  121. getCloseMixSidebarOnChange,
  122. getMixSideTrigger,
  123. getMixSideFixed,
  124. mixSideHasChildren,
  125. };
  126. }