LayoutSideBar.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { computed, defineComponent, nextTick, onMounted, ref, unref } from 'vue';
  2. import { Layout } from 'ant-design-vue';
  3. import SideBarTrigger from './SideBarTrigger';
  4. import { menuStore } from '/@/store/modules/menu';
  5. // import darkMiniIMg from '/@/assets/images/sidebar/dark-mini.png';
  6. // import lightMiniImg from '/@/assets/images/sidebar/light-mini.png';
  7. // import lightImg from '/@/assets/images/sidebar/light.png';
  8. import { appStore } from '/@/store/modules/app';
  9. import { MenuModeEnum, MenuSplitTyeEnum } from '/@/enums/menuEnum';
  10. import { SIDE_BAR_MINI_WIDTH, SIDE_BAR_SHOW_TIT_MINI_WIDTH } from '/@/enums/appEnum';
  11. import { useDebounce } from '/@/hooks/core/useDebounce';
  12. import LayoutMenu from './LayoutMenu';
  13. export default defineComponent({
  14. name: 'DefaultLayoutSideBar',
  15. setup() {
  16. const initRef = ref(false);
  17. const brokenRef = ref(false);
  18. const collapseRef = ref(true);
  19. const dragBarRef = ref<Nullable<HTMLDivElement>>(null);
  20. const sideRef = ref<any>(null);
  21. const getProjectConfigRef = computed(() => {
  22. return appStore.getProjectConfig;
  23. });
  24. const getMiniWidth = computed(() => {
  25. const {
  26. menuSetting: { collapsedShowTitle },
  27. } = unref(getProjectConfigRef);
  28. return collapsedShowTitle ? SIDE_BAR_SHOW_TIT_MINI_WIDTH : SIDE_BAR_MINI_WIDTH;
  29. });
  30. function onCollapseChange(val: boolean) {
  31. if (initRef.value) {
  32. collapseRef.value = val;
  33. menuStore.commitCollapsedState(val);
  34. } else {
  35. const collapsed = appStore.getProjectConfig.menuSetting.collapsed;
  36. !collapsed && menuStore.commitCollapsedState(val);
  37. }
  38. initRef.value = true;
  39. }
  40. // 菜单区域拖拽 - 鼠标移动
  41. function handleMouseMove(ele: any, wrap: any, clientX: number) {
  42. document.onmousemove = function (innerE) {
  43. let iT = ele.left + ((innerE || event).clientX - clientX);
  44. innerE = innerE || window.event;
  45. // let tarnameb = innerE.target || innerE.srcElement;
  46. const maxT = 600;
  47. const minT = unref(getMiniWidth);
  48. iT < 0 && (iT = 0);
  49. iT > maxT && (iT = maxT);
  50. iT < minT && (iT = minT);
  51. ele.style.left = wrap.style.width = iT + 'px';
  52. return false;
  53. };
  54. }
  55. // 菜单区域拖拽 - 鼠标松开
  56. function removeMouseup(ele: any) {
  57. const wrap = unref(sideRef).$el;
  58. document.onmouseup = function () {
  59. document.onmousemove = null;
  60. document.onmouseup = null;
  61. const width = parseInt(wrap.style.width);
  62. menuStore.commitDragStartState(false);
  63. if (!menuStore.getCollapsedState) {
  64. if (width > unref(getMiniWidth) + 20) {
  65. setMenuWidth(width);
  66. } else {
  67. menuStore.commitCollapsedState(true);
  68. }
  69. } else {
  70. if (width > unref(getMiniWidth)) {
  71. setMenuWidth(width);
  72. menuStore.commitCollapsedState(false);
  73. }
  74. }
  75. ele.releaseCapture && ele.releaseCapture();
  76. };
  77. }
  78. function setMenuWidth(width: number) {
  79. appStore.commitProjectConfigState({
  80. menuSetting: {
  81. menuWidth: width,
  82. },
  83. });
  84. }
  85. function changeWrapWidth() {
  86. const ele = unref(dragBarRef) as any;
  87. const side = unref(sideRef);
  88. const wrap = (side || {}).$el;
  89. // const eleWidth = 6;
  90. ele &&
  91. (ele.onmousedown = (e: any) => {
  92. menuStore.commitDragStartState(true);
  93. wrap.style.transition = 'unset';
  94. const clientX = (e || event).clientX;
  95. ele.left = ele.offsetLeft;
  96. handleMouseMove(ele, wrap, clientX);
  97. removeMouseup(ele);
  98. ele.setCapture && ele.setCapture();
  99. return false;
  100. });
  101. }
  102. function handleBreakpoint(broken: boolean) {
  103. brokenRef.value = broken;
  104. }
  105. onMounted(() => {
  106. nextTick(() => {
  107. const [exec] = useDebounce(changeWrapWidth, 20);
  108. exec();
  109. });
  110. });
  111. const getDragBarStyle = computed(() => {
  112. if (menuStore.getCollapsedState) {
  113. return { left: `${unref(getMiniWidth)}px` };
  114. }
  115. return {};
  116. });
  117. const getCollapsedWidth = computed(() => {
  118. return unref(brokenRef) ? 0 : unref(getMiniWidth);
  119. });
  120. function renderDragLine() {
  121. const { menuSetting: { hasDrag = true } = {} } = unref(getProjectConfigRef);
  122. return (
  123. <div
  124. class={[`layout-sidebar__dargbar`, !hasDrag ? 'hide' : '']}
  125. style={unref(getDragBarStyle)}
  126. ref={dragBarRef}
  127. />
  128. );
  129. }
  130. return () => {
  131. const {
  132. menuSetting: { theme, split: splitMenu },
  133. } = unref(getProjectConfigRef);
  134. const { getCollapsedState, getMenuWidthState } = menuStore;
  135. return (
  136. <Layout.Sider
  137. onCollapse={onCollapseChange}
  138. breakpoint="md"
  139. width={getMenuWidthState}
  140. collapsed={getCollapsedState}
  141. collapsible
  142. collapsedWidth={unref(getCollapsedWidth)}
  143. theme={theme}
  144. class="layout-sidebar"
  145. ref={sideRef}
  146. onBreakpoint={handleBreakpoint}
  147. >
  148. {{
  149. trigger: () => <SideBarTrigger />,
  150. default: () => (
  151. <>
  152. <LayoutMenu
  153. theme={theme}
  154. menuMode={splitMenu ? MenuModeEnum.INLINE : null}
  155. splitType={splitMenu ? MenuSplitTyeEnum.LEFT : MenuSplitTyeEnum.NONE}
  156. />
  157. {renderDragLine()}
  158. </>
  159. ),
  160. }}
  161. </Layout.Sider>
  162. );
  163. };
  164. },
  165. });