Files
gureametsabilagun/wp-content/themes/gure-ametsa/assets/js/main.js
Josep Piguillem d7a17a2aca Cuestionario de adopción online y hueco para el aviso de nuevos animales
- [ga_cuestionario]: preguntas editables en ES/EU (Personalizar), borrador guardado en el
  navegador, envío a Mensajes y por correo. Páginas «Cuestionario de adopción /
  Adopzio-galdetegia» fuera del menú y con noindex; admite ?adoptar=ID.
- Pie: formulario de suscripción de MailPoet cuando se configure su shortcode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 10:32:29 +02:00

117 lines
4.6 KiB
JavaScript

( function () {
var toggle = document.querySelector( '.ga-menu-toggle' );
var nav = document.getElementById( 'ga-nav' );
if ( toggle && nav ) {
toggle.addEventListener( 'click', function () {
var open = nav.classList.toggle( 'is-open' );
toggle.setAttribute( 'aria-expanded', open ? 'true' : 'false' );
} );
document.addEventListener( 'keydown', function ( e ) {
if ( e.key === 'Escape' && nav.classList.contains( 'is-open' ) ) {
nav.classList.remove( 'is-open' );
toggle.setAttribute( 'aria-expanded', 'false' );
toggle.focus();
}
} );
}
// Filtros del listado: los <select> envían el formulario al cambiar.
document.querySelectorAll( '.ga-filters select' ).forEach( function ( select ) {
select.addEventListener( 'change', function () {
select.form.submit();
} );
} );
} )();
// Vídeo de portada: se detiene si el usuario prefiere menos movimiento.
( function () {
var video = document.querySelector( '.ga-hero__bg video' );
if ( video && window.matchMedia && window.matchMedia( '(prefers-reduced-motion: reduce)' ).matches ) {
video.removeAttribute( 'autoplay' );
video.pause();
}
} )();
// Carruseles: desplazamiento con botones, ratón/táctil nativo (scroll-snap) y teclado.
( function () {
document.querySelectorAll( '[data-ga-carousel]' ).forEach( function ( carousel ) {
var track = carousel.querySelector( '.ga-carousel__track' );
var buttons = carousel.querySelectorAll( '.ga-carousel__btn' );
if ( ! track ) {
return;
}
function update() {
var max = track.scrollWidth - track.clientWidth - 2;
buttons[ 0 ].disabled = track.scrollLeft <= 2;
buttons[ 1 ].disabled = track.scrollLeft >= max;
carousel.classList.toggle( 'is-static', max <= 0 );
}
buttons.forEach( function ( button ) {
button.addEventListener( 'click', function () {
track.scrollBy( { left: track.clientWidth * 0.9 * Number( button.dataset.dir ), behavior: 'smooth' } );
} );
} );
track.addEventListener( 'keydown', function ( e ) {
if ( e.key === 'ArrowRight' || e.key === 'ArrowLeft' ) {
e.preventDefault();
track.scrollBy( { left: ( e.key === 'ArrowRight' ? 1 : -1 ) * track.clientWidth * 0.9, behavior: 'smooth' } );
}
} );
track.addEventListener( 'scroll', update, { passive: true } );
window.addEventListener( 'resize', update );
update();
} );
} )();
// Ficha: miniaturas de la galería y copiar enlace.
( function () {
var main = document.querySelector( '[data-ga-gallery-main] img' );
document.querySelectorAll( '.ga-thumbs button' ).forEach( function ( btn ) {
btn.addEventListener( 'click', function () {
if ( ! main ) { return; }
main.src = btn.dataset.full;
main.removeAttribute( 'srcset' );
document.querySelectorAll( '.ga-thumbs button' ).forEach( function ( b ) { b.removeAttribute( 'aria-current' ); } );
btn.setAttribute( 'aria-current', 'true' );
} );
} );
document.querySelectorAll( '[data-copy]' ).forEach( function ( btn ) {
btn.addEventListener( 'click', function () {
if ( navigator.clipboard ) {
navigator.clipboard.writeText( btn.dataset.copy ).then( function () { btn.textContent = btn.dataset.done; } );
}
} );
} );
} )();
// Formulario: campo «día y hora» solo cuando el motivo es pedir cita.
( function () {
document.querySelectorAll( '.ga-form select[name="motivo"]' ).forEach( function ( select ) {
var field = select.form.querySelector( '.ga-form__visit' );
if ( ! field ) { return; }
select.addEventListener( 'change', function () { field.hidden = select.value !== 'visita'; } );
} );
} )();
// Cuestionario: guarda el borrador en este navegador hasta que se envía.
( function () {
var done = document.querySelector( '[data-ga-clear-draft]' );
try {
if ( done ) { Object.keys( localStorage ).filter( function ( k ) { return k.indexOf( 'ga-cuestionario-' ) === 0; } ).forEach( function ( k ) { localStorage.removeItem( k ); } ); }
} catch ( e ) {}
document.querySelectorAll( 'form[data-ga-draft]' ).forEach( function ( form ) {
var key = form.dataset.gaDraft, saved = {};
try { saved = JSON.parse( localStorage.getItem( key ) || '{}' ); } catch ( e ) {}
Array.prototype.forEach.call( form.elements, function ( el ) {
if ( el.name && el.type !== 'hidden' && el.type !== 'checkbox' && saved[ el.name ] && ! el.value ) { el.value = saved[ el.name ]; }
} );
form.addEventListener( 'input', function () {
var data = {};
Array.prototype.forEach.call( form.elements, function ( el ) {
if ( el.name && el.type !== 'hidden' && el.type !== 'checkbox' && el.name !== 'web' ) { data[ el.name ] = el.value; }
} );
try { localStorage.setItem( key, JSON.stringify( data ) ); } catch ( e ) {}
} );
} );
} )();