/* ═══════════════════════════════════════════════════════════════════════ SIGN UP TO HEAR MORE. The markup is a real
with a real action, and it works without any of this: submit it with JavaScript off and the browser posts it and shows the endpoint's answer. All this file does is stop the page navigating away and put the answer where the form is. It deliberately does not remember the address, does not put it in localStorage, and does not send it anywhere except this site's own server. ═══════════════════════════════════════════════════════════════════════ */ (function () { var forms = [].slice.call(document.querySelectorAll('[data-signup]')); if (!forms.length || !window.fetch) return; forms.forEach(function (form) { var input = form.querySelector('[data-signupmail]'); var say = form.querySelector('[data-signupsay]'); var go = form.querySelector('.signup__go'); var busy = false; function tell(msg, bad) { if (!say) return; say.textContent = msg; say.classList.toggle('is-bad', !!bad); } form.addEventListener('submit', function (e) { e.preventDefault(); if (busy) return; var email = (input && input.value || '').trim(); if (!email) { tell('Please type your email address.', true); if (input) input.focus(); return; } busy = true; if (go) { go.disabled = true; } tell(''); fetch('/api/signup', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ email: email, /* which chapter the ball was showing, so Backstage can say what somebody was looking at when they decided to ask */ where: (document.querySelector('[data-heroneu] b') || {}).textContent || 'hero', page: location.pathname, }), }) .then(function (r) { return r.json().then(function (j) { return { ok: r.ok, j: j }; }); }) .then(function (out) { if (!out.ok || !out.j || !out.j.ok) { tell((out.j && out.j.say) || 'That did not go through. Try again?', true); return; } /* the field and the button go, the thank-you stays — pressing it a second time by accident is the commonest thing people do */ form.classList.add('is-done'); tell(out.j.say || 'Thank you — we will be in touch.'); }) .catch(function () { tell('That did not go through. Try again?', true); }) .then(function () { busy = false; if (go) go.disabled = false; }); }); }); })();