-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
162 lines (137 loc) · 6.32 KB
/
script.js
File metadata and controls
162 lines (137 loc) · 6.32 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
// Component loader and dynamic imports
(() => {
function resolvePaths() {
// Robustly resolve using absolute URLs
const currentScript = document.currentScript || (function () {
const scripts = document.getElementsByTagName('script');
return scripts[scripts.length - 1];
})();
const scriptUrl = new URL(currentScript.src || currentScript.getAttribute('src'), document.baseURI);
const scriptDirUrl = new URL('.', scriptUrl);
const pageDirUrl = new URL('.', document.baseURI);
function relativeFromTo(fromUrl, toUrl) {
const fromParts = fromUrl.pathname.split('/').filter(Boolean);
const toParts = toUrl.pathname.split('/').filter(Boolean);
let i = 0;
while (i < fromParts.length && i < toParts.length && fromParts[i] === toParts[i]) i++;
const upCount = fromParts.length - i;
const downParts = toParts.slice(i);
const up = upCount > 0 ? '../'.repeat(upCount) : '';
const down = downParts.length ? downParts.join('/') + '/' : '';
return up + down;
}
const baseRel = relativeFromTo(pageDirUrl, scriptDirUrl);
return { baseRel, scriptDirUrl };
}
async function fetchComponent(url) {
const res = await fetch(url.toString());
if (!res.ok) throw new Error(`Failed to fetch ${url}: ${res.status}`);
return res.text();
}
function injectBase(html, base) {
// Replace %BASE% placeholders for links and assets in components
return html.replace(/%BASE%/g, base);
}
function mount(targetSelector, html) {
const el = document.querySelector(targetSelector);
if (el) el.innerHTML = html;
}
async function mountHeaderFooter() {
const { baseRel, scriptDirUrl } = resolvePaths();
const [headerHtmlRaw, footerHtmlRaw] = await Promise.all([
fetchComponent(new URL('components/header.html', scriptDirUrl)),
fetchComponent(new URL('components/footer.html', scriptDirUrl)),
]);
function unwrap(html, expectedTag) {
const doc = new DOMParser().parseFromString(html, 'text/html');
const first = doc.body.firstElementChild;
if (first && first.tagName && first.tagName.toLowerCase() === expectedTag) {
return first.innerHTML;
}
return html;
}
const headerTarget = document.querySelector('header.site-header, #header-mount');
if (headerTarget) {
const content = unwrap(headerHtmlRaw, 'header');
headerTarget.innerHTML = injectBase(content, baseRel);
}
const footerTarget = document.querySelector('footer.site-footer, #footer-mount');
if (footerTarget) {
const content = unwrap(footerHtmlRaw, 'footer');
footerTarget.innerHTML = injectBase(content, baseRel);
}
}
async function mountProjectsPreview() {
const { scriptDirUrl } = resolvePaths();
const mountEl = document.getElementById('projects-mount');
if (!mountEl) return;
const html = await fetchComponent(new URL('components/projects.html', scriptDirUrl));
const doc = new DOMParser().parseFromString(html, 'text/html');
const header = doc.querySelector('header.site-header');
if (header) header.remove();
const footer = doc.querySelector('footer.site-footer');
if (footer) footer.remove();
const main = doc.querySelector('main');
mountEl.innerHTML = main ? main.innerHTML : doc.body.innerHTML;
}
async function mountTeamPreview() {
const { scriptDirUrl } = resolvePaths();
const mountEl = document.getElementById('team-mount');
if (!mountEl) return;
const html = await fetchComponent(new URL('components/team.html', scriptDirUrl));
const doc = new DOMParser().parseFromString(html, 'text/html');
const header = doc.querySelector('header.site-header');
if (header) header.remove();
const footer = doc.querySelector('footer.site-footer');
if (footer) footer.remove();
const teamSection = doc.querySelector('section.team');
const main = doc.querySelector('main');
mountEl.innerHTML = teamSection ? teamSection.outerHTML : (main ? main.innerHTML : doc.body.innerHTML);
}
function initProjectVideoSwitcher() {
const iframe = document.getElementById('demo-iframe');
const buttons = document.querySelectorAll('.project-buttons button[data-project]');
if (!iframe || !buttons.length || typeof project_to_url === 'undefined') return;
function setActive(project) {
buttons.forEach(b => b.classList.toggle('active', b.dataset.project === project));
}
buttons.forEach(btn => {
btn.addEventListener('click', () => {
const project = btn.dataset.project;
const url = project_to_url[project];
if (url) {
iframe.src = url;
setActive(project);
}
});
});
let initialProject = Array.from(buttons).find(
b => project_to_url[b.dataset.project] === iframe.src
)?.dataset.project;
if (!initialProject && buttons[0]) {
initialProject = buttons[0].dataset.project;
const initialUrl = project_to_url[initialProject];
if (initialUrl) iframe.src = initialUrl;
}
if (initialProject) setActive(initialProject);
}
function onReady(fn) {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', fn, { once: true });
} else {
fn();
}
}
document.addEventListener('DOMContentLoaded', () => {
// Year stamp
const yearEl = document.getElementById('year');
if (yearEl) yearEl.textContent = new Date().getFullYear();
// Mount shared components
mountHeaderFooter().catch(err => console.error(err));
// Optional dynamic sections
mountProjectsPreview().catch(err => console.error(err));
mountTeamPreview().catch(err => console.error(err));
// Homepage video switcher (initialize once DOM is ready)
onReady(() => initProjectVideoSwitcher());
});
})();