Files
2026-07-02 15:54:39 -06:00

1 line
12 KiB
JavaScript

(()=>{const e=".gb-form-success",t="gb-form-hide-on-success",r=new WeakMap,a=new WeakMap;function i(e){const t=new AbortController,r=setTimeout((()=>t.abort()),e);return{signal:t.signal,clear:()=>clearTimeout(r)}}function s(e){const t=String(null!=e?e:"");return"undefined"!=typeof CSS&&CSS.escape?CSS.escape(t):t.replace(/\\/g,"\\\\").replace(/"/g,'\\"')}function n(e){e.querySelectorAll("."+t).forEach((e=>{e.classList.remove(t)}))}function o(e,t,r){t&&(e.textContent=t,e.className="gb-form-message "+(r?"gb-form-message--success":"gb-form-message--error"),e.setAttribute("role",r?"status":"alert"),e.setAttribute("aria-live",r?"polite":"assertive"),e.removeAttribute("hidden"),"function"==typeof e.scrollIntoView&&e.scrollIntoView({behavior:"smooth",block:"nearest"}))}function c(e){if(!window.turnstile||"function"!=typeof window.turnstile.reset)return!1;const t=e.querySelector(".cf-turnstile");if(!t)return!1;try{return window.turnstile.reset(t),!0}catch(e){return!1}}function d(e,t){if(!t)return null;const r=e.querySelector('input[name="'+s(t)+'"], textarea[name="'+s(t)+'"]');return!r||r.closest("[data-gb-conditions][hidden]")?null:"TEXTAREA"===r.tagName?r:"INPUT"!==r.tagName?null:["text","email","url","tel","number"].includes(r.type)?r:null}function l(e){return String(e||"").replace(/\s+/g," ").replace(/\s+\*$/,"").trim()}function u(e){if(!e)return"";const t=e.labels?Array.from(e.labels):[];for(const e of t){const t=l(e.textContent);if(t)return t}const r=e.closest?.(".gb-form-field"),a=r?.querySelector?.("label, legend");return l(a?.textContent)}function b(e){let t=!0;return e.querySelectorAll("[data-gb-matches-field]").forEach((r=>{const a=r.dataset.gbFieldName||"",i=r.dataset.gbMatchesField||"",s=d(e,a),n=d(e,i);if(!s||!n||r.closest("[data-gb-conditions][hidden]"))return void(s&&"function"==typeof s.setCustomValidity&&s.setCustomValidity(""));const o=s.value===n.value;"function"==typeof s.setCustomValidity&&s.setCustomValidity(o?"":function(e,t,r){const a=u(t),i=u(r);return a&&i&&e.dataset.gbMatchMessageWithLabel?e.dataset.gbMatchMessageWithLabel.replace("%1$s",(()=>a)).replace("%2$s",(()=>i)):i&&e.dataset.gbMatchMessageWithTarget?e.dataset.gbMatchMessageWithTarget.replace("%s",(()=>i)):e.dataset.gbMatchMessage||"The matching fields must have the same value."}(r,s,n)),o?s.removeAttribute("aria-invalid"):t=!1})),t}function f(e){let t=!1;const r=[];return e.querySelectorAll("[data-gb-conditions]").forEach((t=>{let a;try{a=JSON.parse(t.dataset.gbConditions)}catch(e){return}if(!Array.isArray(a)||!a.length)return;const i=a.every((t=>function(e,t){if(!t)return!1;if(!function(e,t){return!(!t||!e.querySelector('[name="'+s(t)+'"]')&&!e.querySelector('[name="'+s(t+"[]")+'"]'))}(e,t.field))return!1;const r=function(e,t){const r=e.querySelector('[name="'+s(t)+'"]')||e.querySelector('[name="'+s(t+"[]")+'"]');if(r&&r.closest("[data-gb-conditions][hidden]"))return"";const a=e.querySelectorAll('input[type="checkbox"][name="'+s(t+"[]")+'"]');if(a.length){const e=[];return a.forEach((t=>{t.checked&&e.push(t.value)})),e}const i=e.querySelector('input[type="checkbox"][name="'+s(t)+'"]');if(i)return i.checked?i.value:"";const n=e.querySelectorAll('input[type="radio"][name="'+s(t)+'"]');if(n.length){for(const e of n)if(e.checked)return e.value;return""}const o=e.querySelector('[name="'+s(t)+'"]');return o?o.value:""}(e,t.field);switch(t.operator){case"is":return Array.isArray(r)?r.includes(t.value):r===t.value;case"isnot":return Array.isArray(r)?!r.includes(t.value):r!==t.value;case"isempty":return Array.isArray(r)?0===r.length:""===r.trim();case"isnotempty":return Array.isArray(r)?r.length>0:""!==r.trim();default:return!1}}(e,t)));r.push({wrapper:t,visible:i})})),r.forEach((({wrapper:e,visible:r})=>{const a=e.hasAttribute("hidden");r&&a?(e.removeAttribute("hidden"),t=!0):r||a||(e.setAttribute("hidden",""),t=!0),e.querySelectorAll("input, select, textarea").forEach((e=>{r?e.removeAttribute("disabled"):e.setAttribute("disabled","")}))})),t}function g(e){let t=0,r=!0;const a=e.querySelectorAll("[data-gb-conditions]"),i=Math.max(10,a.length+1);for(;r&&t<i;)r=f(e),t++;r&&(window.generateBlocksPro?.debug&&console.warn("GenerateBlocks Form: conditional visibility did not converge for form",e.dataset.gbFormId),a.forEach((e=>{e.setAttribute("hidden",""),e.querySelectorAll("input, select, textarea").forEach((e=>{e.setAttribute("disabled","")}))})))}function m(e){return!!e?.dataset?.gbPreview}function h(e){const t=e.dataset.gbSecurityEndpoint,a=parseInt(e.dataset.gbFormId,10),s=parseInt(e.dataset.gbPostId,10)||0;if(!t||!a)return Promise.resolve(!1);const n=new URL(t,window.location.href);n.searchParams.set("formId",String(a)),n.searchParams.set("postId",String(s));const o=i(15e3);return fetch(n.href,{method:"GET",credentials:"same-origin",referrerPolicy:"strict-origin-when-cross-origin",headers:{Accept:"application/json"},signal:o.signal}).then((e=>{if(!e.ok)throw new Error("status "+e.status);return e.json()})).then((t=>!!(t&&t.gb_form_ts&&t.hp_name)&&(e.dataset.gbFormTs=t.gb_form_ts,r.set(e,t.hp_name),!0))).catch((()=>!1)).finally((()=>o.clear()))}function y(e){const t=e?.closest?.('fieldset.gb-form-field--checkbox-group[aria-invalid="true"]');t&&t.querySelector('input[type="checkbox"]:checked')&&t.removeAttribute("aria-invalid"),e?.matches?.('input[type="radio"]')&&e.checked&&e.name&&e.form&&e.form.querySelectorAll('input[type="radio"][name="'+s(e.name)+'"][aria-invalid="true"]').forEach((e=>{e.validity&&e.validity.valid&&e.removeAttribute("aria-invalid")})),e&&e.getAttribute&&"true"===e.getAttribute("aria-invalid")&&e.validity&&e.validity.valid&&e.removeAttribute("aria-invalid")}const p=new WeakSet,v=new WeakSet;function S(e){return!(p.has(e)||!e.querySelector("[data-gb-conditions]")&&(p.add(e),1))}function A(e){return!(v.has(e)||!e.querySelector("[data-gb-matches-field]")&&(v.add(e),1))}function E(e){const t=r.get(e);if(!t)return;const i=a.get(e);i&&i.remove();const s=document.createElement("div");s.setAttribute("aria-hidden","true"),s.style.cssText="position:absolute;left:-9999px;height:0;width:0;overflow:hidden;";const n=document.createElement("label");n.textContent="Subject";const o=document.createElement("input");o.type="text",o.name=t,o.tabIndex=-1,o.autocomplete="off",o.value="";const c="gb-field-"+(e.dataset.gbFormId||"form")+"-"+(e.dataset.gbInstance||"1");o.id=c,n.setAttribute("for",c),s.appendChild(n),s.appendChild(o),e.appendChild(s),a.set(e,s)}function q(e){g(e),m(e)||h(e).then((t=>{t&&(E(e),e.dataset.gbReady="true")}))}document.addEventListener("change",(e=>{const t=e.target.closest("[data-gb-form-id]");t&&(S(t)&&g(t),A(t)&&b(t),y(e.target))})),document.addEventListener("input",(e=>{const t=e.target.closest("[data-gb-form-id]");t&&(S(t)&&g(t),A(t)&&b(t),y(e.target))})),document.addEventListener("DOMContentLoaded",(()=>{document.querySelectorAll("[data-gb-form-id]").forEach(q)})),document.addEventListener("reset",(e=>{const t=e.target.closest("[data-gb-form-id]");t&&(t.classList.remove("is-submitted"),n(t),delete t.dataset.gbRetriedToken,t.querySelectorAll('[aria-invalid="true"]').forEach((e=>{e.removeAttribute("aria-invalid")})),A(t)&&t.querySelectorAll("[data-gb-matches-field] input, [data-gb-matches-field] textarea").forEach((e=>{"function"==typeof e.setCustomValidity&&e.setCustomValidity("")})),setTimeout((()=>g(t)),0))})),document.addEventListener("submit",(a=>{const s=a.target.closest("[data-gb-form-id]");if(!s)return;if(a.preventDefault(),m(s))return;if(s.classList.contains("is-submitted"))return;const d="true"===s.dataset.gbSkipNativeValidation;if(!d&&A(s)&&b(s),!d&&"function"==typeof s.reportValidity&&!s.reportValidity())return s.querySelectorAll('[aria-invalid="true"]').forEach((e=>{e.validity&&e.validity.valid&&e.removeAttribute("aria-invalid")})),void s.querySelectorAll("input, select, textarea").forEach((e=>{e.disabled||e.validity&&e.validity.valid||e.setAttribute("aria-invalid","true")}));const l=s.querySelectorAll('fieldset.gb-form-field--checkbox-group[data-gb-required="true"]');let u=!0;const f=[];if(l.forEach((e=>{e.closest("[data-gb-conditions][hidden]")||(e.querySelector('input[type="checkbox"]:checked')?e.removeAttribute("aria-invalid"):(u=!1,f.push(e)))})),!u){f.forEach((e=>e.setAttribute("aria-invalid","true")));const e=f[0];e&&"function"==typeof e.focus&&(e.hasAttribute("tabindex")||e.setAttribute("tabindex","-1"),e.focus({preventScroll:!1}));const t=s.querySelector(".gb-form-message");return void(t&&o(t,s.dataset.gbErrorMessage,!1))}if(s.dataset.gbSubmitting)return;s.dataset.gbSubmitting="true";const g=parseInt(s.dataset.gbFormId,10),y=parseInt(s.dataset.gbPostId,10)||0,p=s.dataset.gbFormEndpoint;if(!g||!p)return void delete s.dataset.gbSubmitting;const v=parseInt((s.dataset.gbFormTs||"").split("-")[0],10),S=v&&Math.floor(Date.now()/1e3)-v>3240;if(!s.dataset.gbFormTs||S){const e=s.querySelector(".gb-form-message"),t=s.querySelector('[type="submit"]');return t&&(t.disabled=!0,t.setAttribute("aria-busy","true")),void h(s).then((r=>{if(delete s.dataset.gbSubmitting,r&&(E(s),s.dataset.gbReady="true","function"==typeof s.requestSubmit))return t&&(t.disabled=!1,t.removeAttribute("aria-busy")),void s.requestSubmit();t&&(t.disabled=!1,t.removeAttribute("aria-busy")),e&&o(e,s.dataset.gbErrorMessage,!1)}))}const q=s.querySelector('[type="submit"]'),w=s.querySelector(".gb-form-message");function k(){delete s.dataset.gbSubmitting,q&&(q.disabled=!1,q.removeAttribute("aria-busy"))}let x,I,M;try{w&&w.setAttribute("hidden",""),q&&(q.disabled=!0,q.setAttribute("aria-busy","true")),x=new FormData(s),I={};const e=r.get(s)||"";M=e&&x.get(e)||"";const t=new Set;s.querySelectorAll('input[type="checkbox"][name$="[]"]:not([disabled])').forEach((e=>{t.add(e.name)})),t.forEach((e=>{const t=e.slice(0,-2);I[t]=x.getAll(e)}));for(const[r,a]of x.entries())r!==e&&"cf-turnstile-response"!==r&&(t.has(r)||(I[r]=a));s.querySelectorAll('input[type="checkbox"][name]:not([disabled])').forEach((e=>{e.name.endsWith("[]")||e.checked||e.name in I||(I[e.name]="")})),t.forEach((e=>{const t=e.slice(0,-2);t in I||(I[t]=[])}))}catch(e){return k(),void(w&&o(w,s.dataset.gbErrorMessage,!1))}const T=!!s.querySelector(".cf-turnstile");let C;if(T&&(C=x.get("cf-turnstile-response")||"",!C))return k(),void(w&&o(w,s.dataset.gbErrorMessage,!1));const L={formId:g,postId:y,fields:I,gb_form_ts:s.dataset.gbFormTs||"",gb_hp:M,...T?{cf_turnstile_response:C}:{}},F=i(3e4);fetch(p,{method:"POST",credentials:"same-origin",headers:{"Content-Type":"application/json"},body:JSON.stringify(L),signal:F.signal}).then((e=>{if(!e.ok)throw new Error("Server returned status "+e.status);return e.json()})).then((r=>{const a=!!r.success;if(!a&&r.retry&&!s.dataset.gbRetriedToken)return s.dataset.gbRetriedToken="true",F.clear(),void h(s).then((e=>{if(delete s.dataset.gbSubmitting,e){if(E(s),T)return c(s),delete s.dataset.gbRetriedToken,w&&o(w,s.dataset.gbErrorMessage,!1),void(q&&(q.disabled=!1,q.removeAttribute("aria-busy")));if("function"==typeof s.requestSubmit)return void s.requestSubmit()}delete s.dataset.gbRetriedToken,w&&o(w,s.dataset.gbErrorMessage,!1),q&&(q.disabled=!1,q.removeAttribute("aria-busy"))}));delete s.dataset.gbRetriedToken,!a&&T&&c(s);const i=a?s.querySelector(e):null,d=!!i;if(w&&!d){const e=r.publicMessage&&r.message?r.message:"",t=a?s.dataset.gbSuccessMessage:e||s.dataset.gbErrorMessage||r.message;o(w,t,a)}if(s.dispatchEvent(new CustomEvent(a?"gb-form:success":"gb-form:error",{bubbles:!0,detail:{formId:g,postId:y,fields:I,response:r}})),a){const r=s.dataset.gbRedirectUrl;if(r)try{const e=new URL(r,window.location.href);if("http:"===e.protocol||"https:"===e.protocol)return void(window.location.href=e.href)}catch(e){}d?(function(r){if(n(r),!r.querySelector(e))return;const a=r=>{Array.from(r.children).forEach((r=>{r.matches(e)||(r.querySelector(e)?a(r):r.classList.add(t))}))};a(r)}(s),s.classList.add("is-submitted"),(l=i)&&(l.setAttribute("role","status"),l.setAttribute("aria-live","polite"),l.hasAttribute("tabindex")||l.setAttribute("tabindex","-1"),"function"==typeof l.focus&&l.focus({preventScroll:!0}),"function"==typeof l.scrollIntoView&&l.scrollIntoView({behavior:"smooth",block:"nearest"}))):s.reset(),c(s)}var l})).catch((e=>{delete s.dataset.gbRetriedToken,c(s),w&&o(w,s.dataset.gbErrorMessage,!1),s.dispatchEvent(new CustomEvent("gb-form:error",{bubbles:!0,detail:{formId:g,postId:y,fields:I,error:{message:e?.message||"Network error"}}}))})).finally((()=>{F.clear(),k()}))}))})();