Blame view

src/layouts/default/sider/MixSider.vue 14.3 KB
1
<template>
2
  <div :class="`${prefixCls}-dom`" :style="getDomStyle"></div>
3
4
5

  <div
    v-click-outside="handleClickOutside"
vben authored
6
    :style="getWrapStyle"
7
8
9
10
11
    :class="[
      prefixCls,
      getMenuTheme,
      {
        open: openMenu,
vben authored
12
        mini: getCollapsed,
13
14
      },
    ]"
15
    v-bind="getMenuEvents"
16
17
  >
    <AppLogo :showTitle="false" :class="`${prefixCls}-logo`" />
vben authored
18
19
20

    <Trigger :class="`${prefixCls}-trigger`" />
21
22
23
24
25
26
27
28
29
30
31
    <ScrollContainer>
      <ul :class="`${prefixCls}-module`">
        <li
          :class="[
            `${prefixCls}-module__item `,
            {
              [`${prefixCls}-module__item--active`]: item.path === activePath,
            },
          ]"
          v-for="item in menuModules"
          :key="item.path"
32
          v-bind="getItemEvents(item)"
33
34
        >
          <MenuTag :item="item" :showTitle="false" :isHorizontal="false" />
35
          <Icon
36
            :class="`${prefixCls}-module__icon`"
vben authored
37
            :size="getCollapsed ? 16 : 20"
38
39
            :icon="item.meta && item.meta.icon"
          />
40
41
42
          <p :class="`${prefixCls}-module__name`">
            {{ t(item.name) }}
          </p>
43
44
45
46
47
48
        </li>
      </ul>
    </ScrollContainer>

    <div :class="`${prefixCls}-menu-list`" ref="sideRef" :style="getMenuStyle">
      <div
49
        v-show="openMenu"
50
51
52
53
54
55
56
57
        :class="[
          `${prefixCls}-menu-list__title`,
          {
            show: openMenu,
          },
        ]"
      >
        <span class="text"> {{ title }}</span>
58
59
        <Icon
          :size="16"
vben authored
60
          :icon="getMixSideFixed ? 'ri:pushpin-2-fill' : 'ri:pushpin-2-line'"
61
62
63
          class="pushpin"
          @click="handleFixedMenu"
        />
64
65
      </div>
      <ScrollContainer :class="`${prefixCls}-menu-list__content`">
66
        <SimpleMenu
67
68
          :items="chilrenMenus"
          :theme="getMenuTheme"
69
          mixSider
70
71
72
73
74
75
76
77
78
79
80
81
82
          @menuClick="handleMenuClick"
        />
      </ScrollContainer>
      <div
        v-show="getShowDragBar && openMenu"
        :class="`${prefixCls}-drag-bar`"
        ref="dragBarRef"
      ></div>
    </div>
  </div>
</template>
<script lang="ts">
  import type { Menu } from '/@/router/types';
vben authored
83
84
85
86
87
  import type { CSSProperties } from 'vue';
  import type { RouteLocationNormalized } from 'vue-router';

  import { defineComponent, onMounted, ref, computed, unref } from 'vue';
88
  import { MenuTag } from '/@/components/Menu';
89
  import { ScrollContainer } from '/@/components/Container';
90
  import Icon from '/@/components/Icon';
91
  import { AppLogo } from '/@/components/Application';
vben authored
92
93
  import Trigger from '../trigger/HeaderTrigger.vue';
94
95
  import { useMenuSetting } from '/@/hooks/setting/useMenuSetting';
  import { useDragLine } from './useLayoutSider';
96
  import { useGlobSetting } from '/@/hooks/setting';
vben authored
97
98
99
  import { useDesign } from '/@/hooks/web/useDesign';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useGo } from '/@/hooks/web/usePage';
100
vben authored
101
  import { SIDE_BAR_SHOW_TIT_MINI_WIDTH, SIDE_BAR_MINI_WIDTH } from '/@/enums/appEnum';
102
103

  import clickOutside from '/@/directives/clickOutside';
vben authored
104
105
  import { getShallowMenus, getChildrenMenus, getCurrentParentPath } from '/@/router/menus';
  import { listenerLastChangeTab } from '/@/logics/mitt/tabChange';
106
  import { SimpleMenu } from '/@/components/SimpleMenu';
