(() => {
const THEME_COOKIE_NAME = 'site_theme';
const THEME_OPTIONS = ['default', 'black'];
const getCookie = (name) => {
const match = document.cookie.match(new RegExp(`(?:^|; )${name}=([^;]*)`));
return match ? decodeURIComponent(match[1]) : '';
};
const setThemeCookie = (theme) => {
const expires = new Date(Date.now() + 365 * 24 * 60 * 60 * 1000).toUTCString();
document.cookie = `${THEME_COOKIE_NAME}=${encodeURIComponent(theme)}; expires=${expires}; path=/; SameSite=Lax`;
};
const applyTheme = (theme) => {
const normalizedTheme = THEME_OPTIONS.includes(theme) ? theme : 'default';
document.body.dataset.theme = normalizedTheme;
document.querySelectorAll('[data-theme-option]').forEach((option) => {
const isActive = option.getAttribute('data-theme-option') === normalizedTheme;
option.classList.toggle('active', isActive);
option.setAttribute('aria-pressed', isActive ? 'true' : 'false');
});
const themeToggle = document.getElementById('theme-toggle');
if (themeToggle) {
themeToggle.setAttribute('data-active-theme', normalizedTheme);
}
setThemeCookie(normalizedTheme);
};
const getHeroEyebrowLabel = () => {
const { hostname, pathname, port } = window.location;
const normalizedHost = hostname.replace(/^www\./, '');
const hostLabel = normalizedHost === 'hostname is not in hostLable list'
? 'danielmorrisey.com'
: ['localhost', '127.0.0.1', '[::1]'].includes(normalizedHost) && port
? `${normalizedHost}:${port}`
: normalizedHost;
const normalizedPath = pathname
.replace(/\/index\.html$/i, '/')
.replace(/\.html$/i, '')
.replace(/\/+$/, '');
const pathLabel = normalizedPath && normalizedPath !== '/'
? normalizedPath
: '/';
return `${hostLabel}${pathLabel === '/' ? '/' : pathLabel.startsWith('/') ? pathLabel : `/${pathLabel}`}`;
};
const getHeroSubtitle = () => {
const subtitles = [
'Welcome to my site',
'"Do not throw away your shot" ~ Lin-Manuel Miranda',
];
return subtitles[Math.floor(Math.random() * subtitles.length)];
};
const init = () => {
if (window.__siteCoreLoaded) return;
window.__siteCoreLoaded = true;
const showAnnouncement = window.location.pathname === '/' || window.location.pathname === '/index.html';
const announcementHtml = showAnnouncement ? `
MBD CDN: A simple, fast CDN powered by Cloudflare R2
` : '';
const heroEyebrow = document.querySelector('.hero-eyebrow');
if (heroEyebrow) {
heroEyebrow.textContent = getHeroEyebrowLabel();
} else {
const hero = document.querySelector('.hero');
if (hero) {
const eyebrow = document.createElement('p');
eyebrow.className = 'hero-eyebrow';
eyebrow.textContent = getHeroEyebrowLabel();
hero.insertBefore(eyebrow, hero.firstChild);
}
}
const addHeroSubtitle = () => {
const isTitlePage = window.location.pathname === '/' || window.location.pathname === '/index.html';
if (!isTitlePage) return;
const heroTitle = document.querySelector('.hero-title');
if (!heroTitle) return;
let subtitle = document.querySelector('.hero-subtitle');
if (!subtitle) {
subtitle = document.createElement('p');
subtitle.className = 'hero-subtitle';
heroTitle.insertAdjacentElement('afterend', subtitle);
}
subtitle.textContent = getHeroSubtitle();
};
addHeroSubtitle();
if (!document.querySelector('header.site-header')) {
const headerHtml = `
`;
document.body.insertAdjacentHTML('afterbegin', headerHtml);
}
if (!document.querySelector('footer.site-footer')) {
const footerHtml = `
`;
document.body.insertAdjacentHTML('beforeend', footerHtml);
}
if (!document.getElementById('privacy-notice')) {
const privacyNoticeHtml = `
`;
document.body.insertAdjacentHTML('beforeend', privacyNoticeHtml);
}
if (!document.getElementById('statcounter-script')) {
const statcounterMarkup = `
`;
document.body.insertAdjacentHTML('beforeend', statcounterMarkup);
}
if (!document.getElementById('theme-toggle-wrap')) {
const themeToggleHtml = `
`;
document.body.insertAdjacentHTML('beforeend', themeToggleHtml);
}
const savedTheme = getCookie(THEME_COOKIE_NAME) || 'default';
applyTheme(savedTheme);
const themeToggleWrap = document.getElementById('theme-toggle-wrap');
const themeToggle = document.getElementById('theme-toggle');
const themeMenu = document.getElementById('theme-menu');
const themeOptions = themeMenu ? themeMenu.querySelectorAll('[data-theme-option]') : [];
if (themeToggle && themeMenu && themeToggleWrap) {
themeToggle.addEventListener('click', (event) => {
event.stopPropagation();
const isOpen = themeToggleWrap.classList.toggle('open');
themeToggle.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
});
themeOptions.forEach((option) => {
option.addEventListener('click', () => {
applyTheme(option.getAttribute('data-theme-option'));
themeToggleWrap.classList.remove('open');
themeToggle.setAttribute('aria-expanded', 'false');
});
});
document.addEventListener('click', (event) => {
if (!themeToggleWrap.contains(event.target)) {
themeToggleWrap.classList.remove('open');
themeToggle.setAttribute('aria-expanded', 'false');
}
});
}
const isCompactNav = () => window.matchMedia('(max-width: 600px)').matches;
const navToggle = document.querySelector('.nav-toggle');
const navMenu = document.querySelector('.nav-menu');
if (navToggle && navMenu) {
const setNavOpen = (open) => {
navMenu.classList.toggle('open', open);
navToggle.setAttribute('aria-expanded', open ? 'true' : 'false');
const icon = navToggle.querySelector('i');
if (icon) {
icon.className = open ? 'fa-solid fa-xmark' : 'fa-solid fa-bars';
}
if (!open) {
navMenu.querySelectorAll('.nav-dropdown details[open]').forEach((d) => d.removeAttribute('open'));
}
};
navToggle.addEventListener('click', () => {
setNavOpen(!navMenu.classList.contains('open'));
});
navMenu.addEventListener('click', (event) => {
// close the panel once a real destination is picked
const link = event.target.closest('a');
if (link && !link.closest('summary')) setNavOpen(false);
});
window.addEventListener('resize', () => {
if (!isCompactNav()) setNavOpen(false);
});
}
document.querySelectorAll('.nav-dropdown').forEach((dropdown) => {
const details = dropdown.querySelector('details');
const summary = dropdown.querySelector('summary');
const menu = dropdown.querySelector('.dropdown-menu');
if (!details || !summary || !menu) return;
let hideTimer;
const openMenu = () => {
if (isCompactNav()) return;
clearTimeout(hideTimer);
details.setAttribute('open', '');
};
const closeMenu = () => {
if (isCompactNav()) return;
clearTimeout(hideTimer);
hideTimer = window.setTimeout(() => {
details.removeAttribute('open');
}, 120);
};
dropdown.addEventListener('mouseenter', openMenu);
dropdown.addEventListener('mouseleave', closeMenu);
summary.addEventListener('mouseenter', openMenu);
menu.addEventListener('mouseenter', openMenu);
menu.addEventListener('mouseleave', closeMenu);
dropdown.addEventListener('focusin', () => {
if (isCompactNav()) return;
details.setAttribute('open', '');
});
dropdown.addEventListener('focusout', (event) => {
if (isCompactNav()) return;
if (!dropdown.contains(event.relatedTarget)) {
details.removeAttribute('open');
}
});
summary.addEventListener('click', (event) => {
const clickedLink = event.target.closest('a');
// in the compact menu the summary is a section toggle, not a link —
// otherwise its sub-items would be unreachable
if (isCompactNav()) {
event.preventDefault();
details.toggleAttribute('open');
return;
}
if (clickedLink) {
return;
}
if (window.matchMedia('(hover: hover)').matches) {
event.preventDefault();
}
});
});
const yearEl = document.getElementById('current-year');
if (yearEl) {
yearEl.textContent = new Date().getFullYear();
}
const commitEl = document.getElementById('commit-hash');
if (commitEl) {
fetch('https://tangled-hash.danielmorrisey.com/commit')
.then((r) => r.json())
.then((data) => {
const short = data.commit_hash.slice(0, 7);
const date = new Date(data.deployed_at).toLocaleDateString();
const url = `https://tangled.org/danielmorrisey.com/website/commit/${data.commit_hash}`;
commitEl.innerHTML = `last updated: ${date} · commit ${short}`;
})
.catch(() => {
commitEl.textContent = 'unknown';
});
}
const privacyNotice = document.getElementById('privacy-notice');
const privacyClose = document.getElementById('privacy-close');
if (privacyNotice && privacyClose) {
const privacyDismissedKey = 'privacy_notice_dismissed';
if (localStorage.getItem(privacyDismissedKey)) {
privacyNotice.classList.add('hidden');
}
privacyClose.addEventListener('click', () => {
privacyNotice.classList.add('hidden');
localStorage.setItem(privacyDismissedKey, 'true');
});
}
};
if (document.body) {
init();
} else {
document.addEventListener('DOMContentLoaded', init, { once: true });
}
})();