drink-it-cold/web/assets/js/main.js
torsten bf38980e68 Anfrageformular: Telefonnummer nicht mehr faelschlich ablehnen
- JS setzte seine eigenen Validierungshinweise erst nach checkValidity()
  zurueck; eine einmal gemeldete Telefonnummer blockierte damit jeden
  weiteren Versuch. Hinweise werden jetzt vor der Pruefung geleert
- Telefonpruefung zaehlt nur noch die Ziffern (>= 6) und akzeptiert damit
  Schreibweisen wie 0172 6384350, +49 (0)172 6384350 oder 0172/6384350
- Client und Server (app/inquiry.php) pruefen identisch
- Asset-Query auf v=3 und Service-Worker-Cache auf dic-v19
2026-11-29 16:36:58 +01:00

209 lines
7.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* 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'];
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;
});
});
}
})();