// Submits Core Forms markup through a route on your own site. (function () { function messageText(data, fallback) { var message = data && data.message; if (typeof message === 'string' && message) return message; if (message && typeof message.text === 'string' && message.text) return message.text; return fallback; // message.text can be an empty string } function emit(host, name, detail) { host.dispatchEvent(new CustomEvent(name, { detail: detail, bubbles: true })); } function bind(host) { if (!host) return; var form = host.querySelector('form'); if (!form || form.dataset.cfBound) return; form.dataset.cfBound = '1'; var status = document.createElement('div'); status.className = 'cf-form-status'; status.setAttribute('role', 'status'); status.tabIndex = -1; host.insertBefore(status, host.firstChild); var sending = false; form.addEventListener('submit', async function (event) { event.preventDefault(); if (sending) return; status.textContent = ''; emit(host, 'cf:submit', { form: form }); var payload = Object.create(null); var hasFile = false; new FormData(form).forEach(function (value, name) { if (typeof value !== 'string') { if (value.name) hasFile = true; return; } payload[name] = name in payload ? [].concat(payload[name], value) : value; }); if (hasFile) { status.textContent = 'This form cannot send files. Please use the upload link instead.'; return; } payload._cf_page_url = window.location.href; var buttons = form.querySelectorAll('button[type="submit"], input[type="submit"]'); function setBusy(busy) { sending = busy; form.setAttribute('aria-busy', String(busy)); buttons.forEach(function (button) { button.disabled = busy; }); } form.querySelectorAll('[aria-invalid="true"]').forEach(function (field) { field.removeAttribute('aria-invalid'); }); setBusy(true); try { var response = await fetch(host.dataset.submitUrl || '/api/submit', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' }, body: JSON.stringify(payload), }); var data = await response.json(); if (!response.ok) { status.textContent = messageText(data, 'The form could not be sent. Please try again.'); var field = data.field && (form.elements[data.field] || form.elements[data.field + '[]']); if (field && !field.tagName) field = field[0]; // radio and checkbox groups if (field) { field.setAttribute('aria-invalid', 'true'); field.focus(); } emit(host, 'cf:error', data); } else { emit(host, 'cf:success', data); if (data.redirect_url) { var target = new URL(data.redirect_url, window.location.href); if (target.protocol === 'https:' || target.protocol === 'http:') { window.location.assign(target.href); return; } } status.textContent = messageText(data, 'Thanks. Your message was sent.'); form.remove(); status.focus(); } } catch (error) { status.textContent = 'Network error. Please try again.'; emit(host, 'cf:error', error); } setBusy(false); }); emit(host, 'cf:mounted', null); } function init() { document.querySelectorAll('[data-core-form]').forEach(bind); } window.CoreFormsClient = { bind: bind, init: init }; if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init); else init(); })();