1
0

MixSider.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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-bind="getItemEvents(item)"
  28. v-for="item in menuModules"
  29. :key="item.path"
  30. >
  31. <SimpleMenuTag :item="item" collapseParent dot />
  32. <Icon
  33. :class="`${prefixCls}-module__icon`"
  34. :size="getCollapsed ? 16 : 20"
  35. :icon="item.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 { ScrollContainer } from '/@/components/Container';
  83. import { SimpleMenuTag } from '/@/components/SimpleMenu';
  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 { listenerRouteChange } from '/@/logics/mitt/routeChange';
  97. import { SimpleMenu } from '/@/components/SimpleMenu';
  98. export default defineComponent({
  99. name: 'LayoutMixSider',
  100. components: {
  101. ScrollContainer,
  102. AppLogo,
  103. SimpleMenu,
  104. Icon,
  105. Trigger,
  106. SimpleMenuTag,
  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. listenerRouteChange((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. @width: 80px;
  307. .@{prefix-cls} {
  308. position: fixed;
  309. top: 0;
  310. left: 0;
  311. z-index: @layout-mix-sider-fixed-z-index;
  312. height: 100%;
  313. overflow: hidden;
  314. background-color: @sider-dark-bg-color;
  315. transition: all 0.2s ease 0s;
  316. &-dom {
  317. height: 100%;
  318. overflow: hidden;
  319. transition: all 0.2s ease 0s;
  320. }
  321. &-logo {
  322. display: flex;
  323. height: @header-height;
  324. padding-left: 0 !important;
  325. justify-content: center;
  326. img {
  327. width: @logo-width;
  328. height: @logo-width;
  329. }
  330. }
  331. &.light {
  332. .@{prefix-cls}-logo {
  333. border-bottom: 1px solid rgb(238, 238, 238);
  334. }
  335. &.open {
  336. > .scrollbar {
  337. border-right: 1px solid rgb(238, 238, 238);
  338. }
  339. }
  340. .@{prefix-cls}-module {
  341. &__item {
  342. font-weight: normal;
  343. color: rgba(0, 0, 0, 0.65);
  344. &--active {
  345. color: @primary-color;
  346. background-color: unset;
  347. }
  348. }
  349. }
  350. .@{prefix-cls}-menu-list {
  351. &__content {
  352. box-shadow: 0 0 4px 0 rgba(0, 0, 0, 0.1);
  353. }
  354. &__title {
  355. .pushpin {
  356. color: rgba(0, 0, 0, 0.35);
  357. &:hover {
  358. color: rgba(0, 0, 0, 0.85);
  359. }
  360. }
  361. }
  362. }
  363. }
  364. @border-color: @sider-dark-lighten-bg-color;
  365. &.dark {
  366. &.open {
  367. .@{prefix-cls}-logo {
  368. // border-bottom: 1px solid @border-color;
  369. }
  370. > .scrollbar {
  371. border-right: 1px solid @border-color;
  372. }
  373. }
  374. .@{prefix-cls}-menu-list {
  375. background-color: @sider-dark-bg-color;
  376. &__title {
  377. color: @white;
  378. border-bottom: none;
  379. border-bottom: 1px solid @border-color;
  380. }
  381. }
  382. }
  383. > .scrollbar {
  384. height: calc(100% - @header-height - 38px);
  385. }
  386. &.mini &-module {
  387. &__name {
  388. display: none;
  389. }
  390. &__icon {
  391. margin-bottom: 0;
  392. }
  393. }
  394. &-module {
  395. position: relative;
  396. padding-top: 1px;
  397. &__item {
  398. position: relative;
  399. padding: 12px 0;
  400. color: rgba(255, 255, 255, 0.65);
  401. text-align: center;
  402. cursor: pointer;
  403. transition: all 0.3s ease;
  404. &:hover {
  405. color: @white;
  406. }
  407. // &:hover,
  408. &--active {
  409. font-weight: 700;
  410. color: @white;
  411. background-color: @sider-dark-darken-bg-color;
  412. &::before {
  413. position: absolute;
  414. top: 0;
  415. left: 0;
  416. width: 3px;
  417. height: 100%;
  418. background-color: @primary-color;
  419. content: '';
  420. }
  421. }
  422. }
  423. &__icon {
  424. margin-bottom: 8px;
  425. font-size: 24px;
  426. transition: all 0.2s;
  427. }
  428. &__name {
  429. margin-bottom: 0;
  430. font-size: 12px;
  431. transition: all 0.2s;
  432. }
  433. }
  434. &-trigger {
  435. position: absolute;
  436. bottom: 0;
  437. left: 0;
  438. width: 100%;
  439. padding: 6px;
  440. padding-left: 12px;
  441. font-size: 18px;
  442. color: rgba(255, 255, 255, 0.65);
  443. cursor: pointer;
  444. background-color: @sider-dark-bg-color;
  445. }
  446. &.light &-trigger {
  447. color: rgba(0, 0, 0, 0.65);
  448. background-color: #fff;
  449. }
  450. &-menu-list {
  451. position: fixed;
  452. top: 0;
  453. width: 0;
  454. width: 200px;
  455. height: calc(100%);
  456. background-color: #fff;
  457. transition: all 0.2s;
  458. &__title {
  459. display: flex;
  460. height: @header-height;
  461. // margin-left: -6px;
  462. font-size: 18px;
  463. color: @primary-color;
  464. border-bottom: 1px solid rgb(238, 238, 238);
  465. opacity: 0;
  466. transition: unset;
  467. align-items: center;
  468. justify-content: space-between;
  469. &.show {
  470. min-width: 130px;
  471. opacity: 1;
  472. transition: all 0.5s ease;
  473. }
  474. .pushpin {
  475. margin-right: 6px;
  476. color: rgba(255, 255, 255, 0.65);
  477. cursor: pointer;
  478. &:hover {
  479. color: #fff;
  480. }
  481. }
  482. }
  483. &__content {
  484. height: calc(100% - @header-height) !important;
  485. .scrollbar__wrap {
  486. height: 100%;
  487. overflow-x: hidden;
  488. }
  489. .scrollbar__bar.is-horizontal {
  490. display: none;
  491. }
  492. .ant-menu {
  493. height: 100%;
  494. }
  495. .ant-menu-inline,
  496. .ant-menu-vertical,
  497. .ant-menu-vertical-left {
  498. border-right: 1px solid transparent;
  499. }
  500. }
  501. }
  502. &-drag-bar {
  503. position: absolute;
  504. top: 50px;
  505. right: -1px;
  506. width: 1px;
  507. height: calc(100% - 50px);
  508. cursor: ew-resize;
  509. background-color: #f8f8f9;
  510. border-top: none;
  511. border-bottom: none;
  512. box-shadow: 0 0 4px 0 rgba(28, 36, 56, 0.15);
  513. }
  514. }
  515. </style>