- token, font self-hosted (Fraunces e Inter, sottoinsieme latin) con preload - BaseLayout con head completo (SEO, Open Graph, favicon) e JSON-LD dello studio - intestazione con barra superiore, navigazione e sottomenu servizi accessibili - menu mobile con dialog modale e alternativa senza JavaScript - footer con P.IVA, recapiti, servizi, orari e link legali - componenti: Logo, Icon (Lucide inline), Hero, card servizio, scheda professionista, badge abilitazione, card sede, mappa click-to-load, modulo di contatto, banda CTA, breadcrumb, intestazioni di sezione - generate-icons.mjs: marchio, favicon, apple-touch-icon e immagine OG - collection dei servizi e dei link utili con schemi Zod Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016zJ9ZWGKvoKoxYwbmnRKdJ
133 lines
4.5 KiB
TypeScript
133 lines
4.5 KiB
TypeScript
/**
|
||
* Miglioramento del modulo di contatto: messaggi di validazione in italiano accanto ai campi,
|
||
* riepilogo degli errori annunciato agli screen reader, campo `ts` con l'ora di apertura della
|
||
* pagina e blocco degli invii troppo rapidi (anti-spam senza cookie). Senza JavaScript il modulo
|
||
* usa la validazione nativa del browser.
|
||
*/
|
||
|
||
const MESSAGES = {
|
||
required: 'Compila questo campo.',
|
||
email: 'Inserisci un indirizzo e-mail valido (es. nome@esempio.it).',
|
||
tel: 'Inserisci un numero di telefono valido (solo cifre, spazi e +).',
|
||
short: 'Il testo è troppo breve.',
|
||
consent: 'Per inviare la richiesta devi accettare l’informativa sulla privacy.',
|
||
tooFast: 'Attendi qualche secondo prima di inviare il modulo.',
|
||
};
|
||
|
||
type Field = HTMLInputElement | HTMLTextAreaElement;
|
||
|
||
function messageFor(field: Field): string | null {
|
||
const v = field.validity;
|
||
if (v.valid) return null;
|
||
if (field instanceof HTMLInputElement && field.type === 'checkbox') return MESSAGES.consent;
|
||
if (v.valueMissing) return MESSAGES.required;
|
||
if (v.typeMismatch && field.type === 'email') return MESSAGES.email;
|
||
if (field.type === 'tel' && (v.patternMismatch || v.tooShort || v.tooLong)) return MESSAGES.tel;
|
||
if (v.tooShort) return MESSAGES.short;
|
||
return field.validationMessage || MESSAGES.required;
|
||
}
|
||
|
||
function labelText(form: HTMLFormElement, field: Field): string {
|
||
const label = form.querySelector<HTMLLabelElement>(`label[for="${field.id}"]`);
|
||
return (label?.textContent ?? field.name).replace('*', '').replace(/\s+/g, ' ').trim();
|
||
}
|
||
|
||
function showError(field: Field, message: string | null): void {
|
||
const error = document.getElementById(`${field.id}-err`);
|
||
if (message) {
|
||
field.setAttribute('aria-invalid', 'true');
|
||
if (error) {
|
||
error.textContent = message;
|
||
error.hidden = false;
|
||
}
|
||
} else {
|
||
field.removeAttribute('aria-invalid');
|
||
if (error) {
|
||
error.textContent = '';
|
||
error.hidden = true;
|
||
}
|
||
}
|
||
}
|
||
|
||
function init(form: HTMLFormElement): void {
|
||
const loadedAt = Date.now();
|
||
const minMs = Number(form.dataset.minSeconds ?? '3') * 1000;
|
||
const ts = form.querySelector<HTMLInputElement>('[data-form-ts]');
|
||
if (ts) ts.value = String(loadedAt);
|
||
|
||
const summary = form.querySelector<HTMLElement>('[data-form-summary]');
|
||
const summaryList = form.querySelector<HTMLElement>('[data-form-summary-list]');
|
||
const status = form.querySelector<HTMLElement>('[data-form-status]');
|
||
const fields = Array.from(
|
||
form.querySelectorAll<Field>('input[required], textarea[required]'),
|
||
).filter((f) => f.type !== 'hidden');
|
||
|
||
// Da qui in poi la validazione è gestita dallo script (messaggi in italiano).
|
||
form.noValidate = true;
|
||
|
||
for (const field of fields) {
|
||
const event = field.type === 'checkbox' ? 'change' : 'blur';
|
||
field.addEventListener(event, () => {
|
||
if (field.getAttribute('aria-invalid') === 'true' || field.value !== '') {
|
||
showError(field, messageFor(field));
|
||
}
|
||
});
|
||
field.addEventListener('input', () => {
|
||
if (field.getAttribute('aria-invalid') === 'true') showError(field, messageFor(field));
|
||
});
|
||
}
|
||
|
||
form.addEventListener('submit', (event) => {
|
||
const invalid: { field: Field; message: string }[] = [];
|
||
for (const field of fields) {
|
||
const message = messageFor(field);
|
||
showError(field, message);
|
||
if (message) invalid.push({ field, message });
|
||
}
|
||
|
||
if (invalid.length > 0) {
|
||
event.preventDefault();
|
||
if (summary && summaryList) {
|
||
summaryList.replaceChildren(
|
||
...invalid.map(({ field, message }) => {
|
||
const li = document.createElement('li');
|
||
const a = document.createElement('a');
|
||
a.href = `#${field.id}`;
|
||
a.textContent = `${labelText(form, field)}: ${message}`;
|
||
a.addEventListener('click', (e) => {
|
||
e.preventDefault();
|
||
field.focus();
|
||
});
|
||
li.append(a);
|
||
return li;
|
||
}),
|
||
);
|
||
summary.hidden = false;
|
||
summary.focus();
|
||
}
|
||
if (status) {
|
||
status.textContent = `Il modulo contiene ${invalid.length} ${
|
||
invalid.length === 1 ? 'errore' : 'errori'
|
||
}.`;
|
||
}
|
||
return;
|
||
}
|
||
|
||
if (summary) summary.hidden = true;
|
||
|
||
if (Date.now() - loadedAt < minMs) {
|
||
event.preventDefault();
|
||
if (status) status.textContent = MESSAGES.tooFast;
|
||
return;
|
||
}
|
||
|
||
if (status) status.textContent = 'Invio in corso…';
|
||
});
|
||
}
|
||
|
||
for (const form of document.querySelectorAll<HTMLFormElement>('form[data-contact-form]')) {
|
||
init(form);
|
||
}
|
||
|
||
export {};
|