-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
319 lines (264 loc) · 8.74 KB
/
Copy pathscript.js
File metadata and controls
319 lines (264 loc) · 8.74 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
// Mux Player (loaded via script tag in HTML)
// No import needed - Mux player is a web component
// Constants
const MUX_PLAYBACK_ID = "WiotNBkpu2om5owi02fZhkaGAoQHlrXU3Nzf3qDHZqp8";
const DEBUG_MODE = false; // Global debug flag to prevent video playback
// State Management
let player = null;
let currentSectionId = null;
let isUserNavigating = false; // Flag to track user navigation
let playerInitialized = false;
let pageReady = false; // New flag to track page readiness
let isInitialLoad = true; // Flag to track initial page load
let pendingSeek = null; // Track pending seek operations
let seekTimeout = null; // Track seek timeout for cleanup
// Helper Functions
function getSectionStartTime(sectionId) {
const section = document.getElementById(sectionId);
return section ? parseFloat(section.dataset.start) : 0;
}
function safeSeek(sectionId) {
if (!player) return;
// Clear any pending seek operation
if (seekTimeout) {
clearTimeout(seekTimeout);
}
const targetTime = getSectionStartTime(sectionId);
const wasPlaying = !player.paused;
// Set flag immediately
isUserNavigating = true;
// Pause if playing to prevent playback during seek
if (wasPlaying) {
player.pause();
}
// Perform the seek
try {
player.currentTime = targetTime;
} catch (error) {
console.error("Seek error:", error);
}
// Resume playback if it was playing
if (wasPlaying) {
// Small delay to allow seek to settle
setTimeout(() => {
player.play().catch((err) => {
console.error("Play error:", err);
});
}, 100);
}
// Clear navigation flag after a delay
seekTimeout = setTimeout(() => {
isUserNavigating = false;
seekTimeout = null;
}, 1000);
}
function getActiveSectionId(currentTime) {
const sections = Array.from(
document.querySelectorAll(".content-carousel section")
);
return sections.find((section, index) => {
const start = parseFloat(section.dataset.start);
const end =
index < sections.length - 1
? parseFloat(sections[index + 1].dataset.start)
: Number.MAX_SAFE_INTEGER;
return currentTime >= start && currentTime < end;
})?.id;
}
function updateActiveSection(sectionId) {
if (currentSectionId === sectionId) return;
document.querySelectorAll(".nav-item").forEach((item) => {
item.classList.toggle("active", item.dataset.section === sectionId);
});
document.querySelectorAll(".mobile-menu li").forEach((item) => {
item.classList.toggle("active", item.dataset.section === sectionId);
});
document.querySelectorAll(".content-carousel section").forEach((section) => {
section.classList.toggle("active", section.id === sectionId);
});
currentSectionId = sectionId;
}
// Event Handlers
function handleTimeUpdate({ seconds }) {
const sectionId = getActiveSectionId(seconds);
if (sectionId && !isUserNavigating) {
updateActiveSection(sectionId);
history.replaceState(null, null, `#${sectionId}`);
}
}
// Function to ensure scroll position is at top
function ensureScrollAtTop() {
return new Promise((resolve) => {
if (window.scrollY === 0) {
resolve();
return;
}
window.scrollTo(0, 0);
// Check if scroll has completed
const checkScroll = setInterval(() => {
if (window.scrollY === 0) {
clearInterval(checkScroll);
resolve();
}
}, 10);
// Timeout after 1 second
setTimeout(() => {
clearInterval(checkScroll);
resolve();
}, 1000);
});
}
// Function to show page content
function showPageContent() {
if (pageReady) return;
const contentSection = document.querySelector(".content-carousel");
contentSection.classList.remove("opacity-0");
document.body.classList.remove("no-scroll");
pageReady = true;
}
// Initialize everything after DOM is loaded
document.addEventListener("DOMContentLoaded", async () => {
// Add no-scroll class to body for non-root routes
const hash = window.location.hash.slice(1);
if (hash) {
document.body.classList.add("no-scroll");
}
// DOM Elements
const navItems = document.querySelectorAll(".nav-item");
const muxPlayer = document.getElementById("mux-player");
const thumbnailOverlay = document.getElementById("thumbnail-overlay");
// Handle initial navigation based on hash
const initialSectionId = hash || "intro";
const initialSection = document.getElementById(initialSectionId);
const initialTime = initialSection
? parseFloat(initialSection.dataset.start)
: 0;
// Only delay render for non-root routes
if (hash) {
// Force scroll to top without animation
window.scrollTo(0, 0);
// Wait a bit to ensure scroll is complete
await new Promise((resolve) => setTimeout(resolve, 100));
}
showPageContent();
if (initialSection) {
updateActiveSection(initialSectionId);
}
// Initialize Mux Player
if (muxPlayer) {
player = muxPlayer;
try {
// Wait for Mux player to be ready
await new Promise((resolve) => {
if (player.readyState >= 1) {
resolve();
} else {
player.addEventListener("loadedmetadata", resolve, { once: true });
}
});
playerInitialized = true;
console.log("Player ready, seeking to:", initialTime);
// Event Listeners
player.addEventListener("timeupdate", () => {
handleTimeUpdate({ seconds: player.currentTime });
});
// Set the initial time if needed
if (initialTime > 0) {
player.currentTime = initialTime;
}
// Hide the thumbnail overlay after player is ready
if (thumbnailOverlay) {
thumbnailOverlay.style.display = "none";
}
} catch (error) {
console.error("Error initializing player:", error);
}
}
// Update hash change event listener to ensure scroll position
window.addEventListener("hashchange", async () => {
const newHash = window.location.hash.slice(1);
const newSection = document.getElementById(newHash);
if (newSection && newHash !== currentSectionId) {
// Add no-scroll class
document.body.classList.add("no-scroll");
// Force scroll to top without animation
window.scrollTo(0, 0);
// Wait a bit to ensure scroll is complete
await new Promise((resolve) => setTimeout(resolve, 100));
updateActiveSection(newHash);
// Remove no-scroll class
document.body.classList.remove("no-scroll");
if (player) {
safeSeek(newHash);
}
}
});
// Add click handlers to navigation items
navItems.forEach((item) => {
item.addEventListener("click", async (event) => {
event.preventDefault();
const section = event.currentTarget.dataset.section;
updateActiveSection(section);
history.pushState({ section }, "", `#${section}`);
if (player) {
safeSeek(section);
}
});
});
// Mobile Menu Functionality
const mobileMenuButton = document.querySelector(".mobile-menu-button");
const mobileMenu = document.querySelector(".mobile-menu");
const mobileMenuClose = document.querySelector(".mobile-menu-close");
if (!mobileMenuButton || !mobileMenu || !mobileMenuClose) {
console.error("Mobile menu elements not found");
}
function toggleMobileMenu() {
if (!mobileMenu || !mobileMenuButton) return;
mobileMenu.classList.toggle("active");
const isExpanded =
mobileMenuButton.getAttribute("aria-expanded") === "true";
mobileMenuButton.setAttribute("aria-expanded", !isExpanded);
}
mobileMenuButton?.addEventListener("click", (e) => {
e.preventDefault();
e.stopPropagation();
toggleMobileMenu();
});
mobileMenuClose.addEventListener("click", toggleMobileMenu);
mobileMenu.addEventListener("click", (e) => {
if (e.target === mobileMenu) {
toggleMobileMenu();
}
});
// Add event listeners to mobile menu items
const menuItems = mobileMenu.querySelectorAll("li");
menuItems.forEach((item) => {
item.addEventListener("click", async (event) => {
event.preventDefault();
// Close menu first
mobileMenu.classList.remove("active");
mobileMenuButton.setAttribute("aria-expanded", "false");
// Then update content
const section = event.currentTarget.dataset.section;
updateActiveSection(section);
history.pushState({ section }, "", `#${section}`);
// Handle video navigation
if (player) {
safeSeek(section);
}
});
});
// Add click handlers to continue reading links
document.querySelectorAll('.link[href^="#"]').forEach((link) => {
link.addEventListener("click", async (e) => {
e.preventDefault();
const targetId = link.getAttribute("href").slice(1);
window.location.hash = targetId;
window.scrollTo(0, 0);
// Handle video navigation
if (player) {
safeSeek(targetId);
}
});
});
});