-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
322 lines (266 loc) · 10.6 KB
/
main.js
File metadata and controls
322 lines (266 loc) · 10.6 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
// main.js
console.log('main.js loaded');
(function () {
// --- small DOM helpers ---
const $all = (sel, root = document) => Array.from(root.querySelectorAll(sel));
const $ = (sel, root = document) => root.querySelector(sel);
// =============================================================
// Load welcome page by default on first visit
// =============================================================
window.addEventListener("DOMContentLoaded", () => {
const content = document.querySelector(".content");
if (content) {
fetch("pages/introduction/welcome.html")
.then(r => r.text())
.then(html => {
content.innerHTML = html;
// Optionally highlight the matching menu item
const li = document.querySelector('.submenu li[data-page="pages/welcome.html"]');
if (li) {
document.querySelectorAll(".submenu li.selected").forEach(el => el.classList.remove("selected"));
li.classList.add("selected");
}
})
.catch(err => console.error("Error loading welcome page:", err));
}
});
// clean up badges in the sidebar so text isn’t duplicated in labels
document.querySelectorAll('.submenu .badge').forEach(badge => {
if (!badge.hasAttribute('data-label')) {
badge.setAttribute('data-label', badge.textContent.trim());
badge.textContent = '';
}
badge.setAttribute('aria-hidden', 'true');
});
function setActiveMenuItem(li) {
document.querySelectorAll('.submenu li.selected').forEach(el => el.classList.remove('selected'));
if (li) li.classList.add('selected');
}
// Enable in-panel SPA navigation for data-page elements inside .content too
document.addEventListener('click', function (event) {
const target = event.target.closest('[data-page]');
if (!target) return;
setActiveMenuItem(target);
// Prevent full page reload
event.preventDefault();
const page = target.getAttribute('data-page');
if (!page) return;
// Load the HTML into the main content area
fetch(page)
.then(response => response.text())
.then(html => {
const content = document.querySelector('.content');
if (content) {
content.innerHTML = html;
// Optional: scroll to top after load
content.scrollTo({ top: 0, behavior: 'smooth' });
}
})
.catch(err => console.error('Error loading page:', err));
});
// --- fuzzy matching helpers (used by search overlay) ---
const _norm = (s) => String(s || '')
.toLowerCase()
.replace(/[“”]/g, '"').replace(/[‘’]/g, "'")
.replace(/ /g, ' ')
.replace(/\s+/g, ' ')
.replace(/[^\p{L}\p{N}\s'"]/gu, ' ')
.trim();
const _tokens = (s) => _norm(s).split(' ').filter(Boolean);
function _score(label, wanted) {
const a = _tokens(label);
const b = _tokens(wanted);
if (!a.length || !b.length) return 0;
if (a.join(' ') === b.join(' ')) return 100; // exact match
const aset = new Set(a);
const hits = b.filter(t => aset.has(t)).length;
const coverage = hits / b.length;
const aStr = a.join(' ');
const bStr = b.join(' ');
const starts = aStr.startsWith(bStr) ? 0.2 : 0;
const incl = aStr.includes(bStr) ? 0.1 : 0;
return Math.round((coverage * 80) + (starts * 10) + (incl * 10));
}
// quick alias map if names don’t line up exactly
const TITLE_ALIASES = {
'Project Attributes': 'Project Attributions',
'Project Labels': 'Project Labels',
};
function findMenuItemByTitleSmart(title) {
const wanted = TITLE_ALIASES[title] || title || '';
const esc = (v) => (window.CSS && CSS.escape) ? CSS.escape(v) : v;
// try direct lookup first
let li =
document.querySelector(`.submenu li[data-section="${esc(wanted)}"]`) ||
document.querySelector(`.submenu li[data-title="${esc(wanted)}"]`);
if (li) return li;
// otherwise score all items and pick the best
let best = null, bestScore = 0;
$all('.submenu li').forEach(el => {
const label = el.getAttribute('data-section') ||
el.getAttribute('data-title') ||
el.textContent || '';
const s = _score(label, wanted);
if (s > bestScore) { best = el; bestScore = s; }
});
return bestScore >= 60 ? best : null;
}
// --- scrolling helpers ---
function getScrollContainer(el) {
let node = el?.parentElement;
while (node && node !== document.body) {
const style = getComputedStyle(node);
if (/(auto|scroll|overlay)/i.test(style.overflowY) && node.scrollHeight > node.clientHeight) {
return node;
}
node = node.parentElement;
}
return document.querySelector('.sidebar') || document.scrollingElement || document.documentElement;
}
function scrollIntoViewWithin(container, target) {
if (!container || !target) return;
const offsetTop = target.offsetTop - container.offsetTop;
const targetCenter = offsetTop - (container.clientHeight / 2) + (target.clientHeight / 2);
container.scrollTo({ top: Math.max(0, targetCenter), behavior: 'smooth' });
}
// --- selecting + loading pages ---
function selectMenuItem(li) {
$all('.submenu li.selected').forEach(el => el.classList.remove('selected'));
li.classList.add('selected');
}
function loadPage(page) {
const contentEl = $('.content');
if (!page || !contentEl) return;
fetch(page)
.then(r => r.text())
.then(html => {
contentEl.innerHTML = html;
if (window.hydrateInjectedVideo) {
try { window.hydrateInjectedVideo(contentEl); } catch (e) { console.warn(e); }
}
})
.catch(err => {
console.error('Error loading content:', err);
contentEl.innerHTML = `<p>Failed to load content.</p>`;
});
}
function bindSubmenuClicks() {
$all('.submenu li').forEach(item => {
if (item._bound) return; // don’t double bind
item._bound = true;
item.addEventListener('click', () => {
selectMenuItem(item);
const page = item.getAttribute('data-page');
loadPage(page);
}, { passive: true });
});
}
bindSubmenuClicks();
// --- home button: jumps to Logging In ---
const HOME_TARGET_PAGE = 'pages/introduction/logging-in.html';
const homeBtn = document.querySelector('.home-btn');
if (homeBtn) {
homeBtn.addEventListener('click', () => {
const li = document.querySelector(`.submenu li[data-page="${HOME_TARGET_PAGE}"]`);
if (!li) return console.warn('Home target not found:', HOME_TARGET_PAGE);
// make sure section is open
const submenu = li.closest('.submenu');
if (submenu && submenu.classList.contains('collapsed')) {
const header = submenu.previousElementSibling;
header ? header.click() : submenu.classList.remove('collapsed');
}
// trigger normal click
selectMenuItem(li);
li.click();
// scroll it into view
const container = getScrollContainer(li);
scrollIntoViewWithin(container, li);
// quick spotlight animation
li.classList.add('spotlight');
setTimeout(() => li.classList.remove('spotlight'), 1100);
});
}
// --- sidebar toggle for mobile ---
const toggleBtn = $('#menu-toggle');
const mainContainer = $('.main-container');
toggleBtn?.addEventListener('click', () => {
mainContainer?.classList.toggle('offcanvas');
});
// --- section expand/collapse ---
function initSectionToggles() {
$all('.section-title, .section-header').forEach((header) => {
const submenu = header.nextElementSibling;
if (!submenu || !submenu.classList.contains('submenu')) return;
const isCollapsed = submenu.classList.contains('collapsed');
header.classList.toggle('open', !isCollapsed);
header.classList.toggle('closed', isCollapsed);
header.setAttribute('aria-expanded', String(!isCollapsed));
const icon = header.querySelector('.toggle-icon, .dropdown-icon');
const setIcon = (collapsed) => { if (icon) icon.textContent = collapsed ? '▶' : '▼'; };
setIcon(isCollapsed);
const toggle = () => {
const willCollapse = !submenu.classList.contains('collapsed');
submenu.classList.toggle('collapsed', willCollapse);
header.classList.toggle('open', !willCollapse);
header.classList.toggle('closed', willCollapse);
header.setAttribute('aria-expanded', String(!willCollapse));
setIcon(willCollapse);
};
header.addEventListener('click', (e) => { e.preventDefault(); toggle(); });
header.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); toggle(); }
});
});
}
initSectionToggles();
// --- hook for search modal events ---
window.addEventListener('search:openTutorial', (e) => {
const mainContainer = document.querySelector('.main-container');
const sidebar = document.querySelector('.sidebar');
mainContainer?.classList.remove('offcanvas', 'collapsed');
sidebar?.classList.remove('collapsed');
const title = (e.detail && (e.detail.title || e.detail.layer)) || '';
if (!title) return;
const li = findMenuItemByTitleSmart(title);
if (!li) return console.warn('No menu match for:', title);
const submenu = li.closest('.submenu');
if (submenu && submenu.classList.contains('collapsed')) {
const header = submenu.previousElementSibling;
header ? header.click() : submenu.classList.remove('collapsed');
}
selectMenuItem(li);
li.click();
const container = getScrollContainer(li);
scrollIntoViewWithin(container, li);
li.classList.add('spotlight');
setTimeout(() => li.classList.remove('spotlight'), 1100);
});
// --- Role-card spotlight navigation (for Logging In page) ---
document.addEventListener('click', (e) => {
const card = e.target.closest('.role-card[data-page]');
if (!card) return;
e.preventDefault();
const page = card.getAttribute('data-page');
if (!page) return;
// Find matching sidebar item
const li = document.querySelector(`.submenu li[data-page="${page}"]`);
if (!li) {
console.warn('No matching sidebar item for:', page);
return;
}
// Expand the section if collapsed
const submenu = li.closest('.submenu');
if (submenu && submenu.classList.contains('collapsed')) {
const header = submenu.previousElementSibling;
if (header) header.click();
}
// Select and trigger load
li.classList.add('selected');
li.click();
// Smooth scroll and spotlight (uses your existing functions)
const container = getScrollContainer(li);
scrollIntoViewWithin(container, li);
li.classList.add('spotlight');
setTimeout(() => li.classList.remove('spotlight'), 1100);
});
})();