MixSider.vue 14 KB

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