107
108
109
110
111
112

  export default defineComponent({
    name: 'LayoutMixSider',
    components: {
      ScrollContainer,
      AppLogo,
113
      SimpleMenu,
114
      MenuTag,
115
      Icon,
vben authored
116
      Trigger,
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    },
    directives: {
      clickOutside,
    },
    setup() {
      let menuModules = ref<Menu[]>([]);
      const activePath = ref('');
      const chilrenMenus = ref<Menu[]>([]);
      const openMenu = ref(false);
      const dragBarRef = ref<ElRef>(null);
      const sideRef = ref<ElRef>(null);
      const currentRoute = ref<Nullable<RouteLocationNormalized>>(null);

      const { prefixCls } = useDesign('layout-mix-sider');
      const go = useGo();
      const { t } = useI18n();
      const {
        getMenuWidth,
        getCanDrag,
        getCloseMixSidebarOnChange,
        getMenuTheme,
138
        getMixSideTrigger,
139
140
141
142
        getRealWidth,
        getMixSideFixed,
        mixSideHasChildren,
        setMenuSetting,
vben authored
143
144
        getIsMixSidebar,
        getCollapsed,
145
      } = useMenuSetting();
146
147
148
149
150
151
152
153
154
      const { title } = useGlobSetting();

      useDragLine(sideRef, dragBarRef, true);

      const getMenuStyle = computed(
        (): CSSProperties => {
          return {
            width: unref(openMenu) ? `${unref(getMenuWidth)}px` : 0,
vben authored
155
            left: `${unref(getMixSideWidth)}px`,
156
157
158
159
          };
        }
      );
160
      const getIsFixed = computed(() => {
161
        /* eslint-disable-next-line */
162
163
164
        mixSideHasChildren.value = unref(chilrenMenus).length > 0;
        const isFixed = unref(getMixSideFixed) && unref(mixSideHasChildren);
        if (isFixed) {
165
          /* eslint-disable-next-line */
166
167
168
169
170
          openMenu.value = true;
        }
        return isFixed;
      });
vben authored
171
172
173
174
      const getMixSideWidth = computed(() => {
        return unref(getCollapsed) ? SIDE_BAR_MINI_WIDTH : SIDE_BAR_SHOW_TIT_MINI_WIDTH;
      });
175
176
177
      const getDomStyle = computed(
        (): CSSProperties => {
          const fixedWidth = unref(getIsFixed) ? unref(getRealWidth) : 0;
vben authored
178
179
180
181
182
183
184
185
186
          const width = `${unref(getMixSideWidth) + fixedWidth}px`;
          return getWrapCommonStyle(width);
        }
      );

      const getWrapStyle = computed(
        (): CSSProperties => {
          const width = `${unref(getMixSideWidth)}px`;
          return getWrapCommonStyle(width);
187
188
189
        }
      );
190
      const getMenuEvents = computed(() => {
vben authored
191
192
193
194
195
196
197
        return !unref(getMixSideFixed)
          ? {
              onMouseleave: () => {
                closeMenu();
              },
            }
          : {};
198
199
      });
200
201
202
203
204
205
206
207
      const getShowDragBar = computed(() => unref(getCanDrag));

      onMounted(async () => {
        menuModules.value = await getShallowMenus();
      });

      listenerLastChangeTab((route) => {
        currentRoute.value = route;
208
        setActive(true);
209
        if (unref(getCloseMixSidebarOnChange)) {
210
          closeMenu();
211
212
213
        }
      });
vben authored
214
215
216
217
218
219
220
221
222
223
      function getWrapCommonStyle(width: string): CSSProperties {
        return {
          width,
          maxWidth: width,
          minWidth: width,
          flex: `0 0 ${width}`,
        };
      }

      // Process module menu click
224
      async function hanldeModuleClick(path: string, hover = false) {
225
226
227
        const children = await getChildrenMenus(path);

        if (unref(activePath) === path) {
228
          if (!hover) {
229
230
231
232
233
            if (!unref(openMenu)) {
              openMenu.value = true;
            } else {
              closeMenu();
            }
234
          }
235
236
237
238
239
240
241
242
243
244
245
          if (!unref(openMenu)) {
            setActive();
          }
        } else {
          openMenu.value = true;
          activePath.value = path;
        }

        if (!children || children.length === 0) {
          go(path);
          chilrenMenus.value = [];
246
          closeMenu();
247
248
249
250
251
          return;
        }
        chilrenMenus.value = children;
      }
vben authored
252
      // Set the currently active menu and submenu
253
      async function setActive(setChildren = false) {
254
255
256
257
258
        const path = currentRoute.value?.path;
        if (!path) return;
        const parentPath = await getCurrentParentPath(path);
        activePath.value = parentPath;
        // hanldeModuleClick(parentPath);
vben authored
259
        if (unref(getIsMixSidebar)) {
260
261
262
263
264
265
          const activeMenu = unref(menuModules).find((item) => item.path === unref(activePath));
          const p = activeMenu?.path;
          if (p) {
            const children = await getChildrenMenus(p);
            if (setChildren) {
              chilrenMenus.value = children;
vben authored
266
267
268
269

              if (unref(getMixSideFixed)) {
                openMenu.value = children.length > 0;
              }
270
271
272
273
274
275
            }
            if (children.length === 0) {
              chilrenMenus.value = [];
            }
          }
        }
276
277
278
279
280
281
282
      }

      function handleMenuClick(path: string) {
        go(path);
      }

      function handleClickOutside() {
283
        setActive(true);
284
        closeMenu();
285
286
      }
287
288
289
290
291
292
293
294
295
296
297
      function getItemEvents(item: Menu) {
        if (unref(getMixSideTrigger) === 'hover') {
          return {
            onMouseenter: () => hanldeModuleClick(item.path, true),
          };
        }
        return {
          onClick: () => hanldeModuleClick(item.path),
        };
      }
298
299
300
301
302
303
      function handleFixedMenu() {
        setMenuSetting({
          mixSideFixed: !unref(getIsFixed),
        });
      }
vben authored
304
      // Close menu
305
306
307
308
309
310
      function closeMenu() {
        if (!unref(getIsFixed)) {
          openMenu.value = false;
        }
      }
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
      return {
        t,
        prefixCls,
        menuModules,
        hanldeModuleClick,
        activePath,
        chilrenMenus,
        getShowDragBar,
        handleMenuClick,
        getMenuStyle,
        handleClickOutside,
        sideRef,
        dragBarRef,
        title,
        openMenu,
        getMenuTheme,
327
328
        getItemEvents,
        getMenuEvents,
329
330
331
        getDomStyle,
        handleFixedMenu,
        getMixSideFixed,
vben authored
332
333
        getWrapStyle,
        getCollapsed,
334
335
336
337
338
339
340
      };
    },
  });
