Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
217 lines (186 loc) · 7.42 KB
/
Copy pathapp.js
File metadata and controls
217 lines (186 loc) · 7.42 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
(() => {
const legacyHost = "aiops-lab-nku.github.io";
const legacyBasePath = "/BuildBench";
const currentPath = window.location.pathname;
const normalizedPath = currentPath.toLowerCase();
const normalizedBasePath = legacyBasePath.toLowerCase();
if (window.location.hostname.toLowerCase() !== legacyHost) return;
if (
normalizedPath !== normalizedBasePath &&
!normalizedPath.startsWith(`${normalizedBasePath}/`)
) {
return;
}
const relativePath = currentPath.slice(legacyBasePath.length) || "/";
const destination = new URL(
`https://matrix.cstcloud.cn/build-bench${relativePath}`,
);
destination.search = window.location.search;
destination.hash = window.location.hash;
window.location.replace(destination.href);
})();
const menuButton = document.querySelector("[data-menu-button]");
const nav = document.querySelector("[data-nav]");
const siteHeader = document.querySelector(".site-header");
const navMore = document.querySelector("[data-nav-more]");
const navMoreButton = document.querySelector("[data-nav-more-toggle]");
const localNavLinks = Array.from(document.querySelectorAll('.page-rail nav a[href^="#"]'));
function translate(value) {
return window.BuildBenchI18n?.t(value) || value;
}
function renderIcons() {
window.lucide?.createIcons({ attrs: { "stroke-width": 1.8 } });
}
function escapeHtml(value) {
return String(value)
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """)
.replaceAll("'", "'");
}
function renderAccountState(account, session) {
const teamName = session?.team?.name ? String(session.team.name) : "";
const stateKey = teamName ? `team:${teamName}` : "anonymous";
if (account.dataset.accountState === stateKey) return;
if (teamName) {
account.innerHTML = `
<a href="team.html" class="account-action account-action--team account-team-link">
<i data-lucide="users" aria-hidden="true"></i>
<span>${escapeHtml(teamName)}</span>
</a>
<a class="account-action account-action--primary" href="my-submissions.html">${translate("My Submissions")}</a>
<button class="account-action account-action--danger" type="button" data-account-signout>${translate("Sign out")}</button>
`;
account.querySelector("[data-account-signout]")?.addEventListener("click", async () => {
try {
await window.BuildBenchAuth.logout();
} finally {
window.location.assign("index.html");
}
});
} else {
account.innerHTML = `
<a class="account-action account-action--secondary" href="login.html">${translate("Sign in")}</a>
<a class="account-action account-action--primary account-register-link" href="register.html">${translate("Register team")}</a>
`;
}
account.dataset.accountState = stateKey;
account.hidden = false;
renderIcons();
}
async function renderAccountNavigation() {
const actions = document.querySelector(".header-actions");
if (!actions) return;
let account = actions.querySelector("[data-account-navigation]");
if (!account) {
account = document.createElement("div");
account.className = "account-navigation";
account.dataset.accountNavigation = "";
actions.append(account);
}
if (account.dataset.accountHydrating === "true") return;
account.dataset.accountHydrating = "true";
const hint = window.BuildBenchAuth?.getSessionHint?.();
const hintedSession = hint?.authenticated
? { team: { name: hint.team_name } }
: null;
renderAccountState(account, hintedSession);
const session = await window.BuildBenchAuth?.getSession?.();
renderAccountState(account, session);
delete account.dataset.accountHydrating;
}
function setMoreMenu(open) {
if (!navMore || !navMoreButton) return;
navMore.classList.toggle("open", open);
navMoreButton.setAttribute("aria-expanded", String(open));
}
function setMenu(open) {
if (!menuButton || !nav) return;
updateMobileNavGeometry();
menuButton.setAttribute("aria-expanded", String(open));
menuButton.setAttribute(
"aria-label",
translate(open ? "Close navigation" : "Open navigation"),
);
menuButton.setAttribute("title", translate(open ? "Close navigation" : "Open navigation"));
nav.classList.toggle("open", open);
document.body.classList.toggle("menu-open", open);
if (!open) setMoreMenu(false);
menuButton.innerHTML = `<i data-lucide="${open ? "x" : "menu"}" aria-hidden="true"></i>`;
renderIcons();
}
function updateMobileNavGeometry() {
if (!siteHeader) return;
const headerRect = siteHeader.getBoundingClientRect();
const top = Math.max(0, Math.round(headerRect.height));
const height = Math.max(0, Math.round(window.innerHeight - Math.max(0, headerRect.bottom)));
document.documentElement.style.setProperty("--mobile-nav-top", `${top}px`);
document.documentElement.style.setProperty("--mobile-nav-height", `${height}px`);
}
menuButton?.addEventListener("click", () => {
setMenu(menuButton.getAttribute("aria-expanded") !== "true");
});
navMoreButton?.addEventListener("click", (event) => {
event.stopPropagation();
setMoreMenu(navMoreButton.getAttribute("aria-expanded") !== "true");
});
navMore?.addEventListener("focusout", (event) => {
if (!navMore.contains(event.relatedTarget)) setMoreMenu(false);
});
document.querySelectorAll(".site-nav a").forEach((link) => {
link.addEventListener("click", () => setMenu(false));
});
document.addEventListener("click", (event) => {
if (!navMore?.contains(event.target)) setMoreMenu(false);
});
document.addEventListener("keydown", (event) => {
if (event.key !== "Escape") return;
const moreWasOpen = navMoreButton?.getAttribute("aria-expanded") === "true";
setMoreMenu(false);
setMenu(false);
if (moreWasOpen && window.innerWidth > 1080) navMoreButton?.focus();
});
window.addEventListener("resize", () => {
updateMobileNavGeometry();
if (window.innerWidth > 1080) setMenu(false);
});
window.addEventListener("buildbench:languagechange", () => {
updateMobileNavGeometry();
setMenu(menuButton?.getAttribute("aria-expanded") === "true");
document.querySelector("[data-account-navigation]")?.remove();
renderAccountNavigation();
});
if ("IntersectionObserver" in window && localNavLinks.length) {
const sections = localNavLinks
.map((link) => document.getElementById(decodeURIComponent(link.getAttribute("href").slice(1))))
.filter((section, index, items) => section && items.indexOf(section) === index);
const setActiveLocalSection = (sectionId) => {
localNavLinks.forEach((link) => {
const isActive = link.getAttribute("href") === `#${sectionId}`;
link.classList.toggle("active", isActive);
if (isActive) link.setAttribute("aria-current", "location");
else link.removeAttribute("aria-current");
});
};
const initialSectionId = window.location.hash.slice(1);
setActiveLocalSection(
sections.some((section) => section.id === initialSectionId) ? initialSectionId : sections[0]?.id,
);
const observer = new IntersectionObserver(
(entries) => {
const visible = entries
.filter((entry) => entry.isIntersecting)
.sort((a, b) => b.intersectionRatio - a.intersectionRatio)[0];
if (!visible) return;
setActiveLocalSection(visible.target.id);
},
{ rootMargin: "-24% 0px -64%", threshold: [0, 0.1, 0.3] },
);
sections.forEach((section) => observer.observe(section));
}
renderAccountNavigation();
window.addEventListener("DOMContentLoaded", () => {
updateMobileNavGeometry();
renderIcons();
});