SidebarMenuItem.vue
13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
<script setup lang="ts">
import type { MenuItem } from "@/types/menu";
import { computed, nextTick, ref } from "vue";
import { ArrowDown, ArrowRight } from "@element-plus/icons-vue";
interface Props {
menu: MenuItem;
activePath: string;
expandedKeys: Set<string>;
collapsed?: boolean;
}
const props = withDefaults(defineProps<Props>(), {
collapsed: false,
});
const emit = defineEmits<{
click: [menu: MenuItem];
toggle: [menuId: string];
}>();
// 悬浮显示子菜单
const showPopover = ref<boolean>(false);
const menuItemRef = ref<HTMLElement | null>(null);
const popoverTop = ref<number>(0);
// 是否有子菜单(只计算非隐藏的子菜单)
const hasChildren = computed(() => {
return (
props.menu.children &&
props.menu.children.filter((child) => !child.hidden).length > 0
);
});
// 是否展开
const isExpanded = computed(() => {
return props.expandedKeys.has(props.menu.id);
});
// 递归检查菜单或其子菜单是否激活
const checkMenuActive = (menu: MenuItem): boolean => {
// 当前菜单路径匹配
if (
props.activePath === menu.path ||
props.activePath.startsWith(`${menu.path}/`)
) {
return true;
}
// 如果有子菜单,递归检查子菜单是否有激活的
if (menu.children && menu.children.length > 0) {
return menu.children.some((child) => checkMenuActive(child));
}
return false;
};
// 是否激活(当前路由或子路由激活,包括子菜单激活时父菜单也激活)
const isActive = computed(() => {
return checkMenuActive(props.menu);
});
// 点击菜单项
const handleClick = () => {
if (hasChildren.value) {
handleToggle();
} else {
emit("click", props.menu);
}
};
// 展开/收起子菜单
const handleToggle = () => {
emit("toggle", props.menu.id);
};
// 鼠标进入
const handleMouseEnter = async () => {
if (props.collapsed && props.menu.level === 1 && hasChildren.value) {
await nextTick();
if (menuItemRef.value) {
const rect = menuItemRef.value.getBoundingClientRect();
popoverTop.value = rect.top;
}
showPopover.value = true;
}
};
// 鼠标离开
const handleMouseLeave = () => {
if (props.collapsed) {
showPopover.value = false;
}
};
// 检查子菜单是否激活
const checkChildActive = (child: MenuItem): boolean => {
if (
props.activePath === child.path ||
props.activePath.startsWith(`${child.path}/`)
) {
return true;
}
if (child.children && child.children.length > 0) {
return child.children.some((c) => checkChildActive(c));
}
return false;
};
// 递归检查菜单是否有可见的子菜单(考虑 hidden 属性)
const hasVisibleChildren = (menu: MenuItem): boolean => {
if (!menu.children || menu.children.length === 0) {
return false;
}
// 检查是否有任何非隐藏的子菜单
const visibleChildren = menu.children.filter((child) => !child.hidden);
if (visibleChildren.length === 0) {
return false;
}
// 递归检查:至少有一个可见的子菜单有可见的子菜单
// 如果所有可见子菜单都没有可见的子菜单(递归),则返回 false
return visibleChildren.some((child) => {
// 如果子菜单有可见的子菜单(递归检查),则返回 true
return hasVisibleChildren(child);
});
};
// 点击子菜单项
const handleChildClick = (child: MenuItem) => {
emit("click", child);
showPopover.value = false;
};
</script>
<template>
<div v-if="!menu.hidden"
ref="menuItemRef"
class="sidebar-menu-item"
:class="{ collapsed }"
@mouseenter="handleMouseEnter"
@mouseleave="handleMouseLeave">
<!-- 菜单项本身 -->
<div class="menu-item-content"
:class="{
active: isActive,
'has-children': hasChildren,
[`level-${menu.level || 1}`]: true,
collapsed,
}"
@click="handleClick">
<!-- 折叠模式:上下布局 -->
<template v-if="collapsed && menu.level === 1">
<div class="menu-icon-wrapper">
<img v-if="menu?.level === 1"
alt=""
class="menu-icon"
src="/images/icon-menu-agentic.png"
srcset="" />
</div>
<div class="menu-title-wrapper">
<span class="menu-title">{{ menu.title }}</span>
</div>
</template>
<!-- 正常模式:左右布局 -->
<template v-else>
<!-- 菜单图标 -->
<img v-if="menu?.level === 1"
alt=""
class="menu-icon"
src="/images/icon-menu-agentic.png"
srcset="" />
<!-- 菜单标题 -->
<span class="menu-title">{{ menu.title }}</span>
<!-- 展开/收起图标 -->
<span v-if="hasChildren && !collapsed"
class="expand-icon"
@click.stop="handleToggle">
<el-icon v-if="isExpanded"
color="#1956FE"
size="1.5em">
<ArrowDown />
</el-icon>
<el-icon v-else
color="#1956FE"
size="1.5em">
<ArrowRight />
</el-icon>
</span>
<!-- 徽章 -->
<span v-if="menu.meta?.badge" class="menu-badge">
{{ menu.meta.badge }}
</span>
</template>
</div>
<!-- 正常模式的子菜单 -->
<transition name="submenu-expand">
<div v-if="hasChildren && isExpanded && !collapsed"
class="submenu">
<SidebarMenuItem v-for="child in menu.children?.filter(c => !c.hidden)"
:key="child.id"
:active-path="activePath"
:collapsed="collapsed"
:expanded-keys="expandedKeys"
:menu="child"
@click="$emit('click', $event)"
@toggle="$emit('toggle', $event)" />
</div>
</transition>
<!-- 折叠模式的悬浮子菜单卡片 -->
<transition name="popover-fade">
<div v-if="collapsed && hasChildren && showPopover && menu.level === 1"
class="submenu-popover"
:style="{ top: `${popoverTop}px` }">
<div class="popover-arrow"></div>
<div class="popover-content">
<div class="popover-menu-list">
<div v-for="child in menu.children?.filter(c => !c.hidden)"
:key="child.id"
class="popover-menu-item"
:class="{ active: checkChildActive(child) }"
@click="handleChildClick(child)">
<span class="popover-menu-title">{{ child.title }}</span>
<el-icon v-if="hasVisibleChildren(child)">
<ArrowRight />
</el-icon>
</div>
</div>
</div>
</div>
</transition>
</div>
</template>
<style scoped>
.sidebar-menu-item {
position: relative;
width: 100%;
padding: 0 8px;
}
.menu-item-content {
position: relative;
display: flex;
gap: 8px;
align-items: center;
padding: 8px 16px;
margin-top: 8px;
color: #333;
cursor: pointer;
user-select: none;
transition: all 0.3s;
}
/* 折叠模式样式 */
.sidebar-menu-item.collapsed {
padding: 0 8px;
}
.menu-item-content.collapsed {
flex-direction: column;
gap: 2px;
align-items: center;
justify-content: center;
min-height: 60px;
padding: 12px 8px;
margin-top: 4px;
border-radius: 8px;
}
.menu-item-content.collapsed:hover {
background-color: #f0f4ff;
}
.menu-item-content.collapsed.active {
color: #1956fe;
background-color: #e8f0ff;
}
/* 折叠状态下的一级菜单选中样式 */
.menu-item-content.collapsed.level-1.active {
background-color: #fff !important;
}
.menu-item-content.collapsed.level-1.active:hover {
background-color: #fff !important;
}
.menu-icon-wrapper {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
}
.menu-title-wrapper {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
}
.menu-item-content.collapsed .menu-title {
display: -webkit-box;
overflow: hidden;
text-overflow: ellipsis;
-webkit-line-clamp: 2;
font-size: 12px;
line-height: 1.2;
text-align: center;
word-break: break-all;
-webkit-box-orient: vertical;
}
/* 层级缩进 */
.menu-item-content.level-1 {
padding-left: 14px;
}
/* 折叠模式下覆盖层级缩进 */
.menu-item-content.collapsed.level-1 {
padding-right: 8px;
padding-left: 8px;
}
.menu-item-content.level-2 {
padding-left: 32px;
font-size: 14px;
}
.menu-item-content.level-3 {
padding-left: 48px;
font-size: 14px;
}
.menu-item-content.level-4 {
padding-left: 64px;
font-size: 14px;
}
.menu-item-content:hover {
color: #1956fe;
background-color: #f5f5f5;
}
.menu-item-content.active {
color: #1956fe;
}
/* 一级菜单选中样式:仅改变文字颜色,无背景色和边框 */
.menu-item-content.level-1.active {
background-color: transparent !important;
}
.menu-item-content.level-1.active:hover {
background-color: transparent !important;
}
.menu-item-content.level-1.active::before {
display: none;
}
/* 子菜单选中样式:保留背景色,移除边框 */
.menu-item-content.level-2.active,
.menu-item-content.level-3.active,
.menu-item-content.level-4.active {
background-color: #e8f0ff;
border-radius: 6px;
}
.menu-item-content.level-2.active::before,
.menu-item-content.level-3.active::before {
display: none;
}
.expand-icon {
display: flex;
flex-shrink: 0;
align-items: center;
justify-content: center;
width: 1.5em;
height: 1.5em;
font-size: 10px;
color: #666;
transition: transform 0.3s;
}
.menu-icon {
display: flex;
flex-shrink: 0;
align-items: center;
justify-content: center;
width: 20px;
height: 20px;
font-size: 16px;
}
.menu-item-content.collapsed .menu-icon {
width: 24px;
height: 24px;
}
.menu-title {
flex: 1;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.menu-badge {
flex-shrink: 0;
min-width: 18px;
height: 18px;
padding: 0 6px;
font-size: 11px;
line-height: 18px;
color: #fff;
text-align: center;
background-color: #ff4d4f;
border-radius: 9px;
}
.submenu {
padding-left: 3px;
overflow: hidden;
}
/* 子菜单展开动画 */
.submenu-expand-enter-active,
.submenu-expand-leave-active {
transition: all 0.3s ease;
}
.submenu-expand-enter-from,
.submenu-expand-leave-to {
max-height: 0;
opacity: 0;
transform: translateY(-10px);
}
.submenu-expand-enter-to,
.submenu-expand-leave-from {
max-height: 1000px;
opacity: 1;
transform: translateY(0);
}
/* 有子菜单的项目样式 */
.menu-item-content.has-children {
/* 样式已通过其他选择器定义 */
}
/* 二级菜单样式 */
.level-2 .menu-item-content {
background-color: rgb(0 0 0 / 2%);
}
.level-2 .menu-item-content:hover {
background-color: #f0f0f0;
}
/* 三级菜单样式 */
.level-3 .menu-item-content {
background-color: rgb(0 0 0 / 4%);
}
.level-3 .menu-item-content:hover {
background-color: #ebebeb;
}
/* 四级菜单样式 */
.level-4 .menu-item-content {
background-color: rgb(0 0 0 / 6%);
}
.level-4 .menu-item-content:hover {
background-color: #e5e5e5;
}
/* 折叠模式下的悬浮子菜单卡片 */
.submenu-popover {
position: fixed;
left: 88px;
z-index: 1000;
min-width: 200px;
max-width: 300px;
max-height: 400px;
padding: 8px 0;
overflow-y: auto;
pointer-events: auto;
background: #fff;
border-radius: 8px;
box-shadow: 0 4px 12px rgb(0 0 0 / 15%);
}
.popover-arrow {
position: absolute;
top: 20px;
left: -6px;
width: 0;
height: 0;
border-top: 6px solid transparent;
border-right: 6px solid #fff;
border-bottom: 6px solid transparent;
filter: drop-shadow(-2px 0 2px rgb(0 0 0 / 10%));
}
.popover-content {
position: relative;
background: #fff;
border-radius: 8px;
}
.popover-title {
padding: 8px 16px;
font-size: 14px;
font-weight: 600;
color: #333;
border-bottom: 1px solid #f0f0f0;
}
.popover-menu-list {
padding:0 4px;
}
.popover-menu-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px 16px;
color: #333;
cursor: pointer;
transition: all 0.2s;
}
.popover-menu-item:hover {
color: #1956fe;
background-color: #f0f4ff;
border-radius: 4px;
}
.popover-menu-item.active {
font-weight: 500;
color: #1956fe;
background-color: #e8f0ff;
border-radius: 4px;
}
.popover-menu-title {
flex: 1;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* 悬浮卡片动画 */
.popover-fade-enter-active,
.popover-fade-leave-active {
transition: all 0.2s ease;
}
.popover-fade-enter-from {
opacity: 0;
transform: translateX(-10px);
}
.popover-fade-leave-to {
opacity: 0;
transform: translateX(-10px);
}
</style>