BlowfishCMS
A small content management system for OpenBSD
version 0.45.6 · ISC · 2026-12-03 · releases
221 lines · 12.5 KB · download this file
// BlowfishCMS: the script for public pages (navigation, slideshows and other
// blocks, the picture viewer and players). One file so a page needs one request.
// ==== nav ====
// The menu button on small screens: opens and closes the menu, and closes it
// on Escape or a click elsewhere. "nav-ready" tells site.css the script runs
// (without it the menu is simply shown).
(() => {
const nav = document.querySelector('.site-nav');
const toggle = nav?.querySelector('.site-nav__toggle');
if (!toggle) return;
const close = () => { nav.classList.remove('is-open'); toggle.setAttribute('aria-expanded','false'); };
toggle.addEventListener('click', () => {
const open = nav.classList.toggle('is-open');
toggle.setAttribute('aria-expanded', String(open));
});
document.addEventListener('keydown', event => {
if (event.key === 'Escape' && nav.classList.contains('is-open')) { close(); toggle.focus(); }
});
document.addEventListener('click', event => { if (!nav.contains(event.target)) close(); });
document.documentElement.classList.add('nav-ready');
})();
// Menu groups: one open at a time; Escape or a click elsewhere closes it.
// They're <details>, so they open without the script too.
(() => {
const all = [...document.querySelectorAll('.site-nav .nav-group')];
if (!all.length) return;
all.forEach(g => g.addEventListener('toggle', () => { if (g.open) all.forEach(o => { if (o !== g) o.open = false; }); }));
document.addEventListener('click', event => all.forEach(g => { if (g.open && !g.contains(event.target)) g.open = false; }));
document.addEventListener('keydown', event => {
if (event.key !== 'Escape') return;
const g = all.find(o => o.open); if (!g) return;
g.open = false; g.querySelector('summary').focus();
});
})();
// ==== site ====
// The contact form's message after sending (?sent=1 or 0), and the Resume
// add-on's print button (hidden until there's a script to press it).
(() => {
const status = document.getElementById('contact-status');
if (status) {
const sent = new URLSearchParams(window.location.search).get('sent');
if (sent === '1') status.textContent = status.dataset.sent || 'Your message was sent. Thank you for getting in touch.';
if (sent === '0') status.textContent = 'Your message could not be sent. Please try again later.';
}
const print = document.querySelector('.print-resume');
if (print) { print.hidden = false; print.addEventListener('click', () => window.print()); }
})();
// Slideshow blocks: one picture at a time. Without JavaScript the pictures sit
// in a strip that scrolls sideways (and snaps); this adds arrows, a counter,
// the arrow keys, and moving on by itself when the block asks to (never for
// visitors who prefer reduced motion, and not while pointed at or focused).
document.querySelectorAll('[data-slideshow]').forEach(show => {
const track = show.querySelector('.slideshow__track');
const slides = [...track.children];
if (slides.length < 2) return;
const bar = document.createElement('div'); bar.className = 'slideshow__bar';
const prev = document.createElement('button'); prev.type = 'button'; prev.className = 'slideshow__arrow'; prev.textContent = '‹'; prev.setAttribute('aria-label', 'Previous picture');
const next = document.createElement('button'); next.type = 'button'; next.className = 'slideshow__arrow'; next.textContent = '›'; next.setAttribute('aria-label', 'Next picture');
const count = document.createElement('span'); count.className = 'slideshow__count'; count.setAttribute('aria-live', 'polite');
// Controls: arrows and a counter (the default), arrows and dots, or none.
const controls = show.dataset.controls || 'arrows';
const dots = slides.map((s, i) => { const d = document.createElement('button'); d.type = 'button'; d.className = 'slideshow__dot'; d.setAttribute('aria-label', `Picture ${i + 1}`); d.addEventListener('click', () => go(i)); return d; });
if (controls === 'dots') { const row = document.createElement('span'); row.className = 'slideshow__dots'; row.append(...dots); bar.append(prev, row, next); }
else bar.append(prev, count, next);
if (controls !== 'none') show.append(bar);
let index = 0;
const mark = () => { count.textContent = `${index + 1} / ${slides.length}`; dots.forEach((d, i) => d.setAttribute('aria-current', i === index ? 'true' : 'false')); };
const go = (i, smooth = true) => {
index = (i + slides.length) % slides.length;
track.scrollTo({left: slides[index].offsetLeft - track.offsetLeft, behavior: smooth ? 'smooth' : 'auto'});
mark();
};
prev.addEventListener('click', () => go(index - 1));
next.addEventListener('click', () => go(index + 1));
show.tabIndex = 0;
show.addEventListener('keydown', event => {
if (event.key === 'ArrowLeft') { event.preventDefault(); go(index - 1); }
if (event.key === 'ArrowRight') { event.preventDefault(); go(index + 1); }
});
// Swiping or scrolling the strip moves the counter along.
let settle;
track.addEventListener('scroll', () => {
clearTimeout(settle);
settle = setTimeout(() => {
const left = track.scrollLeft;
let best = 0;
slides.forEach((s, i) => { if (Math.abs(s.offsetLeft - track.offsetLeft - left) < Math.abs(slides[best].offsetLeft - track.offsetLeft - left)) best = i; });
index = best; mark();
}, 120);
});
go(0, false);
const seconds = Number(show.dataset.interval || 0);
if (seconds > 0 && !matchMedia('(prefers-reduced-motion: reduce)').matches) {
let paused = false;
['mouseenter', 'focusin'].forEach(e => show.addEventListener(e, () => { paused = true; }));
['mouseleave', 'focusout'].forEach(e => show.addEventListener(e, () => { paused = false; }));
setInterval(() => { if (!paused && !document.hidden) go(index + 1); }, seconds * 1000);
}
});
// ==== media ====
(() => {
// A list (gallery, music, videos, files) can have a collection picker and
// tag buttons; an item shows when it matches both.
// Each panel with a collection menu or tag buttons is filtered on its own.
const lists = new Set([...document.querySelectorAll('.collection-filter, .tag-filter')].map(f => f.closest('.window__body') || document.body));
lists.forEach(block => {
const picker = block.querySelector('.collection-filter select');
const buttons = block.querySelector('.tag-filter');
let tag = '';
const apply = () => block.querySelectorAll('[data-collection], [data-tags]').forEach(item => {
const col = picker ? picker.value : '';
item.hidden = Boolean((col && item.dataset.collection !== col) || (tag && !(item.dataset.tags || '').split(' ').includes(tag)));
});
if (picker) { picker.closest('label').hidden = false; picker.addEventListener('change', apply); }
if (buttons) {
buttons.hidden = false;
buttons.addEventListener('click', e => {
const b = e.target.closest('button[data-tag]'); if (!b) return;
tag = b.dataset.tag;
buttons.querySelectorAll('button').forEach(x => x.setAttribute('aria-pressed', String(x === b)));
apply();
});
}
});
// The picture viewer: clicking a gallery picture fills the <dialog> with it
// and opens it; the close button or a click outside the picture closes it.
const viewer = document.getElementById('photo-viewer');
if (viewer && typeof viewer.showModal === 'function') {
const image = document.getElementById('viewer-image');
document.querySelectorAll('.photo-open').forEach(button => button.addEventListener('click', () => {
image.src = button.dataset.src; image.alt = button.dataset.title;
document.getElementById('viewer-title').textContent = button.dataset.title;
const page = document.getElementById('viewer-page');
if (page) { page.hidden = !button.dataset.page; if (button.dataset.page) page.href = button.dataset.page; }
viewer.showModal();
}));
document.getElementById('viewer-close').addEventListener('click', () => viewer.close());
viewer.addEventListener('click', event => { if (event.target === viewer) viewer.close(); });
}
// One player at a time: starting a track or video pauses the others.
document.querySelectorAll('audio, video').forEach(player => player.addEventListener('play', () => document.querySelectorAll('audio, video').forEach(other => { if (player !== other) other.pause(); })));
// Discourage casual saving: no context menu, dragging or long-press on photos.
// (Anything shown in a browser can still be captured; originals are never served.)
const protectedImage = target => target instanceof Element && target.closest('.photo-card img, #viewer-image, .album-art, .blog-body img, .video-card video');
document.addEventListener('contextmenu', event => { if (protectedImage(event.target)) event.preventDefault(); });
document.addEventListener('dragstart', event => { if (protectedImage(event.target)) event.preventDefault(); });
})();
// A video block set to play by itself (muted, looping) stays still for
// visitors who prefer reduced motion; its controls still play it.
if (matchMedia('(prefers-reduced-motion: reduce)').matches) {
document.querySelectorAll('video.video-ambient').forEach(v => { v.autoplay = false; v.pause(); });
}
// ==== directories ====
// A list of cards with search, filters and sorting (GitHub projects, Repositories).
// The markup says what to do: [data-directory] holds a form [data-directory-tools]
// (hidden until this runs) with an input [data-directory-search], selects
// [data-directory-filter=KEY] (a card matches when data-KEY equals the value, or,
// with data-match="words", contains it as a word; data-fill adds each card's
// values as options) and a select [data-directory-sort] (option values: a key,
// "-" first for descending; numbers sort as numbers); the cards [data-card] in
// [data-directory-grid]; [data-directory-count] (data-noun), [data-directory-empty]
// and buttons [data-directory-reset].
(() => {
const collator = new Intl.Collator(undefined, { sensitivity: 'base', numeric: true });
document.querySelectorAll('[data-directory]').forEach(dir => {
const tools = dir.querySelector('[data-directory-tools]');
const grid = dir.querySelector('[data-directory-grid]');
if (!tools || !grid) return;
const cards = [...grid.querySelectorAll('[data-card]')];
const search = tools.querySelector('[data-directory-search]');
const filters = [...tools.querySelectorAll('[data-directory-filter]')];
const sort = tools.querySelector('[data-directory-sort]');
const count = dir.querySelector('[data-directory-count]');
const empty = dir.querySelector('[data-directory-empty]');
const resets = [...dir.querySelectorAll('[data-directory-reset]')];
filters.filter(f => f.hasAttribute('data-fill')).forEach(f => {
const key = f.dataset.directoryFilter;
for (const v of [...new Set(cards.map(c => c.dataset[key]).filter(Boolean))].sort(collator.compare)) f.add(new Option(v, v));
});
const matches = (card, f) => {
if (!f.value) return true;
const v = card.dataset[f.dataset.directoryFilter] || '';
return f.dataset.match === 'words' ? v.split(' ').includes(f.value) : v === f.value;
};
function update() {
const query = search ? search.value.trim().toLocaleLowerCase() : '';
const filtered = Boolean(query || filters.some(f => f.value));
if (sort && sort.value) {
const desc = sort.value.startsWith('-'), key = sort.value.replace(/^-/, '');
cards.sort((a, b) => {
const x = a.dataset[key] || '', y = b.dataset[key] || '';
const n = x !== '' && y !== '' && !isNaN(x) && !isNaN(y) ? Number(x) - Number(y) : collator.compare(x, y);
return (desc ? -n : n) || collator.compare(a.dataset.name || '', b.dataset.name || '');
});
}
let shown = 0;
cards.forEach(card => {
const ok = (!query || card.textContent.toLocaleLowerCase().includes(query)) && filters.every(f => matches(card, f));
card.hidden = !ok; if (ok) shown++;
grid.append(card);
});
if (count) count.textContent = `${shown} of ${cards.length} ${count.dataset.noun || ''}`.trim();
if (empty) empty.hidden = shown !== 0 || cards.length === 0;
resets.forEach(r => { if (!r.closest('[data-directory-empty]')) r.hidden = !filtered; });
}
const clear = () => { tools.reset(); update(); if (search) search.focus(); };
tools.addEventListener('submit', e => e.preventDefault());
if (search) search.addEventListener('input', update);
[...filters, sort].filter(Boolean).forEach(c => c.addEventListener('change', update));
resets.forEach(r => r.addEventListener('click', clear));
tools.hidden = false;
update();
});
})();