MixSider.vue 14 KB

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