useMenuSetting.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 (
  13. unref(getSplit) ||
  14. (unref(getShowMenu) && unref(getMenuMode) !== MenuModeEnum.HORIZONTAL && !unref(fullContent))
  15. );
  16. });
  17. const getCollapsed = computed(() => appStore.getMenuSetting.collapsed);
  18. const getMenuType = computed(() => appStore.getMenuSetting.type);
  19. const getMenuMode = computed(() => appStore.getMenuSetting.mode);
  20. const getMenuFixed = computed(() => appStore.getMenuSetting.fixed);
  21. const getShowMenu = computed(() => appStore.getMenuSetting.show);
  22. const getMenuHidden = computed(() => appStore.getMenuSetting.hidden);
  23. const getMenuWidth = computed(() => appStore.getMenuSetting.menuWidth);
  24. const getTrigger = computed(() => appStore.getMenuSetting.trigger);
  25. const getMenuTheme = computed(() => appStore.getMenuSetting.theme);
  26. const getSplit = computed(() => appStore.getMenuSetting.split);
  27. const getMenuBgColor = computed(() => appStore.getMenuSetting.bgColor);
  28. const getMixSideTrigger = computed(() => appStore.getMenuSetting.mixSideTrigger);
  29. const getCanDrag = computed(() => appStore.getMenuSetting.canDrag);
  30. const getAccordion = computed(() => appStore.getMenuSetting.accordion);
  31. const getMixSideFixed = computed(() => appStore.getMenuSetting.mixSideFixed);
  32. const getTopMenuAlign = computed(() => appStore.getMenuSetting.topMenuAlign);
  33. const getCloseMixSidebarOnChange = computed(
  34. () => appStore.getMenuSetting.closeMixSidebarOnChange,
  35. );
  36. const getIsSidebarType = computed(() => unref(getMenuType) === MenuTypeEnum.SIDEBAR);
  37. const getIsTopMenu = computed(() => unref(getMenuType) === MenuTypeEnum.TOP_MENU);
  38. const getCollapsedShowTitle = computed(() => appStore.getMenuSetting.collapsedShowTitle);
  39. const getShowTopMenu = computed(() => {
  40. return unref(getMenuMode) === MenuModeEnum.HORIZONTAL || unref(getSplit);
  41. });
  42. const getShowHeaderTrigger = computed(() => {
  43. if (
  44. unref(getMenuType) === MenuTypeEnum.TOP_MENU ||
  45. !unref(getShowMenu) ||
  46. unref(getMenuHidden)
  47. ) {
  48. return false;
  49. }
  50. return unref(getTrigger) === TriggerEnum.HEADER;
  51. });
  52. const getIsHorizontal = computed(() => {
  53. return unref(getMenuMode) === MenuModeEnum.HORIZONTAL;
  54. });
  55. const getIsMixSidebar = computed(() => {
  56. return unref(getMenuType) === MenuTypeEnum.MIX_SIDEBAR;
  57. });
  58. const getIsMixMode = computed(() => {
  59. return unref(getMenuMode) === MenuModeEnum.INLINE && unref(getMenuType) === MenuTypeEnum.MIX;
  60. });
  61. const getRealWidth = computed(() => {
  62. if (unref(getIsMixSidebar)) {
  63. return unref(getCollapsed) && !unref(getMixSideFixed)
  64. ? unref(getMiniWidthNumber)
  65. : unref(getMenuWidth);
  66. }
  67. return unref(getCollapsed) ? unref(getMiniWidthNumber) : unref(getMenuWidth);
  68. });
  69. const getMiniWidthNumber = computed(() => {
  70. const { collapsedShowTitle, siderHidden } = appStore.getMenuSetting;
  71. return siderHidden
  72. ? 0
  73. : collapsedShowTitle
  74. ? SIDE_BAR_SHOW_TIT_MINI_WIDTH
  75. : SIDE_BAR_MINI_WIDTH;
  76. });
  77. const getCalcContentWidth = computed(() => {
  78. const width =
  79. unref(getIsTopMenu) || !unref(getShowMenu) || (unref(getSplit) && unref(getMenuHidden))
  80. ? 0
  81. : unref(getIsMixSidebar)
  82. ? (unref(getCollapsed) ? SIDE_BAR_MINI_WIDTH : SIDE_BAR_SHOW_TIT_MINI_WIDTH) +
  83. (unref(getMixSideFixed) && unref(mixSideHasChildren) ? unref(getRealWidth) : 0)
  84. : unref(getRealWidth);
  85. return `calc(100% - ${unref(width)}px)`;
  86. });
  87. // Set menu configuration
  88. function setMenuSetting(menuSetting: Partial<MenuSetting>): void {
  89. appStore.setMenuSetting(menuSetting);
  90. }
  91. function toggleCollapsed() {
  92. setMenuSetting({
  93. collapsed: !unref(getCollapsed),
  94. });
  95. }
  96. return {
  97. setMenuSetting,
  98. toggleCollapsed,
  99. getMenuFixed,
  100. getRealWidth,
  101. getMenuType,
  102. getMenuMode,
  103. getShowMenu,
  104. getCollapsed,
  105. getMiniWidthNumber,
  106. getCalcContentWidth,
  107. getMenuWidth,
  108. getTrigger,
  109. getSplit,
  110. getMenuTheme,
  111. getCanDrag,
  112. getCollapsedShowTitle,
  113. getIsHorizontal,
  114. getIsSidebarType,
  115. getAccordion,
  116. getShowTopMenu,
  117. getShowHeaderTrigger,
  118. getTopMenuAlign,
  119. getMenuHidden,
  120. getIsTopMenu,
  121. getMenuBgColor,
  122. getShowSidebar,
  123. getIsMixMode,
  124. getIsMixSidebar,
  125. getCloseMixSidebarOnChange,
  126. getMixSideTrigger,
  127. getMixSideFixed,
  128. mixSideHasChildren,
  129. };
  130. }