MixSider.vue 14 KB

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