MixSider.vue 15 KB

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