-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscript.js
More file actions
183 lines (158 loc) · 5.71 KB
/
Copy pathscript.js
File metadata and controls
183 lines (158 loc) · 5.71 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
const NAV_OFFSET = 80;
// Find category section by anchor name
function getCategoryForAnchor(targetId) {
return document.querySelector(`section.category[data-category="${targetId}"]`);
}
// Open only target category, collapse others
function openOnlyCategory(targetId) {
document.querySelectorAll('section.category').forEach(c => {
if (c.dataset.category === targetId) {
c.classList.add('expanded');
} else {
c.classList.remove('expanded');
}
});
}
// Scroll to anchor with offset
function scrollToAnchorWithOffset(targetId) {
const anchor = document.querySelector(`a[name="${targetId}"]`);
if (!anchor) return;
const top = anchor.getBoundingClientRect().top + window.pageYOffset - NAV_OFFSET;
window.scrollTo({ top: top, behavior: 'smooth' });
}
// Scrollspy
function updateActiveNav() {
const navItems = document.querySelectorAll('.bk_item');
const sections = document.querySelectorAll('section.category');
const triggerLine = window.innerHeight / 2;
let activeId = null;
sections.forEach(s => {
const rect = s.getBoundingClientRect();
if (rect.top <= triggerLine && rect.bottom > triggerLine) {
activeId = s.dataset.category;
}
});
if (!activeId) {
sections.forEach(s => {
const rect = s.getBoundingClientRect();
if (rect.top <= triggerLine) {
activeId = s.dataset.category;
}
});
}
navItems.forEach(item => {
const link = item.querySelector('a');
if (!link) return;
const itemId = link.getAttribute('href').substring(1);
const isActive = (itemId === activeId);
const wasActive = item.classList.contains('active');
item.classList.toggle('active', isActive);
if (isActive && !wasActive && window.innerWidth <= 640) {
item.scrollIntoView({
behavior: 'smooth',
block: 'nearest',
inline: 'center'
});
}
});
}
// Nav clicks
document.querySelectorAll('.bk_item a').forEach(link => {
link.addEventListener('click', (e) => {
const targetId = link.getAttribute('href').substring(1);
e.preventDefault();
// Remove focus to prevent sticky highlight on touch devices
link.blur();
// Detect whether any other category needs to collapse
const sectionsToCollapse = Array.from(document.querySelectorAll('section.category.expanded'))
.filter(c => c.dataset.category !== targetId);
openOnlyCategory(targetId);
// Wait for collapse transition to finish before scrolling,
// otherwise the target anchor position shifts after layout settles
const delay = sectionsToCollapse.length > 0 ? 450 : 50;
setTimeout(() => {
scrollToAnchorWithOffset(targetId);
history.replaceState(null, '', '#' + targetId);
}, delay);
});
});
// Toggle button clicks
document.querySelectorAll('.cat_toggle').forEach(btn => {
btn.addEventListener('click', () => {
const section = btn.closest('section.category');
if (!section) return;
const wasExpanded = section.classList.contains('expanded');
section.classList.toggle('expanded');
// If collapsing, scroll back to top of section to avoid disorientation
if (wasExpanded) {
const targetId = section.dataset.category;
setTimeout(() => scrollToAnchorWithOffset(targetId), 50);
}
setTimeout(updateActiveNav, 100);
});
});
// Initial load
window.addEventListener('load', () => {
if (window.location.hash) {
const targetId = window.location.hash.substring(1);
openOnlyCategory(targetId);
setTimeout(() => scrollToAnchorWithOffset(targetId), 100);
}
updateActiveNav();
});
// Throttled scrollspy
let scrollTimer = null;
window.addEventListener('scroll', () => {
if (scrollTimer) return;
scrollTimer = setTimeout(() => {
updateActiveNav();
scrollTimer = null;
}, 100);
});
// Adjust preview height based on first row tile height
function adjustPreviewHeight() {
document.querySelectorAll('section.category').forEach(section => {
const trow = section.querySelector('.trow');
if (!trow) return;
const tiles = trow.querySelectorAll('.tile:not([style*="background-color: white"])');
if (tiles.length === 0) return;
// Find the tallest tile in the first row
const firstTileTop = tiles[0].offsetTop;
let maxFirstRowHeight = 0;
tiles.forEach(tile => {
if (tile.offsetTop === firstTileTop) {
maxFirstRowHeight = Math.max(maxFirstRowHeight, tile.offsetHeight);
}
});
// Preview = first row + ~40% of second row for fade effect
const previewHeight = maxFirstRowHeight + 60;
section.style.setProperty('--preview-height', previewHeight + 'px');
section.style.setProperty('--preview-height-mobile', previewHeight + 'px')
});
}
window.addEventListener('load', adjustPreviewHeight);
window.addEventListener('resize', adjustPreviewHeight);
// ===== Badge handling =====
// Auto-wrap badges into a flex container for natural-width truncation
function wrapBadges() {
document.querySelectorAll('.tile').forEach(tile => {
const venue = tile.querySelector('.tile_venue');
const award = tile.querySelector('.tile_award');
if (!venue && !award) return;
if (tile.querySelector('.tile_badges')) return; // already wrapped
const wrapper = document.createElement('div');
wrapper.className = 'tile_badges';
if (venue) wrapper.appendChild(venue);
if (award) wrapper.appendChild(award);
tile.insertBefore(wrapper, tile.firstChild);
});
}
// Add title attribute to badges (for truncated text tooltip)
function addBadgeTitles() {
document.querySelectorAll('.tile_venue, .tile_award').forEach(el => {
if (!el.title) el.title = el.textContent.trim();
});
}
// Run as early as possible to avoid FOUC
document.addEventListener('DOMContentLoaded', wrapBadges);
window.addEventListener('load', addBadgeTitles);