1(() => { 2 'use strict'; 3 4 const bindContactMethod = (form) => { 5 const methodEl = form.querySelector('[data-contact-method]'); 6 const valueEl = form.querySelector('[data-contact-value]'); 7 const labelEl = form.querySelector('[data-contact-label]'); 8 if (!(methodEl instanceof HTMLSelectElement) || !(valueEl instanceof HTMLInputElement)) { 9 return; 10 } 11 12 const apply = () => { 13 const email = methodEl.value === 'email'; 14 valueEl.type = email ? 'email' : 'tel'; 15 valueEl.autocomplete = email ? 'email' : 'tel'; 16 valueEl.maxLength = email ? 191 : 32; 17 const label = email ? 'Email address' : 'WhatsApp number'; 18 valueEl.placeholder = label; 19 if (labelEl instanceof HTMLElement) { 20 labelEl.textContent = label; 21 } 22 }; 23 24 methodEl.addEventListener('change', apply); 25 apply(); 26 }; 27 28 const bindInquiryForm = (form) => { 29 if (!(form instanceof HTMLFormElement)) { 30 return; 31 } 32 33 bindContactMethod(form); 34 35 const statusEl = form.querySelector('[data-form-status]'); 36 const csrfInput = form.querySelector('input[name="csrf_test_name"]'); 37 const csrfMeta = document.querySelector('meta[name="X-CSRF-TOKEN"]'); 38 const inlineSuccess = form.hasAttribute('data-inline-success'); 39 40 const readCsrf = () => { 41 if (csrfInput instanceof HTMLInputElement && csrfInput.value) { 42 return csrfInput.value; 43 } 44 return csrfMeta instanceof HTMLMetaElement ? csrfMeta.content : ''; 45 }; 46 47 const setCsrf = (hash) => { 48 if (typeof hash !== 'string' || hash === '') { 49 return; 50 } 51 if (csrfInput instanceof HTMLInputElement) { 52 csrfInput.value = hash; 53 } 54 if (csrfMeta instanceof HTMLMetaElement) { 55 csrfMeta.content = hash; 56 } 57 }; 58 59 const showStatus = (message, ok) => { 60 if (!(statusEl instanceof HTMLElement)) { 61 return; 62 } 63 statusEl.hidden = false; 64 statusEl.textContent = message; 65 statusEl.classList.toggle('is-error', !ok); 66 statusEl.classList.toggle('is-ok', ok); 67 }; 68 69 form.addEventListener('submit', async (event) => { 70 if (!form.checkValidity()) { 71 event.preventDefault(); 72 form.reportValidity(); 73 return; 74 } 75 76 event.preventDefault(); 77 const token = readCsrf(); 78 const body = new FormData(form); 79 const submitBtn = form.querySelector('button[type="submit"]'); 80 if (submitBtn instanceof HTMLButtonElement) { 81 submitBtn.disabled = true; 82 } 83 84 try { 85 const response = await fetch(form.action, { 86 method: 'POST', 87 credentials: 'same-origin', 88 headers: { 89 'X-CSRF-TOKEN': token, 90 'X-Requested-With': 'XMLHttpRequest', 91 Accept: 'application/json', 92 }, 93 body, 94 }); 95 96 let payload = {}; 97 try { 98 payload = await response.json(); 99 } catch (error) { 100 payload = {}; 101 } 102 103 if (payload && typeof payload.csrf_hash === 'string') { 104 setCsrf(payload.csrf_hash); 105 } 106 107 if (!response.ok || !payload.ok) { 108 const message = typeof payload.message === 'string' 109 ? payload.message 110 : 'Unable to submit your request. Please try again.'; 111 showStatus(message, false); 112 return; 113 } 114 115 const successMessage = typeof payload.message === 'string' 116 ? payload.message 117 : "The message was being recorded, we'll get you in touch."; 118 119 if (inlineSuccess || payload.stay === true) { 120 showStatus(successMessage, true); 121 form.reset(); 122 bindContactMethod(form); 123 return; 124 } 125 126 if (typeof payload.redirect === 'string' && payload.redirect.startsWith(window.location.origin)) { 127 window.location.assign(payload.redirect); 128 return; 129 } 130 131 window.location.assign('/forms/success/'); 132 } catch (error) { 133 showStatus('Unable to submit your request. Please try again.', false); 134 } finally { 135 if (submitBtn instanceof HTMLButtonElement) { 136 submitBtn.disabled = false;
137 } 138 } 139 }); 140 }; 141 142 document.querySelectorAll('[data-inquiry-form]').forEach(bindInquiryForm); 143})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.