428 lines
14 KiB
JavaScript
428 lines
14 KiB
JavaScript
/* ==========================================================================
|
|
Andrea Slop Fotografie — interactie
|
|
--------------------------------------------------------------------------
|
|
Geen build-stap, geen dependencies. Elke module is een losse init-functie
|
|
zodat je ze straks een voor een kunt vervangen door een React-component
|
|
of -hook.
|
|
========================================================================== */
|
|
|
|
(function () {
|
|
'use strict';
|
|
|
|
var prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Sticky header */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
function initHeader() {
|
|
var header = document.querySelector('[data-header]');
|
|
if (!header) return;
|
|
|
|
var threshold = 60;
|
|
|
|
function update() {
|
|
header.classList.toggle('header--sticky', window.scrollY > threshold);
|
|
}
|
|
|
|
update();
|
|
window.addEventListener('scroll', update, { passive: true });
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Mobiel menu */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
function initMobileNav() {
|
|
var toggle = document.querySelector('[data-nav-toggle]');
|
|
var nav = document.querySelector('[data-mobile-nav]');
|
|
if (!toggle || !nav) return;
|
|
|
|
function setOpen(open) {
|
|
nav.classList.toggle('is-open', open);
|
|
toggle.setAttribute('aria-expanded', String(open));
|
|
toggle.setAttribute('aria-label', open ? 'Menu sluiten' : 'Menu openen');
|
|
document.body.classList.toggle('is-locked', open);
|
|
}
|
|
|
|
toggle.addEventListener('click', function () {
|
|
setOpen(!nav.classList.contains('is-open'));
|
|
});
|
|
|
|
nav.addEventListener('click', function (event) {
|
|
if (event.target.closest('a')) setOpen(false);
|
|
});
|
|
|
|
document.addEventListener('keydown', function (event) {
|
|
if (event.key === 'Escape' && nav.classList.contains('is-open')) setOpen(false);
|
|
});
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Side area ("Info Menu") */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
function initSideArea() {
|
|
var area = document.querySelector('[data-side-area]');
|
|
if (!area) return;
|
|
|
|
var openers = document.querySelectorAll('[data-side-area-open]');
|
|
var closers = area.querySelectorAll('[data-side-area-close]');
|
|
var lastFocused = null;
|
|
|
|
function setOpen(open) {
|
|
area.classList.toggle('is-open', open);
|
|
area.setAttribute('aria-hidden', String(!open));
|
|
document.body.classList.toggle('is-locked', open);
|
|
|
|
if (open) {
|
|
lastFocused = document.activeElement;
|
|
var firstClose = area.querySelector('[data-side-area-close]');
|
|
if (firstClose) firstClose.focus();
|
|
} else if (lastFocused) {
|
|
lastFocused.focus();
|
|
}
|
|
}
|
|
|
|
openers.forEach(function (btn) {
|
|
btn.addEventListener('click', function () { setOpen(true); });
|
|
});
|
|
|
|
closers.forEach(function (btn) {
|
|
btn.addEventListener('click', function () { setOpen(false); });
|
|
});
|
|
|
|
document.addEventListener('keydown', function (event) {
|
|
if (event.key === 'Escape' && area.classList.contains('is-open')) setOpen(false);
|
|
});
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Hero slider */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
function initHeroSlider() {
|
|
var hero = document.querySelector('[data-hero]');
|
|
if (!hero) return;
|
|
|
|
var slides = Array.prototype.slice.call(hero.querySelectorAll('[data-hero-slide]'));
|
|
var thumbs = Array.prototype.slice.call(hero.querySelectorAll('[data-hero-thumb]'));
|
|
var progress = hero.querySelector('[data-hero-progress]');
|
|
if (slides.length < 2) return;
|
|
|
|
var DURATION = 6000; // ms per slide
|
|
var TICK = 50;
|
|
var index = 0;
|
|
var elapsed = 0;
|
|
var timer = null;
|
|
|
|
function show(next) {
|
|
index = (next + slides.length) % slides.length;
|
|
elapsed = 0;
|
|
|
|
slides.forEach(function (slide, i) {
|
|
slide.classList.toggle('is-active', i === index);
|
|
slide.setAttribute('aria-hidden', String(i !== index));
|
|
});
|
|
|
|
thumbs.forEach(function (thumb, i) {
|
|
thumb.classList.toggle('is-active', i === index);
|
|
thumb.setAttribute('aria-current', i === index ? 'true' : 'false');
|
|
});
|
|
}
|
|
|
|
function tick() {
|
|
elapsed += TICK;
|
|
if (progress) progress.style.width = (elapsed / DURATION) * 100 + '%';
|
|
if (elapsed >= DURATION) show(index + 1);
|
|
}
|
|
|
|
function start() {
|
|
if (prefersReducedMotion || timer) return;
|
|
timer = window.setInterval(tick, TICK);
|
|
}
|
|
|
|
function stop() {
|
|
window.clearInterval(timer);
|
|
timer = null;
|
|
}
|
|
|
|
thumbs.forEach(function (thumb, i) {
|
|
thumb.addEventListener('click', function () {
|
|
show(i);
|
|
if (progress) progress.style.width = '0%';
|
|
});
|
|
});
|
|
|
|
hero.addEventListener('mouseenter', stop);
|
|
hero.addEventListener('mouseleave', start);
|
|
|
|
document.addEventListener('visibilitychange', function () {
|
|
if (document.hidden) stop(); else start();
|
|
});
|
|
|
|
show(0);
|
|
start();
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Lightbox (portfolio) */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
function initLightbox() {
|
|
var triggers = Array.prototype.slice.call(document.querySelectorAll('[data-lightbox]'));
|
|
var box = document.querySelector('[data-lightbox-box]');
|
|
if (!triggers.length || !box) return;
|
|
|
|
var img = box.querySelector('[data-lightbox-img]');
|
|
var caption = box.querySelector('[data-lightbox-caption]');
|
|
var counter = box.querySelector('[data-lightbox-counter]');
|
|
var current = 0;
|
|
var lastFocused = null;
|
|
|
|
var items = triggers.map(function (trigger) {
|
|
return {
|
|
src: trigger.getAttribute('data-lightbox-src') || trigger.querySelector('img').src,
|
|
alt: trigger.querySelector('img') ? trigger.querySelector('img').alt : ''
|
|
};
|
|
});
|
|
|
|
function render() {
|
|
var item = items[current];
|
|
img.src = item.src;
|
|
img.alt = item.alt;
|
|
caption.textContent = item.alt;
|
|
counter.textContent = (current + 1) + ' / ' + items.length;
|
|
}
|
|
|
|
function open(i) {
|
|
current = i;
|
|
lastFocused = document.activeElement;
|
|
render();
|
|
box.classList.add('is-open');
|
|
box.setAttribute('aria-hidden', 'false');
|
|
document.body.classList.add('is-locked');
|
|
var closeBtn = box.querySelector('[data-lightbox-close]');
|
|
if (closeBtn) closeBtn.focus();
|
|
}
|
|
|
|
function close() {
|
|
box.classList.remove('is-open');
|
|
box.setAttribute('aria-hidden', 'true');
|
|
document.body.classList.remove('is-locked');
|
|
if (lastFocused) lastFocused.focus();
|
|
}
|
|
|
|
function step(delta) {
|
|
current = (current + delta + items.length) % items.length;
|
|
render();
|
|
}
|
|
|
|
triggers.forEach(function (trigger, i) {
|
|
trigger.addEventListener('click', function (event) {
|
|
event.preventDefault();
|
|
open(i);
|
|
});
|
|
});
|
|
|
|
box.addEventListener('click', function (event) {
|
|
if (event.target.closest('[data-lightbox-close]') || event.target === box) close();
|
|
if (event.target.closest('[data-lightbox-prev]')) step(-1);
|
|
if (event.target.closest('[data-lightbox-next]')) step(1);
|
|
});
|
|
|
|
document.addEventListener('keydown', function (event) {
|
|
if (!box.classList.contains('is-open')) return;
|
|
if (event.key === 'Escape') close();
|
|
if (event.key === 'ArrowLeft') step(-1);
|
|
if (event.key === 'ArrowRight') step(1);
|
|
});
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Contactformulier */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
/**
|
|
* Verstuurt het formulier.
|
|
*
|
|
* Er is nu nog geen back-end, dus dit logt de payload en doet alsof het
|
|
* gelukt is. Vervang de body straks door een fetch naar de .NET API:
|
|
*
|
|
* return fetch('/api/contact', {
|
|
* method: 'POST',
|
|
* headers: { 'Content-Type': 'application/json' },
|
|
* body: JSON.stringify(payload)
|
|
* }).then(function (res) {
|
|
* if (!res.ok) throw new Error('Versturen mislukt');
|
|
* return res.json();
|
|
* });
|
|
*/
|
|
function submitContactForm(payload) {
|
|
console.info('[contact] payload', payload);
|
|
return new Promise(function (resolve) {
|
|
window.setTimeout(resolve, 600);
|
|
});
|
|
}
|
|
|
|
function initContactForm() {
|
|
var forms = Array.prototype.slice.call(document.querySelectorAll('[data-contact-form]'));
|
|
if (!forms.length) return;
|
|
|
|
var emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
|
|
|
|
forms.forEach(function (form) {
|
|
var status = form.querySelector('[data-form-status]');
|
|
var submit = form.querySelector('[type="submit"]');
|
|
|
|
function setError(field, message) {
|
|
var wrapper = field.closest('.form__field');
|
|
var slot = wrapper ? wrapper.querySelector('[data-form-error]') : null;
|
|
if (slot) slot.textContent = message || '';
|
|
field.setAttribute('aria-invalid', message ? 'true' : 'false');
|
|
return !message;
|
|
}
|
|
|
|
function validate() {
|
|
var valid = true;
|
|
|
|
var naam = form.elements.naam;
|
|
valid = setError(naam, naam.value.trim() ? '' : 'Vul je naam in.') && valid;
|
|
|
|
var email = form.elements.email;
|
|
var emailValue = email.value.trim();
|
|
var emailMessage = '';
|
|
if (!emailValue) emailMessage = 'Vul je e-mailadres in.';
|
|
else if (!emailPattern.test(emailValue)) emailMessage = 'Dit e-mailadres klopt nog niet.';
|
|
valid = setError(email, emailMessage) && valid;
|
|
|
|
var onderwerp = form.elements.onderwerp;
|
|
valid = setError(onderwerp, onderwerp.value.trim() ? '' : 'Vul een onderwerp in.') && valid;
|
|
|
|
var bericht = form.elements.bericht;
|
|
valid = setError(bericht, bericht.value.trim() ? '' : 'Schrijf hier je vraag.') && valid;
|
|
|
|
return valid;
|
|
}
|
|
|
|
form.addEventListener('submit', function (event) {
|
|
event.preventDefault();
|
|
|
|
if (status) {
|
|
status.hidden = true;
|
|
status.textContent = '';
|
|
}
|
|
|
|
if (!validate()) {
|
|
var firstInvalid = form.querySelector('[aria-invalid="true"]');
|
|
if (firstInvalid) firstInvalid.focus();
|
|
return;
|
|
}
|
|
|
|
var payload = {
|
|
naam: form.elements.naam.value.trim(),
|
|
email: form.elements.email.value.trim(),
|
|
onderwerp: form.elements.onderwerp.value.trim(),
|
|
bericht: form.elements.bericht.value.trim()
|
|
};
|
|
|
|
if (submit) submit.disabled = true;
|
|
|
|
submitContactForm(payload)
|
|
.then(function () {
|
|
form.reset();
|
|
if (status) {
|
|
status.hidden = false;
|
|
status.textContent = 'Bedankt voor je bericht. Andrea reageert binnen 2-3 werkdagen.';
|
|
}
|
|
})
|
|
.catch(function () {
|
|
if (status) {
|
|
status.hidden = false;
|
|
status.textContent = 'Versturen lukte niet. Mail gerust rechtstreeks naar info@andreaslop.nl.';
|
|
}
|
|
})
|
|
.finally(function () {
|
|
if (submit) submit.disabled = false;
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Scroll-reveal */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
function initReveal() {
|
|
var items = Array.prototype.slice.call(document.querySelectorAll('.reveal'));
|
|
if (!items.length) return;
|
|
|
|
if (prefersReducedMotion || !('IntersectionObserver' in window)) {
|
|
items.forEach(function (item) { item.classList.add('is-visible'); });
|
|
return;
|
|
}
|
|
|
|
var observer = new IntersectionObserver(function (entries) {
|
|
entries.forEach(function (entry) {
|
|
if (!entry.isIntersecting) return;
|
|
entry.target.classList.add('is-visible');
|
|
observer.unobserve(entry.target);
|
|
});
|
|
}, { threshold: 0.12, rootMargin: '0px 0px -60px 0px' });
|
|
|
|
items.forEach(function (item) { observer.observe(item); });
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Terug naar boven */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
function initToTop() {
|
|
var btn = document.querySelector('[data-to-top]');
|
|
if (!btn) return;
|
|
|
|
function update() {
|
|
btn.classList.toggle('is-visible', window.scrollY > window.innerHeight * 0.6);
|
|
}
|
|
|
|
btn.addEventListener('click', function () {
|
|
window.scrollTo({ top: 0, behavior: prefersReducedMotion ? 'auto' : 'smooth' });
|
|
});
|
|
|
|
update();
|
|
window.addEventListener('scroll', update, { passive: true });
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Jaartal in de footer */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
function initYear() {
|
|
var slots = document.querySelectorAll('[data-year]');
|
|
var year = String(new Date().getFullYear());
|
|
slots.forEach(function (slot) { slot.textContent = year; });
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Start */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
function init() {
|
|
initHeader();
|
|
initMobileNav();
|
|
initSideArea();
|
|
initHeroSlider();
|
|
initLightbox();
|
|
initContactForm();
|
|
initReveal();
|
|
initToTop();
|
|
initYear();
|
|
}
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', init);
|
|
} else {
|
|
init();
|
|
}
|
|
})();
|