- Wird "Von" gewaehlt, uebernimmt "Bis" denselben Tag (wie im Buchungsformular der App); ein frueheres Bis wird korrigiert - min-Attribut auf dem Bis-Feld sperrt Tage vor "Von" auch im Kalender - Asset-Query auf v=4, Service-Worker-Cache dic-v20
224 lines
7.7 KiB
JavaScript
224 lines
7.7 KiB
JavaScript
/* Drink it cold – Interaktionen
|
||
Kein Framework, kein Tracking. */
|
||
(function () {
|
||
'use strict';
|
||
|
||
var reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
||
|
||
/* ---------- Jahr im Footer ---------- */
|
||
var year = document.getElementById('year');
|
||
if (year) year.textContent = new Date().getFullYear();
|
||
|
||
/* ---------- Header: Zustand beim Scrollen ---------- */
|
||
var header = document.getElementById('header');
|
||
var lastY = -1;
|
||
function onScroll() {
|
||
var y = window.scrollY;
|
||
if (y === lastY) return;
|
||
lastY = y;
|
||
header.classList.toggle('is-scrolled', y > 24);
|
||
}
|
||
onScroll();
|
||
window.addEventListener('scroll', onScroll, { passive: true });
|
||
|
||
/* ---------- Mobile Navigation ---------- */
|
||
var toggle = document.getElementById('navToggle');
|
||
var nav = document.getElementById('nav');
|
||
|
||
function setNav(open) {
|
||
header.classList.toggle('is-open', open);
|
||
toggle.setAttribute('aria-expanded', String(open));
|
||
toggle.setAttribute('aria-label', open ? 'Menü schließen' : 'Menü öffnen');
|
||
document.body.style.overflow = open && window.innerWidth <= 1200 ? 'hidden' : '';
|
||
}
|
||
|
||
if (toggle && nav) {
|
||
toggle.addEventListener('click', function () {
|
||
setNav(!header.classList.contains('is-open'));
|
||
});
|
||
|
||
nav.addEventListener('click', function (e) {
|
||
if (e.target.closest('a')) setNav(false);
|
||
});
|
||
|
||
document.addEventListener('keydown', function (e) {
|
||
if (e.key === 'Escape' && header.classList.contains('is-open')) {
|
||
setNav(false);
|
||
toggle.focus();
|
||
}
|
||
});
|
||
|
||
document.addEventListener('click', function (e) {
|
||
if (!header.classList.contains('is-open')) return;
|
||
if (header.contains(e.target)) return;
|
||
setNav(false);
|
||
});
|
||
|
||
window.addEventListener('resize', function () {
|
||
if (window.innerWidth > 1200) setNav(false);
|
||
});
|
||
}
|
||
|
||
/* ---------- Einblenden beim Scrollen ---------- */
|
||
var revealItems = document.querySelectorAll('.reveal');
|
||
if (!reduceMotion && 'IntersectionObserver' in window) {
|
||
var io = new IntersectionObserver(function (entries) {
|
||
entries.forEach(function (entry) {
|
||
if (!entry.isIntersecting) return;
|
||
var el = entry.target;
|
||
var siblings = Array.prototype.slice.call(
|
||
el.parentElement ? el.parentElement.children : []
|
||
).filter(function (n) { return n.classList.contains('reveal'); });
|
||
var i = Math.max(0, siblings.indexOf(el));
|
||
el.style.transitionDelay = Math.min(i * 70, 350) + 'ms';
|
||
el.classList.add('is-visible');
|
||
io.unobserve(el);
|
||
});
|
||
}, { rootMargin: '0px 0px -8% 0px', threshold: 0.08 });
|
||
|
||
revealItems.forEach(function (el) { io.observe(el); });
|
||
} else {
|
||
revealItems.forEach(function (el) { el.classList.add('is-visible'); });
|
||
}
|
||
|
||
/* ---------- Galerie-Lightbox ---------- */
|
||
var lightbox = document.getElementById('lightbox');
|
||
var lightboxImg = document.getElementById('lightboxImg');
|
||
var lightboxClose = document.getElementById('lightboxClose');
|
||
var lastFocus = null;
|
||
|
||
function openLightbox(img) {
|
||
lastFocus = document.activeElement;
|
||
lightboxImg.src = img.currentSrc || img.src;
|
||
lightboxImg.alt = img.alt || '';
|
||
lightbox.hidden = false;
|
||
document.body.style.overflow = 'hidden';
|
||
lightboxClose.focus();
|
||
}
|
||
|
||
function closeLightbox() {
|
||
lightbox.hidden = true;
|
||
lightboxImg.removeAttribute('src');
|
||
document.body.style.overflow = '';
|
||
if (lastFocus) lastFocus.focus();
|
||
}
|
||
|
||
document.querySelectorAll('#gallery figure').forEach(function (fig) {
|
||
var img = fig.querySelector('img');
|
||
if (!img) return;
|
||
fig.addEventListener('click', function () { openLightbox(img); });
|
||
fig.setAttribute('tabindex', '0');
|
||
fig.setAttribute('role', 'button');
|
||
fig.setAttribute('aria-label', 'Bild vergrößern: ' + (img.alt || ''));
|
||
fig.addEventListener('keydown', function (e) {
|
||
if (e.key === 'Enter' || e.key === ' ') {
|
||
e.preventDefault();
|
||
openLightbox(img);
|
||
}
|
||
});
|
||
});
|
||
|
||
if (lightbox) {
|
||
lightboxClose.addEventListener('click', closeLightbox);
|
||
lightbox.addEventListener('click', function (e) {
|
||
if (e.target === lightbox) closeLightbox();
|
||
});
|
||
document.addEventListener('keydown', function (e) {
|
||
if (e.key === 'Escape' && !lightbox.hidden) closeLightbox();
|
||
});
|
||
}
|
||
|
||
/* ---------- Anfrageformular ---------- */
|
||
var form = document.getElementById('requestForm');
|
||
var status = document.getElementById('formStatus');
|
||
var MAIL = 'drinkitcold@web.de';
|
||
|
||
function showStatus(message, isError) {
|
||
if (!status) return;
|
||
status.textContent = message;
|
||
status.classList.add('is-visible');
|
||
status.classList.toggle('is-error', !!isError);
|
||
}
|
||
|
||
if (form) {
|
||
var submitButton = form.querySelector('button[type="submit"]');
|
||
var phone = form.elements['phone'];
|
||
var dateFrom = form.elements['from'];
|
||
var dateTo = form.elements['to'];
|
||
|
||
// „Bis“ folgt „Von“: gleicher Tag als Vorschlag, frühere Tage sind gesperrt
|
||
// (so wie im Buchungsformular der App).
|
||
if (dateFrom && dateTo) {
|
||
var syncDates = function () {
|
||
if (!dateFrom.value) return;
|
||
dateTo.min = dateFrom.value;
|
||
if (!dateTo.value || dateTo.value < dateFrom.value) {
|
||
dateTo.value = dateFrom.value;
|
||
}
|
||
};
|
||
dateFrom.addEventListener('change', syncDates);
|
||
dateFrom.addEventListener('input', syncDates);
|
||
if (dateFrom.value) dateTo.min = dateFrom.value;
|
||
}
|
||
|
||
form.addEventListener('submit', function (e) {
|
||
e.preventDefault();
|
||
|
||
if (!form.checkValidity()) {
|
||
form.reportValidity();
|
||
return;
|
||
}
|
||
|
||
// Eigene Hinweise zuerst leeren: sonst blockiert eine alte Meldung
|
||
// (z. B. "Bitte eine erreichbare Telefonnummer angeben") auch nach der
|
||
// Korrektur noch jeden weiteren Versuch.
|
||
if (phone) phone.setCustomValidity('');
|
||
if (dateTo) dateTo.setCustomValidity('');
|
||
|
||
// Nur die Ziffern zählen – Schreibweisen wie 0172 6384350,
|
||
// +49 172 638 43 50 oder 0172/6384350 sind alle gültig.
|
||
if (phone && phone.value.replace(/[^0-9]/g, '').length < 6) {
|
||
phone.setCustomValidity('Bitte eine erreichbare Telefonnummer angeben.');
|
||
form.reportValidity();
|
||
return;
|
||
}
|
||
|
||
if (dateFrom && dateTo && dateFrom.value && dateTo.value && dateTo.value < dateFrom.value) {
|
||
dateTo.setCustomValidity('Das Ende der Miete muss nach dem Beginn liegen.');
|
||
form.reportValidity();
|
||
return;
|
||
}
|
||
|
||
if (submitButton) submitButton.disabled = true;
|
||
showStatus('Ihre Anfrage wird gesendet …', false);
|
||
|
||
fetch(form.action, {
|
||
method: 'POST',
|
||
body: new FormData(form),
|
||
headers: { 'X-Requested-With': 'fetch', 'Accept': 'application/json' },
|
||
credentials: 'omit'
|
||
})
|
||
.then(function (response) {
|
||
return response.json()
|
||
.catch(function () { return {}; })
|
||
.then(function (payload) {
|
||
return { ok: response.ok && payload.ok !== false, message: payload.message || '' };
|
||
});
|
||
})
|
||
.then(function (result) {
|
||
if (result.ok) {
|
||
form.reset();
|
||
showStatus(result.message || 'Vielen Dank! Ihre Anfrage ist bei uns eingegangen.', false);
|
||
} else {
|
||
showStatus(result.message || ('Die Anfrage konnte nicht gesendet werden. Bitte schreiben Sie uns an ' + MAIL + '.'), true);
|
||
}
|
||
})
|
||
.catch(function () {
|
||
showStatus('Die Anfrage konnte nicht gesendet werden – bitte prüfen Sie Ihre Verbindung oder schreiben Sie uns an ' + MAIL + '.', true);
|
||
})
|
||
.then(function () {
|
||
if (submitButton) submitButton.disabled = false;
|
||
});
|
||
});
|
||
}
|
||
})();
|