MixSider.vue 15 KB

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