</script>
<style lang="less">
  @prefix-cls: ~'@{namespace}-layout-mix-sider';
  @tag-prefix-cls: ~'@{namespace}-basic-menu-item-tag';
341
  @menu-prefix-cls: ~'@{namespace}-menu';
342
343
344
345
346
347
348
349
350
  @width: 80px;
  .@{prefix-cls} {
    position: fixed;
    top: 0;
    left: 0;
    z-index: @layout-mix-sider-fixed-z-index;
    height: 100%;
    overflow: hidden;
    background: @sider-dark-bg-color;
vben authored
351
    transition: all 0.2s ease 0s;
352
353
354
355
356
357
    .@{tag-prefix-cls} {
      position: absolute;
      top: 6px;
      right: 2px;
    }
358
359
360
361
    .@{menu-prefix-cls} {
      width: 100% !important;
    }
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
    &-dom {
      height: 100%;
      overflow: hidden;
      transition: all 0.2s ease 0s;
    }

    &-logo {
      display: flex;
      height: @header-height;
      padding-left: 0 !important;
      justify-content: center;

      img {
        width: @logo-width;
        height: @logo-width;
      }
    }

    &.light {
      .@{prefix-cls}-logo {
        border-bottom: 1px solid rgb(238, 238, 238);
      }

      &.open {
vben authored
386
        > .scrollbar {
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
          border-right: 1px solid rgb(238, 238, 238);
        }
      }

      .@{prefix-cls}-module {
        &__item {
          font-weight: normal;
          color: rgba(0, 0, 0, 0.65);

          &--active {
            color: @primary-color;
            background: unset;
          }
        }
      }
402
      .@{prefix-cls}-menu-list {
403
404
405
406
        &__content {
          box-shadow: 0 0 4px 0 rgba(0, 0, 0, 0.1);
        }
407
408
409
410
411
412
413
414
415
416
        &__title {
          .pushpin {
            color: rgba(0, 0, 0, 0.35);

            &:hover {
              color: rgba(0, 0, 0, 0.85);
            }
          }
        }
      }
417
    }
vben authored
418
    @border-color: @sider-dark-lighten-1-bg-color;
419
420
421
422

    &.dark {
      &.open {
        .@{prefix-cls}-logo {
vben authored
423
          border-bottom: 1px solid @border-color;
424
425
        }
vben authored
426
        > .scrollbar {
vben authored
427
          border-right: 1px solid @border-color;
428
429
430
431
432
433
434
435
        }
      }
      .@{prefix-cls}-menu-list {
        background: @sider-dark-bg-color;

        &__title {
          color: @white;
          border-bottom: none;
vben authored
436
          border-bottom: 1px solid @border-color;
437
438
439
440
        }
      }
    }
441
    > .scrollbar {
vben authored
442
      height: calc(100% - @header-height - 38px);
443
444
    }
vben authored
445
446
447
448
449
450
451
452
453
454
    &.mini &-module {
      &__name {
        display: none;
      }

      &__icon {
        margin-bottom: 0;
      }
    }
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
    &-module {
      position: relative;
      padding-top: 1px;

      &__item {
        position: relative;
        padding: 12px 0;
        color: rgba(255, 255, 255, 0.65);
        text-align: center;
        cursor: pointer;
        transition: all 0.3s ease;

        &:hover {
          color: @white;
        }
        // &:hover,
        &--active {
          font-weight: 700;
          color: @white;
          background: @sider-dark-darken-bg-color;

          &::before {
            position: absolute;
            top: 0;
            left: 0;
            width: 3px;
            height: 100%;
            background: @primary-color;
            content: '';
          }
        }
      }

      &__icon {
        margin-bottom: 8px;
        font-size: 24px;
vben authored
491
        transition: all 0.2s;
492
493
494
495
496
      }

      &__name {
        margin-bottom: 0;
        font-size: 12px;
vben authored
497
        transition: all 0.2s;
498
499
500
      }
    }
vben authored
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
    &-trigger {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      padding: 6px;
      padding-left: 12px;
      font-size: 18px;
      color: rgba(255, 255, 255, 0.65);
      cursor: pointer;
      background: @sider-dark-bg-color;
    }

    &.light &-trigger {
      color: rgba(0, 0, 0, 0.65);
      background: #fff;
    }
519
520
521
522
523
524
525
    &-menu-list {
      position: fixed;
      top: 0;
      width: 0;
      width: 200px;
      height: calc(100%);
      background: #fff;
vben authored
526
      transition: all 0.2s;
527
528
529
530
      .@{tag-prefix-cls} {
        position: absolute;
        top: 10px;
        right: 30px;
531
532
533
534
535

        &--dot {
          top: 50%;
          margin-top: -3px;
        }
536
537
538
539
540
      }

      &__title {
        display: flex;
        height: @header-height;
541
        // margin-left: -6px;
542
543
544
545
546
547
        font-size: 18px;
        color: @primary-color;
        border-bottom: 1px solid rgb(238, 238, 238);
        opacity: 0;
        transition: unset;
        align-items: center;
548
        justify-content: space-between;
549
550

        &.show {
551
          min-width: 130px;
552
553
554
          opacity: 1;
          transition: all 0.5s ease;
        }
555
556
557
558
559
560
561
562
563
564

        .pushpin {
          margin-right: 6px;
          color: rgba(255, 255, 255, 0.65);
          cursor: pointer;

          &:hover {
            color: #fff;
          }
        }
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
      }

      &__content {
        height: calc(100% - @header-height) !important;

        .scrollbar__wrap {
          height: 100%;
          overflow-x: hidden;
        }

        .scrollbar__bar.is-horizontal {
          display: none;
        }

        .ant-menu {
          height: 100%;
        }

        .ant-menu-inline,
        .ant-menu-vertical,
        .ant-menu-vertical-left {
          border-right: 1px solid transparent;
        }
      }
    }

    &-drag-bar {
      position: absolute;
593
594
595
596
      top: 50px;
      right: -1px;
      width: 1px;
      height: calc(100% - 50px);
597
598
599
600
601
602
603
604
      cursor: ew-resize;
      background: #f8f8f9;
      border-top: none;
      border-bottom: none;
      box-shadow: 0 0 4px 0 rgba(28, 36, 56, 0.15);
    }
  }
</style>