Skip to content

Commit ce62333

Browse files
committed
fix: 导航栏切换语言后自动折叠 bug 修复,连续点击展开 bug 修复
1 parent 8c776e9 commit ce62333

File tree

2 files changed

+108
-95
lines changed

2 files changed

+108
-95
lines changed

components/bar/BarLeft.vue

Lines changed: 104 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,72 @@
11
<script setup>
2-
const { tm } = useI18n();
2+
const { tm, locale } = useI18n();
33
4-
const navigationList = computed(() => {
4+
const getComp = computed(() => {
55
const textValue = tm('BarLeft');
66
const linkValue = tm('allUniversalLink');
77
const localLink = linkValue.local;
88
99
return [
10-
{
11-
title: textValue.title1,
12-
children: [
13-
localLink.aoscOs,
14-
localLink.afterglow,
15-
localLink.liblol,
16-
localLink.oma,
17-
localLink.l10n
18-
],
19-
show: true
20-
},
21-
{
22-
title: textValue.title2,
23-
children: [
24-
localLink.news,
25-
localLink.gallery,
26-
useTIndex(localLink.contact, 1),
27-
useTIndex(linkValue.AOSCWiki, 1)
28-
],
29-
show: true
30-
},
31-
{
32-
title: textValue.title3,
33-
children: [
34-
localLink.about,
35-
localLink.events,
36-
localLink.internship,
37-
localLink.sponsors,
38-
localLink.crowdsourcing,
39-
localLink.guidelines,
40-
localLink.mascot
41-
],
42-
show: true
43-
},
44-
{
45-
title: textValue.title4,
46-
children: [
47-
localLink.paste,
48-
linkValue.forum,
49-
useTIndex(linkValue.GitHub, 1),
50-
linkValue.mail20,
51-
linkValue.buildbots,
52-
linkValue.buildit
53-
],
54-
show: true
55-
}
10+
[
11+
{
12+
title: textValue.title1,
13+
children: [
14+
localLink.aoscOs,
15+
localLink.afterglow,
16+
localLink.liblol,
17+
localLink.oma,
18+
localLink.l10n
19+
],
20+
show: true
21+
},
22+
{
23+
title: textValue.title2,
24+
children: [
25+
localLink.news,
26+
localLink.gallery,
27+
useTIndex(localLink.contact, 1),
28+
useTIndex(linkValue.AOSCWiki, 1)
29+
],
30+
show: true
31+
},
32+
{
33+
title: textValue.title3,
34+
children: [
35+
localLink.about,
36+
localLink.events,
37+
localLink.internship,
38+
localLink.sponsors,
39+
localLink.crowdsourcing,
40+
localLink.guidelines,
41+
localLink.mascot
42+
],
43+
show: true
44+
},
45+
{
46+
title: textValue.title4,
47+
children: [
48+
localLink.paste,
49+
linkValue.forum,
50+
useTIndex(linkValue.GitHub, 1),
51+
linkValue.mail20,
52+
linkValue.buildbots,
53+
linkValue.buildit
54+
],
55+
show: true
56+
}
57+
],
58+
['0', '1', '2', '3']
5659
];
5760
});
5861
62+
const navigationList = computed(() => {
63+
return getComp.value[0];
64+
});
65+
66+
const defaultOpeneds = computed(() => {
67+
return getComp.value[1];
68+
});
69+
5970
const openMenuList = new Set();
6071
6172
const menuDivRef = useTemplateRef('menuDiv');
@@ -67,35 +78,36 @@ const rowHeightpx = `${rowHeight}px`;
6778
const route = useRoute();
6879
6980
const openMenu = (MenuOpenEvent) => {
70-
const result = navigationList.value.find(
71-
(item) => item.title === MenuOpenEvent
72-
);
7381
let height =
74-
result.children.length * rowHeight +
82+
navigationList.value[MenuOpenEvent].children.length * rowHeight +
7583
chunkPading +
7684
menuDivRef.value.clientHeight;
7785
for (const item of openMenuList) {
7886
if (highlyIsQualified(height)) {
7987
break;
8088
} else {
81-
height = height - item[1] * rowHeight - chunkPading;
82-
openMenuList.delete(item);
83-
console.log(item[0]);
84-
menuRef.value.close(item[0]);
89+
height -=
90+
navigationList.value[item].children.length * rowHeight - chunkPading;
91+
closeMenu(item);
8592
}
8693
}
87-
openMenuList.add([MenuOpenEvent, result.children.length]);
94+
openMenuList.add(MenuOpenEvent);
8895
};
8996
90-
const closeMenu = (MenuOpenEvent) => {
91-
for (const arr of openMenuList) {
92-
if (arr[0] === MenuOpenEvent) {
93-
openMenuList.delete(arr);
94-
break; // 删除后跳出循环,因为我们只删除第一个
97+
const deleteSetItem = (target, setList) => {
98+
for (const item of setList) {
99+
if (item === target) {
100+
setList.delete(item);
101+
break;
95102
}
96103
}
97104
};
98105
106+
const closeMenu = (MenuOpenEvent) => {
107+
deleteSetItem(MenuOpenEvent, openMenuList);
108+
menuRef.value.close(MenuOpenEvent);
109+
};
110+
99111
const highlyIsQualified = (height) => {
100112
// 在中心内容长度小于window.innerHeight时,回到首页弹窗不可能出来
101113
// 此时高度比较参照中心内容长度即可,不需要算上弹窗和底栏
@@ -112,7 +124,9 @@ const highlyIsQualified = (height) => {
112124
const { $mitt } = useNuxtApp();
113125
onMounted(() => {
114126
$mitt.on('routeSwitching', () => {
115-
retractMenuBar();
127+
nextTick(() => {
128+
retractMenuBar();
129+
});
116130
});
117131
});
118132
onBeforeUnmount(() => {
@@ -126,15 +140,14 @@ const retractMenuBar = () => {
126140
break;
127141
} else {
128142
if (openMenuList.size === 1) break;
129-
height =
130-
height -
131-
navigationList.value.find((item1) => item1.title === item).children
132-
.length *
133-
rowHeight;
134-
openMenuList.delete(item);
135-
menuRef.value.close(item);
143+
height -=
144+
navigationList.value[item].children.length * rowHeight - chunkPading;
145+
closeMenu(item);
136146
}
137147
}
148+
// for (const item of openMenuList) {
149+
// menuRef.value.open(item);
150+
// }
138151
};
139152
onMounted(() => {
140153
// 每次缩放改变的时候,判断有没有栏目需要缩回去,先展开的,优先缩进
@@ -151,35 +164,30 @@ onMounted(() => {
151164
let height = menuDivRef.value.clientHeight;
152165
// 初次加载的时候尝试打开当前栏目分类
153166
// 记一下目前所在分类的title
154-
let thisTitle = null;
155-
for (const item of navigationList.value.values()) {
156-
const resule = item.children.find((item1) =>
157-
route.path.includes(item1.url)
158-
);
159-
if (resule) {
160-
height = height + item.children.length * rowHeight;
161-
// 展开但不进入队列,因为要给这个放到队列尾
162-
thisTitle = item.title;
163-
break;
164-
}
165-
}
166-
// 然后在剩余空间里按顺序遍历栏目,能展开尽量展开
167-
for (const item of navigationList.value) {
168-
if (thisTitle !== item.title) {
169-
const cache_h = height + item.children.length * rowHeight + chunkPading;
170-
if (highlyIsQualified(cache_h)) {
171-
height = cache_h;
172-
menuRef.value.open(item.title);
167+
let thisElSubEnum = null;
168+
for (const [index, item] of navigationList.value.entries()) {
169+
if (thisElSubEnum === null) {
170+
if (item.children.find((item1) => route.path.includes(item1.url))) {
171+
// 当前所在不入列尾
172+
thisElSubEnum = index;
173173
} else {
174-
break;
174+
openMenuList.add(String(index));
175175
}
176+
} else {
177+
openMenuList.add(String(index));
176178
}
177179
}
178-
// 剩余栏目展开完毕,展开当前所在栏目,此时屏幕缩小优先关闭其他栏目
179-
if (thisTitle !== null) {
180-
menuRef.value.open(thisTitle);
180+
// 然后在剩余空间里按顺序遍历栏目,能展开尽量展开
181+
// 默认全部展开,根据空间从下向上依次关闭,跳过当前栏目
182+
for (const [index, item] of reverseIterator(navigationList.value)) {
183+
if (highlyIsQualified(height)) {
184+
break;
185+
}
186+
if (thisElSubEnum !== index) {
187+
height = height - (item.children.length * rowHeight + chunkPading);
188+
closeMenu(String(index));
189+
}
181190
}
182-
183191
// 判断当前所在位置是否需要回到顶部按钮
184192
returnFromTop();
185193
// 挂载上面监听器
@@ -223,12 +231,13 @@ const backToTopBtnShow = ref(false);
223231
<el-menu
224232
ref="menu"
225233
class="my-el-menu"
234+
:default-openeds="defaultOpeneds"
226235
@close="closeMenu"
227236
@open="openMenu">
228237
<el-sub-menu
229238
v-for="(item, index) in navigationList"
230239
:key="`barleft-1-link-${index}`"
231-
:index="item.title">
240+
:index="String(index)">
232241
<template #title>
233242
<span>{{ item.title }}</span>
234243
</template>

utils/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ export const requestPostJson = (() => {
7272
};
7373
})();
7474

75+
export function* reverseIterator(arr) {
76+
let i = arr.length - 1;
77+
while (i >= 0) yield [i, arr[i--]];
78+
}
7579
export const BToMB = (byteSize, fixed = 3) => {
7680
return (byteSize / 1024 / 1024).toFixed(fixed);
7781
};

0 commit comments

Comments
 (0)