Files
fewo-stommer/resources/themes/ostsee-licht/theme.js
T
LarsandClaude Opus 5.5 cc47cac93e Hauptadresse statt Zweitadresse, Abspielgeschwindigkeit je Video
- Weitere Adressen einer Website (z. B. www.stommer.info) leiten per 301 auf die
  Hauptadresse um; vorher blockierte der Browser Skripte, Schriften und Videos,
  die von der Hauptadresse kamen (kein Titelvideo, Ersatzschrift)
- Site::origin() nimmt verbindlich das Feld domain
- Videos: Abspielgeschwindigkeit 0,5x bis 1x, gilt im Titelbereich und Rundgang

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 11:30:42 +01:00

174 lines
7.4 KiB
JavaScript

/**
* Theme "Ostsee-Licht": small progressive enhancements. Everything works without JavaScript.
*/
// Lightbox for galleries: links with data-lightbox open the large image in a <dialog>.
document.addEventListener('click', (event) => {
const link = event.target.closest('a[data-lightbox]');
if (!link) {
return;
}
const dialog = document.getElementById('lightbox');
if (!dialog || typeof dialog.showModal !== 'function') {
return;
}
event.preventDefault();
const image = dialog.querySelector('img');
image.src = link.href;
image.alt = link.dataset.alt ?? '';
dialog.querySelector('[data-caption]').textContent = link.dataset.alt ?? '';
dialog.showModal();
});
document.addEventListener('click', (event) => {
const dialog = event.target.closest('dialog#lightbox');
if (dialog && (event.target === dialog || event.target.closest('[data-close]'))) {
dialog.close();
}
});
// Availability search in the hero: forwards the dates to the booking page.
document.addEventListener('submit', (event) => {
const form = event.target.closest('form[data-availability-search]');
if (!form) {
return;
}
const arrival = form.querySelector('[name=anreise]').value;
const departure = form.querySelector('[name=abreise]').value;
if (!arrival || !departure) {
event.preventDefault();
form.querySelector('[name=anreise]').focus();
}
});
// Rotating offers: one slide at a time, advancing every 7 s; pauses on hover/focus and
// does not advance by itself when the visitor prefers reduced motion.
document.querySelectorAll('[data-rotator]').forEach((rotator) => {
const slides = [...rotator.querySelectorAll('[data-rotator-slide]')];
const dots = [...rotator.querySelectorAll('[data-rotator-dot]')];
let current = 0;
let paused = false;
const show = (index) => {
current = (index + slides.length) % slides.length;
slides.forEach((slide, i) => { slide.hidden = i !== current; });
dots.forEach((dot, i) => dot.setAttribute('aria-selected', i === current ? 'true' : 'false'));
};
rotator.querySelector('[data-rotator-prev]')?.addEventListener('click', () => show(current - 1));
rotator.querySelector('[data-rotator-next]')?.addEventListener('click', () => show(current + 1));
dots.forEach((dot, i) => dot.addEventListener('click', () => show(i)));
['mouseenter', 'focusin'].forEach((type) => rotator.addEventListener(type, () => { paused = true; }));
['mouseleave', 'focusout'].forEach((type) => rotator.addEventListener(type, () => { paused = false; }));
if (!window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
setInterval(() => { if (!paused && !document.hidden) show(current + 1); }, 7000);
}
});
const reducedMotion = () => window.matchMedia('(prefers-reduced-motion: reduce)').matches;
// Playback speed set per video in the admin (data-playback-rate); also after the browser resets it on load.
document.querySelectorAll('video[data-playback-rate]').forEach((video) => {
const rate = Number(video.dataset.playbackRate) || 1;
const apply = () => { video.defaultPlaybackRate = rate; video.playbackRate = rate; };
apply();
video.addEventListener('loadedmetadata', apply);
});
// Silent video loops (partials/video-loop): loaded only with motion allowed and without Save-Data, playing while
// visible; the button pauses and resumes. Otherwise the poster stays and nothing is downloaded.
document.querySelectorAll('[data-video-loop]').forEach((box) => {
const video = box.querySelector('video');
const toggle = box.querySelector('[data-video-toggle]');
if (!video?.dataset.src || reducedMotion() || navigator.connection?.saveData) {
return;
}
let stopped = false;
const play = () => video.play().catch(() => {});
video.src = video.dataset.src;
video.preload = 'auto';
toggle.hidden = false;
toggle.addEventListener('click', () => {
stopped = !stopped;
stopped ? video.pause() : play();
const label = stopped ? 'Video abspielen' : 'Video anhalten';
toggle.setAttribute('aria-pressed', String(stopped));
toggle.setAttribute('aria-label', label);
toggle.title = label;
toggle.querySelector('[data-icon-pause]').classList.toggle('hidden', stopped);
toggle.querySelector('[data-icon-play]').classList.toggle('hidden', !stopped);
});
new IntersectionObserver(([entry]) => {
entry.isIntersecting && !stopped ? play() : video.pause();
}).observe(video);
});
// Video tour (sections/tour): start overlay, chapter buttons, highlighting the current chapter; floor plan rooms
// with [data-tour-link] jump into the tour on the same page. Without JavaScript the native controls remain.
const tour = document.querySelector('[data-tour]');
if (tour) {
const video = tour.querySelector('video');
const start = tour.querySelector('[data-tour-start]');
const seeks = [...tour.querySelectorAll('[data-tour-seek]')];
const playFrom = (seconds) => {
start.hidden = true;
const go = () => {
if (seconds !== null) {
video.currentTime = seconds;
}
video.play().catch(() => {});
};
video.readyState >= 1 ? go() : video.addEventListener('loadedmetadata', go, { once: true });
if (video.readyState < 1) {
video.load();
}
};
start.hidden = false;
start.addEventListener('click', () => playFrom(null));
video.addEventListener('play', () => { start.hidden = true; });
tour.querySelector('[data-tour-chapters]')?.removeAttribute('hidden');
seeks.forEach((button) => button.addEventListener('click', () => playFrom(Number(button.dataset.tourSeek))));
video.addEventListener('timeupdate', () => {
const current = seeks.filter((button) => Number(button.dataset.tourSeek) <= video.currentTime + 0.25).pop();
seeks.forEach((button) => (button === current ? button.setAttribute('aria-current', 'true') : button.removeAttribute('aria-current')));
});
document.querySelectorAll('[data-tour-link]').forEach((link) => {
link.hidden = false;
link.addEventListener('click', (event) => {
event.preventDefault();
tour.scrollIntoView({ behavior: reducedMotion() ? 'auto' : 'smooth', block: 'center' });
playFrom(Number(link.dataset.tourLink));
});
});
}
// One-time reveal of a graphic when it scrolls into view (e.g. the sunrise of theme Seebad): the theme's CSS
// styles [data-reveal="ready"] (start) and the transition to [data-reveal="shown"]. Without JavaScript or with
// reduced motion the graphic is simply shown.
if (!reducedMotion() && 'IntersectionObserver' in window) {
const reveal = new IntersectionObserver((entries) => {
entries.filter((entry) => entry.isIntersecting).forEach((entry) => {
entry.target.dataset.reveal = 'shown';
reveal.unobserve(entry.target);
});
}, { threshold: 0.6 });
document.querySelectorAll('[data-reveal]').forEach((element) => {
if (element.getBoundingClientRect().top < window.innerHeight * 0.85) {
element.dataset.reveal = 'ready';
requestAnimationFrame(() => requestAnimationFrame(() => { element.dataset.reveal = 'shown'; }));
return;
}
element.dataset.reveal = 'ready';
reveal.observe(element);
});
}