MixSider.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <template>
  2. <div :class="`${prefixCls}-dom`" :style="getDomStyle" />
  3. <div
  4. v-click-outside="handleClickOutside"
  5. :class="[
  6. prefixCls,
  7. getMenuTheme,
  8. {
  9. open: openMenu,
  10. },
  11. ]"
  12. v-bind="getMenuEvents"
  13. >
  14. <AppLogo :showTitle="false" :class="`${prefixCls}-logo`" />
  15. <ScrollContainer>
  16. <ul :class="`${prefixCls}-module`">
  17. <li
  18. :class="[
  19. `${prefixCls}-module__item `,
  20. {
  21. [`${prefixCls}-module__item--active`]: item.path === activePath,
  22. },
  23. ]"
  24. v-for="item in menuModules"
  25. :key="item.path"
  26. v-bind="getItemEvents(item)"
  27. >
  28. <MenuTag :item="item" :showTitle="false" :isHorizontal="false" />
  29. <Icon
  30. :class="`${prefixCls}-module__icon`"
  31. :size="22"
  32. :icon="item.meta && item.meta.icon"
  33. />
  34. <p :class="`${prefixCls}-module__name`">{{ t(item.name) }}</p>
  35. </li>
  36. </ul>
  37. </ScrollContainer>
  38. <div :class="`${prefixCls}-menu-list`" ref="sideRef" :style="getMenuStyle">
  39. <div
  40. v-show="openMenu"
  41. :class="[
  42. `${prefixCls}-menu-list__title`,
  43. {
  44. show: openMenu,
  45. },
  46. ]"
  47. >
  48. <span class="text"> {{ title }}</span>
  49. <Icon
  50. :size="16"
  51. v-if="getMixSideFixed"
  52. icon="ri:pushpin-2-fill"
  53. class="pushpin"
  54. @click="handleFixedMenu"
  55. />
  56. <Icon :size="16" v-else icon="ri:pushpin-2-line" class="pushpin" @click="handleFixedMenu" />
  57. </div>
  58. <ScrollContainer :class="`${prefixCls}-menu-list__content`">
  59. <BasicMenu
  60. :isHorizontal="false"
  61. mode="inline"
  62. :items="chilrenMenus"
  63. :theme="getMenuTheme"
  64. mixSider
  65. @menuClick="handleMenuClick"
  66. />
  67. </ScrollContainer>
  68. <div
  69. v-show="getShowDragBar && openMenu"
  70. :class="`${prefixCls}-drag-bar`"
  71. ref="dragBarRef"
  72. ></div>
  73. </div>
  74. </div>
  75. </template>
  76. <script lang="ts">
  77. import { defineComponent, onMounted, ref, computed, CSSProperties, unref } from 'vue';
  78. import type { Menu } from '/@/router/types';
  79. import { RouteLocationNormalized } from 'vue-router';
  80. import { useDesign } from '/@/hooks/web/useDesign';
  81. import { getShallowMenus, getChildrenMenus, getCurrentParentPath } from '/@/router/menus';
  82. import { useI18n } from '/@/hooks/web/useI18n';
  83. import { ScrollContainer } from '/@/components/Container';
  84. import Icon from '/@/components/Icon';
  85. import { AppLogo } from '/@/components/Application';
  86. import { useGo } from '/@/hooks/web/usePage';
  87. import { BasicMenu, MenuTag } from '/@/components/Menu';
  88. import { listenerLastChangeTab } from '/@/logics/mitt/tabChange';
  89. import { useMenuSetting } from '/@/hooks/setting/useMenuSetting';
  90. import { useDragLine } from './useLayoutSider';
  91. import { useGlobSetting } from '/@/hooks/setting';
  92. import { SIDE_BAR_SHOW_TIT_MINI_WIDTH } from '/@/enums/appEnum';
  93. import clickOutside from '/@/directives/clickOutside';
  94. export default defineComponent({
  95. name: 'LayoutMixSider',
  96. components: {
  97. ScrollContainer,
  98. AppLogo,
  99. BasicMenu,
  100. MenuTag,
  101. Icon,
  102. },
  103. directives: {
  104. clickOutside,
  105. },
  106. setup() {
  107. let menuModules = ref<Menu[]>([]);
  108. const activePath = ref('');
  109. const chilrenMenus = ref<Menu[]>([]);
  110. const openMenu = ref(false);
  111. const dragBarRef = ref<ElRef>(null);
  112. const sideRef = ref<ElRef>(null);
  113. const currentRoute = ref<Nullable<RouteLocationNormalized>>(null);
  114. const { prefixCls } = useDesign('layout-mix-sider');
  115. const go = useGo();
  116. const { t } = useI18n();
  117. const {
  118. getMenuWidth,
  119. getCanDrag,
  120. getCloseMixSidebarOnChange,
  121. getMenuTheme,
  122. getMixSideTrigger,
  123. getRealWidth,
  124. getMixSideFixed,
  125. mixSideHasChildren,
  126. setMenuSetting,
  127. } = useMenuSetting();
  128. const { title } = useGlobSetting();
  129. useDragLine(sideRef, dragBarRef, true);
  130. const getMenuStyle = computed(
  131. (): CSSProperties => {
  132. return {
  133. width: unref(openMenu) ? `${unref(getMenuWidth)}px` : 0,
  134. };
  135. }
  136. );
  137. const getIsFixed = computed(() => {
  138. mixSideHasChildren.value = unref(chilrenMenus).length > 0;
  139. const isFixed = unref(getMixSideFixed) && unref(mixSideHasChildren);
  140. if (isFixed) {
  141. openMenu.value = true;
  142. }
  143. return isFixed;
  144. });
  145. const getDomStyle = computed(
  146. (): CSSProperties => {
  147. const fixedWidth = unref(getIsFixed) ? unref(getRealWidth) : 0;
  148. const width = `${SIDE_BAR_SHOW_TIT_MINI_WIDTH + fixedWidth}px`;
  149. return {
  150. width,
  151. maxWidth: width,
  152. minWidth: width,
  153. flex: `0 0 ${width}`,
  154. };
  155. }
  156. );
  157. const getMenuEvents = computed(() => {
  158. // return unref(getMixSideTrigger) === 'hover'
  159. // ? {
  160. // onMouseleave: () => {
  161. // closeMenu();
  162. // },
  163. // }
  164. // : {};
  165. return {
  166. onMouseleave: () => {
  167. closeMenu();
  168. },
  169. };
  170. });
  171. const getShowDragBar = computed(() => unref(getCanDrag));
  172. onMounted(async () => {
  173. menuModules.value = await getShallowMenus();
  174. });
  175. listenerLastChangeTab((route) => {
  176. currentRoute.value = route;
  177. setActive(true);
  178. if (unref(getCloseMixSidebarOnChange)) {
  179. closeMenu();
  180. }
  181. });
  182. async function hanldeModuleClick(path: string, hover = false) {
  183. const children = await getChildrenMenus(path);
  184. if (unref(activePath) === path) {
  185. if (!hover) {
  186. if (!unref(openMenu)) {
  187. openMenu.value = true;
  188. } else {
  189. closeMenu();
  190. }
  191. }
  192. if (!unref(openMenu)) {
  193. setActive();
  194. }
  195. } else {
  196. openMenu.value = true;
  197. activePath.value = path;
  198. }
  199. if (!children || children.length === 0) {
  200. go(path);
  201. chilrenMenus.value = [];
  202. closeMenu();
  203. return;
  204. }
  205. chilrenMenus.value = children;
  206. }
  207. async function setActive(setChildren = false) {
  208. const path = currentRoute.value?.path;
  209. if (!path) return;
  210. const parentPath = await getCurrentParentPath(path);
  211. activePath.value = parentPath;
  212. // hanldeModuleClick(parentPath);
  213. if (unref(getMixSideFixed)) {
  214. const activeMenu = unref(menuModules).find((item) => item.path === unref(activePath));
  215. const p = activeMenu?.path;
  216. if (p) {
  217. const children = await getChildrenMenus(p);
  218. if (setChildren) {
  219. chilrenMenus.value = children;
  220. openMenu.value = children.length > 0;
  221. }
  222. if (children.length === 0) {
  223. chilrenMenus.value = [];
  224. }
  225. }
  226. }
  227. }
  228. function handleMenuClick(path: string) {
  229. go(path);
  230. }
  231. function handleClickOutside() {
  232. setActive(true);
  233. closeMenu();
  234. }
  235. function getItemEvents(item: Menu) {
  236. if (unref(getMixSideTrigger) === 'hover') {
  237. return {
  238. onMouseenter: () => hanldeModuleClick(item.path, true),
  239. };
  240. }
  241. return {
  242. onClick: () => hanldeModuleClick(item.path),
  243. };
  244. }
  245. function handleFixedMenu() {
  246. setMenuSetting({
  247. mixSideFixed: !unref(getIsFixed),
  248. });
  249. }
  250. function closeMenu() {
  251. if (!unref(getIsFixed)) {
  252. openMenu.value = false;
  253. }
  254. }
  255. return {
  256. t,
  257. prefixCls,
  258. menuModules,
  259. hanldeModuleClick,
  260. activePath,
  261. chilrenMenus,
  262. getShowDragBar,
  263. handleMenuClick,
  264. getMenuStyle,
  265. handleClickOutside,
  266. sideRef,
  267. dragBarRef,
  268. title,
  269. openMenu,
  270. getMenuTheme,
  271. getItemEvents,
  272. getMenuEvents,
  273. getDomStyle,
  274. handleFixedMenu,
  275. getMixSideFixed,
  276. };
  277. },
  278. });
  279. </script>
  280. <style lang="less">
  281. @import (reference) '../../../design/index.less';
  282. @prefix-cls: ~'@{namespace}-layout-mix-sider';
  283. @tag-prefix-cls: ~'@{namespace}-basic-menu-item-tag';
  284. @width: 80px;
  285. .@{prefix-cls} {
  286. position: fixed;
  287. top: 0;
  288. left: 0;
  289. z-index: @layout-mix-sider-fixed-z-index;
  290. width: @width;
  291. height: 100%;
  292. max-width: @width;
  293. min-width: @width;
  294. overflow: hidden;
  295. background: @sider-dark-bg-color;
  296. transition: all 0.3s ease 0s;
  297. flex: 0 0 @width;
  298. .@{tag-prefix-cls} {
  299. position: absolute;
  300. top: 6px;
  301. right: 2px;
  302. }
  303. &-dom {
  304. width: @width;
  305. height: 100%;
  306. max-width: @width;
  307. min-width: @width;
  308. overflow: hidden;
  309. transition: all 0.2s ease 0s;
  310. flex: 0 0 @width;
  311. }
  312. &-logo {
  313. display: flex;
  314. height: @header-height;
  315. padding-left: 0 !important;
  316. justify-content: center;
  317. img {
  318. width: @logo-width;
  319. height: @logo-width;
  320. }
  321. }
  322. &.light {
  323. .@{prefix-cls}-logo {
  324. border-bottom: 1px solid rgb(238, 238, 238);
  325. }
  326. &.open {
  327. > .scroll-container {
  328. border-right: 1px solid rgb(238, 238, 238);
  329. }
  330. }
  331. .@{prefix-cls}-module {
  332. &__item {
  333. font-weight: normal;
  334. color: rgba(0, 0, 0, 0.65);
  335. &--active {
  336. color: @primary-color;
  337. background: unset;
  338. }
  339. }
  340. }
  341. .@{prefix-cls}-menu-list {
  342. &__title {
  343. .pushpin {
  344. color: rgba(0, 0, 0, 0.35);
  345. &:hover {
  346. color: rgba(0, 0, 0, 0.85);
  347. }
  348. }
  349. }
  350. }
  351. }
  352. @border-color: @sider-dark-lighten-1-bg-color;
  353. &.dark {
  354. &.open {
  355. .@{prefix-cls}-logo {
  356. border-bottom: 1px solid @border-color;
  357. }
  358. > .scroll-container {
  359. border-right: 1px solid @border-color;
  360. }
  361. }
  362. .@{prefix-cls}-menu-list {
  363. background: @sider-dark-bg-color;
  364. &__title {
  365. color: @white;
  366. border-bottom: none;
  367. border-bottom: 1px solid @border-color;
  368. }
  369. }
  370. }
  371. > .scrollbar {
  372. height: calc(100% - @header-height) !important;
  373. }
  374. &-module {
  375. position: relative;
  376. padding-top: 1px;
  377. &__item {
  378. position: relative;
  379. padding: 12px 0;
  380. color: rgba(255, 255, 255, 0.65);
  381. text-align: center;
  382. cursor: pointer;
  383. transition: all 0.3s ease;
  384. &:hover {
  385. color: @white;
  386. }
  387. // &:hover,
  388. &--active {
  389. font-weight: 700;
  390. color: @white;
  391. background: @sider-dark-darken-bg-color;
  392. &::before {
  393. position: absolute;
  394. top: 0;
  395. left: 0;
  396. width: 3px;
  397. height: 100%;
  398. background: @primary-color;
  399. content: '';
  400. }
  401. }
  402. }
  403. &__icon {
  404. margin-bottom: 8px;
  405. font-size: 24px;
  406. }
  407. &__name {
  408. margin-bottom: 0;
  409. font-size: 12px;
  410. }
  411. }
  412. &-menu-list {
  413. position: fixed;
  414. top: 0;
  415. left: 80px;
  416. width: 0;
  417. width: 200px;
  418. height: calc(100%);
  419. background: #fff;
  420. transition: width 0.2s;
  421. .@{tag-prefix-cls} {
  422. position: absolute;
  423. top: 10px;
  424. right: 30px;
  425. &--dot {
  426. top: 50%;
  427. margin-top: -3px;
  428. }
  429. }
  430. &__title {
  431. display: flex;
  432. height: @header-height;
  433. // margin-left: -6px;
  434. font-size: 18px;
  435. color: @primary-color;
  436. border-bottom: 1px solid rgb(238, 238, 238);
  437. opacity: 0;
  438. transition: unset;
  439. align-items: center;
  440. justify-content: space-between;
  441. &.show {
  442. min-width: 130px;
  443. opacity: 1;
  444. transition: all 0.5s ease;
  445. }
  446. .pushpin {
  447. margin-right: 6px;
  448. color: rgba(255, 255, 255, 0.65);
  449. cursor: pointer;
  450. &:hover {
  451. color: #fff;
  452. }
  453. }
  454. }
  455. &__content {
  456. height: calc(100% - @header-height) !important;
  457. .scrollbar__wrap {
  458. height: 100%;
  459. overflow-x: hidden;
  460. }
  461. .scrollbar__bar.is-horizontal {
  462. display: none;
  463. }
  464. .ant-menu {
  465. height: 100%;
  466. }
  467. .ant-menu-inline,
  468. .ant-menu-vertical,
  469. .ant-menu-vertical-left {
  470. border-right: 1px solid transparent;
  471. }
  472. }
  473. }
  474. &-drag-bar {
  475. position: absolute;
  476. top: 0;
  477. right: -3px;
  478. width: 3px;
  479. height: 100%;
  480. cursor: ew-resize;
  481. background: #f8f8f9;
  482. border-top: none;
  483. border-bottom: none;
  484. box-shadow: 0 0 4px 0 rgba(28, 36, 56, 0.15);
  485. }
  486. }
  487. </style>