// The board moderator form posts JSON to its data-api endpoint and shows field messages next // to the fields, and swaps to the confirmation when it is stored. Cloudflare Turnstile loads only // when the API reports a site key (appearance "interaction-only", so most people never see it). // The hidden "website" field is the honeypot: people never see it, and the API ignores any // submission that fills it. (function () { var form = document.querySelector('form[data-api]'); if (!form) return; var status = form.querySelector('.form-status'); var submit = form.querySelector('button[type="submit"]'); var done = document.querySelector('[data-done]'); var widget = form.querySelector('.turnstile'); var siteKey = null, widgetId = null, token = ''; // Field choices come from the API so the form and the apps always agree. fetch('/api/meta', { headers: { accept: 'application/json' } }) .then(function (r) { if (!r.ok) throw new Error(); return r.json(); }) .then(function (m) { form.querySelectorAll('select[data-options]').forEach(function (sel) { var map = m[sel.dataset.options] || {}; var keys = Array.isArray(map) ? map : Object.keys(map); keys.forEach(function (k) { var o = document.createElement('option'); o.value = k; o.textContent = Array.isArray(map) ? (k === 'hour' ? 'an ' : 'a ') + k : map[k]; if (sel.dataset.default === k) o.selected = true; sel.appendChild(o); }); }); siteKey = m.turnstile_site_key; if (siteKey && widget) loadTurnstile(); }) .catch(function () { say('We couldn’t reach the server just now. You can still fill this in; try sending again in a moment.'); }); function loadTurnstile() { window.onAppTurnstile = function () { widgetId = window.turnstile.render(widget, { sitekey: siteKey, appearance: 'interaction-only', callback: function (t) { token = t; }, 'expired-callback': function () { token = ''; }, 'error-callback': function () { token = ''; } }); }; var s = document.createElement('script'); s.src = 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit&onload=onAppTurnstile'; s.async = true; s.defer = true; document.head.appendChild(s); } function say(text) { status.textContent = text || ''; } function clearErrors() { form.querySelectorAll('.error').forEach(function (e) { e.textContent = ''; }); form.querySelectorAll('[aria-invalid]').forEach(function (e) { e.removeAttribute('aria-invalid'); }); } function collect() { var body = {}; Array.prototype.forEach.call(form.elements, function (el) { if (!el.name) return; if (el.type === 'checkbox') body[el.name] = el.checked; else if (el.dataset.kind === 'int') { if (el.value.trim() !== '') body[el.name] = /^\d+$/.test(el.value.trim().replace(/[,\s]/g, '')) ? Number(el.value.trim().replace(/[,\s]/g, '')) : el.value.trim(); } else if (el.dataset.kind === 'links') body[el.name] = el.value.split(/\s+/).filter(Boolean); else if (el.value.trim() !== '') body[el.name] = el.value; }); if (token) body.turnstile_token = token; return body; } form.addEventListener('submit', function (e) { e.preventDefault(); clearErrors(); say(''); var missing = null; [['name', 'Please add your name.'], ['email', 'Please add a valid email.'], ['message', 'Please write a short message.']].forEach(function (pair) { var el = form.elements[pair[0]]; if (!el || missing) return; var v = el.value.trim(); var bad = !v || (pair[0] === 'email' && !/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(v)); if (bad) { missing = el; var slot = form.querySelector('[data-error="' + pair[0] + '"]'); if (slot) slot.textContent = pair[1]; el.setAttribute('aria-invalid', 'true'); say(pair[1]); } }); if (missing) { missing.focus(); return; } if (siteKey && !token) { say('One moment: we’re checking you’re a person. Please try again in a few seconds.'); return; } submit.disabled = true; var label = submit.textContent; submit.textContent = 'Sending…'; fetch(form.dataset.api, { method: 'POST', headers: { 'content-type': 'application/json', accept: 'application/json' }, body: JSON.stringify(collect()) }) .then(function (r) { return r.json().then(function (d) { return { status: r.status, data: d }; }); }) .then(function (res) { if (res.data && res.data.ok) return finish(res.data); if (res.status === 422 && res.data.fields) { var first = null; Object.keys(res.data.fields).forEach(function (name) { var el = form.elements[name]; var slot = form.querySelector('[data-error="' + name + '"]'); if (slot) slot.textContent = res.data.fields[name]; if (el && el.setAttribute) { el.setAttribute('aria-invalid', 'true'); first = first || el; } }); say('Please fix the marked fields.'); if (first && first.focus) first.focus(); } else { say((res.data && res.data.message) || 'Something went wrong. Please try again.'); } if (window.turnstile && widgetId !== null) { token = ''; window.turnstile.reset(widgetId); } }) .catch(function () { say('We couldn’t reach the server. Your answers are still here; please try again.'); }) .then(function () { submit.disabled = false; submit.textContent = label; }); }); function finish(data) { form.hidden = true; var ref = done.querySelector('[data-ref]'); if (ref && data.id) ref.textContent = data.id; done.hidden = false; done.setAttribute('tabindex', '-1'); done.focus(); done.scrollIntoView({ block: 'start' }); } })();