-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
354 lines (323 loc) · 14.6 KB
/
Copy pathscripts.js
File metadata and controls
354 lines (323 loc) · 14.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
Portfolio Site JavaScript (orchestrator)
Author: Josh Lollis
Date: December 3, 2024
Refactor: modularized components (December 15, 2025)
Purpose:
- Orchestrates page initialization and dynamic loading of small components
- Components now live under `/js/components/` (bio, header, footer, pfp, carousel, modal, wakatime)
- Keeps page-level scripts minimal and improves maintainability
*/
document.addEventListener("DOMContentLoaded", function () {
// Component loading and initialization
/**
* Dynamically load a component script and return a Promise that resolves when loaded.
* Uses `appUtils.loadScript` if available for de-duplication, otherwise inserts a script tag.
* @param {string} url - URL to the script to load
* @returns {Promise<void>}
*/
function loadComponentScript(url) {
if (window.appUtils && typeof window.appUtils.loadScript === 'function') {
return window.appUtils.loadScript(url);
}
return new Promise((resolve, reject) => {
const s = document.createElement('script');
s.src = url;
s.defer = true;
s.onload = () => resolve();
s.onerror = (e) => reject(e);
document.head.appendChild(s);
});
}
// Load bio component
(function loadBio() {
const initialData = {
tags: ["Front-End", "Interactive Apps", "Creative Tools"],
bio: "Software engineer focused on front-end development, interactive interfaces, and creative tools. I have 10+ years of personal and academic project experience across websites, desktop apps, games, and music-related utilities. Recently graduated Cum Laude with a B.S. in Computer Science from California State University, Fullerton after a non-traditional educational path."
};
if (window.bioComponent) {
window.bioComponent.setData && window.bioComponent.setData(initialData);
window.bioComponent.render && window.bioComponent.render("#bio-placeholder");
return;
}
loadComponentScript('/js/components/bio.js')
.then(() => {
if (window.bioComponent && window.bioComponent.setData) {
window.bioComponent.setData(initialData);
}
if (window.bioComponent && window.bioComponent.render) {
window.bioComponent.render("#bio-placeholder");
}
})
.catch(() => console.error("Failed to load bio component script."));
})();
// Initialize carousel component
(function loadCarousel() {
// Only load carousel on pages that include a carousel element
if (!document.querySelector('.carousel-images')) return;
if (window.carouselComponent) {
window.carouselComponent.init();
return;
}
loadComponentScript('/js/components/carousel.js')
.then(() => {
if (window.carouselComponent && window.carouselComponent.init) {
window.carouselComponent.init();
}
})
.catch(() => console.error('Failed to load carousel component script.'));
})();
// Load header component
(function loadHeader() {
if (window.headerComponent) {
window.headerComponent.init("#header-placeholder");
loadProjectTabs(); // Load after header is ready
return;
}
loadComponentScript('/js/components/header.js')
.then(() => {
if (window.headerComponent && window.headerComponent.init) {
window.headerComponent.init("#header-placeholder");
}
loadProjectTabs(); // Load after header is ready
})
.catch(() => console.error('Failed to load header component script.'));
})();
// Load project tabs component (depends on header for --header-height)
function loadProjectTabs() {
// Only load on pages with sliding projects
if (!document.querySelector('.sliding-projects')) return;
if (window.projectTabsComponent) {
window.projectTabsComponent.init();
return;
}
loadComponentScript('/js/components/projectTabs.js')
.then(() => {
if (window.projectTabsComponent && window.projectTabsComponent.init) {
window.projectTabsComponent.init();
}
})
.catch(() => console.error('Failed to load projectTabs component script.'));
}
// Load footer component
(function loadFooter() {
if (window.footerComponent) {
window.footerComponent.init("#footer-placeholder");
return;
}
loadComponentScript('/js/components/footer.js')
.then(() => {
if (window.footerComponent && window.footerComponent.init) {
window.footerComponent.init("#footer-placeholder");
}
})
.catch(() => console.error('Failed to load footer component script.'));
})();
// Load back-to-top button component
(function loadBackToTop() {
if (window.backToTopComponent) {
window.backToTopComponent.init();
return;
}
loadComponentScript('/js/components/backToTop.js')
.then(() => {
if (window.backToTopComponent && window.backToTopComponent.init) {
window.backToTopComponent.init();
}
})
.catch(() => console.error('Failed to load back-to-top component script.'));
})();
// Load profile picture component
(function loadPfp() {
if (window.pfpComponent) {
window.pfpComponent.init('#pfp-placeholder');
return;
}
loadComponentScript('/js/components/pfp.js')
.then(() => {
if (window.pfpComponent && window.pfpComponent.init) {
window.pfpComponent.init('#pfp-placeholder');
}
})
.catch(() => console.error('Failed to load pfp component script.'));
})();
// Load blog dropdown component
(function loadBlogDropdown() {
// Only load on pages that have dropdown sections
if (!document.querySelector('.dropdown-section')) return;
if (window.blogDropdownComponent) {
window.blogDropdownComponent.init();
return;
}
loadComponentScript('/js/components/blogDropdown.js')
.then(() => {
if (window.blogDropdownComponent && window.blogDropdownComponent.init) {
window.blogDropdownComponent.init();
}
})
.catch(() => console.error('Failed to load blog dropdown component script.'));
})();
// Load directory tree component
(function loadDirectoryTree() {
// Only load on pages that have directory tree containers
if (!document.querySelector('.directory-tree-container')) return;
if (window.directoryTree) {
// Trees are initialized via inline script in the page
return;
}
loadComponentScript('/js/components/directoryTree.js')
.then(() => {
// Trees are initialized via inline script in the page after component loads
})
.catch(() => console.error('Failed to load directory tree component script.'));
})();
// Load show view toggle component
(function loadShowViewToggle() {
// Only load on pages that have switch toggles
if (!document.querySelector('.switch')) return;
if (window.showViewToggleComponent) {
window.showViewToggleComponent.init();
return;
}
loadComponentScript('/js/components/showViewToggle.js')
.then(() => {
if (window.showViewToggleComponent && window.showViewToggleComponent.init) {
window.showViewToggleComponent.init();
}
})
.catch(() => console.error('Failed to load show view toggle component script.'));
})();
// Lazy-load WakaTime charts when their canvas becomes visible
(function lazyLoadWaka() {
const chartSelectors = ['#desktop-wakatime30DayLangChart', '#mobile-wakatime30DayLangChart', '#desktop-wakatimeAllTimeLangChart', '#mobile-wakatimeAllTimeLangChart', '#desktop-editorsUsedChart', '#mobile-editorsUsedChart', '#desktop-editorChart', '#mobile-editorChart'];
const firstChart = document.querySelector(chartSelectors.join(','));
if (!firstChart) return; // no charts on this page
const loadWaka = () => {
if (window.wakatimeLoaded) return;
window.wakatimeLoaded = true;
loadComponentScript('/js/components/wakatime.js')
.then(() => {
if (window.wakatimeComponent && window.wakatimeComponent.init) {
window.wakatimeComponent.init();
}
})
.catch(() => console.error('Failed to load wakatime component script.'));
};
if ('IntersectionObserver' in window) {
const obs = new IntersectionObserver((entries, observer) => {
entries.forEach(e => {
if (e.isIntersecting) {
loadWaka();
observer.disconnect();
}
});
}, { rootMargin: '200px' });
obs.observe(firstChart);
// Fallback: if user doesn't scroll into view within 10s, load automatically
setTimeout(() => { if (!window.wakatimeLoaded) loadWaka(); }, 10000);
} else {
// Older browsers: load after small delay and on first scroll
setTimeout(loadWaka, 3000);
window.addEventListener('scroll', loadWaka, { once: true });
}
})();
// Load modal component
(function loadModal() {
if (window.modalComponent) {
window.modalComponent.init();
return;
}
loadComponentScript('/js/components/modal.js')
.then(() => {
if (window.modalComponent && window.modalComponent.init) {
window.modalComponent.init();
}
})
.catch(() => console.error('Failed to load modal component script.'));
})();
/**
* Lazy-load images across the site by setting `loading="lazy"` on images
* Exclude images that must be loaded eagerly: icons, profile picture, wakatime badge, or images marked `.no-lazy`.
*/
(function lazyLoadImages() {
const exclude = 'img.no-lazy, img#pfp-img, a#wakatime-site-hours img, img.icon, img.small';
const images = document.querySelectorAll('img:not(' + exclude + ')');
images.forEach(img => {
if (!img.hasAttribute('loading')) {
try { img.setAttribute('loading', 'lazy'); } catch (e) { /* defensive */ }
}
});
// Fallback for browsers that don't support native loading attribute and use data-src pattern
if (!('loading' in HTMLImageElement.prototype)) {
const lazyImgs = document.querySelectorAll('img[data-src]');
if ('IntersectionObserver' in window) {
const io = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const el = entry.target;
el.src = el.getAttribute('data-src');
el.removeAttribute('data-src');
observer.unobserve(el);
}
});
}, { rootMargin: '200px' });
lazyImgs.forEach(img => io.observe(img));
} else {
// No support: load them all after a timeout
setTimeout(() => lazyImgs.forEach(img => {
img.src = img.getAttribute('data-src');
img.removeAttribute('data-src');
}), 2000);
}
}
})();
// Lazy-load small project thumbnail videos (WebM/MP4). Videos should have data-webm/data-mp4 attributes.
(function lazyLoadThumbVideos() {
const vids = document.querySelectorAll('video.project-thumb[data-webm], video.project-thumb[data-mp4]');
if (!vids.length) return;
function loadVideo(v) {
if (v.dataset.loaded) return;
const webmRaw = v.getAttribute('data-webm');
const mp4Raw = v.getAttribute('data-mp4');
function normalizePath(p) {
if (!p) return null;
if (p.startsWith('http') || p.startsWith('//') || p.startsWith('/')) return p;
return '/' + p.replace(/^\.?\/+/, '');
}
const webm = normalizePath(webmRaw);
const mp4 = normalizePath(mp4Raw);
const existingSources = Array.from(v.querySelectorAll('source')).map(s => s.src);
if (webm && !existingSources.includes(new URL(webm, location.href).href)) {
const s = document.createElement('source'); s.src = webm; s.type = 'video/webm'; v.appendChild(s);
}
if (mp4 && !existingSources.includes(new URL(mp4, location.href).href)) {
const s2 = document.createElement('source'); s2.src = mp4; s2.type = 'video/mp4'; v.appendChild(s2);
}
try { v.load(); v.play().catch(() => { }); } catch (e) { }
v.dataset.loaded = '1';
}
if ('IntersectionObserver' in window) {
const obs = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
loadVideo(entry.target);
observer.unobserve(entry.target);
}
});
}, { rootMargin: '200px' });
vids.forEach(v => obs.observe(v));
// Also immediately load any videos already visible in the viewport
vids.forEach(v => {
const rect = v.getBoundingClientRect();
if (rect.top < (window.innerHeight + 200) && rect.bottom > -200) {
loadVideo(v);
try { obs.unobserve(v); } catch (e) { }
}
});
} else {
// Fallback: load after short delay and on first scroll
setTimeout(() => vids.forEach(loadVideo), 3000);
window.addEventListener('scroll', () => vids.forEach(loadVideo), { once: true });
}
})();
});