domma-cms 1.25.1 → 1.26.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/admin/index.html +2 -2
- package/admin/js/templates/docs/tutorial-forms.html +15 -0
- package/admin/js/templates/seo.html +2 -0
- package/admin/js/views/form-editor.js +7 -7
- package/admin/js/views/seo.js +24 -24
- package/docs/DommaFeatures.md +1 -1
- package/package.json +1 -1
- package/plugins/_lib/admin/ui/page-menu.js +379 -0
- package/public/css/forms.css +1 -1
- package/public/js/form-logic-engine.js +1 -1
- package/server/templates/page.html +2 -2
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import{apiRequest as fe}from"/admin/js/api.js";import{makeIconInput as je}from"/admin/js/lib/shortcode-modal.js";import{enhancePickerFields as bt}from"/public/js/forms.js";import{buildWizardSteps as dn}from"/public/js/form-steps.mjs";import{DEFAULT_MAX_FILES as gt,MAX_FILES_LIMIT as He,captureFileFields as yt,destroyDropzones as vt,fileBlueprint as
|
|
2
|
-
`).filter(
|
|
3
|
-
`).filter(y=>y.trim()).map(y=>{const[C,...x]=y.split(":");return{value:C.trim(),label:x.join(":").trim()||C.trim()}});n.cascade={sourceField:m,mapping:v,defaultOptions:f},a=!0}}return a?n:void 0}function V(){T=T.map((e,t)=>qn(t))}function An(e){const t=e.firstElementChild;if(!t||t.querySelector(".fb-drag-grip"))return;const s=document.createElement("span");s.className="fb-drag-grip",s.setAttribute("data-icon","grip-vertical"),s.setAttribute("data-icon-size","16"),s.title="Drag to reorder",t.insertBefore(s,t.firstChild),window.I?.scan?.(t)}function ne(e){const t=e.find("#fields-list").get(0),s=e.find("#fields-empty-msg").get(0);if(t){if(Array.from(t.querySelectorAll(".fb-field-card")).forEach(n=>n.remove()),T.length===0){s&&(s.style.display=""),ve?.(t),Ke();return}s&&(s.style.display="none"),T.forEach((n,a)=>{const i=n.type==="page-break"?Fn(n,a,e):n.type==="spacer"?Bn(n,a,e):In(n,a,e);An(i),t.appendChild(i)}),ve?.(t),Ke()}}function Fn(e,t,s){const n=document.createElement("div");n.className="fb-field-card",n.dataset.index=t,n.style.cssText="border:2px dashed var(--dm-border);border-radius:8px;overflow:hidden;margin-bottom:.75rem;background:var(--dm-card-bg);";const a=document.createElement("div");a.style.cssText="display:flex;align-items:center;gap:.6rem;padding:.6rem .8rem;cursor:pointer;user-select:none;";const i=document.createElement("span");i.textContent="- Page Break -",i.style.cssText="font-size:.7rem;padding:.15rem .5rem;border-radius:999px;background:color-mix(in srgb, var(--dm-text-muted) 20%, transparent);color:var(--dm-text-muted);white-space:nowrap;flex-shrink:0;font-style:italic;";const o=document.createElement("span");o.textContent=e.label||"Untitled Step",o.style.cssText="flex:1;font-weight:600;font-size:.9rem;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:var(--dm-text-muted);";const l=document.createElement("div");l.style.cssText="display:flex;gap:.25rem;flex-shrink:0;margin-left:.5rem;";const d=document.createElement("button");d.className="btn btn-xs btn-ghost",d.title="Edit step",O(d,"more-horizontal"),d.addEventListener("click",v=>{v.stopPropagation(),u.style.display=u.style.display==="none"?"":"none"}),l.appendChild(d);const r=document.createElement("button");r.className="btn btn-xs btn-ghost",r.style.cssText="line-height:1;opacity:.6;",r.title="Remove page break",O(r,"x"),r.addEventListener("click",async v=>{v.stopPropagation(),await E.confirm("Remove this page break?")&&(V(),T.splice(t,1),ne(s))}),l.appendChild(r),a.appendChild(i),a.appendChild(o),a.appendChild(l),a.addEventListener("click",()=>{u.style.display=u.style.display==="none"?"":"none"});const u=document.createElement("div");u.className="fb-field-body",u.style.cssText="padding:.8rem;border-top:1px dashed var(--dm-border);display:none;";const p=z([P("Step Title",`fb-pb-label-${t}`,"text",e.label||"","Shown as the wizard step heading"),P("Step Description",`fb-pb-desc-${t}`,"text",e.description||"","Optional sub-heading")]),m=p.querySelector(`#fb-pb-label-${t}`);return m&&m.addEventListener("input",()=>{o.textContent=m.value||"Untitled Step"}),u.appendChild(p),n.appendChild(a),n.appendChild(u),n}function Bn(e,t,s){const n=document.createElement("div");n.className="fb-field-card",n.dataset.index=t,n.style.cssText="border:1px dashed var(--dm-border);border-radius:6px;margin-bottom:.5rem;background:transparent;";const a=document.createElement("div");a.style.cssText="display:flex;align-items:center;gap:.6rem;padding:.4rem .8rem;";const i=document.createElement("div");i.style.cssText="flex:1;height:1px;background:var(--dm-border);";const o=document.createElement("span");o.textContent="Spacer",o.style.cssText="font-size:.7rem;color:var(--dm-text-muted);white-space:nowrap;padding:0 .4rem;font-style:italic;";const l=document.createElement("div");l.style.cssText="flex:1;height:1px;background:var(--dm-border);";const d=document.createElement("div");d.style.cssText="display:flex;gap:.25rem;flex-shrink:0;";const r=document.createElement("button");return r.className="btn btn-xs btn-ghost",r.style.cssText="line-height:1;opacity:.6;",r.title="Remove spacer",O(r,"x"),r.addEventListener("click",async u=>{u.stopPropagation(),V(),T.splice(t,1),ne(s)}),d.appendChild(r),a.appendChild(i),a.appendChild(o),a.appendChild(l),a.appendChild(d),n.appendChild(a),n}function In(e,t,s){const n=document.createElement("div");n.className="fb-field-card",n.dataset.index=t,n.style.cssText="border:1px solid var(--dm-border);border-radius:8px;overflow:hidden;margin-bottom:.75rem;background:var(--dm-card-bg);box-shadow:var(--dm-shadow-sm);";const a=document.createElement("div");a.style.cssText="display:flex;align-items:center;gap:.6rem;padding:.65rem .85rem;background:var(--dm-card-header-bg);cursor:pointer;user-select:none;";const i=document.createElement("span");i.textContent=qt(e.type),i.style.cssText="font-size:.7rem;padding:.15rem .45rem;border-radius:999px;background:color-mix(in srgb, var(--dm-primary) 14%, transparent);color:var(--dm-primary);white-space:nowrap;flex-shrink:0;";const o=document.createElement("span");o.textContent=e.label||"(unlabelled)",o.style.cssText="flex:1;font-weight:600;font-size:.9rem;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;";const l=document.createElement("div");l.style.cssText="display:flex;gap:.25rem;flex-shrink:0;margin-left:.5rem;";const d=document.createElement("button");d.className="btn btn-xs btn-ghost",d.title="Edit field",O(d,"more-horizontal"),d.addEventListener("click",p=>{p.stopPropagation(),u.style.display=u.style.display==="none"?"":"none"}),l.appendChild(d);const r=document.createElement("button");if(r.className="btn btn-xs btn-ghost",r.style.cssText="line-height:1;opacity:.6;",r.title="Remove field",O(r,"x"),r.addEventListener("click",async p=>{p.stopPropagation(),await E.confirm("Remove this field?")&&(V(),T.splice(t,1),ne(s))}),l.appendChild(r),a.appendChild(i),a.appendChild(o),e.required){const p=document.createElement("span");p.textContent="required",p.style.cssText="font-size:.7rem;color:var(--dm-danger);flex-shrink:0;",a.appendChild(p)}if(At(e.logic)){const p=document.createElement("span");O(p,"zap"),p.title="Has conditional logic",p.style.cssText="font-size:.75rem;color:var(--dm-primary);flex-shrink:0;",a.appendChild(p)}a.appendChild(l),a.addEventListener("click",()=>{u.style.display=u.style.display==="none"?"":"none"});const u=Mn(e,t,o);return u.style.display="none",n.appendChild(a),n.appendChild(u),n}function Mn(e,t,s){const n=document.createElement("div");n.className="fb-field-body",n.style.cssText="padding:1rem .9rem;border-top:1px solid var(--dm-border);";const a=z([P("Label",`fb-label-${t}`,"text",e.label||"","Shown above the field"),P("Field Name",`fb-name-${t}`,"text",e.name||"","Used as data key")]),i=z([ia("Type",`fb-type-${t}`,Tt,e.type||"string"),Ie("Required",`fb-required-${t}`,e.required||!1)]),o=z([P("Placeholder",`fb-placeholder-${t}`,"text",e.placeholder||"","Hint text inside the field")]),l=z([P("Helper Text",`fb-helper-${t}`,"text",e.helper||"","Shown below the field"),P("Tooltip",`fb-tooltip-${t}`,"text",e.tooltip||"","Shown on hover via a help icon next to the label")]),d=z([P("CSS classes",`fb-class-${t}`,"text",e.class||"","Your own class names, separated by spaces, on the field's wrapper - style them in Custom CSS")]),r=e.formConfig?.span,u=z([P("Column Span",`fb-span-${t}`,"number",r&&r!=="full"?String(r):"1","Columns to span (grid only)"),Ie("Full Width",`fb-fullwidth-${t}`,r==="full")]);u.classList.add("fb-grid-row"),u.style.display=document.getElementById("setting-layout")?.value==="grid"?"flex":"none",n.appendChild(a),n.appendChild(i),n.appendChild(o),n.appendChild(l),n.appendChild(d),n.appendChild(u);const p=n.querySelector(`#fb-label-${t}`),m=n.querySelector(`#fb-name-${t}`);p&&p.addEventListener("input",()=>{s&&(s.textContent=p.value||"(unlabelled)"),m&&!m.dataset.manuallyEdited&&(m.value=Nt(p.value))}),m&&m.addEventListener("input",()=>{m.dataset.manuallyEdited="1"});const v=n.querySelector(`#fb-type-${t}`);v&&v.addEventListener("change",()=>{const y=n.closest(".fb-field-card");if(y){const b=y.querySelector("span");b&&(b.textContent=qt(v.value))}const C=n.querySelector(".fb-field-extras");C&&C.remove();const x=Gt(v.value,e,t);x&&n.appendChild(x)});const f=Gt(e.type,e,t);return f&&n.appendChild(f),n.appendChild(_n(e,t)),n.appendChild(Gn(e,t)),n}const Ne=[{value:"equals",label:"equals"},{value:"not_equals",label:"does not equal"},{value:"contains",label:"contains"},{value:"not_contains",label:"does not contain"},{value:"starts_with",label:"starts with"},{value:"ends_with",label:"ends with"},{value:"greater_than",label:"is greater than"},{value:"less_than",label:"is less than"},{value:"is_empty",label:"is empty"},{value:"is_not_empty",label:"is not empty"},{value:"in",label:"is one of (comma sep)"},{value:"not_in",label:"is not one of (comma sep)"},{value:"matches_regex",label:"matches regex"}],Ye=new Set(["is_empty","is_not_empty"]),Ft={equals:"includes",not_equals:"does not include",contains:"any value contains",not_contains:"no value contains",starts_with:"any value starts with",ends_with:"any value ends with",greater_than:"any value is greater than",less_than:"any value is less than",is_empty:"nothing selected",is_not_empty:"something selected",in:"includes one of (comma sep)",not_in:"includes none of (comma sep)",matches_regex:"any value matches regex"};function Je(e){return!!e&&(e.type==="checkbox-group"||!!e.multiple)}function qe(e){const t=document.createElement("p");return t.textContent=e,t.appendChild(A({Visibility:"Choose whether this field starts visible or hidden, then add rules to show or hide it based on answers. The transition and speed control how it appears.","Conditional Requirement":"Make this field required or optional according to another answer. Hidden fields are not required.","Custom Validation":"Regex tests the answer against a regular expression; flags such as i make it case insensitive. Match field compares with another field name, such as password confirmation. Set the error message visitors see if the check fails.","Cascade Options":"Replace this field's choices according to another field's answer. Map source values to option lists and provide default options for unmatched or empty answers."}[e],e)),t.style.cssText="font-size:.75rem;font-weight:700;color:var(--dm-text-muted);margin:0 0 .7rem;text-transform:uppercase;letter-spacing:.06em;",t}function $e(e,t,s,n,a){const i=document.createElement("div");i.className="fb-logic-cond-row",i.style.cssText="display:flex;gap:.55rem;align-items:center;margin-bottom:.6rem;flex-wrap:wrap;";const o=document.createElement("span");o.textContent="When",o.style.cssText="font-size:.73rem;color:var(--dm-text-muted);flex-shrink:0;";const l=document.createElement("select");l.className="form-select fb-logic-cond-field",l.style.cssText="flex:1 1 140px;min-width:140px;",t.forEach(f=>{const y=document.createElement("option");y.value=f.value,y.textContent=f.label,e&&f.value===e.field&&(y.selected=!0),l.appendChild(y)});const d=document.createElement("select");d.className="form-select fb-logic-cond-op",d.style.cssText="flex:1 1 160px;min-width:140px;",Ne.forEach(f=>{const y=document.createElement("option");y.value=f.value,y.textContent=f.label,e&&f.value===e.operator&&(y.selected=!0),d.appendChild(y)});const r=()=>{const f=t.find(y=>y.value===l.value);Array.from(d.options).forEach(y=>{const C=Ne.find(x=>x.value===y.value);y.textContent=f?.multi&&Ft[y.value]||C?.label||y.value})};r(),l.addEventListener("change",r);const u=document.createElement("input");u.type="text",u.className="form-input fb-logic-cond-val",u.placeholder="value",u.style.cssText="flex:1 1 120px;min-width:100px;",u.value=e?.value||"",e&&Ye.has(e.operator)&&(u.style.display="none"),d.addEventListener("change",()=>{u.style.display=Ye.has(d.value)?"none":""});const p=document.createElement("span");O(p,"arrow-right"),p.style.cssText="font-size:.73rem;color:var(--dm-text-muted);flex-shrink:0;";const m=document.createElement("select");m.className=`form-select ${n}`,m.style.cssText="flex:1 1 140px;min-width:120px;",s.forEach(f=>{const y=document.createElement("option");y.value=f.value,y.textContent=f.label,f.value===a&&(y.selected=!0),m.appendChild(y)});const v=document.createElement("button");return v.type="button",v.className="btn btn-xs btn-ghost",v.style.cssText="line-height:1;opacity:.6;",O(v,"x"),v.style.flexShrink="0",v.addEventListener("click",()=>i.remove()),i.appendChild(o),i.appendChild(L("Field",l,{help:"The answer this rule tests. Select another named field from the form."})),i.appendChild(L("Comparison",d,{help:"How to compare the answer. Multi-choice fields test their selected values. Empty and not empty do not need a value."})),i.appendChild(L("Value",u,{help:"The stored value to compare against. For one of, enter comma-separated values. For regex, enter a regular expression."})),i.appendChild(p),i.appendChild(L("Result",m,{help:"What happens to this field when the condition matches: change its visibility or whether an answer is required."})),i.appendChild(v),i}function On(e,t,s){const n=document.createElement("div");n.dataset.logicSection="visibility",n.appendChild(qe("Visibility"));const a=document.createElement("div");a.style.cssText="display:flex;align-items:center;gap:.6rem;margin-bottom:.65rem;flex-wrap:wrap;";const i=document.createElement("span");i.textContent="Default:",i.style.cssText="font-size:.8rem;flex-shrink:0;";const o=document.createElement("select");o.className="form-select fb-logic-vis-default",o.style.cssText="max-width:240px;",[{value:"visible",label:"Visible"},{value:"hidden",label:"Hidden"}].forEach(x=>{const b=document.createElement("option");b.value=x.value,b.textContent=x.label,x.value===(e.default||"visible")&&(b.selected=!0),o.appendChild(b)}),i.appendChild(A("Whether the field is visible before any rule matches.","Default visibility")),a.appendChild(i),a.appendChild(o),n.appendChild(a);const l=document.createElement("div");l.style.cssText="display:flex;align-items:center;gap:.6rem;margin-bottom:.65rem;flex-wrap:wrap;";const d=document.createElement("span");d.textContent="Transition:",d.style.cssText="font-size:.8rem;flex-shrink:0;";const r=document.createElement("select");r.className="form-select fb-logic-vis-transition",r.style.cssText="max-width:240px;",[{value:"none",label:"None (instant)"},{value:"fade",label:"Fade"},{value:"slide",label:"Slide"},{value:"scale",label:"Scale"}].forEach(x=>{const b=document.createElement("option");b.value=x.value,b.textContent=x.label,x.value===(e.transition||"none")&&(b.selected=!0),r.appendChild(b)}),d.appendChild(A("The animation used when this field appears or disappears. None switches instantly.","Transition")),l.appendChild(d),l.appendChild(r),n.appendChild(l);const u=document.createElement("div");u.style.cssText="display:flex;align-items:center;gap:.6rem;margin-bottom:.65rem;flex-wrap:wrap;";const p=document.createElement("span");p.textContent="Speed:",p.style.cssText="font-size:.8rem;flex-shrink:0;";const m=document.createElement("select");m.className="form-select fb-logic-vis-duration",m.style.cssText="max-width:240px;",[{value:"",label:"Default"},{value:"120",label:"Very fast (120ms)"},{value:"200",label:"Fast (200ms)"},{value:"350",label:"Medium (350ms)"},{value:"600",label:"Slow (600ms)"},{value:"1000",label:"Very slow (1s)"}].forEach(x=>{const b=document.createElement("option");b.value=x.value,b.textContent=x.label,x.value===String(e.duration??"")&&(b.selected=!0),m.appendChild(b)}),p.appendChild(A("How long the visibility animation lasts. Default uses the effect timing; shorter times are faster.","Speed")),u.appendChild(p),u.appendChild(m),n.appendChild(u);const v=()=>{u.style.display=r.value==="none"?"none":""};v(),r.addEventListener("change",v);const f=document.createElement("div");f.className="fb-logic-vis-rules";const y=s.map(x=>({value:x.name,label:x.label||x.name,multi:Je(x)})),C=[{value:"visible",label:"Show"},{value:"hidden",label:"Hide"}];if((e.conditions||[]).forEach(x=>{const b=(x.when?.all||x.when?.any||[])[0],w=x.then==="hidden"?"hidden":"visible";y.length>0&&f.appendChild($e(b,y,C,"fb-logic-vis-then",w))}),n.appendChild(f),y.length>0){const x=document.createElement("button");x.type="button",x.className="btn btn-xs btn-ghost",x.style.cssText="font-size:.73rem;margin-top:.35rem;",be(x,"plus","Add visibility rule"),x.addEventListener("click",()=>f.appendChild($e(null,y,C,"fb-logic-vis-then","visible"))),n.appendChild(x)}return n}function Rn(e,t,s){const n=document.createElement("div");n.dataset.logicSection="requirement",n.appendChild(qe("Conditional Requirement"));const a=document.createElement("label");a.style.cssText="display:flex;align-items:center;gap:.5rem;font-size:.8rem;cursor:pointer;margin-bottom:.65rem;";const i=document.createElement("input");i.type="checkbox",i.className="fb-logic-req-default",i.checked=e.default===!0,a.appendChild(i),a.appendChild(document.createTextNode("Required by default")),a.appendChild(A("Whether this field requires an answer before a conditional requirement rule matches.","Required by default")),n.appendChild(a);const o=document.createElement("div");o.className="fb-logic-req-rules";const l=s.map(r=>({value:r.name,label:r.label||r.name,multi:Je(r)})),d=[{value:"true",label:"Make required"},{value:"false",label:"Make optional"}];if((e.conditions||[]).forEach(r=>{const u=(r.when?.all||r.when?.any||[])[0],p=r.then===!0?"true":"false";l.length>0&&o.appendChild($e(u,l,d,"fb-logic-req-then",p))}),n.appendChild(o),l.length>0){const r=document.createElement("button");r.type="button",r.className="btn btn-xs btn-ghost",r.style.cssText="font-size:.73rem;margin-top:.35rem;",be(r,"plus","Add requirement rule"),r.addEventListener("click",()=>o.appendChild($e(null,l,d,"fb-logic-req-then","true"))),n.appendChild(r)}return n}function Bt(e){const t=document.createElement("div");t.className="fb-logic-val-rule",t.style.cssText="display:flex;gap:.55rem;align-items:center;margin-bottom:.6rem;flex-wrap:wrap;";const s=document.createElement("select");s.className="form-select fb-logic-val-type",s.style.cssText="flex:0 0 140px;",[{value:"regex",label:"Regex"},{value:"match",label:"Match field"}].forEach(l=>{const d=document.createElement("option");d.value=l.value,d.textContent=l.label,l.value===(e?.type||"regex")&&(d.selected=!0),s.appendChild(d)});const n=document.createElement("input");n.type="text",n.className="form-input fb-logic-val-pattern",n.placeholder=e?.type==="match"?"field name":"pattern",n.value=e?.pattern||e?.field||"",n.style.cssText="flex:3;";const a=document.createElement("input");a.type="text",a.className="form-input fb-logic-val-flags",a.placeholder="flags",a.value=e?.flags||"",a.style.cssText="flex:0 0 55px;",e?.type==="match"&&(a.style.display="none"),s.addEventListener("change",()=>{a.style.display=s.value==="match"?"none":"",n.placeholder=s.value==="match"?"field name":"pattern"});const i=document.createElement("input");i.type="text",i.className="form-input fb-logic-val-message",i.placeholder="Error message",i.value=e?.message||"",i.style.cssText="flex:4;";const o=document.createElement("button");return o.type="button",o.className="btn btn-xs btn-ghost",o.style.cssText="line-height:1;opacity:.6;",O(o,"x"),o.style.flexShrink="0",o.addEventListener("click",()=>t.remove()),t.appendChild(L("Validation",s,{help:"Regex checks a pattern. Match field requires the same answer as another named field."})),t.appendChild(L("Pattern or field name",n,{help:"For Regex, enter a regular expression without enclosing slashes. For Match field, enter the other field name, such as password."})),t.appendChild(L("Flags",a,{help:"Optional regular expression flags, such as i for case insensitive matching. Applies only to Regex."})),t.appendChild(L("Error message",i,{help:"The explanation visitors see when the answer fails this validation rule."})),t.appendChild(o),t}function Dn(e,t,s){const n=document.createElement("div");n.dataset.logicSection="validation",n.appendChild(qe("Custom Validation"));const a=document.createElement("div");a.className="fb-logic-val-rules",(e||[]).forEach(o=>a.appendChild(Bt(o))),n.appendChild(a);const i=document.createElement("button");return i.type="button",i.className="btn btn-xs btn-ghost",i.style.cssText="font-size:.73rem;margin-top:.35rem;",be(i,"plus","Add validation rule"),i.addEventListener("click",()=>a.appendChild(Bt(null))),n.appendChild(i),n}function zn(e,t,s){const n=document.createElement("div");n.dataset.logicSection="cascade",n.appendChild(qe("Cascade Options")),n.lastChild.appendChild(A("Make this field's options depend on another field. When the source field changes, the options here are replaced by the mapping entry for its value; if nothing matches, the default options below are shown."));const a=document.createElement("div");a.style.cssText="display:flex;align-items:center;gap:.5rem;margin-bottom:.5rem;";const i=document.createElement("span");i.textContent="Source field:",i.style.cssText="font-size:.8rem;flex-shrink:0;";const o=document.createElement("select");o.className="form-select fb-logic-cascade-source",o.style.cssText="flex:1;";const l=document.createElement("option");l.value="",l.textContent="- none -",o.appendChild(l),s.forEach(m=>{const v=document.createElement("option");v.value=m.name,v.textContent=m.label||m.name,m.name===e.sourceField&&(v.selected=!0),o.appendChild(v)}),a.appendChild(i),a.appendChild(o),n.appendChild(a);const d=document.createElement("p");d.textContent='Mapping JSON - {"value":[{"value":"...","label":"..."}]}',d.appendChild(A(`Each key is a value of the source field; its list becomes this field's options when that value is picked. Example: {"uk":[{"value":"london","label":"London"}]} shows London when the source is "uk".`)),d.style.cssText="font-size:.73rem;color:var(--dm-text-muted);margin:.3rem 0 .2rem;";const r=document.createElement("textarea");r.className="form-input fb-logic-cascade-mapping",r.rows=4,r.style.cssText="font-family:monospace;",r.placeholder='{"uk": [{"value": "london", "label": "London"}]}',r.value=e.mapping?JSON.stringify(e.mapping,null,2):"",n.appendChild(d),n.appendChild(r);const u=document.createElement("p");u.textContent="Default options (one per line: value:Label)",u.appendChild(A("Shown before the source field has a value, or when its value has no mapping entry.")),u.style.cssText="font-size:.73rem;color:var(--dm-text-muted);margin:.3rem 0 .2rem;";const p=document.createElement("textarea");return p.className="form-input fb-logic-cascade-defaults",p.rows=3,p.style.cssText="font-family:monospace;",p.placeholder=`option1:Option 1
|
|
4
|
-
option2:Option 2`,
|
|
5
|
-
`),n.appendChild(u),n.appendChild(p),n}function _n(e,t){const s=e.logic||{},n=T.filter((p,m)=>m!==t&&p.type!=="page-break"&&p.type!=="spacer"),a=document.createElement("div");a.className="fb-field-logic",a.style.cssText="margin-top:.75rem;border-top:1px solid var(--dm-border);padding-top:.5rem;";const i=document.createElement("div");i.style.cssText="display:flex;align-items:center;justify-content:space-between;cursor:pointer;padding:.15rem 0;";const o=document.createElement("span");o.style.cssText="font-size:.8rem;font-weight:600;color:var(--dm-text-muted);",o.textContent="\u26A1 Conditional Logic";const l=At(s),d=document.createElement("button");d.type="button",d.className="btn btn-xs btn-ghost",O(d,l?"chevron-down":"chevron-right",12);const r=document.createElement("div");r.className="fb-logic-body",r.style.cssText="padding:.25rem 0 .25rem;"+(l?"":"display:none;"),i.addEventListener("click",()=>{const p=r.style.display==="none";r.style.display=p?"":"none",O(d,p?"chevron-down":"chevron-right",12)}),i.appendChild(o),i.appendChild(d),a.appendChild(i),r.appendChild(On(s.visibility||{},t,n)),r.appendChild(Rn(s.requirement||{},t,n)),r.appendChild(Dn(s.validation||[],t,n));const u=document.getElementById(`fb-type-${t}`)?.value||e.type;return Ue.has(u)&&r.appendChild(zn(s.cascade||{},t,n)),a.appendChild(r),a}const xe=[{value:"banner",label:"Show a banner",kind:"state",params:["message","level","position","dismissible"],placeholder:"e.g. You must be 18 or over to continue."},{value:"toast",label:"Show a toast",kind:"event",params:["message","level"],placeholder:"e.g. Thanks - carry on."},{value:"hide-fields",label:"Hide fields",kind:"state",params:["target"]},{value:"disable-fields",label:"Disable fields",kind:"state",params:["target"]},{value:"require-fields",label:"Make fields required",kind:"state",params:["target"]},{value:"block-submit",label:"Block submission",kind:"state",params:["message"],messageLabel:"Reason, shown under the button",placeholder:'Blank: "This form cannot be submitted yet."'},{value:"end-form",label:"End the form here",kind:"state",params:["message","finish","finishLabel","doneMessage","record"],messageLabel:"Message shown in place of the rest",placeholder:"e.g. Sorry - you must be 18 or over to apply."},{value:"goto-step",label:"Jump to a wizard step",kind:"event",params:["step"]},{value:"redirect",label:"Redirect after submit",kind:"event",params:["url"]},{value:"celebrate",label:"Celebrate",kind:"event",params:["theme","intensity"]},{value:"run-action",label:"Run a CMS action",kind:"event",params:["slug"]},{value:"notify",label:"Notify the admins",kind:"event",params:["message","level"],messageLabel:"Notification title",placeholder:"Blank: titled after the form",noOnce:!0,kindText:"when the form is submitted"}],Ae=[{value:"info",label:"Info"},{value:"success",label:"Success"},{value:"warning",label:"Warning"},{value:"error",label:"Error"}],It=[{value:"field-below",label:"Below this field"},{value:"field-above",label:"Above this field"},{value:"form-top",label:"Top of the form"}],Mt=[{value:"rest",label:"The rest of the form"},{value:"all",label:"Every other field"},{value:"listed",label:"Chosen fields"}],Ot=[{value:"auto",label:"Whatever is in season"},{value:"christmas",label:"Christmas"},{value:"halloween",label:"Halloween"},{value:"valentines",label:"Valentine's Day"},{value:"guy-fawkes",label:"Guy Fawkes Night"},{value:"st-patricks",label:"St Patrick's Day"},{value:"st-andrews",label:"St Andrew's Day"},{value:"st-davids",label:"St David's Day"},{value:"st-georges",label:"St George's Day"}];function Rt(e,t=!1){const s=(o,l)=>({value:o.name,label:`${o.label||o.name}${l?" (this field)":""}`,multi:Je(o)}),n=o=>o&&o.type!=="page-break"&&o.type!=="spacer"&&o.name,a=T.filter((o,l)=>l!==e&&n(o)).map(o=>s(o,!1)),i=T[e];return t&&n(i)?[s(i,!0),...a]:a}function Q(e,t,s){const n=document.createElement("select");return n.className=`form-select ${e}`,t.forEach(a=>{const i=document.createElement("option");i.value=a.value,i.textContent=a.label,a.value===s&&(i.selected=!0),n.appendChild(i)}),n}function re(e,t,s){const n=document.createElement("input");return n.type="text",n.className=`form-input ${e}`,n.value=t==null?"":String(t),s&&(n.placeholder=s),n}function Xe(e,t){const s=document.createElement("button");return s.type="button",s.className="btn btn-xs btn-ghost fb-trig-remove",t&&(s.title=t,s.setAttribute("aria-label",t)),O(s,"x"),s.addEventListener("click",n=>{n.stopPropagation(),e()}),s}function L(e,t,{full:s=!1,hint:n="",help:a=""}={}){const i=document.createElement("label");i.className=`fb-trig-f${s?" fb-trig-f--full":""}`,i.style.flex="1";const o=document.createElement("span");if(o.className="fb-trig-f-label",o.textContent=e,(a||n||ye[e])&&o.appendChild(A(a||n||ye[e],e)),i.appendChild(o),i.appendChild(t),n){const l=document.createElement("span");l.className="fb-trig-f-hint",l.textContent=n,i.appendChild(l)}return i}function we(e,t,s,n){const a=document.createElement("label");a.className="fb-trig-check";const i=document.createElement("input");return i.type="checkbox",i.className=`form-check ${e}`,i.checked=!!t,a.appendChild(i),a.appendChild(document.createTextNode(s)),a.appendChild(A(n||"Allow the visitor to close this banner while the condition holds. It can appear again when the condition becomes true again.",s)),a}function Dt(e,t){const s=document.createElement("div");s.className="fb-trig-cond-row";const n=Q("fb-trig-cond-field",t,e?.field),a=Q("fb-trig-cond-op",Ne,e?.operator),i=re("fb-trig-cond-val",e?.value,"value"),o=L("Value",i,{help:"Compare with the stored value. For one of, separate values with commas; for regex, enter a regular expression."}),l=()=>{const r=t.find(u=>u.value===n.value);Array.from(a.options).forEach(u=>{const p=Ne.find(m=>m.value===u.value);u.textContent=r?.multi&&Ft[u.value]||p?.label||u.value})},d=()=>{o.hidden=Ye.has(a.value)};return l(),d(),n.addEventListener("change",l),a.addEventListener("change",d),s.appendChild(L("Field",n,{help:"The answer to test, including this field or another named field."})),s.appendChild(L("Comparison",a,{help:"How to compare the answer. Multi-choice fields test their selected values. Empty and not empty need no comparison value."})),s.appendChild(o),s.appendChild(Xe(()=>s.remove(),"Remove condition")),s}const zt=["banner","toast","block-submit","end-form"],_t=["password","file","image","signature"],Pn="Put an answer in with {{field_name}} - e.g. Hello {{name}}. Pick one from Insert field.";function jn(){try{V()}catch{}return T.filter(e=>e&&e.name&&e.type!=="page-break"&&e.type!=="spacer"&&!_t.includes(e.type))}function Pt(e,t,s){const n=re(e,t,s),a=document.createElement("span");a.className="fb-token-row";const i=document.createElement("select");i.className="form-input fb-token-pick",i.title="Insert an answer into the message",i.setAttribute("aria-label","Insert a field's answer into the message");const o=()=>{i.replaceChildren(new Option("Insert field",""));for(const l of jn())i.appendChild(new Option(`${l.label||l.name} ({{${l.name}}})`,l.name))};return o(),i.addEventListener("focus",o),i.addEventListener("mousedown",o),i.addEventListener("change",()=>{const l=i.value;if(i.value="",!l)return;const d=`{{${l}}}`,r=n.selectionStart??n.value.length,u=n.selectionEnd??r;n.value=n.value.slice(0,r)+d+n.value.slice(u),n.focus(),n.setSelectionRange(r+d.length,r+d.length),n.dispatchEvent(new Event("input",{bubbles:!0})),n.dispatchEvent(new Event("change",{bubbles:!0}))}),a.append(n,i),a}function Hn(e){const t=window.FormLogicEngine;if(!t?.triggerTokens)return[];const s=new Map((e||[]).filter(a=>a&&a.name).map(a=>[a.name,a])),n=new Set;for(const a of e||[])for(const i of a?.triggers||[])for(const o of[...i.then||[],...i.otherwise||[]])if(zt.includes(o?.type))for(const l of[...t.triggerTokens(o.message),...t.triggerTokens(o.doneMessage)]){const d=s.get(l);(!d||_t.includes(d.type))&&n.add(l)}return[...n]}function jt(e,t){const s=document.createElement("div");s.className="fb-trig-params";const n=xe.find(d=>d.value===(e.type||"banner"));if(!n)return s;const a=document.createElement("div");a.className="fb-trig-grid";const i=document.createElement("div");i.className="fb-trig-opts";const o=[];for(const d of n.params)switch(d){case"message":a.appendChild(zt.includes(n.value)?L(n.messageLabel||"Message",Pt("fb-trig-p-message",e.message,n.placeholder||""),{full:!0,hint:Pn,help:ye[n.messageLabel||"Message"]}):L(n.messageLabel||"Message",re("fb-trig-p-message",e.message,n.placeholder||""),{full:!0}));break;case"finish":i.appendChild(we("fb-trig-p-finish-on",e.finish!==!1,"Let the visitor finish (send the answers given so far)",'Untick to stop the form dead: the submit button is disabled and nothing is sent, so nothing is saved and "Notify the admins" cannot fire.'));break;case"finishLabel":{const r=L("Button text",re("fb-trig-p-finish",e.finishLabel,"Finish"));o.push(r),a.appendChild(r);break}case"doneMessage":{const r=L("Closing message",Pt("fb-trig-p-done",e.doneMessage,"Blank: the form's own success message"));o.push(r),a.appendChild(r);break}case"record":{const r=we("fb-trig-p-record",e.record!==!1,"Save the answers given so far",`Untick to keep nothing: no submission is stored and the form's email, webhook and actions do not run. A "Notify the admins" action still fires.`);o.push(r),i.appendChild(r);break}case"level":a.appendChild(L(n.value==="notify"?"Severity":"Level",Q("fb-trig-p-level",Ae,e.level||"info")));break;case"position":a.appendChild(L("Where",Q("fb-trig-p-position",It,e.position||"field-below")));break;case"dismissible":i.appendChild(we("fb-trig-p-dismissible",e.dismissible!==!1,"The visitor can dismiss it"));break;case"target":{const r=Q("fb-trig-p-target",Mt,e.target||(e.fields?.length?"listed":"rest")),u=document.createElement("select");u.className="form-select fb-trig-p-fields",u.multiple=!0,u.size=4;const p=new Set(e.fields||[]);Rt(t).forEach(f=>{const y=document.createElement("option");y.value=f.value,y.textContent=f.label,p.has(f.value)&&(y.selected=!0),u.appendChild(y)});const m=L("Fields",u,{hint:"Ctrl/Cmd-click to pick several",help:ye.Fields}),v=()=>{m.hidden=r.value!=="listed"};v(),r.addEventListener("change",v),a.appendChild(L("Which fields",r)),a.appendChild(m);break}case"step":{const r=re("fb-trig-p-step",e.step,"0");r.type="number",r.min="0",a.appendChild(L("Step",r,{hint:"Counting from 0",help:ye.Step}));break}case"url":a.appendChild(L("Send them to",re("fb-trig-p-url",e.url,"/thank-you"),{full:!0}));break;case"theme":a.appendChild(L("Theme",Q("fb-trig-p-theme",Ot,e.theme||"auto")));break;case"intensity":a.appendChild(L("Intensity",Q("fb-trig-p-intensity",[{value:"low",label:"Low"},{value:"medium",label:"Medium"},{value:"high",label:"High"}],e.intensity||"medium")));break;case"slug":{const r=[{value:"",label:ke.length?"Choose an action":"No actions available"}].concat(ke.map(u=>({value:u.slug,label:u.title||u.slug})));a.appendChild(L("Action",Q("fb-trig-p-slug",r,e.slug||""),{full:!0,help:"The CMS action to run after a matching submission is saved. Choose an action defined under Data > Actions. Requires Pro with MongoDB."}));break}default:break}n.kind==="event"&&!n.noOnce&&i.appendChild(we("fb-trig-p-once",!!e.once,"Only once","Fire the first time only, rather than every time the answer swings back."));const l=i.querySelector(".fb-trig-p-finish-on");if(l){const d=()=>o.forEach(r=>{r.hidden=!l.checked});d(),l.addEventListener("change",d)}return a.children.length&&s.appendChild(a),i.children.length&&s.appendChild(i),s}function Vn(e){return e?.kindText?e.kindText:e?.kind==="event"?"once, when it becomes true":"while it is true"}const Wn=(e,t=48)=>e.length>t?`${e.slice(0,t-1)}\u2026`:e,ce=(e,t)=>e.find(s=>s.value===t)?.label||t;function Un(e){if(!e)return"";const t=[],s=a=>a?`"${Wn(a)}"`:"",n=()=>e.target==="listed"?e.fields?.length?e.fields.join(", "):"no fields chosen":ce(Mt,e.target||"rest").toLowerCase();switch(e.type){case"banner":t.push(s(e.message)||"No message yet",ce(Ae,e.level||"info"),ce(It,e.position||"field-below").toLowerCase());break;case"toast":t.push(s(e.message)||"No message yet",ce(Ae,e.level||"info"));break;case"hide-fields":case"disable-fields":case"require-fields":t.push(n());break;case"block-submit":t.push(s(e.message)||"the standard reason");break;case"end-form":t.push(s(e.message)||"no message"),e.finish===!1?t.push("submit disabled"):t.push(`button "${e.finishLabel||"Finish"}"`,e.record===!1?"saves nothing":"saves the answers");break;case"goto-step":t.push(Number.isFinite(e.step)?`step ${e.step}`:"no step yet");break;case"redirect":t.push(e.url||"no address yet");break;case"celebrate":t.push(ce(Ot,e.theme||"auto"),`${e.intensity||"medium"} intensity`);break;case"run-action":t.push(e.slug?ke.find(a=>a.slug===e.slug)?.title||e.slug:"no action chosen");break;case"notify":t.push(s(e.message)||"titled after the form",ce(Ae,e.level||"info"));break;default:break}return e.once&&t.push("only once"),t.filter(Boolean).join(" \xB7 ")}function Qe(e,t,{open:s=!1}={}){const n=document.createElement("div");n.className="fb-trig-action-row";const a=document.createElement("div");a.className="fb-trig-action-head",a.tabIndex=0,a.setAttribute("role","button");const i=document.createElement("span");i.className="fb-trig-chevron";const o=document.createElement("span");o.className="fb-trig-action-main";const l=document.createElement("strong"),d=document.createElement("small");d.className="fb-trig-action-summary",o.append(l,d);const r=document.createElement("span");r.className="fb-trig-kind",a.append(i,o,r,Xe(()=>n.remove(),"Remove action"));const u=document.createElement("div");u.className="fb-trig-action-body";const p=Q("fb-trig-action-type",xe,e.type||"banner");u.appendChild(L("Action",p,{full:!0})),u.appendChild(jt(e,t));const m=()=>{const f=xe.find(y=>y.value===p.value);l.textContent=f?.label||p.value,r.textContent=Vn(f),r.classList.toggle("fb-trig-kind--event",f?.kind==="event"),d.textContent=Un(Ut(n))},v=f=>{u.hidden=!f,n.classList.toggle("is-open",f),a.setAttribute("aria-expanded",String(f)),O(i,f?"chevron-down":"chevron-right",12)};return a.addEventListener("click",()=>v(u.hidden)),a.addEventListener("keydown",f=>{f.target!==a||f.key!=="Enter"&&f.key!==" "||(f.preventDefault(),v(u.hidden))}),p.addEventListener("change",()=>{const f=jt({type:p.value},t);u.replaceChild(f,u.querySelector(".fb-trig-params")),m(),window.I?.scan?.()}),u.addEventListener("input",m),u.addEventListener("change",m),n.append(a,u),v(s),m(),n}function Ht(e,t,s){const n=document.createElement("section");n.className=`fb-trig-sec fb-trig-sec--${e}`;const a=document.createElement("div");a.className="fb-trig-sec-head";const i=document.createElement("span");if(i.className="fb-trig-sec-title",i.textContent=t,i.appendChild(A({When:"Choose the answers to test. All requires every condition to match; Any requires at least one. Compare stored option values, not their display labels.",Then:"Actions applied when the conditions match.",Otherwise:"Optional actions applied when the conditions do not match. Leave empty to do nothing in this branch."}[t],t)),a.appendChild(i),s){const o=document.createElement("span");o.className="fb-trig-sec-hint",o.textContent=s,a.appendChild(o)}return n.appendChild(a),n}function Ze(e,t){const s=document.createElement("button");return s.type="button",s.className="btn btn-xs btn-ghost fb-trig-add",be(s,"plus",e,12),s.addEventListener("click",t),s}function Vt(e,t,s,n){const i=Ht(e,t,e==="then"?"What happens while the answers match.":"What happens while they do not. Leave empty for nothing.");i.classList.add("fb-trig-branch"),i.dataset.branch=e;const o=document.createElement("div");return o.className="fb-trig-action-list",(s||[]).forEach(l=>o.appendChild(Qe(l,n))),i.appendChild(o),i.appendChild(Ze("Add action",()=>{o.appendChild(Qe({type:"banner"},n,{open:!0})),window.I?.scan?.()})),i}function Wt(e,t,s){const n=Rt(t,!0),a=document.createElement("div");a.className="fb-trig-card",a.dataset.triggerId=e.id||`trg-${Date.now()}-${Math.random().toString(36).slice(2,7)}`;const i=document.createElement("div");i.className="fb-trig-card-head";const o=document.createElement("span");o.className="fb-trig-card-title",o.textContent=`Trigger ${s+1}`,i.appendChild(o);const l=we("fb-trig-enabled",e.enabled!==!1,"Enabled","Untick to keep the trigger but switch it off.");if(i.appendChild(l),i.appendChild(Xe(()=>a.remove(),"Remove trigger")),a.appendChild(i),!n.length){const p=document.createElement("p");return p.className="fb-trig-empty",p.textContent="Give this field a name first - a trigger tests an answer, and it needs something to test.",a.appendChild(p),a}const d=Ht("when","When");d.querySelector(".fb-trig-sec-head").appendChild(Q("fb-trig-match",[{value:"all",label:"all of these are true"},{value:"any",label:"any of these is true"}],Array.isArray(e.when?.any)?"any":"all"));const r=document.createElement("div");r.className="fb-trig-cond-list";const u=e.when?.all||e.when?.any||[];return(u.length?u:[{}]).forEach(p=>r.appendChild(Dt(p,n))),d.appendChild(r),d.appendChild(Ze("Add condition",()=>{r.appendChild(Dt({},n)),window.I?.scan?.()})),a.appendChild(d),a.appendChild(Vt("then","Then",e.then,t)),a.appendChild(Vt("otherwise","Otherwise",e.otherwise,t)),a}function Gn(e,t){const s=Array.isArray(e.triggers)?e.triggers:[],n=document.createElement("div");n.className="fb-field-triggers";const a=document.createElement("div");a.className="fb-trig-panel-head";const i=document.createElement("span");i.className="fb-trig-panel-title",i.textContent=`\u{1F3AF} Triggers${s.length?` (${s.length})`:""}`,i.appendChild(A("Triggers test answers and choose a Then or Otherwise branch. Add conditions and actions below. Enabled switches a trigger on without deleting its settings.","Triggers"));const o=document.createElement("button");o.type="button",o.className="btn btn-xs btn-ghost",O(o,s.length?"chevron-down":"chevron-right",12);const l=document.createElement("div");l.className="fb-triggers-body",l.hidden=!s.length,a.addEventListener("click",()=>{l.hidden=!l.hidden,O(o,l.hidden?"chevron-right":"chevron-down",12)});const d=document.createElement("p");d.className="fb-trig-blurb",d.textContent="Do something to the form when the answers meet a condition - show a banner or toast, hide, disable or require other fields, refuse the submit, end the form early, notify the admins, jump to a step, or celebrate.",l.appendChild(d);const r=document.createElement("div");return r.className="fb-trig-list",s.forEach((u,p)=>r.appendChild(Wt(u,t,p))),l.appendChild(r),l.appendChild(Ze("Add trigger",()=>{const u=Wt({enabled:!0,then:[]},t,r.children.length);r.appendChild(u),u.querySelector('.fb-trig-branch[data-branch="then"] .fb-trig-action-list')?.appendChild(Qe({type:"banner"},t,{open:!0})),window.I?.scan?.()})),a.appendChild(i),a.appendChild(o),n.appendChild(a),n.appendChild(l),n}function Ut(e){const t=e.querySelector(".fb-trig-action-type")?.value,s=xe.find(i=>i.value===t);if(!s)return null;const n={type:t},a=i=>e.querySelector(i)?.value?.trim()||"";for(const i of s.params)switch(i){case"message":n.message=a(".fb-trig-p-message");break;case"finishLabel":{const o=a(".fb-trig-p-finish");o&&(n.finishLabel=o);break}case"doneMessage":{const o=a(".fb-trig-p-done");o&&(n.doneMessage=o);break}case"finish":e.querySelector(".fb-trig-p-finish-on")?.checked||(n.finish=!1);break;case"record":e.querySelector(".fb-trig-p-record")?.checked||(n.record=!1);break;case"level":n.level=a(".fb-trig-p-level")||"info";break;case"position":n.position=a(".fb-trig-p-position")||"field-below";break;case"dismissible":n.dismissible=!!e.querySelector(".fb-trig-p-dismissible")?.checked;break;case"target":{const o=a(".fb-trig-p-target")||"rest";n.target=o,o==="listed"&&(n.fields=Array.from(e.querySelectorAll(".fb-trig-p-fields option")).filter(l=>l.selected).map(l=>l.value));break}case"step":{const o=parseInt(a(".fb-trig-p-step"),10);Number.isFinite(o)&&(n.step=o);break}case"url":n.url=a(".fb-trig-p-url");break;case"theme":n.theme=a(".fb-trig-p-theme")||"auto";break;case"intensity":n.intensity=a(".fb-trig-p-intensity")||"medium";break;case"slug":n.slug=a(".fb-trig-p-slug");break;default:break}return s.kind==="event"&&!s.noOnce&&e.querySelector(".fb-trig-p-once")?.checked&&(n.once=!0),n}function Kn(e){const t=Ut(e);if(!t)return null;const s=xe.find(i=>i.value===t.type),n=t.type,a=["block-submit","end-form","notify"].includes(s.value);return s.params.includes("message")&&!t.message&&!a?(delete t.message,null):(a&&!t.message&&delete t.message,n==="redirect"&&!t.url||n==="run-action"&&!t.slug?null:t)}function Yn(e){const s=document.querySelector(`.fb-field-card[data-index="${e}"]`)?.querySelector(".fb-field-triggers");if(!s)return;const n=[];return s.querySelectorAll(".fb-trig-card").forEach(a=>{const i=Array.from(a.querySelectorAll(".fb-trig-cond-row")).map(p=>{const m=p.querySelector(".fb-trig-cond-field")?.value,v=p.querySelector(".fb-trig-cond-op")?.value;if(!m||!v)return null;const f=p.querySelector(".fb-trig-cond-val")?.value??"";return{field:m,operator:v,value:f}}).filter(Boolean);if(!i.length)return;const o=p=>Array.from(a.querySelectorAll(`.fb-trig-branch[data-branch="${p}"] .fb-trig-action-row`)).map(Kn).filter(Boolean),l=o("then"),d=o("otherwise");if(!l.length&&!d.length)return;const r=a.querySelector(".fb-trig-match")?.value==="any"?"any":"all",u={id:a.dataset.triggerId,enabled:!!a.querySelector(".fb-trig-enabled")?.checked,when:{[r]:i}};l.length&&(u.then=l),d.length&&(u.otherwise=d),n.push(u)}),n.length?n:void 0}function Gt(e,t,s){const n=document.createElement("div");if(n.className="fb-field-extras",e==="chooser")n.appendChild(Qn(t,s)),n.appendChild(ea(t.options||[],s));else if(Ue.has(e)&&n.appendChild(sa(t.options||[],s)),e==="textarea"&&n.appendChild(z([P("Rows",`fb-rows-${s}`,"number",t.formConfig?.rows||4,"Height of textarea")])),(e==="string"||e==="textarea")&&n.appendChild(z([P("Min Length",`fb-minlength-${s}`,"number",t.minLength||"",""),P("Max Length",`fb-maxlength-${s}`,"number",t.maxLength||"","")])),e==="number"&&n.appendChild(z([P("Min",`fb-min-${s}`,"number",t.min??"",""),P("Max",`fb-max-${s}`,"number",t.max??"","")])),e==="file"){const a=P("Most files",`fb-file-max-${s}`,"number",t.file?.maxFiles||"",`${gt} when left empty, ${He} at most`),i=a.querySelector("input");i.min="1",i.max=String(He),i.placeholder=String(gt);const o=Ie("Several files",`fb-file-multiple-${s}`,!!t.multiple),l=()=>{a.hidden=!o.querySelector("input").checked};o.querySelector("input").addEventListener("change",l),l(),n.appendChild(z([a,o]))}return Kt.includes(e)&&n.appendChild(Xn(t,s)),n.children.length?n:null}const Kt=["string","email","tel","number","url","date","time","password","select","textarea"],et=[{key:"reveal",label:"Show / hide button",types:["password"],hint:"Show / hide lets the visitor see what they typed."},{key:"clear",label:"Clear button",types:["string","email","tel","url","textarea"],hint:"Clear adds an x that empties the field."},{key:"stepper",label:"- / + buttons",types:["number"],hint:"The - and + buttons step the number within its min, max and step."},{key:"counter",label:"Character counter",types:["string","textarea"],hint:"The counter shows how many characters are used, against Max Length if one is set."}];function Jn(e){return e?typeof e=="string"?{icon:"",text:e}:{icon:e.icon||"",text:e.icon?"":e.text??""}:{icon:"",text:""}}function Xn(e,t){const s=document.createElement("div");s.className="fb-addons";const n=(o,l)=>{const d=Jn(e.formConfig?.[o]),r=document.createElement("div");r.style.flex="1";const u=document.createElement("span");u.className="form-label",u.style.fontSize=".8rem",u.textContent=l,u.appendChild(A("An icon or short text joined to this side of the input, such as a currency symbol or unit. Choose an icon or enter text; leave both blank for none.",l));const p=je("Icon, e.g. search",d.icon);p.input.id=`fb-${o}-icon-${t}`,p.input.setAttribute("aria-label",`${l}: icon`);const m=document.createElement("input");return m.type="text",m.className="form-input",m.id=`fb-${o}-text-${t}`,m.value=d.text,m.placeholder="or text, e.g. \xA3 or kg",m.maxLength=24,m.setAttribute("aria-label",`${l}: text`),m.style.marginTop=".35rem",p.input.addEventListener("input",()=>{p.input.value.trim()&&(m.value="")}),m.addEventListener("input",()=>{m.value.trim()&&(p.input.value="")}),r.append(u,p.el,m),r};s.appendChild(z([n("prefix","Before the field"),n("suffix","After the field")]));const a=document.createElement("p");a.className="form-hint text-muted",a.style.cssText="font-size:.73rem;margin:-.4rem 0 .75rem;",a.textContent="An icon or a short text joined to the start or end of the input, like \xA3 before a price or kg after a weight. Leave both empty for none.",s.appendChild(a);const i=et.filter(o=>o.types.includes(e.type));if(i.length){s.appendChild(z(i.map(l=>Ie(l.label,`fb-extra-${l.key}-${t}`,!!e.formConfig?.[l.key]))));const o=document.createElement("p");o.className="form-hint text-muted",o.style.cssText="font-size:.73rem;margin:-.4rem 0 .75rem;",o.textContent=i.map(l=>l.hint).join(" "),s.appendChild(o)}return s}function Yt(e){const t=document.createElement("div");t.className="fb-chooser-section",t.style.cssText="border:1px solid var(--dm-border); border-radius:6px; margin-top:.5rem; overflow:hidden;";const s=document.createElement("div");s.style.cssText="padding:.4rem .6rem; background:color-mix(in srgb, var(--dm-background) 60%, transparent); font-weight:600; font-size:.82rem; border-bottom:1px solid var(--dm-border);",s.textContent=e,t.appendChild(s);const n=document.createElement("div");return n.className="fb-chooser-section-body",n.style.cssText="padding:.55rem .6rem; display:flex; flex-direction:column; gap:.45rem;",t.appendChild(n),{wrap:t,body:n}}function Fe(e,t,s,n,a){const i=document.createElement("div");i.style.flex="1";const o=document.createElement("label");o.htmlFor=e,o.className="form-label",o.textContent=t,a&&o.appendChild(A(a,t)),o.style.fontSize=".8rem";const l=document.createElement("select");if(l.id=e,l.className="form-input",n.forEach(([d,r])=>{const u=document.createElement("option");u.value=d,u.textContent=r,String(d)===String(s)&&(u.selected=!0),l.appendChild(u)}),i.appendChild(o),i.appendChild(l),a){const d=document.createElement("p");d.className="form-hint text-muted",d.textContent=a,d.style.cssText="font-size:.72rem;margin-top:.2rem;",i.appendChild(d)}return i}function Jt(e,t,s,n){const a=document.createElement("div");a.style.flex="1";const i=document.createElement("label");i.style.cssText="display:inline-flex;align-items:center;gap:.4rem;font-size:.82rem;";const o=document.createElement("input");if(o.id=e,o.type="checkbox",o.checked=!!s,i.appendChild(o),i.appendChild(document.createTextNode(" "+t)),n&&i.appendChild(A(n,t)),a.appendChild(i),n){const l=document.createElement("p");l.className="form-hint text-muted",l.textContent=n,l.style.cssText="font-size:.72rem;margin-top:.2rem;",a.appendChild(l)}return a}function tt(e,t,s,n,a){const i=document.createElement("div");i.style.flex="1";const o=document.createElement("label");o.htmlFor=e,o.className="form-label",o.textContent=t,a&&o.appendChild(A(a,t)),o.style.fontSize=".8rem",i.appendChild(o);const l=document.createElement("div");l.style.cssText="display:flex;gap:.4rem;align-items:stretch;";const d=document.createElement("input");d.id=e,d.type="text",d.className="form-input",d.value=s||"",n&&(d.placeholder=n),d.style.flex="1";const r=document.createElement("input");r.type="color",r.title="Pick a colour",r.style.cssText="width:36px;height:auto;padding:0;border:1px solid var(--dm-border);border-radius:4px;cursor:pointer;";const u=String(d.value).match(/^#[0-9a-fA-F]{3,8}$/);if(r.value=u?d.value:"#000000",r.addEventListener("input",()=>{d.value=r.value,d.dispatchEvent(new Event("input",{bubbles:!0}))}),l.appendChild(d),l.appendChild(r),i.appendChild(l),a){const p=document.createElement("p");p.className="form-hint text-muted",p.textContent=a,p.style.cssText="font-size:.72rem;margin-top:.2rem;",i.appendChild(p)}return i}function Qn(e,t){const s=document.createElement("div");s.style.cssText="margin-top:.5rem; display:flex; flex-direction:column; gap:.4rem;";const n=Yt("Layout");n.body.appendChild(z([Fe(`fb-chooser-variant-${t}`,"Variant",e.variant||"card",[["card","Card - rich tiles"],["chip","Chip - compact pills"]],"Card supports description and grid columns; chips wrap."),Jt(`fb-chooser-multiple-${t}`,"Multi-select",!!e.multiple,"Off = single (radio); on = multi (checkbox).")])),n.body.appendChild(z([Fe(`fb-chooser-density-${t}`,"Density",e.density||"comfortable",[["comfortable","Comfortable"],["compact","Compact"]],"Compact strips description (card variant only)."),P("Columns",`fb-chooser-columns-${t}`,"number",e.columns||3,"Card grid (1-6). Ignored for chips.")])),s.appendChild(n.wrap);const a=Yt("Visual");return a.body.appendChild(z([tt(`fb-chooser-accent-${t}`,"Accent",e.accent||"","primary / success / #ec4899","Selected highlight colour - semantic name or any CSS colour."),Fe(`fb-chooser-accent-style-${t}`,"Accent style",e.accentStyle||"border",[["border","Border (default)"],["solid","Solid"],["glow","Glow"],["overlay","Overlay"],["underline","Underline"]],"Visual treatment of the selected state.")])),a.body.appendChild(z([Jt(`fb-chooser-glow-${t}`,"Glow on selected",!!e.glow,"Soft outer glow."),tt(`fb-chooser-glow-colour-${t}`,"Glow colour",e.glowColour||"","defaults to accent","Optional; semantic name or CSS colour.")])),a.body.appendChild(z([Fe(`fb-chooser-shadow-${t}`,"Shadow",e.shadow||"none",[["none","None"],["sm","Small"],["md","Medium"],["lg","Large"],["xl","Extra Large"]],"Drop shadow on every option."),tt(`fb-chooser-shadow-colour-${t}`,"Shadow colour",e.shadowColour||"","rgba(0,0,0,0.1)","Optional shadow tint.")])),s.appendChild(a.wrap),s}function Zn(e,t,s,n){const a=document.createElement("div");a.className="fb-chooser-option-row",a.dataset.optIdx=String(s),a.style.cssText="border:1px solid var(--dm-border); border-radius:4px; padding:.5rem; background:var(--dm-background-alt); display:flex; flex-direction:column; gap:.35rem;";const i=document.createElement("div");i.style.cssText="display:flex; gap:.4rem; align-items:center;";const o=document.createElement("div");o.style.cssText="display:flex; flex-direction:column; gap:1px;";const l=document.createElement("button");l.type="button",l.className="btn btn-xs btn-ghost",l.title="Move up",O(l,"arrow-up"),l.addEventListener("click",j=>{j.preventDefault(),Xt(t,s,-1,n)});const d=document.createElement("button");d.type="button",d.className="btn btn-xs btn-ghost",d.title="Move down",O(d,"arrow-down"),d.addEventListener("click",j=>{j.preventDefault(),Xt(t,s,1,n)}),o.appendChild(l),o.appendChild(d),i.appendChild(o);const r=document.createElement("input");r.type="text",r.className="form-input fb-chooser-opt-value",r.placeholder="value",r.value=e.value||"",r.style.flex="1",i.appendChild(L("Value",r,{help:"The value stored with the submission and compared by trigger conditions. Keep it unique within this field."}));const u=document.createElement("input");u.type="text",u.className="form-input fb-chooser-opt-label",u.placeholder="label (visible to users)",u.value=e.label||"",u.style.flex="2",i.appendChild(L("Label",u,{help:"The choice text visitors see. The stored value can be different."}));const p=document.createElement("button");p.type="button",p.className="btn btn-xs btn-ghost",p.style.cssText="line-height:1;opacity:.6;",p.title="Remove option",O(p,"x"),p.addEventListener("click",j=>{j.preventDefault(),aa(t,s,n)}),i.appendChild(p),a.appendChild(i);const m=document.createElement("div");m.style.cssText="display:flex; gap:.4rem;";const v=je("icon name (optional)",e.icon||"");v.input.classList.add("fb-chooser-opt-icon"),v.el.style.flex="1",m.appendChild(L("Icon",v.el,{help:"An optional icon for this choice. Browse for an icon or leave blank for none."}));const f=document.createElement("input");f.type="text",f.className="form-input fb-chooser-opt-description",f.placeholder="description (card + comfortable only)",f.value=e.description||"",f.style.flex="2",m.appendChild(L("Description",f,{help:"Extra explanation shown by the card variant at comfortable density."})),a.appendChild(m);const y=document.createElement("div");y.style.cssText="display:flex; gap:.4rem;";const C=document.createElement("input");C.type="text",C.className="form-input fb-chooser-opt-tooltip",C.placeholder="tooltip (hover hint)",C.value=e.tooltip||"",C.style.flex="2",y.appendChild(L("Tooltip",C,{help:"An optional hover hint for this choice."}));const x=document.createElement("input");x.type="text",x.className="form-input fb-chooser-opt-badge-text",x.placeholder="badge text",x.value=e.badge?.text||"",x.style.flex="1",y.appendChild(L("Badge text",x,{help:"A short badge label attached to this choice. Leave blank for no badge."}));const b=document.createElement("select");b.className="form-input fb-chooser-opt-badge-type",b.style.flex="1",[["","no badge"],["primary","primary"],["success","success"],["info","info"],["warning","warning"],["danger","danger"]].forEach(([j,Y])=>{const U=document.createElement("option");U.value=j,U.textContent=Y,(e.badge?.type||"")===j&&(U.selected=!0),b.appendChild(U)}),y.appendChild(L("Badge style",b,{help:"The theme colour used for the badge."})),a.appendChild(y);const w=document.createElement("div");w.style.cssText="display:flex; gap:1rem; font-size:.8rem;";const q=document.createElement("label");q.style.cssText="display:inline-flex;align-items:center;gap:.3rem;";const B=document.createElement("input");B.type="checkbox",B.className="fb-chooser-opt-recommended",B.checked=!!e.recommended,q.appendChild(B),q.appendChild(document.createTextNode(" Recommended")),q.appendChild(A("Highlight this choice as recommended. This does not select it automatically.","Recommended")),w.appendChild(q);const _=document.createElement("label");_.style.cssText="display:inline-flex;align-items:center;gap:.3rem;";const W=document.createElement("input");return W.type="checkbox",W.className="fb-chooser-opt-disabled",W.checked=!!e.disabled,_.appendChild(W),_.appendChild(document.createTextNode(" Disabled")),_.appendChild(A("Show this choice but prevent the visitor from selecting it.","Disabled")),w.appendChild(_),a.appendChild(w),a}function ea(e,t){const s=document.createElement("div");s.style.cssText="margin-top:.5rem;";const n=document.createElement("div");n.style.cssText="display:flex; align-items:center; justify-content:space-between; margin-bottom:.4rem;";const a=document.createElement("p");a.textContent="Options",a.appendChild(A("Each option has a stored value and a label shown to visitors. Keep values unique and use them in trigger conditions. Add an icon, description, tooltip or badge to explain a choice. Recommended highlights an option; Disabled prevents selection.","Chooser options")),a.style.cssText="font-size:.85rem;font-weight:600;margin:0;",n.appendChild(a);const i=document.createElement("button");i.type="button",i.className="btn btn-xs btn-ghost",be(i,"plus","Add option"),n.appendChild(i),s.appendChild(n);const o=document.createElement("div");o.className="fb-chooser-options-list",o.id=`fb-chooser-options-${t}`,o.style.cssText="display:flex; flex-direction:column; gap:.4rem;",s.appendChild(o);const l=()=>{o.replaceChildren();const d=ta(t);(d.length?d:e||[]).forEach((u,p)=>{o.appendChild(Zn(u,t,p,l))})};return i.addEventListener("click",d=>{d.preventDefault(),na(t,l)}),l(),s}function ta(e){const t=T[e];return!t||!Array.isArray(t.options)?[]:t.options}function nt(e){return T[e]?(Array.isArray(T[e].options)||(T[e].options=[]),T[e].options):[]}function na(e,t){Be(e),nt(e).push({value:"new",label:"New option"}),t()}function aa(e,t,s){Be(e),nt(e).splice(t,1),s()}function Xt(e,t,s,n){Be(e);const a=nt(e),i=t+s;if(i<0||i>=a.length)return;const[o]=a.splice(t,1);a.splice(i,0,o),n()}function Be(e){const t=document.getElementById(`fb-chooser-options-${e}`);if(!t)return;const s=t.querySelectorAll(".fb-chooser-option-row"),n=[];s.forEach(a=>{const i={value:a.querySelector(".fb-chooser-opt-value")?.value.trim()||"",label:a.querySelector(".fb-chooser-opt-label")?.value.trim()||""},o=a.querySelector(".fb-chooser-opt-icon")?.value.trim();o&&(i.icon=o);const l=a.querySelector(".fb-chooser-opt-description")?.value.trim();l&&(i.description=l);const d=a.querySelector(".fb-chooser-opt-tooltip")?.value.trim();d&&(i.tooltip=d);const r=a.querySelector(".fb-chooser-opt-badge-text")?.value.trim(),u=a.querySelector(".fb-chooser-opt-badge-type")?.value;(r||u)&&(i.badge={},r&&(i.badge.text=r),u&&(i.badge.type=u)),a.querySelector(".fb-chooser-opt-recommended")?.checked&&(i.recommended=!0),a.querySelector(".fb-chooser-opt-disabled")?.checked&&(i.disabled=!0),n.push(i)}),T[e]&&(T[e].options=n)}function z(e){const t=document.createElement("div");return t.style.cssText="display:flex;gap:.85rem;margin-bottom:.75rem;",e.forEach(s=>{s&&t.appendChild(s)}),t}function P(e,t,s,n,a){const i=document.createElement("div");i.style.flex="1";const o=document.createElement("label");o.htmlFor=t,o.className="form-label",o.textContent=e,o.style.fontSize=".8rem",(ge[e]||a)&&o.appendChild(A(ge[e]||a,e));const l=document.createElement("input");if(l.id=t,l.type=s||"text",l.className="form-input",l.value=n??"",i.appendChild(o),i.appendChild(l),a){const d=document.createElement("p");d.className="form-hint text-muted",d.textContent=a,d.style.cssText="font-size:.73rem;margin-top:.35rem;",i.appendChild(d)}return i}function ia(e,t,s,n){const a=document.createElement("div");a.style.flex="1";const i=document.createElement("label");i.htmlFor=t,i.className="form-label",i.textContent=e,i.style.fontSize=".8rem",ge[e]&&i.appendChild(A(ge[e],e));const o=document.createElement("select");return o.id=t,o.className="form-select",s.forEach(l=>{const d=document.createElement("option");d.value=l.value,d.textContent=l.label,l.value===n&&(d.selected=!0),o.appendChild(d)}),a.appendChild(i),a.appendChild(o),a}function Ie(e,t,s){const n=document.createElement("div");n.style.cssText="flex:0;min-width:80px;display:flex;flex-direction:column;justify-content:flex-end;";const a=document.createElement("label");a.style.cssText="display:flex;align-items:center;gap:.4rem;cursor:pointer;font-size:.8rem;white-space:nowrap;";const i=document.createElement("input");return i.id=t,i.type="checkbox",i.checked=s,a.appendChild(i),a.appendChild(document.createTextNode(e)),a.appendChild(A(ge[e]||et.find(o=>o.label===e)?.hint||e,e)),n.appendChild(a),n}function sa(e,t){const s=document.createElement("div");s.style.cssText="margin-top:.4rem;";const n=document.createElement("p");n.textContent="Options (one per line: value or value:Label)",n.style.cssText="font-size:.8rem;font-weight:600;margin-bottom:.3rem;";const a=document.createElement("textarea");return a.id=`fb-options-${t}`,a.className="form-input",a.rows=4,a.placeholder=`yes:Yes
|
|
1
|
+
import{apiRequest as fe}from"/admin/js/api.js";import{makeIconInput as je}from"/admin/js/lib/shortcode-modal.js";import{enhancePickerFields as bt}from"/public/js/forms.js";import{buildWizardSteps as dn}from"/public/js/form-steps.mjs";import{DEFAULT_MAX_FILES as gt,MAX_FILES_LIMIT as He,captureFileFields as yt,destroyDropzones as vt,fileBlueprint as pn,restoreFileFields as un,splitFiles as mn,submitBody as fn,wireFileFields as hn}from"/public/js/form-files.mjs";import{applyFormClasses as xt,applySubmitButtons as bn,applyWizardButtons as gn,buttonSettingsProblems as yn,formClassProblems as vn,resolveSubmitAlign as wt}from"/public/js/form-submit.mjs";import{addBannerAction as Ve}from"../lib/plugin-chrome.js";import{helpPopovers as xn}from"../lib/help-popover.js";import{openMenuAt as Ct,sidebarCount as wn}from"../lib/tool-kit.js";import{draftBaseOf as Cn,draftChoice as En,draftKey as he,draftOf as kn,duplicateField as Sn,editorDraftQuestion as Tn,fieldTitle as We,fingerprint as Et,leavingEditor as kt,moveField as St,restoreDraft as Ln,snapshotOf as Ee}from"../lib/form-editor-arrange.js";import{fmtDateTime as Nn}from"../lib/dates.js";const Tt=[{value:"string",label:"Text (single line)"},{value:"email",label:"Email"},{value:"tel",label:"Phone"},{value:"number",label:"Number"},{value:"textarea",label:"Textarea (multi-line)"},{value:"select",label:"Dropdown (select)"},{value:"radio",label:"Radio buttons"},{value:"checkbox",label:"Single checkbox"},{value:"checkbox-group",label:"Checkbox group"},{value:"chooser",label:"Chooser (visual options)"},{value:"icon",label:"Icon (icon picker)"},{value:"image",label:"Image (media library)"},{value:"date",label:"Date"},{value:"time",label:"Time"},{value:"url",label:"URL"},{value:"password",label:"Password"},{value:"file",label:"File upload"},{value:"hidden",label:"Hidden field"}],Ue=new Set(["select","radio","checkbox-group","chooser"]);let T=[],ke=[],I=null,Se=null,oe=null,Te={};function O(e,t,s=14){e.textContent="";const n=document.createElement("span");return n.setAttribute("data-icon",t),n.setAttribute("data-icon-size",String(s)),n.style.cssText="display:inline-flex;align-items:center;vertical-align:middle;",e.appendChild(n),window.I?.scan?.(e),e}function be(e,t,s,n=14){return O(e,t,n),e.appendChild(document.createTextNode(" "+s)),e}function B(e,t=""){const s=document.createElement("span");return s.setAttribute("data-help",e),t&&s.setAttribute("data-help-title",t),s}const ge={Label:"The name visitors see above this field.","Field Name":"The key stored with this answer. Use a unique name without spaces, such as email_address. Conditions, message tokens and integrations refer to this name.",Type:"The kind of answer this field accepts. Dropdowns, radio buttons and checkbox groups use the Options list below.",Required:"The visitor must answer this field before submitting. Conditional logic can change whether an answer is required.",Placeholder:"An example or short hint inside an empty input. It disappears when the visitor types.","Helper Text":"Instructions shown below the field, even after the visitor enters an answer.",Tooltip:"Help for visitors, shown beside the field label. Enter the explanation you want visitors to read.","Column Span":"How many columns this field occupies in a grid layout. Full Width spans the entire row.","Full Width":"Make this field span every column in the grid layout.","Min Length":"The fewest characters an answer may contain. Leave blank for no minimum; use Required to prevent an empty answer.","Max Length":"The most characters an answer may contain. Leave blank for no maximum.",Min:"The lowest number allowed. Leave blank for no lower limit.",Max:"The highest number allowed. Leave blank for no upper limit."},ye={Action:"Choose what happens when this branch is active. Banners, field changes and submission blocks apply while the condition holds. Toasts, step jumps and celebrations fire when the branch becomes active. Redirects, CMS actions and admin notifications are handled on submission.",Message:"The text visitors see. Use {{field_name}} to insert an answer; password and upload answers are never included.","Reason, shown under the button":"Explain why submission is blocked. Leave blank to use the default refusal message.","Message shown in place of the rest":"Explain why the form ends here. The remaining fields are hidden while this condition holds. You can insert answers with {{field_name}}.","Notification title":"The title of the admin notification created when the submitted answers meet this condition. Leave blank to use the form title. Field tokens are not expanded here.","Button text":"The button used to send the answers given so far when the form ends early. Leave blank for Finish.","Closing message":"Shown after the visitor finishes early. Leave blank for the form success message. You can insert answers with {{field_name}}.",Level:"The visual style of the message: Info, Success, Warning or Error.",Severity:"The level attached to the admin notification: Info, Success, Warning or Error.",Where:"Place the banner above or below the field that owns this trigger, or at the top of the form.","Which fields":"The rest of the form means fields after this trigger's field. Every other field excludes this field. Chosen fields lets you select specific targets.",Fields:"Select the fields this action changes. Use Ctrl-click on Windows or Linux, or Cmd-click on macOS, to select several.",Step:"The wizard step to open when this branch becomes active. Steps count from zero: 0 is the first step, 1 is the second. Add Page Breaks to create steps.","Send them to":"The destination after a successful submission when this condition matches. Use a site path such as /thank-you or a full URL. Field tokens are not expanded.",Theme:"The celebration to play when this branch becomes active. Whatever is in season chooses the seasonal theme automatically.",Intensity:"How much animation the celebration produces. Low is subtle; High adds more activity."};let Le=null,Lt=null,Ge=null,ve=null;function Ke(){Ge?.(),clearTimeout(Lt),Lt=setTimeout(()=>{Le&&Le()},350)}function Nt(e){return e.toLowerCase().replace(/[^a-z0-9]+/g,"_").replace(/^_|_$/g,"")}function qt(e){return Tt.find(t=>t.value===e)?.label||e}function $t(e){return(e||[]).some(t=>t.logic||Array.isArray(t.triggers)&&t.triggers.length)}function At(e){return e?!!(e.visibility?.conditions?.length||e.requirement?.conditions?.length||e.validation?.length||e.cascade?.sourceField):!1}function qn(e){const t={...T[e]};if(t.type==="spacer")return t;if(t.type==="page-break"){const x=document.getElementById(`fb-pb-label-${e}`),b=document.getElementById(`fb-pb-desc-${e}`);return x&&(t.label=x.value.trim()||t.label),b&&(t.description=b.value.trim()),t}const s=document.getElementById(`fb-label-${e}`),n=document.getElementById(`fb-name-${e}`),i=document.getElementById(`fb-type-${e}`),a=document.getElementById(`fb-required-${e}`),o=document.getElementById(`fb-placeholder-${e}`),l=document.getElementById(`fb-helper-${e}`),p=document.getElementById(`fb-tooltip-${e}`);s&&(t.label=s.value.trim()||t.label),n&&(t.name=n.value.trim()||t.name),i&&(t.type=i.value||t.type),a&&(t.required=a.checked),o&&(t.placeholder=o.value.trim()),l&&(t.helper=l.value.trim()),p&&(t.tooltip=p.value.trim());const r=document.getElementById(`fb-class-${e}`);if(r){const x=r.value.trim();x?t.class=x:delete t.class}if(t.type==="chooser"){const x=document.getElementById(`fb-chooser-variant-${e}`)?.value;x&&(t.variant=x);const b=document.getElementById(`fb-chooser-multiple-${e}`);b&&(t.multiple=b.checked);const C=document.getElementById(`fb-chooser-density-${e}`)?.value;C&&(t.density=C);const $=document.getElementById(`fb-chooser-columns-${e}`)?.value;$!==""&&$!==void 0&&(t.columns=parseInt($,10)||3);const _=document.getElementById(`fb-chooser-accent-${e}`)?.value.trim();_?t.accent=_:delete t.accent;const W=document.getElementById(`fb-chooser-accent-style-${e}`)?.value;W?t.accentStyle=W:delete t.accentStyle;const j=document.getElementById(`fb-chooser-glow-${e}`);j&&(t.glow=j.checked);const Y=document.getElementById(`fb-chooser-glow-colour-${e}`)?.value.trim();Y?t.glowColour=Y:delete t.glowColour;const U=document.getElementById(`fb-chooser-shadow-${e}`)?.value;U&&U!=="none"?t.shadow=U:delete t.shadow;const ie=document.getElementById(`fb-chooser-shadow-colour-${e}`)?.value.trim();ie?t.shadowColour=ie:delete t.shadowColour,Be(e),T[e]&&Array.isArray(T[e].options)&&(t.options=T[e].options)}else if(Ue.has(t.type)){["variant","density","columns","accent","accentStyle","glow","glowColour","shadow","shadowColour"].forEach(b=>delete t[b]),t.type!=="checkbox-group"&&t.type!=="multiselect"&&delete t.multiple;const x=document.getElementById(`fb-options-${e}`);x&&(t.options=x.value.split(`
|
|
2
|
+
`).filter(b=>b.trim()).map(b=>{const[C,...$]=b.split(":");return{value:C.trim(),label:$.join(":").trim()||C.trim()}}))}for(const x of["prefix","suffix"]){const b=document.getElementById(`fb-${x}-icon-${e}`),C=document.getElementById(`fb-${x}-text-${e}`),$={...t.formConfig||{}};if(b||C){const _=b?.value.trim()||"",W=C?.value.trim()||"";_&&/^[a-z0-9-]+$/i.test(_)?$[x]={icon:_}:W?$[x]={text:W.slice(0,24)}:delete $[x]}else Kt.includes(t.type)||delete $[x];t.formConfig=Object.keys($).length?$:void 0}{const x={...t.formConfig||{}};for(const b of et){const C=document.getElementById(`fb-extra-${b.key}-${e}`);C&&b.types.includes(t.type)?C.checked?x[b.key]=!0:delete x[b.key]:b.types.includes(t.type)||delete x[b.key]}t.formConfig=Object.keys(x).length?x:void 0}if(t.type==="file"){const x=document.getElementById(`fb-file-multiple-${e}`);if(x){const b={...t.file||{}};if(x.checked){t.multiple=!0;const C=parseInt(document.getElementById(`fb-file-max-${e}`)?.value,10);C>=1?b.maxFiles=Math.min(C,He):delete b.maxFiles}else delete t.multiple,delete b.maxFiles;t.file=Object.keys(b).length?b:void 0}}if(t.type==="textarea"){const x=parseInt(document.getElementById(`fb-rows-${e}`)?.value,10);x>0&&(t.formConfig={...t.formConfig||{},rows:x})}const d=document.getElementById(`fb-span-${e}`),u=document.getElementById(`fb-fullwidth-${e}`);if(d||u){const x={...t.formConfig||{}};if(u?.checked)x.span="full";else{const b=parseInt(d?.value,10);b>1?x.span=b:delete x.span}t.formConfig=Object.keys(x).length?x:void 0}const m=document.getElementById(`fb-minlength-${e}`)?.value;m&&(t.minLength=parseInt(m,10));const g=document.getElementById(`fb-maxlength-${e}`)?.value;g&&(t.maxLength=parseInt(g,10));const f=document.getElementById(`fb-min-${e}`)?.value;f!==""&&f!==void 0&&(t.min=parseFloat(f));const v=document.getElementById(`fb-max-${e}`)?.value;v!==""&&v!==void 0&&(t.max=parseFloat(v));const w=$n(e);w?t.logic=w:delete t.logic;const N=Yn(e);return N?t.triggers=N:delete t.triggers,t}function $n(e){const t=document.querySelector(`.fb-field-card[data-index="${e}"]`);if(!t)return;const s=t.querySelector(".fb-field-logic");if(!s)return;const n={};let i=!1;const a=s.querySelector('[data-logic-section="visibility"]');if(a){const r=a.querySelector(".fb-logic-vis-default"),d=a.querySelector(".fb-logic-vis-transition"),u=Array.from(a.querySelectorAll(".fb-logic-cond-row")).map(v=>{const w=v.querySelector(".fb-logic-cond-field"),N=v.querySelector(".fb-logic-cond-op"),x=v.querySelector(".fb-logic-cond-val"),b=v.querySelector(".fb-logic-vis-then");return w?.value?{when:{all:[{field:w.value,operator:N.value,value:x.value}]},then:b.value}:null}).filter(Boolean),m=r?.value||"visible",g=d?.value||"none",f=parseInt(a.querySelector(".fb-logic-vis-duration")?.value||"",10);(m!=="visible"||u.length>0||g!=="none")&&(n.visibility={default:m,conditions:u},g!=="none"&&(n.visibility.transition=g),g!=="none"&&Number.isFinite(f)&&(n.visibility.duration=f),i=!0)}const o=s.querySelector('[data-logic-section="requirement"]');if(o){const r=o.querySelector(".fb-logic-req-default"),d=Array.from(o.querySelectorAll(".fb-logic-cond-row")).map(u=>{const m=u.querySelector(".fb-logic-cond-field"),g=u.querySelector(".fb-logic-cond-op"),f=u.querySelector(".fb-logic-cond-val"),v=u.querySelector(".fb-logic-req-then");return m?.value?{when:{all:[{field:m.value,operator:g.value,value:f.value}]},then:v.value==="true"}:null}).filter(Boolean);d.length>0&&(n.requirement={default:r?.checked===!0,conditions:d},i=!0)}const l=s.querySelector('[data-logic-section="validation"]');if(l){const r=Array.from(l.querySelectorAll(".fb-logic-val-rule")).map(d=>{const u=d.querySelector(".fb-logic-val-type"),m=d.querySelector(".fb-logic-val-pattern"),g=d.querySelector(".fb-logic-val-flags"),f=d.querySelector(".fb-logic-val-message");if(!m?.value.trim())return null;const v=u?.value||"regex",w={type:v,message:f?.value.trim()||"Invalid value."};return v==="regex"?(w.pattern=m.value.trim(),g?.value.trim()&&(w.flags=g.value.trim())):w.field=m.value.trim(),w}).filter(Boolean);r.length>0&&(n.validation=r,i=!0)}const p=s.querySelector('[data-logic-section="cascade"]');if(p){const r=p.querySelector(".fb-logic-cascade-source"),d=p.querySelector(".fb-logic-cascade-mapping"),u=p.querySelector(".fb-logic-cascade-defaults"),m=r?.value?.trim();if(m){let g={};try{g=JSON.parse(d?.value||"{}")}catch{}const f=(u?.value||"").split(`
|
|
3
|
+
`).filter(v=>v.trim()).map(v=>{const[w,...N]=v.split(":");return{value:w.trim(),label:N.join(":").trim()||w.trim()}});n.cascade={sourceField:m,mapping:g,defaultOptions:f},i=!0}}return i?n:void 0}function V(){T=T.map((e,t)=>qn(t))}function An(e){const t=e.firstElementChild;if(!t||t.querySelector(".fb-drag-grip"))return;const s=document.createElement("span");s.className="fb-drag-grip",s.setAttribute("data-icon","grip-vertical"),s.setAttribute("data-icon-size","16"),s.title="Drag to reorder",t.insertBefore(s,t.firstChild),window.I?.scan?.(t)}function ne(e){const t=e.find("#fields-list").get(0),s=e.find("#fields-empty-msg").get(0);if(t){if(Array.from(t.querySelectorAll(".fb-field-card")).forEach(n=>n.remove()),T.length===0){s&&(s.style.display=""),ve?.(t),Ke();return}s&&(s.style.display="none"),T.forEach((n,i)=>{const a=n.type==="page-break"?Fn(n,i,e):n.type==="spacer"?Bn(n,i,e):In(n,i,e);An(a),t.appendChild(a)}),ve?.(t),Ke()}}function Fn(e,t,s){const n=document.createElement("div");n.className="fb-field-card",n.dataset.index=t,n.style.cssText="border:2px dashed var(--dm-border);border-radius:8px;overflow:hidden;margin-bottom:.75rem;background:var(--dm-card-bg);";const i=document.createElement("div");i.style.cssText="display:flex;align-items:center;gap:.6rem;padding:.6rem .8rem;cursor:pointer;user-select:none;";const a=document.createElement("span");a.textContent="- Page Break -",a.style.cssText="font-size:.7rem;padding:.15rem .5rem;border-radius:999px;background:color-mix(in srgb, var(--dm-text-muted) 20%, transparent);color:var(--dm-text-muted);white-space:nowrap;flex-shrink:0;font-style:italic;";const o=document.createElement("span");o.textContent=e.label||"Untitled Step",o.style.cssText="flex:1;font-weight:600;font-size:.9rem;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:var(--dm-text-muted);";const l=document.createElement("div");l.style.cssText="display:flex;gap:.25rem;flex-shrink:0;margin-left:.5rem;";const p=document.createElement("button");p.className="btn btn-xs btn-ghost",p.title="Edit step",O(p,"more-horizontal"),p.addEventListener("click",g=>{g.stopPropagation(),d.style.display=d.style.display==="none"?"":"none"}),l.appendChild(p);const r=document.createElement("button");r.className="btn btn-xs btn-ghost",r.style.cssText="line-height:1;opacity:.6;",r.title="Remove page break",O(r,"x"),r.addEventListener("click",async g=>{g.stopPropagation(),await E.confirm("Remove this page break?")&&(V(),T.splice(t,1),ne(s))}),l.appendChild(r),i.appendChild(a),i.appendChild(o),i.appendChild(l),i.addEventListener("click",()=>{d.style.display=d.style.display==="none"?"":"none"});const d=document.createElement("div");d.className="fb-field-body",d.style.cssText="padding:.8rem;border-top:1px dashed var(--dm-border);display:none;";const u=z([P("Step Title",`fb-pb-label-${t}`,"text",e.label||"","Shown as the wizard step heading"),P("Step Description",`fb-pb-desc-${t}`,"text",e.description||"","Optional sub-heading")]),m=u.querySelector(`#fb-pb-label-${t}`);return m&&m.addEventListener("input",()=>{o.textContent=m.value||"Untitled Step"}),d.appendChild(u),n.appendChild(i),n.appendChild(d),n}function Bn(e,t,s){const n=document.createElement("div");n.className="fb-field-card",n.dataset.index=t,n.style.cssText="border:1px dashed var(--dm-border);border-radius:6px;margin-bottom:.5rem;background:transparent;";const i=document.createElement("div");i.style.cssText="display:flex;align-items:center;gap:.6rem;padding:.4rem .8rem;";const a=document.createElement("div");a.style.cssText="flex:1;height:1px;background:var(--dm-border);";const o=document.createElement("span");o.textContent="Spacer",o.style.cssText="font-size:.7rem;color:var(--dm-text-muted);white-space:nowrap;padding:0 .4rem;font-style:italic;";const l=document.createElement("div");l.style.cssText="flex:1;height:1px;background:var(--dm-border);";const p=document.createElement("div");p.style.cssText="display:flex;gap:.25rem;flex-shrink:0;";const r=document.createElement("button");return r.className="btn btn-xs btn-ghost",r.style.cssText="line-height:1;opacity:.6;",r.title="Remove spacer",O(r,"x"),r.addEventListener("click",async d=>{d.stopPropagation(),V(),T.splice(t,1),ne(s)}),p.appendChild(r),i.appendChild(a),i.appendChild(o),i.appendChild(l),i.appendChild(p),n.appendChild(i),n}function In(e,t,s){const n=document.createElement("div");n.className="fb-field-card",n.dataset.index=t,n.style.cssText="border:1px solid var(--dm-border);border-radius:8px;overflow:hidden;margin-bottom:.75rem;background:var(--dm-card-bg);box-shadow:var(--dm-shadow-sm);";const i=document.createElement("div");i.style.cssText="display:flex;align-items:center;gap:.6rem;padding:.65rem .85rem;background:var(--dm-card-header-bg);cursor:pointer;user-select:none;";const a=document.createElement("span");a.textContent=qt(e.type),a.style.cssText="font-size:.7rem;padding:.15rem .45rem;border-radius:999px;background:color-mix(in srgb, var(--dm-primary) 14%, transparent);color:var(--dm-primary);white-space:nowrap;flex-shrink:0;";const o=document.createElement("span");o.textContent=e.label||"(unlabelled)",o.style.cssText="flex:1;font-weight:600;font-size:.9rem;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;";const l=document.createElement("div");l.style.cssText="display:flex;gap:.25rem;flex-shrink:0;margin-left:.5rem;";const p=document.createElement("button");p.className="btn btn-xs btn-ghost",p.title="Edit field",O(p,"more-horizontal"),p.addEventListener("click",u=>{u.stopPropagation(),d.style.display=d.style.display==="none"?"":"none"}),l.appendChild(p);const r=document.createElement("button");if(r.className="btn btn-xs btn-ghost",r.style.cssText="line-height:1;opacity:.6;",r.title="Remove field",O(r,"x"),r.addEventListener("click",async u=>{u.stopPropagation(),await E.confirm("Remove this field?")&&(V(),T.splice(t,1),ne(s))}),l.appendChild(r),i.appendChild(a),i.appendChild(o),e.required){const u=document.createElement("span");u.textContent="required",u.style.cssText="font-size:.7rem;color:var(--dm-danger);flex-shrink:0;",i.appendChild(u)}if(At(e.logic)){const u=document.createElement("span");O(u,"zap"),u.title="Has conditional logic",u.style.cssText="font-size:.75rem;color:var(--dm-primary);flex-shrink:0;",i.appendChild(u)}i.appendChild(l),i.addEventListener("click",()=>{d.style.display=d.style.display==="none"?"":"none"});const d=On(e,t,o);return d.style.display="none",n.appendChild(i),n.appendChild(d),n}function On(e,t,s){const n=document.createElement("div");n.className="fb-field-body",n.style.cssText="padding:1rem .9rem;border-top:1px solid var(--dm-border);";const i=z([P("Label",`fb-label-${t}`,"text",e.label||"","Shown above the field"),P("Field Name",`fb-name-${t}`,"text",e.name||"","Used as data key")]),a=z([ai("Type",`fb-type-${t}`,Tt,e.type||"string"),Ie("Required",`fb-required-${t}`,e.required||!1)]),o=z([P("Placeholder",`fb-placeholder-${t}`,"text",e.placeholder||"","Hint text inside the field")]),l=z([P("Helper Text",`fb-helper-${t}`,"text",e.helper||"","Shown below the field"),P("Tooltip",`fb-tooltip-${t}`,"text",e.tooltip||"","Shown on hover via a help icon next to the label")]),p=z([P("CSS classes",`fb-class-${t}`,"text",e.class||"","Your own class names, separated by spaces, on the field's wrapper - style them in Custom CSS")]),r=e.formConfig?.span,d=z([P("Column Span",`fb-span-${t}`,"number",r&&r!=="full"?String(r):"1","Columns to span (grid only)"),Ie("Full Width",`fb-fullwidth-${t}`,r==="full")]);d.classList.add("fb-grid-row"),d.style.display=document.getElementById("setting-layout")?.value==="grid"?"flex":"none",n.appendChild(i),n.appendChild(a),n.appendChild(o),n.appendChild(l),n.appendChild(p),n.appendChild(d);const u=n.querySelector(`#fb-label-${t}`),m=n.querySelector(`#fb-name-${t}`);u&&u.addEventListener("input",()=>{s&&(s.textContent=u.value||"(unlabelled)"),m&&!m.dataset.manuallyEdited&&(m.value=Nt(u.value))}),m&&m.addEventListener("input",()=>{m.dataset.manuallyEdited="1"});const g=n.querySelector(`#fb-type-${t}`);g&&g.addEventListener("change",()=>{const v=n.closest(".fb-field-card");if(v){const x=v.querySelector("span");x&&(x.textContent=qt(g.value))}const w=n.querySelector(".fb-field-extras");w&&w.remove();const N=Gt(g.value,e,t);N&&n.appendChild(N)});const f=Gt(e.type,e,t);return f&&n.appendChild(f),n.appendChild(_n(e,t)),n.appendChild(Gn(e,t)),n}const Ne=[{value:"equals",label:"equals"},{value:"not_equals",label:"does not equal"},{value:"contains",label:"contains"},{value:"not_contains",label:"does not contain"},{value:"starts_with",label:"starts with"},{value:"ends_with",label:"ends with"},{value:"greater_than",label:"is greater than"},{value:"less_than",label:"is less than"},{value:"is_empty",label:"is empty"},{value:"is_not_empty",label:"is not empty"},{value:"in",label:"is one of (comma sep)"},{value:"not_in",label:"is not one of (comma sep)"},{value:"matches_regex",label:"matches regex"}],Ye=new Set(["is_empty","is_not_empty"]),Ft={equals:"includes",not_equals:"does not include",contains:"any value contains",not_contains:"no value contains",starts_with:"any value starts with",ends_with:"any value ends with",greater_than:"any value is greater than",less_than:"any value is less than",is_empty:"nothing selected",is_not_empty:"something selected",in:"includes one of (comma sep)",not_in:"includes none of (comma sep)",matches_regex:"any value matches regex"};function Je(e){return!!e&&(e.type==="checkbox-group"||!!e.multiple)}function qe(e){const t=document.createElement("p");return t.textContent=e,t.appendChild(B({Visibility:"Choose whether this field starts visible or hidden, then add rules to show or hide it based on answers. The transition and speed control how it appears.","Conditional Requirement":"Make this field required or optional according to another answer. Hidden fields are not required.","Custom Validation":"Regex tests the answer against a regular expression; flags such as i make it case insensitive. Match field compares with another field name, such as password confirmation. Set the error message visitors see if the check fails.","Cascade Options":"Replace this field's choices according to another field's answer. Map source values to option lists and provide default options for unmatched or empty answers."}[e],e)),t.style.cssText="font-size:.75rem;font-weight:700;color:var(--dm-text-muted);margin:0 0 .7rem;text-transform:uppercase;letter-spacing:.06em;",t}function $e(e,t,s,n,i){const a=document.createElement("div");a.className="fb-logic-cond-row",a.style.cssText="display:flex;gap:.55rem;align-items:center;margin-bottom:.6rem;flex-wrap:wrap;";const o=document.createElement("span");o.textContent="When",o.style.cssText="font-size:.73rem;color:var(--dm-text-muted);flex-shrink:0;";const l=document.createElement("select");l.className="form-select fb-logic-cond-field",l.style.cssText="flex:1 1 140px;min-width:140px;",t.forEach(f=>{const v=document.createElement("option");v.value=f.value,v.textContent=f.label,e&&f.value===e.field&&(v.selected=!0),l.appendChild(v)});const p=document.createElement("select");p.className="form-select fb-logic-cond-op",p.style.cssText="flex:1 1 160px;min-width:140px;",Ne.forEach(f=>{const v=document.createElement("option");v.value=f.value,v.textContent=f.label,e&&f.value===e.operator&&(v.selected=!0),p.appendChild(v)});const r=()=>{const f=t.find(v=>v.value===l.value);Array.from(p.options).forEach(v=>{const w=Ne.find(N=>N.value===v.value);v.textContent=f?.multi&&Ft[v.value]||w?.label||v.value})};r(),l.addEventListener("change",r);const d=document.createElement("input");d.type="text",d.className="form-input fb-logic-cond-val",d.placeholder="value",d.style.cssText="flex:1 1 120px;min-width:100px;",d.value=e?.value||"",e&&Ye.has(e.operator)&&(d.style.display="none"),p.addEventListener("change",()=>{d.style.display=Ye.has(p.value)?"none":""});const u=document.createElement("span");O(u,"arrow-right"),u.style.cssText="font-size:.73rem;color:var(--dm-text-muted);flex-shrink:0;";const m=document.createElement("select");m.className=`form-select ${n}`,m.style.cssText="flex:1 1 140px;min-width:120px;",s.forEach(f=>{const v=document.createElement("option");v.value=f.value,v.textContent=f.label,f.value===i&&(v.selected=!0),m.appendChild(v)});const g=document.createElement("button");return g.type="button",g.className="btn btn-xs btn-ghost",g.style.cssText="line-height:1;opacity:.6;",O(g,"x"),g.style.flexShrink="0",g.addEventListener("click",()=>a.remove()),a.appendChild(o),a.appendChild(L("Field",l,{help:"The answer this rule tests. Select another named field from the form."})),a.appendChild(L("Comparison",p,{help:"How to compare the answer. Multi-choice fields test their selected values. Empty and not empty do not need a value."})),a.appendChild(L("Value",d,{help:"The stored value to compare against. For one of, enter comma-separated values. For regex, enter a regular expression."})),a.appendChild(u),a.appendChild(L("Result",m,{help:"What happens to this field when the condition matches: change its visibility or whether an answer is required."})),a.appendChild(g),a}function Mn(e,t,s){const n=document.createElement("div");n.dataset.logicSection="visibility",n.appendChild(qe("Visibility"));const i=document.createElement("div");i.style.cssText="display:flex;align-items:center;gap:.6rem;margin-bottom:.65rem;flex-wrap:wrap;";const a=document.createElement("span");a.textContent="Default:",a.style.cssText="font-size:.8rem;flex-shrink:0;";const o=document.createElement("select");o.className="form-select fb-logic-vis-default",o.style.cssText="max-width:240px;",[{value:"visible",label:"Visible"},{value:"hidden",label:"Hidden"}].forEach(b=>{const C=document.createElement("option");C.value=b.value,C.textContent=b.label,b.value===(e.default||"visible")&&(C.selected=!0),o.appendChild(C)}),a.appendChild(B("Whether the field is visible before any rule matches.","Default visibility")),i.appendChild(a),i.appendChild(o),n.appendChild(i);const l=document.createElement("div");l.style.cssText="display:flex;align-items:center;gap:.6rem;margin-bottom:.65rem;flex-wrap:wrap;";const p=document.createElement("span");p.textContent="Transition:",p.style.cssText="font-size:.8rem;flex-shrink:0;";const r=document.createElement("select");r.className="form-select fb-logic-vis-transition",r.style.cssText="max-width:240px;",(window.FormLogicEngine?.FIELD_TRANSITIONS||[{value:"none",label:"None (instant)"},{value:"fade",label:"Fade"},{value:"slide",label:"Slide"},{value:"scale",label:"Scale"}]).forEach(b=>{const C=document.createElement("option");C.value=b.value,C.textContent=b.label,b.value===(e.transition||"none")&&(C.selected=!0),r.appendChild(C)}),p.appendChild(B("The animation used when this field appears or disappears, from a simple fade to a bubble pop. None switches instantly. Use Preview to try it.","Transition")),l.appendChild(p),l.appendChild(r),n.appendChild(l);const d=document.createElement("div");d.style.cssText="display:flex;align-items:center;gap:.6rem;margin-bottom:.65rem;flex-wrap:wrap;";const u=document.createElement("span");u.textContent="Speed:",u.style.cssText="font-size:.8rem;flex-shrink:0;";const m=document.createElement("select");m.className="form-select fb-logic-vis-duration",m.style.cssText="max-width:240px;",[{value:"",label:"Default"},{value:"120",label:"Very fast (120ms)"},{value:"200",label:"Fast (200ms)"},{value:"350",label:"Medium (350ms)"},{value:"600",label:"Slow (600ms)"},{value:"1000",label:"Very slow (1s)"}].forEach(b=>{const C=document.createElement("option");C.value=b.value,C.textContent=b.label,b.value===String(e.duration??"")&&(C.selected=!0),m.appendChild(C)}),u.appendChild(B("How long the visibility animation lasts. Default uses the effect timing; shorter times are faster.","Speed")),d.appendChild(u),d.appendChild(m),n.appendChild(d);const g=document.createElement("button");g.type="button",g.className="btn btn-xs btn-ghost fb-logic-vis-preview",g.textContent="Preview";const f=document.createElement("span");f.className="fb-logic-vis-preview-chip",f.textContent="Sample field",f.style.cssText="display:inline-block;padding:.2rem .6rem;border:1px solid var(--dm-border);border-radius:var(--dm-radius, 6px);font-size:.75rem;background:var(--dm-surface);",g.addEventListener("click",()=>{window.FormLogicEngine?.previewTransition?.(f,r.value,m.value===""?void 0:Number(m.value))}),d.appendChild(g),d.appendChild(f);const v=()=>{d.style.display=r.value==="none"?"none":""};v(),r.addEventListener("change",v);const w=document.createElement("div");w.className="fb-logic-vis-rules";const N=s.map(b=>({value:b.name,label:b.label||b.name,multi:Je(b)})),x=[{value:"visible",label:"Show"},{value:"hidden",label:"Hide"}];if((e.conditions||[]).forEach(b=>{const C=(b.when?.all||b.when?.any||[])[0],$=b.then==="hidden"?"hidden":"visible";N.length>0&&w.appendChild($e(C,N,x,"fb-logic-vis-then",$))}),n.appendChild(w),N.length>0){const b=document.createElement("button");b.type="button",b.className="btn btn-xs btn-ghost",b.style.cssText="font-size:.73rem;margin-top:.35rem;",be(b,"plus","Add visibility rule"),b.addEventListener("click",()=>w.appendChild($e(null,N,x,"fb-logic-vis-then","visible"))),n.appendChild(b)}return n}function Rn(e,t,s){const n=document.createElement("div");n.dataset.logicSection="requirement",n.appendChild(qe("Conditional Requirement"));const i=document.createElement("label");i.style.cssText="display:flex;align-items:center;gap:.5rem;font-size:.8rem;cursor:pointer;margin-bottom:.65rem;";const a=document.createElement("input");a.type="checkbox",a.className="fb-logic-req-default",a.checked=e.default===!0,i.appendChild(a),i.appendChild(document.createTextNode("Required by default")),i.appendChild(B("Whether this field requires an answer before a conditional requirement rule matches.","Required by default")),n.appendChild(i);const o=document.createElement("div");o.className="fb-logic-req-rules";const l=s.map(r=>({value:r.name,label:r.label||r.name,multi:Je(r)})),p=[{value:"true",label:"Make required"},{value:"false",label:"Make optional"}];if((e.conditions||[]).forEach(r=>{const d=(r.when?.all||r.when?.any||[])[0],u=r.then===!0?"true":"false";l.length>0&&o.appendChild($e(d,l,p,"fb-logic-req-then",u))}),n.appendChild(o),l.length>0){const r=document.createElement("button");r.type="button",r.className="btn btn-xs btn-ghost",r.style.cssText="font-size:.73rem;margin-top:.35rem;",be(r,"plus","Add requirement rule"),r.addEventListener("click",()=>o.appendChild($e(null,l,p,"fb-logic-req-then","true"))),n.appendChild(r)}return n}function Bt(e){const t=document.createElement("div");t.className="fb-logic-val-rule",t.style.cssText="display:flex;gap:.55rem;align-items:center;margin-bottom:.6rem;flex-wrap:wrap;";const s=document.createElement("select");s.className="form-select fb-logic-val-type",s.style.cssText="flex:0 0 140px;",[{value:"regex",label:"Regex"},{value:"match",label:"Match field"}].forEach(l=>{const p=document.createElement("option");p.value=l.value,p.textContent=l.label,l.value===(e?.type||"regex")&&(p.selected=!0),s.appendChild(p)});const n=document.createElement("input");n.type="text",n.className="form-input fb-logic-val-pattern",n.placeholder=e?.type==="match"?"field name":"pattern",n.value=e?.pattern||e?.field||"",n.style.cssText="flex:3;";const i=document.createElement("input");i.type="text",i.className="form-input fb-logic-val-flags",i.placeholder="flags",i.value=e?.flags||"",i.style.cssText="flex:0 0 55px;",e?.type==="match"&&(i.style.display="none"),s.addEventListener("change",()=>{i.style.display=s.value==="match"?"none":"",n.placeholder=s.value==="match"?"field name":"pattern"});const a=document.createElement("input");a.type="text",a.className="form-input fb-logic-val-message",a.placeholder="Error message",a.value=e?.message||"",a.style.cssText="flex:4;";const o=document.createElement("button");return o.type="button",o.className="btn btn-xs btn-ghost",o.style.cssText="line-height:1;opacity:.6;",O(o,"x"),o.style.flexShrink="0",o.addEventListener("click",()=>t.remove()),t.appendChild(L("Validation",s,{help:"Regex checks a pattern. Match field requires the same answer as another named field."})),t.appendChild(L("Pattern or field name",n,{help:"For Regex, enter a regular expression without enclosing slashes. For Match field, enter the other field name, such as password."})),t.appendChild(L("Flags",i,{help:"Optional regular expression flags, such as i for case insensitive matching. Applies only to Regex."})),t.appendChild(L("Error message",a,{help:"The explanation visitors see when the answer fails this validation rule."})),t.appendChild(o),t}function Dn(e,t,s){const n=document.createElement("div");n.dataset.logicSection="validation",n.appendChild(qe("Custom Validation"));const i=document.createElement("div");i.className="fb-logic-val-rules",(e||[]).forEach(o=>i.appendChild(Bt(o))),n.appendChild(i);const a=document.createElement("button");return a.type="button",a.className="btn btn-xs btn-ghost",a.style.cssText="font-size:.73rem;margin-top:.35rem;",be(a,"plus","Add validation rule"),a.addEventListener("click",()=>i.appendChild(Bt(null))),n.appendChild(a),n}function zn(e,t,s){const n=document.createElement("div");n.dataset.logicSection="cascade",n.appendChild(qe("Cascade Options")),n.lastChild.appendChild(B("Make this field's options depend on another field. When the source field changes, the options here are replaced by the mapping entry for its value; if nothing matches, the default options below are shown."));const i=document.createElement("div");i.style.cssText="display:flex;align-items:center;gap:.5rem;margin-bottom:.5rem;";const a=document.createElement("span");a.textContent="Source field:",a.style.cssText="font-size:.8rem;flex-shrink:0;";const o=document.createElement("select");o.className="form-select fb-logic-cascade-source",o.style.cssText="flex:1;";const l=document.createElement("option");l.value="",l.textContent="- none -",o.appendChild(l),s.forEach(m=>{const g=document.createElement("option");g.value=m.name,g.textContent=m.label||m.name,m.name===e.sourceField&&(g.selected=!0),o.appendChild(g)}),i.appendChild(a),i.appendChild(o),n.appendChild(i);const p=document.createElement("p");p.textContent='Mapping JSON - {"value":[{"value":"...","label":"..."}]}',p.appendChild(B(`Each key is a value of the source field; its list becomes this field's options when that value is picked. Example: {"uk":[{"value":"london","label":"London"}]} shows London when the source is "uk".`)),p.style.cssText="font-size:.73rem;color:var(--dm-text-muted);margin:.3rem 0 .2rem;";const r=document.createElement("textarea");r.className="form-input fb-logic-cascade-mapping",r.rows=4,r.style.cssText="font-family:monospace;",r.placeholder='{"uk": [{"value": "london", "label": "London"}]}',r.value=e.mapping?JSON.stringify(e.mapping,null,2):"",n.appendChild(p),n.appendChild(r);const d=document.createElement("p");d.textContent="Default options (one per line: value:Label)",d.appendChild(B("Shown before the source field has a value, or when its value has no mapping entry.")),d.style.cssText="font-size:.73rem;color:var(--dm-text-muted);margin:.3rem 0 .2rem;";const u=document.createElement("textarea");return u.className="form-input fb-logic-cascade-defaults",u.rows=3,u.style.cssText="font-family:monospace;",u.placeholder=`option1:Option 1
|
|
4
|
+
option2:Option 2`,u.value=(e.defaultOptions||[]).map(m=>{const g=typeof m=="string"?m:m.value??"",f=typeof m=="string"?m:m.label??g;return g===f?g:`${g}:${f}`}).join(`
|
|
5
|
+
`),n.appendChild(d),n.appendChild(u),n}function _n(e,t){const s=e.logic||{},n=T.filter((u,m)=>m!==t&&u.type!=="page-break"&&u.type!=="spacer"),i=document.createElement("div");i.className="fb-field-logic",i.style.cssText="margin-top:.75rem;border-top:1px solid var(--dm-border);padding-top:.5rem;";const a=document.createElement("div");a.style.cssText="display:flex;align-items:center;justify-content:space-between;cursor:pointer;padding:.15rem 0;";const o=document.createElement("span");o.style.cssText="font-size:.8rem;font-weight:600;color:var(--dm-text-muted);",o.textContent="\u26A1 Conditional Logic";const l=At(s),p=document.createElement("button");p.type="button",p.className="btn btn-xs btn-ghost",O(p,l?"chevron-down":"chevron-right",12);const r=document.createElement("div");r.className="fb-logic-body",r.style.cssText="padding:.25rem 0 .25rem;"+(l?"":"display:none;"),a.addEventListener("click",()=>{const u=r.style.display==="none";r.style.display=u?"":"none",O(p,u?"chevron-down":"chevron-right",12)}),a.appendChild(o),a.appendChild(p),i.appendChild(a),r.appendChild(Mn(s.visibility||{},t,n)),r.appendChild(Rn(s.requirement||{},t,n)),r.appendChild(Dn(s.validation||[],t,n));const d=document.getElementById(`fb-type-${t}`)?.value||e.type;return Ue.has(d)&&r.appendChild(zn(s.cascade||{},t,n)),i.appendChild(r),i}const xe=[{value:"banner",label:"Show a banner",kind:"state",params:["message","level","position","dismissible"],placeholder:"e.g. You must be 18 or over to continue."},{value:"toast",label:"Show a toast",kind:"event",params:["message","level"],placeholder:"e.g. Thanks - carry on."},{value:"hide-fields",label:"Hide fields",kind:"state",params:["target"]},{value:"disable-fields",label:"Disable fields",kind:"state",params:["target"]},{value:"require-fields",label:"Make fields required",kind:"state",params:["target"]},{value:"block-submit",label:"Block submission",kind:"state",params:["message"],messageLabel:"Reason, shown under the button",placeholder:'Blank: "This form cannot be submitted yet."'},{value:"end-form",label:"End the form here",kind:"state",params:["message","finish","finishLabel","doneMessage","record"],messageLabel:"Message shown in place of the rest",placeholder:"e.g. Sorry - you must be 18 or over to apply."},{value:"goto-step",label:"Jump to a wizard step",kind:"event",params:["step"]},{value:"redirect",label:"Redirect after submit",kind:"event",params:["url"]},{value:"celebrate",label:"Celebrate",kind:"event",params:["theme","intensity"]},{value:"run-action",label:"Run a CMS action",kind:"event",params:["slug"]},{value:"notify",label:"Notify the admins",kind:"event",params:["message","level"],messageLabel:"Notification title",placeholder:"Blank: titled after the form",noOnce:!0,kindText:"when the form is submitted"}],Ae=[{value:"info",label:"Info"},{value:"success",label:"Success"},{value:"warning",label:"Warning"},{value:"error",label:"Error"}],It=[{value:"field-below",label:"Below this field"},{value:"field-above",label:"Above this field"},{value:"form-top",label:"Top of the form"}],Ot=[{value:"rest",label:"The rest of the form"},{value:"all",label:"Every other field"},{value:"listed",label:"Chosen fields"}],Mt=[{value:"auto",label:"Whatever is in season"},{value:"christmas",label:"Christmas"},{value:"halloween",label:"Halloween"},{value:"valentines",label:"Valentine's Day"},{value:"guy-fawkes",label:"Guy Fawkes Night"},{value:"st-patricks",label:"St Patrick's Day"},{value:"st-andrews",label:"St Andrew's Day"},{value:"st-davids",label:"St David's Day"},{value:"st-georges",label:"St George's Day"}];function Rt(e,t=!1){const s=(o,l)=>({value:o.name,label:`${o.label||o.name}${l?" (this field)":""}`,multi:Je(o)}),n=o=>o&&o.type!=="page-break"&&o.type!=="spacer"&&o.name,i=T.filter((o,l)=>l!==e&&n(o)).map(o=>s(o,!1)),a=T[e];return t&&n(a)?[s(a,!0),...i]:i}function Q(e,t,s){const n=document.createElement("select");return n.className=`form-select ${e}`,t.forEach(i=>{const a=document.createElement("option");a.value=i.value,a.textContent=i.label,i.value===s&&(a.selected=!0),n.appendChild(a)}),n}function re(e,t,s){const n=document.createElement("input");return n.type="text",n.className=`form-input ${e}`,n.value=t==null?"":String(t),s&&(n.placeholder=s),n}function Xe(e,t){const s=document.createElement("button");return s.type="button",s.className="btn btn-xs btn-ghost fb-trig-remove",t&&(s.title=t,s.setAttribute("aria-label",t)),O(s,"x"),s.addEventListener("click",n=>{n.stopPropagation(),e()}),s}function L(e,t,{full:s=!1,hint:n="",help:i=""}={}){const a=document.createElement("label");a.className=`fb-trig-f${s?" fb-trig-f--full":""}`,a.style.flex="1";const o=document.createElement("span");if(o.className="fb-trig-f-label",o.textContent=e,(i||n||ye[e])&&o.appendChild(B(i||n||ye[e],e)),a.appendChild(o),a.appendChild(t),n){const l=document.createElement("span");l.className="fb-trig-f-hint",l.textContent=n,a.appendChild(l)}return a}function we(e,t,s,n){const i=document.createElement("label");i.className="fb-trig-check";const a=document.createElement("input");return a.type="checkbox",a.className=`form-check ${e}`,a.checked=!!t,i.appendChild(a),i.appendChild(document.createTextNode(s)),i.appendChild(B(n||"Allow the visitor to close this banner while the condition holds. It can appear again when the condition becomes true again.",s)),i}function Dt(e,t){const s=document.createElement("div");s.className="fb-trig-cond-row";const n=Q("fb-trig-cond-field",t,e?.field),i=Q("fb-trig-cond-op",Ne,e?.operator),a=re("fb-trig-cond-val",e?.value,"value"),o=L("Value",a,{help:"Compare with the stored value. For one of, separate values with commas; for regex, enter a regular expression."}),l=()=>{const r=t.find(d=>d.value===n.value);Array.from(i.options).forEach(d=>{const u=Ne.find(m=>m.value===d.value);d.textContent=r?.multi&&Ft[d.value]||u?.label||d.value})},p=()=>{o.hidden=Ye.has(i.value)};return l(),p(),n.addEventListener("change",l),i.addEventListener("change",p),s.appendChild(L("Field",n,{help:"The answer to test, including this field or another named field."})),s.appendChild(L("Comparison",i,{help:"How to compare the answer. Multi-choice fields test their selected values. Empty and not empty need no comparison value."})),s.appendChild(o),s.appendChild(Xe(()=>s.remove(),"Remove condition")),s}const zt=["banner","toast","block-submit","end-form"],_t=["password","file","image","signature"],Pn="Put an answer in with {{field_name}} - e.g. Hello {{name}}. Pick one from Insert field.";function jn(){try{V()}catch{}return T.filter(e=>e&&e.name&&e.type!=="page-break"&&e.type!=="spacer"&&!_t.includes(e.type))}function Pt(e,t,s){const n=re(e,t,s),i=document.createElement("span");i.className="fb-token-row";const a=document.createElement("select");a.className="form-input fb-token-pick",a.title="Insert an answer into the message",a.setAttribute("aria-label","Insert a field's answer into the message");const o=()=>{a.replaceChildren(new Option("Insert field",""));for(const l of jn())a.appendChild(new Option(`${l.label||l.name} ({{${l.name}}})`,l.name))};return o(),a.addEventListener("focus",o),a.addEventListener("mousedown",o),a.addEventListener("change",()=>{const l=a.value;if(a.value="",!l)return;const p=`{{${l}}}`,r=n.selectionStart??n.value.length,d=n.selectionEnd??r;n.value=n.value.slice(0,r)+p+n.value.slice(d),n.focus(),n.setSelectionRange(r+p.length,r+p.length),n.dispatchEvent(new Event("input",{bubbles:!0})),n.dispatchEvent(new Event("change",{bubbles:!0}))}),i.append(n,a),i}function Hn(e){const t=window.FormLogicEngine;if(!t?.triggerTokens)return[];const s=new Map((e||[]).filter(i=>i&&i.name).map(i=>[i.name,i])),n=new Set;for(const i of e||[])for(const a of i?.triggers||[])for(const o of[...a.then||[],...a.otherwise||[]])if(zt.includes(o?.type))for(const l of[...t.triggerTokens(o.message),...t.triggerTokens(o.doneMessage)]){const p=s.get(l);(!p||_t.includes(p.type))&&n.add(l)}return[...n]}function jt(e,t){const s=document.createElement("div");s.className="fb-trig-params";const n=xe.find(p=>p.value===(e.type||"banner"));if(!n)return s;const i=document.createElement("div");i.className="fb-trig-grid";const a=document.createElement("div");a.className="fb-trig-opts";const o=[];for(const p of n.params)switch(p){case"message":i.appendChild(zt.includes(n.value)?L(n.messageLabel||"Message",Pt("fb-trig-p-message",e.message,n.placeholder||""),{full:!0,hint:Pn,help:ye[n.messageLabel||"Message"]}):L(n.messageLabel||"Message",re("fb-trig-p-message",e.message,n.placeholder||""),{full:!0}));break;case"finish":a.appendChild(we("fb-trig-p-finish-on",e.finish!==!1,"Let the visitor finish (send the answers given so far)",'Untick to stop the form dead: the submit button is disabled and nothing is sent, so nothing is saved and "Notify the admins" cannot fire.'));break;case"finishLabel":{const r=L("Button text",re("fb-trig-p-finish",e.finishLabel,"Finish"));o.push(r),i.appendChild(r);break}case"doneMessage":{const r=L("Closing message",Pt("fb-trig-p-done",e.doneMessage,"Blank: the form's own success message"));o.push(r),i.appendChild(r);break}case"record":{const r=we("fb-trig-p-record",e.record!==!1,"Save the answers given so far",`Untick to keep nothing: no submission is stored and the form's email, webhook and actions do not run. A "Notify the admins" action still fires.`);o.push(r),a.appendChild(r);break}case"level":i.appendChild(L(n.value==="notify"?"Severity":"Level",Q("fb-trig-p-level",Ae,e.level||"info")));break;case"position":i.appendChild(L("Where",Q("fb-trig-p-position",It,e.position||"field-below")));break;case"dismissible":a.appendChild(we("fb-trig-p-dismissible",e.dismissible!==!1,"The visitor can dismiss it"));break;case"target":{const r=Q("fb-trig-p-target",Ot,e.target||(e.fields?.length?"listed":"rest")),d=document.createElement("select");d.className="form-select fb-trig-p-fields",d.multiple=!0,d.size=4;const u=new Set(e.fields||[]);Rt(t).forEach(f=>{const v=document.createElement("option");v.value=f.value,v.textContent=f.label,u.has(f.value)&&(v.selected=!0),d.appendChild(v)});const m=L("Fields",d,{hint:"Ctrl/Cmd-click to pick several",help:ye.Fields}),g=()=>{m.hidden=r.value!=="listed"};g(),r.addEventListener("change",g),i.appendChild(L("Which fields",r)),i.appendChild(m);break}case"step":{const r=re("fb-trig-p-step",e.step,"0");r.type="number",r.min="0",i.appendChild(L("Step",r,{hint:"Counting from 0",help:ye.Step}));break}case"url":i.appendChild(L("Send them to",re("fb-trig-p-url",e.url,"/thank-you"),{full:!0}));break;case"theme":i.appendChild(L("Theme",Q("fb-trig-p-theme",Mt,e.theme||"auto")));break;case"intensity":i.appendChild(L("Intensity",Q("fb-trig-p-intensity",[{value:"low",label:"Low"},{value:"medium",label:"Medium"},{value:"high",label:"High"}],e.intensity||"medium")));break;case"slug":{const r=[{value:"",label:ke.length?"Choose an action":"No actions available"}].concat(ke.map(d=>({value:d.slug,label:d.title||d.slug})));i.appendChild(L("Action",Q("fb-trig-p-slug",r,e.slug||""),{full:!0,help:"The CMS action to run after a matching submission is saved. Choose an action defined under Data > Actions. Requires Pro with MongoDB."}));break}default:break}n.kind==="event"&&!n.noOnce&&a.appendChild(we("fb-trig-p-once",!!e.once,"Only once","Fire the first time only, rather than every time the answer swings back."));const l=a.querySelector(".fb-trig-p-finish-on");if(l){const p=()=>o.forEach(r=>{r.hidden=!l.checked});p(),l.addEventListener("change",p)}return i.children.length&&s.appendChild(i),a.children.length&&s.appendChild(a),s}function Vn(e){return e?.kindText?e.kindText:e?.kind==="event"?"once, when it becomes true":"while it is true"}const Wn=(e,t=48)=>e.length>t?`${e.slice(0,t-1)}\u2026`:e,ce=(e,t)=>e.find(s=>s.value===t)?.label||t;function Un(e){if(!e)return"";const t=[],s=i=>i?`"${Wn(i)}"`:"",n=()=>e.target==="listed"?e.fields?.length?e.fields.join(", "):"no fields chosen":ce(Ot,e.target||"rest").toLowerCase();switch(e.type){case"banner":t.push(s(e.message)||"No message yet",ce(Ae,e.level||"info"),ce(It,e.position||"field-below").toLowerCase());break;case"toast":t.push(s(e.message)||"No message yet",ce(Ae,e.level||"info"));break;case"hide-fields":case"disable-fields":case"require-fields":t.push(n());break;case"block-submit":t.push(s(e.message)||"the standard reason");break;case"end-form":t.push(s(e.message)||"no message"),e.finish===!1?t.push("submit disabled"):t.push(`button "${e.finishLabel||"Finish"}"`,e.record===!1?"saves nothing":"saves the answers");break;case"goto-step":t.push(Number.isFinite(e.step)?`step ${e.step}`:"no step yet");break;case"redirect":t.push(e.url||"no address yet");break;case"celebrate":t.push(ce(Mt,e.theme||"auto"),`${e.intensity||"medium"} intensity`);break;case"run-action":t.push(e.slug?ke.find(i=>i.slug===e.slug)?.title||e.slug:"no action chosen");break;case"notify":t.push(s(e.message)||"titled after the form",ce(Ae,e.level||"info"));break;default:break}return e.once&&t.push("only once"),t.filter(Boolean).join(" \xB7 ")}function Qe(e,t,{open:s=!1}={}){const n=document.createElement("div");n.className="fb-trig-action-row";const i=document.createElement("div");i.className="fb-trig-action-head",i.tabIndex=0,i.setAttribute("role","button");const a=document.createElement("span");a.className="fb-trig-chevron";const o=document.createElement("span");o.className="fb-trig-action-main";const l=document.createElement("strong"),p=document.createElement("small");p.className="fb-trig-action-summary",o.append(l,p);const r=document.createElement("span");r.className="fb-trig-kind",i.append(a,o,r,Xe(()=>n.remove(),"Remove action"));const d=document.createElement("div");d.className="fb-trig-action-body";const u=Q("fb-trig-action-type",xe,e.type||"banner");d.appendChild(L("Action",u,{full:!0})),d.appendChild(jt(e,t));const m=()=>{const f=xe.find(v=>v.value===u.value);l.textContent=f?.label||u.value,r.textContent=Vn(f),r.classList.toggle("fb-trig-kind--event",f?.kind==="event"),p.textContent=Un(Ut(n))},g=f=>{d.hidden=!f,n.classList.toggle("is-open",f),i.setAttribute("aria-expanded",String(f)),O(a,f?"chevron-down":"chevron-right",12)};return i.addEventListener("click",()=>g(d.hidden)),i.addEventListener("keydown",f=>{f.target!==i||f.key!=="Enter"&&f.key!==" "||(f.preventDefault(),g(d.hidden))}),u.addEventListener("change",()=>{const f=jt({type:u.value},t);d.replaceChild(f,d.querySelector(".fb-trig-params")),m(),window.I?.scan?.()}),d.addEventListener("input",m),d.addEventListener("change",m),n.append(i,d),g(s),m(),n}function Ht(e,t,s){const n=document.createElement("section");n.className=`fb-trig-sec fb-trig-sec--${e}`;const i=document.createElement("div");i.className="fb-trig-sec-head";const a=document.createElement("span");if(a.className="fb-trig-sec-title",a.textContent=t,a.appendChild(B({When:"Choose the answers to test. All requires every condition to match; Any requires at least one. Compare stored option values, not their display labels.",Then:"Actions applied when the conditions match.",Otherwise:"Optional actions applied when the conditions do not match. Leave empty to do nothing in this branch."}[t],t)),i.appendChild(a),s){const o=document.createElement("span");o.className="fb-trig-sec-hint",o.textContent=s,i.appendChild(o)}return n.appendChild(i),n}function Ze(e,t){const s=document.createElement("button");return s.type="button",s.className="btn btn-xs btn-ghost fb-trig-add",be(s,"plus",e,12),s.addEventListener("click",t),s}function Vt(e,t,s,n){const a=Ht(e,t,e==="then"?"What happens while the answers match.":"What happens while they do not. Leave empty for nothing.");a.classList.add("fb-trig-branch"),a.dataset.branch=e;const o=document.createElement("div");return o.className="fb-trig-action-list",(s||[]).forEach(l=>o.appendChild(Qe(l,n))),a.appendChild(o),a.appendChild(Ze("Add action",()=>{o.appendChild(Qe({type:"banner"},n,{open:!0})),window.I?.scan?.()})),a}function Wt(e,t,s){const n=Rt(t,!0),i=document.createElement("div");i.className="fb-trig-card",i.dataset.triggerId=e.id||`trg-${Date.now()}-${Math.random().toString(36).slice(2,7)}`;const a=document.createElement("div");a.className="fb-trig-card-head";const o=document.createElement("span");o.className="fb-trig-card-title",o.textContent=`Trigger ${s+1}`,a.appendChild(o);const l=we("fb-trig-enabled",e.enabled!==!1,"Enabled","Untick to keep the trigger but switch it off.");if(a.appendChild(l),a.appendChild(Xe(()=>i.remove(),"Remove trigger")),i.appendChild(a),!n.length){const u=document.createElement("p");return u.className="fb-trig-empty",u.textContent="Give this field a name first - a trigger tests an answer, and it needs something to test.",i.appendChild(u),i}const p=Ht("when","When");p.querySelector(".fb-trig-sec-head").appendChild(Q("fb-trig-match",[{value:"all",label:"all of these are true"},{value:"any",label:"any of these is true"}],Array.isArray(e.when?.any)?"any":"all"));const r=document.createElement("div");r.className="fb-trig-cond-list";const d=e.when?.all||e.when?.any||[];return(d.length?d:[{}]).forEach(u=>r.appendChild(Dt(u,n))),p.appendChild(r),p.appendChild(Ze("Add condition",()=>{r.appendChild(Dt({},n)),window.I?.scan?.()})),i.appendChild(p),i.appendChild(Vt("then","Then",e.then,t)),i.appendChild(Vt("otherwise","Otherwise",e.otherwise,t)),i}function Gn(e,t){const s=Array.isArray(e.triggers)?e.triggers:[],n=document.createElement("div");n.className="fb-field-triggers";const i=document.createElement("div");i.className="fb-trig-panel-head";const a=document.createElement("span");a.className="fb-trig-panel-title",a.textContent=`\u{1F3AF} Triggers${s.length?` (${s.length})`:""}`,a.appendChild(B("Triggers test answers and choose a Then or Otherwise branch. Add conditions and actions below. Enabled switches a trigger on without deleting its settings.","Triggers"));const o=document.createElement("button");o.type="button",o.className="btn btn-xs btn-ghost",O(o,s.length?"chevron-down":"chevron-right",12);const l=document.createElement("div");l.className="fb-triggers-body",l.hidden=!s.length,i.addEventListener("click",()=>{l.hidden=!l.hidden,O(o,l.hidden?"chevron-right":"chevron-down",12)});const p=document.createElement("p");p.className="fb-trig-blurb",p.textContent="Do something to the form when the answers meet a condition - show a banner or toast, hide, disable or require other fields, refuse the submit, end the form early, notify the admins, jump to a step, or celebrate.",l.appendChild(p);const r=document.createElement("div");return r.className="fb-trig-list",s.forEach((d,u)=>r.appendChild(Wt(d,t,u))),l.appendChild(r),l.appendChild(Ze("Add trigger",()=>{const d=Wt({enabled:!0,then:[]},t,r.children.length);r.appendChild(d),d.querySelector('.fb-trig-branch[data-branch="then"] .fb-trig-action-list')?.appendChild(Qe({type:"banner"},t,{open:!0})),window.I?.scan?.()})),i.appendChild(a),i.appendChild(o),n.appendChild(i),n.appendChild(l),n}function Ut(e){const t=e.querySelector(".fb-trig-action-type")?.value,s=xe.find(a=>a.value===t);if(!s)return null;const n={type:t},i=a=>e.querySelector(a)?.value?.trim()||"";for(const a of s.params)switch(a){case"message":n.message=i(".fb-trig-p-message");break;case"finishLabel":{const o=i(".fb-trig-p-finish");o&&(n.finishLabel=o);break}case"doneMessage":{const o=i(".fb-trig-p-done");o&&(n.doneMessage=o);break}case"finish":e.querySelector(".fb-trig-p-finish-on")?.checked||(n.finish=!1);break;case"record":e.querySelector(".fb-trig-p-record")?.checked||(n.record=!1);break;case"level":n.level=i(".fb-trig-p-level")||"info";break;case"position":n.position=i(".fb-trig-p-position")||"field-below";break;case"dismissible":n.dismissible=!!e.querySelector(".fb-trig-p-dismissible")?.checked;break;case"target":{const o=i(".fb-trig-p-target")||"rest";n.target=o,o==="listed"&&(n.fields=Array.from(e.querySelectorAll(".fb-trig-p-fields option")).filter(l=>l.selected).map(l=>l.value));break}case"step":{const o=parseInt(i(".fb-trig-p-step"),10);Number.isFinite(o)&&(n.step=o);break}case"url":n.url=i(".fb-trig-p-url");break;case"theme":n.theme=i(".fb-trig-p-theme")||"auto";break;case"intensity":n.intensity=i(".fb-trig-p-intensity")||"medium";break;case"slug":n.slug=i(".fb-trig-p-slug");break;default:break}return s.kind==="event"&&!s.noOnce&&e.querySelector(".fb-trig-p-once")?.checked&&(n.once=!0),n}function Kn(e){const t=Ut(e);if(!t)return null;const s=xe.find(a=>a.value===t.type),n=t.type,i=["block-submit","end-form","notify"].includes(s.value);return s.params.includes("message")&&!t.message&&!i?(delete t.message,null):(i&&!t.message&&delete t.message,n==="redirect"&&!t.url||n==="run-action"&&!t.slug?null:t)}function Yn(e){const s=document.querySelector(`.fb-field-card[data-index="${e}"]`)?.querySelector(".fb-field-triggers");if(!s)return;const n=[];return s.querySelectorAll(".fb-trig-card").forEach(i=>{const a=Array.from(i.querySelectorAll(".fb-trig-cond-row")).map(u=>{const m=u.querySelector(".fb-trig-cond-field")?.value,g=u.querySelector(".fb-trig-cond-op")?.value;if(!m||!g)return null;const f=u.querySelector(".fb-trig-cond-val")?.value??"";return{field:m,operator:g,value:f}}).filter(Boolean);if(!a.length)return;const o=u=>Array.from(i.querySelectorAll(`.fb-trig-branch[data-branch="${u}"] .fb-trig-action-row`)).map(Kn).filter(Boolean),l=o("then"),p=o("otherwise");if(!l.length&&!p.length)return;const r=i.querySelector(".fb-trig-match")?.value==="any"?"any":"all",d={id:i.dataset.triggerId,enabled:!!i.querySelector(".fb-trig-enabled")?.checked,when:{[r]:a}};l.length&&(d.then=l),p.length&&(d.otherwise=p),n.push(d)}),n.length?n:void 0}function Gt(e,t,s){const n=document.createElement("div");if(n.className="fb-field-extras",e==="chooser")n.appendChild(Qn(t,s)),n.appendChild(ei(t.options||[],s));else if(Ue.has(e)&&n.appendChild(si(t.options||[],s)),e==="textarea"&&n.appendChild(z([P("Rows",`fb-rows-${s}`,"number",t.formConfig?.rows||4,"Height of textarea")])),(e==="string"||e==="textarea")&&n.appendChild(z([P("Min Length",`fb-minlength-${s}`,"number",t.minLength||"",""),P("Max Length",`fb-maxlength-${s}`,"number",t.maxLength||"","")])),e==="number"&&n.appendChild(z([P("Min",`fb-min-${s}`,"number",t.min??"",""),P("Max",`fb-max-${s}`,"number",t.max??"","")])),e==="file"){const i=P("Most files",`fb-file-max-${s}`,"number",t.file?.maxFiles||"",`${gt} when left empty, ${He} at most`),a=i.querySelector("input");a.min="1",a.max=String(He),a.placeholder=String(gt);const o=Ie("Several files",`fb-file-multiple-${s}`,!!t.multiple),l=()=>{i.hidden=!o.querySelector("input").checked};o.querySelector("input").addEventListener("change",l),l(),n.appendChild(z([i,o]))}return Kt.includes(e)&&n.appendChild(Xn(t,s)),n.children.length?n:null}const Kt=["string","email","tel","number","url","date","time","password","select","textarea"],et=[{key:"reveal",label:"Show / hide button",types:["password"],hint:"Show / hide lets the visitor see what they typed."},{key:"clear",label:"Clear button",types:["string","email","tel","url","textarea"],hint:"Clear adds an x that empties the field."},{key:"stepper",label:"- / + buttons",types:["number"],hint:"The - and + buttons step the number within its min, max and step."},{key:"counter",label:"Character counter",types:["string","textarea"],hint:"The counter shows how many characters are used, against Max Length if one is set."}];function Jn(e){return e?typeof e=="string"?{icon:"",text:e}:{icon:e.icon||"",text:e.icon?"":e.text??""}:{icon:"",text:""}}function Xn(e,t){const s=document.createElement("div");s.className="fb-addons";const n=(o,l)=>{const p=Jn(e.formConfig?.[o]),r=document.createElement("div");r.style.flex="1";const d=document.createElement("span");d.className="form-label",d.style.fontSize=".8rem",d.textContent=l,d.appendChild(B("An icon or short text joined to this side of the input, such as a currency symbol or unit. Choose an icon or enter text; leave both blank for none.",l));const u=je("Icon, e.g. search",p.icon);u.input.id=`fb-${o}-icon-${t}`,u.input.setAttribute("aria-label",`${l}: icon`);const m=document.createElement("input");return m.type="text",m.className="form-input",m.id=`fb-${o}-text-${t}`,m.value=p.text,m.placeholder="or text, e.g. \xA3 or kg",m.maxLength=24,m.setAttribute("aria-label",`${l}: text`),m.style.marginTop=".35rem",u.input.addEventListener("input",()=>{u.input.value.trim()&&(m.value="")}),m.addEventListener("input",()=>{m.value.trim()&&(u.input.value="")}),r.append(d,u.el,m),r};s.appendChild(z([n("prefix","Before the field"),n("suffix","After the field")]));const i=document.createElement("p");i.className="form-hint text-muted",i.style.cssText="font-size:.73rem;margin:-.4rem 0 .75rem;",i.textContent="An icon or a short text joined to the start or end of the input, like \xA3 before a price or kg after a weight. Leave both empty for none.",s.appendChild(i);const a=et.filter(o=>o.types.includes(e.type));if(a.length){s.appendChild(z(a.map(l=>Ie(l.label,`fb-extra-${l.key}-${t}`,!!e.formConfig?.[l.key]))));const o=document.createElement("p");o.className="form-hint text-muted",o.style.cssText="font-size:.73rem;margin:-.4rem 0 .75rem;",o.textContent=a.map(l=>l.hint).join(" "),s.appendChild(o)}return s}function Yt(e){const t=document.createElement("div");t.className="fb-chooser-section",t.style.cssText="border:1px solid var(--dm-border); border-radius:6px; margin-top:.5rem; overflow:hidden;";const s=document.createElement("div");s.style.cssText="padding:.4rem .6rem; background:color-mix(in srgb, var(--dm-background) 60%, transparent); font-weight:600; font-size:.82rem; border-bottom:1px solid var(--dm-border);",s.textContent=e,t.appendChild(s);const n=document.createElement("div");return n.className="fb-chooser-section-body",n.style.cssText="padding:.55rem .6rem; display:flex; flex-direction:column; gap:.45rem;",t.appendChild(n),{wrap:t,body:n}}function Fe(e,t,s,n,i){const a=document.createElement("div");a.style.flex="1";const o=document.createElement("label");o.htmlFor=e,o.className="form-label",o.textContent=t,i&&o.appendChild(B(i,t)),o.style.fontSize=".8rem";const l=document.createElement("select");if(l.id=e,l.className="form-input",n.forEach(([p,r])=>{const d=document.createElement("option");d.value=p,d.textContent=r,String(p)===String(s)&&(d.selected=!0),l.appendChild(d)}),a.appendChild(o),a.appendChild(l),i){const p=document.createElement("p");p.className="form-hint text-muted",p.textContent=i,p.style.cssText="font-size:.72rem;margin-top:.2rem;",a.appendChild(p)}return a}function Jt(e,t,s,n){const i=document.createElement("div");i.style.flex="1";const a=document.createElement("label");a.style.cssText="display:inline-flex;align-items:center;gap:.4rem;font-size:.82rem;";const o=document.createElement("input");if(o.id=e,o.type="checkbox",o.checked=!!s,a.appendChild(o),a.appendChild(document.createTextNode(" "+t)),n&&a.appendChild(B(n,t)),i.appendChild(a),n){const l=document.createElement("p");l.className="form-hint text-muted",l.textContent=n,l.style.cssText="font-size:.72rem;margin-top:.2rem;",i.appendChild(l)}return i}function tt(e,t,s,n,i){const a=document.createElement("div");a.style.flex="1";const o=document.createElement("label");o.htmlFor=e,o.className="form-label",o.textContent=t,i&&o.appendChild(B(i,t)),o.style.fontSize=".8rem",a.appendChild(o);const l=document.createElement("div");l.style.cssText="display:flex;gap:.4rem;align-items:stretch;";const p=document.createElement("input");p.id=e,p.type="text",p.className="form-input",p.value=s||"",n&&(p.placeholder=n),p.style.flex="1";const r=document.createElement("input");r.type="color",r.title="Pick a colour",r.style.cssText="width:36px;height:auto;padding:0;border:1px solid var(--dm-border);border-radius:4px;cursor:pointer;";const d=String(p.value).match(/^#[0-9a-fA-F]{3,8}$/);if(r.value=d?p.value:"#000000",r.addEventListener("input",()=>{p.value=r.value,p.dispatchEvent(new Event("input",{bubbles:!0}))}),l.appendChild(p),l.appendChild(r),a.appendChild(l),i){const u=document.createElement("p");u.className="form-hint text-muted",u.textContent=i,u.style.cssText="font-size:.72rem;margin-top:.2rem;",a.appendChild(u)}return a}function Qn(e,t){const s=document.createElement("div");s.style.cssText="margin-top:.5rem; display:flex; flex-direction:column; gap:.4rem;";const n=Yt("Layout");n.body.appendChild(z([Fe(`fb-chooser-variant-${t}`,"Variant",e.variant||"card",[["card","Card - rich tiles"],["chip","Chip - compact pills"]],"Card supports description and grid columns; chips wrap."),Jt(`fb-chooser-multiple-${t}`,"Multi-select",!!e.multiple,"Off = single (radio); on = multi (checkbox).")])),n.body.appendChild(z([Fe(`fb-chooser-density-${t}`,"Density",e.density||"comfortable",[["comfortable","Comfortable"],["compact","Compact"]],"Compact strips description (card variant only)."),P("Columns",`fb-chooser-columns-${t}`,"number",e.columns||3,"Card grid (1-6). Ignored for chips.")])),s.appendChild(n.wrap);const i=Yt("Visual");return i.body.appendChild(z([tt(`fb-chooser-accent-${t}`,"Accent",e.accent||"","primary / success / #ec4899","Selected highlight colour - semantic name or any CSS colour."),Fe(`fb-chooser-accent-style-${t}`,"Accent style",e.accentStyle||"border",[["border","Border (default)"],["solid","Solid"],["glow","Glow"],["overlay","Overlay"],["underline","Underline"]],"Visual treatment of the selected state.")])),i.body.appendChild(z([Jt(`fb-chooser-glow-${t}`,"Glow on selected",!!e.glow,"Soft outer glow."),tt(`fb-chooser-glow-colour-${t}`,"Glow colour",e.glowColour||"","defaults to accent","Optional; semantic name or CSS colour.")])),i.body.appendChild(z([Fe(`fb-chooser-shadow-${t}`,"Shadow",e.shadow||"none",[["none","None"],["sm","Small"],["md","Medium"],["lg","Large"],["xl","Extra Large"]],"Drop shadow on every option."),tt(`fb-chooser-shadow-colour-${t}`,"Shadow colour",e.shadowColour||"","rgba(0,0,0,0.1)","Optional shadow tint.")])),s.appendChild(i.wrap),s}function Zn(e,t,s,n){const i=document.createElement("div");i.className="fb-chooser-option-row",i.dataset.optIdx=String(s),i.style.cssText="border:1px solid var(--dm-border); border-radius:4px; padding:.5rem; background:var(--dm-background-alt); display:flex; flex-direction:column; gap:.35rem;";const a=document.createElement("div");a.style.cssText="display:flex; gap:.4rem; align-items:center;";const o=document.createElement("div");o.style.cssText="display:flex; flex-direction:column; gap:1px;";const l=document.createElement("button");l.type="button",l.className="btn btn-xs btn-ghost",l.title="Move up",O(l,"arrow-up"),l.addEventListener("click",j=>{j.preventDefault(),Xt(t,s,-1,n)});const p=document.createElement("button");p.type="button",p.className="btn btn-xs btn-ghost",p.title="Move down",O(p,"arrow-down"),p.addEventListener("click",j=>{j.preventDefault(),Xt(t,s,1,n)}),o.appendChild(l),o.appendChild(p),a.appendChild(o);const r=document.createElement("input");r.type="text",r.className="form-input fb-chooser-opt-value",r.placeholder="value",r.value=e.value||"",r.style.flex="1",a.appendChild(L("Value",r,{help:"The value stored with the submission and compared by trigger conditions. Keep it unique within this field."}));const d=document.createElement("input");d.type="text",d.className="form-input fb-chooser-opt-label",d.placeholder="label (visible to users)",d.value=e.label||"",d.style.flex="2",a.appendChild(L("Label",d,{help:"The choice text visitors see. The stored value can be different."}));const u=document.createElement("button");u.type="button",u.className="btn btn-xs btn-ghost",u.style.cssText="line-height:1;opacity:.6;",u.title="Remove option",O(u,"x"),u.addEventListener("click",j=>{j.preventDefault(),ii(t,s,n)}),a.appendChild(u),i.appendChild(a);const m=document.createElement("div");m.style.cssText="display:flex; gap:.4rem;";const g=je("icon name (optional)",e.icon||"");g.input.classList.add("fb-chooser-opt-icon"),g.el.style.flex="1",m.appendChild(L("Icon",g.el,{help:"An optional icon for this choice. Browse for an icon or leave blank for none."}));const f=document.createElement("input");f.type="text",f.className="form-input fb-chooser-opt-description",f.placeholder="description (card + comfortable only)",f.value=e.description||"",f.style.flex="2",m.appendChild(L("Description",f,{help:"Extra explanation shown by the card variant at comfortable density."})),i.appendChild(m);const v=document.createElement("div");v.style.cssText="display:flex; gap:.4rem;";const w=document.createElement("input");w.type="text",w.className="form-input fb-chooser-opt-tooltip",w.placeholder="tooltip (hover hint)",w.value=e.tooltip||"",w.style.flex="2",v.appendChild(L("Tooltip",w,{help:"An optional hover hint for this choice."}));const N=document.createElement("input");N.type="text",N.className="form-input fb-chooser-opt-badge-text",N.placeholder="badge text",N.value=e.badge?.text||"",N.style.flex="1",v.appendChild(L("Badge text",N,{help:"A short badge label attached to this choice. Leave blank for no badge."}));const x=document.createElement("select");x.className="form-input fb-chooser-opt-badge-type",x.style.flex="1",[["","no badge"],["primary","primary"],["success","success"],["info","info"],["warning","warning"],["danger","danger"]].forEach(([j,Y])=>{const U=document.createElement("option");U.value=j,U.textContent=Y,(e.badge?.type||"")===j&&(U.selected=!0),x.appendChild(U)}),v.appendChild(L("Badge style",x,{help:"The theme colour used for the badge."})),i.appendChild(v);const b=document.createElement("div");b.style.cssText="display:flex; gap:1rem; font-size:.8rem;";const C=document.createElement("label");C.style.cssText="display:inline-flex;align-items:center;gap:.3rem;";const $=document.createElement("input");$.type="checkbox",$.className="fb-chooser-opt-recommended",$.checked=!!e.recommended,C.appendChild($),C.appendChild(document.createTextNode(" Recommended")),C.appendChild(B("Highlight this choice as recommended. This does not select it automatically.","Recommended")),b.appendChild(C);const _=document.createElement("label");_.style.cssText="display:inline-flex;align-items:center;gap:.3rem;";const W=document.createElement("input");return W.type="checkbox",W.className="fb-chooser-opt-disabled",W.checked=!!e.disabled,_.appendChild(W),_.appendChild(document.createTextNode(" Disabled")),_.appendChild(B("Show this choice but prevent the visitor from selecting it.","Disabled")),b.appendChild(_),i.appendChild(b),i}function ei(e,t){const s=document.createElement("div");s.style.cssText="margin-top:.5rem;";const n=document.createElement("div");n.style.cssText="display:flex; align-items:center; justify-content:space-between; margin-bottom:.4rem;";const i=document.createElement("p");i.textContent="Options",i.appendChild(B("Each option has a stored value and a label shown to visitors. Keep values unique and use them in trigger conditions. Add an icon, description, tooltip or badge to explain a choice. Recommended highlights an option; Disabled prevents selection.","Chooser options")),i.style.cssText="font-size:.85rem;font-weight:600;margin:0;",n.appendChild(i);const a=document.createElement("button");a.type="button",a.className="btn btn-xs btn-ghost",be(a,"plus","Add option"),n.appendChild(a),s.appendChild(n);const o=document.createElement("div");o.className="fb-chooser-options-list",o.id=`fb-chooser-options-${t}`,o.style.cssText="display:flex; flex-direction:column; gap:.4rem;",s.appendChild(o);const l=()=>{o.replaceChildren();const p=ti(t);(p.length?p:e||[]).forEach((d,u)=>{o.appendChild(Zn(d,t,u,l))})};return a.addEventListener("click",p=>{p.preventDefault(),ni(t,l)}),l(),s}function ti(e){const t=T[e];return!t||!Array.isArray(t.options)?[]:t.options}function nt(e){return T[e]?(Array.isArray(T[e].options)||(T[e].options=[]),T[e].options):[]}function ni(e,t){Be(e),nt(e).push({value:"new",label:"New option"}),t()}function ii(e,t,s){Be(e),nt(e).splice(t,1),s()}function Xt(e,t,s,n){Be(e);const i=nt(e),a=t+s;if(a<0||a>=i.length)return;const[o]=i.splice(t,1);i.splice(a,0,o),n()}function Be(e){const t=document.getElementById(`fb-chooser-options-${e}`);if(!t)return;const s=t.querySelectorAll(".fb-chooser-option-row"),n=[];s.forEach(i=>{const a={value:i.querySelector(".fb-chooser-opt-value")?.value.trim()||"",label:i.querySelector(".fb-chooser-opt-label")?.value.trim()||""},o=i.querySelector(".fb-chooser-opt-icon")?.value.trim();o&&(a.icon=o);const l=i.querySelector(".fb-chooser-opt-description")?.value.trim();l&&(a.description=l);const p=i.querySelector(".fb-chooser-opt-tooltip")?.value.trim();p&&(a.tooltip=p);const r=i.querySelector(".fb-chooser-opt-badge-text")?.value.trim(),d=i.querySelector(".fb-chooser-opt-badge-type")?.value;(r||d)&&(a.badge={},r&&(a.badge.text=r),d&&(a.badge.type=d)),i.querySelector(".fb-chooser-opt-recommended")?.checked&&(a.recommended=!0),i.querySelector(".fb-chooser-opt-disabled")?.checked&&(a.disabled=!0),n.push(a)}),T[e]&&(T[e].options=n)}function z(e){const t=document.createElement("div");return t.style.cssText="display:flex;gap:.85rem;margin-bottom:.75rem;",e.forEach(s=>{s&&t.appendChild(s)}),t}function P(e,t,s,n,i){const a=document.createElement("div");a.style.flex="1";const o=document.createElement("label");o.htmlFor=t,o.className="form-label",o.textContent=e,o.style.fontSize=".8rem",(ge[e]||i)&&o.appendChild(B(ge[e]||i,e));const l=document.createElement("input");if(l.id=t,l.type=s||"text",l.className="form-input",l.value=n??"",a.appendChild(o),a.appendChild(l),i){const p=document.createElement("p");p.className="form-hint text-muted",p.textContent=i,p.style.cssText="font-size:.73rem;margin-top:.35rem;",a.appendChild(p)}return a}function ai(e,t,s,n){const i=document.createElement("div");i.style.flex="1";const a=document.createElement("label");a.htmlFor=t,a.className="form-label",a.textContent=e,a.style.fontSize=".8rem",ge[e]&&a.appendChild(B(ge[e],e));const o=document.createElement("select");return o.id=t,o.className="form-select",s.forEach(l=>{const p=document.createElement("option");p.value=l.value,p.textContent=l.label,l.value===n&&(p.selected=!0),o.appendChild(p)}),i.appendChild(a),i.appendChild(o),i}function Ie(e,t,s){const n=document.createElement("div");n.style.cssText="flex:0;min-width:80px;display:flex;flex-direction:column;justify-content:flex-end;";const i=document.createElement("label");i.style.cssText="display:flex;align-items:center;gap:.4rem;cursor:pointer;font-size:.8rem;white-space:nowrap;";const a=document.createElement("input");return a.id=t,a.type="checkbox",a.checked=s,i.appendChild(a),i.appendChild(document.createTextNode(e)),i.appendChild(B(ge[e]||et.find(o=>o.label===e)?.hint||e,e)),n.appendChild(i),n}function si(e,t){const s=document.createElement("div");s.style.cssText="margin-top:.4rem;";const n=document.createElement("p");n.textContent="Options (one per line: value or value:Label)",n.style.cssText="font-size:.8rem;font-weight:600;margin-bottom:.3rem;";const i=document.createElement("textarea");return i.id=`fb-options-${t}`,i.className="form-input",i.rows=4,i.placeholder=`yes:Yes
|
|
6
6
|
no:No
|
|
7
|
-
maybe:Maybe`,
|
|
8
|
-
`),a.style.fontFamily="monospace",n.appendChild(A("Enter one choice per line. Use value:Label, for example yes:Yes please. The value is stored and tested by conditions; the label is shown to visitors. A line without a colon uses the same text for both.","Options")),s.appendChild(n),s.appendChild(a),s}function Me(e){return parseInt(e,10)===3?3:2}const oa=[["#setting-submit-icon-start-slot","setting-submit-icon-start"],["#setting-submit-icon-end-slot","setting-submit-icon-end"],["#setting-second-icon-start-slot","setting-second-icon-start"],["#setting-second-icon-end-slot","setting-second-icon-end"]];function la(e){oa.forEach(([t,s])=>{const n=e.find(t).get(0);if(!n||n.querySelector(`#${s}`))return;const a=je("none","");a.input.id=s,a.input.classList.add("form-input"),n.appendChild(a.el)})}function Qt(e){const t=e.find("#setting-second-kind").val()||"",s=e.find("#second-button-group").get(0);s&&(s.style.display=t?"":"none");const n=e.find("#second-url-group").get(0);n&&(n.style.display=t==="link"?"":"none");const a=e.find("#setting-second-label").get(0);a&&(a.placeholder=t==="link"?"Cancel":"Clear form")}function ra(e){const t=i=>String(e.find(i).val()||"").trim(),s={},n={style:t("#setting-submit-style")||"primary",size:t("#setting-submit-size"),iconStart:t("#setting-submit-icon-start"),iconEnd:t("#setting-submit-icon-end")};(n.style!=="primary"||n.size||n.iconStart||n.iconEnd)&&(s.submitButton=n);const a=t("#setting-second-kind");return(a==="reset"||a==="link")&&(s.secondaryButton={kind:a,label:t("#setting-second-label"),...a==="link"?{url:t("#setting-second-url")}:{},style:t("#setting-second-style")||"secondary",iconStart:t("#setting-second-icon-start"),iconEnd:t("#setting-second-icon-end"),first:t("#setting-second-order")==="first",stack:t("#setting-second-arrange")==="stack"}),s}function Zt(e,t){const s=t&&t.submitButton||{},n=t&&t.secondaryButton||{};e.find("#setting-submit-style").val(s.style||"primary"),e.find("#setting-submit-size").val(s.size||""),e.find("#setting-submit-icon-start").val(s.iconStart||""),e.find("#setting-submit-icon-end").val(s.iconEnd||""),e.find("#setting-second-kind").val(n.kind==="reset"||n.kind==="link"?n.kind:""),e.find("#setting-second-label").val(n.label||""),e.find("#setting-second-url").val(n.url||""),e.find("#setting-second-style").val(n.style||"secondary"),e.find("#setting-second-icon-start").val(n.iconStart||""),e.find("#setting-second-icon-end").val(n.iconEnd||""),e.find("#setting-second-order").val(n.first?"first":"after"),e.find("#setting-second-arrange").val(n.stack?"stack":"row"),Qt(e)}function Ce(e){const t=e.find("#field-project").val()||"";return V(),{title:e.find("#field-title").val().trim(),slug:e.find("#field-slug").val().trim(),description:e.find("#field-description").val().trim(),...e.find("#field-bundled").is(":checked")?{bundled:!0}:{},meta:{...Te||{},project:t||null},fields:T,settings:{submitText:e.find("#setting-submit-text").val().trim()||"Submit",successMessage:e.find("#setting-success-message").val().trim()||"Thank you.",layout:e.find("#setting-layout").val()||"grid",columns:Me(e.find("#setting-columns").val()),submitAlign:e.find("#setting-submit-align").val()||"",...ra(e),...e.find("#setting-form-class").val().trim()&&{class:e.find("#setting-form-class").val().trim()},...e.find("#setting-submit-class").val().trim()&&{submitClass:e.find("#setting-submit-class").val().trim()},honeypot:e.find("#setting-honeypot").prop("checked"),rateLimitPerMinute:parseInt(e.find("#setting-rate-limit").val(),10)||3,successRedirect:e.find("#setting-success-redirect").val().trim()||null,actionSlug:e.find("#action-cms-slug").val()||null},actions:{email:{enabled:e.find("#action-email-enabled").prop("checked"),recipients:e.find("#action-email-recipients").val().trim(),subjectPrefix:e.find("#action-email-subject-prefix").val().trim()},webhook:{enabled:e.find("#action-webhook-enabled").prop("checked"),url:e.find("#action-webhook-url").val().trim(),method:e.find("#action-webhook-method").val()},...Se!==null&&{collection:Se}}}}function en(e,t){const s={};return e.forEach(n=>{if(n.type==="page-break"||n.type==="spacer")return;const a={...n.formConfig||{}};a.span==="full"&&t&&(a.span=t);const i=n.type==="chooser"||!!(n.variant&&["select","radio","checkbox-group"].includes(n.type)),l={type:n.type==="checkbox"?"boolean":n.type==="date"||n.type==="icon"||n.type==="image"?"string":i?"chooser":n.type,label:n.label,required:n.required,options:n.options,formConfig:{...n.placeholder&&{placeholder:n.placeholder},...n.helper&&{helperText:n.helper},...n.tooltip&&{tooltip:n.tooltip},...a}};i&&(n.variant&&(l.variant=n.variant),(n.multiple||n.type==="checkbox-group")&&(l.multiple=!0),n.density&&(l.density=n.density),n.columns&&(l.columns=n.columns),n.accent&&(l.accent=n.accent),n.accentStyle&&(l.accentStyle=n.accentStyle),n.glow&&(l.glow=!0),n.glowColour&&(l.glowColour=n.glowColour),n.shadow&&(l.shadow=n.shadow),n.shadowColour&&(l.shadowColour=n.shadowColour)),s[n.name]=n.type==="file"?un(n,l):l}),s}function tn(e,t){const s=typeof e=="string"?document.querySelector(e):e;s&&(t||[]).forEach(n=>{if(n.type!=="date"||!n.name)return;const a=s.querySelector(`[name="${n.name}"]`);a&&a.type!=="date"&&(a.type="date")})}function at(e,t){const s=e?.querySelector?.(".dm-plugin-banner-name .dm-plugin-banner-sep");if(s){for(;s.nextSibling;)s.nextSibling.remove();s.after(document.createTextNode(String(t||"")))}}const nn=e=>String(e??"").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""");let J=null,an=0,sn=!1;const Oe=e=>String(e||"").split("?")[0].replace(/\/$/,"");function ca(){sn||!window.R?.use||(sn=!0,R.use((e,t,s)=>!J||!kt(t?.path,e?.path)||J.path!==Oe(t?.path)||(J.flush(),!J.dirty())?s():E.confirm("You have unsaved changes to this form. Leave anyway? They are kept as a draft for your next visit to it.").then(n=>{n?s():history.replaceState(null,"",`#${t.path}`)})),M.subscribe("router:afterChange",({to:e,from:t}={})=>{if(J&&J.path===Oe(t?.path)&&kt(t?.path,e?.path)){const s=J;J=null,s.dispose()}}))}export const formEditorView={templateUrl:"/admin/js/templates/form-editor.html",async onMount(e){if(ca(),J){const c=J;J=null,c.dispose()}const t=Oe(location.hash.replace(/^#/,"")),s=++an,n=()=>s!==an||!e.get(0)?.isConnected||Oe(location.hash.replace(/^#/,""))!==t;T=[],I=null,Se=null,la(e),e.find("#setting-second-kind").get(0)?.addEventListener("change",()=>Qt(e));const a=window.location.hash.match(/\/forms\/edit\/([^/?#]+)/);I=a?a[1]:null;let i=null,o=null;const l=E.loader(e.get(0),{type:"dots"});if(I)try{if(i=await fe(`/forms/${I}`),n()){l.destroy();return}T=i.fields||[],Se=i.actions?.collection??null}catch(c){if(n()){l.destroy();return}o={missing:c?.status===404,message:c?.message||String(c)},E.toast(o.missing?"This form no longer exists.":`Could not load the form: ${o.message}`,{type:"error"})}Te={};try{const c=await fe("/projects").catch(()=>[]);if(n()){l.destroy();return}const h=e.find("#field-project").get(0);h&&(Array.isArray(c)?c:[]).forEach(g=>{const k=document.createElement("option");k.value=g.slug,k.textContent=g.name||g.slug,h.appendChild(k)})}catch{}if(l.destroy(),at(e.get(0),i?i.title||i.slug:"New form"),I||e.find("#field-title").get(0).addEventListener("input",function(){e.find("#field-slug").val(Nt(this.value))}),E.tabs(e.find("#editor-tabs").get(0)),i){e.find("#field-title").val(i.title),e.find("#field-slug").val(i.slug),e.find("#field-description").val(i.description||""),e.find("#field-bundled").prop("checked",!!i.bundled),Te=i.meta||{},e.find("#field-project").val(i.meta?.project||"");const c=i.settings||{};e.find("#setting-submit-text").val(c.submitText||"Submit"),e.find("#setting-success-message").val(c.successMessage||""),e.find("#setting-layout").val(c.layout||"grid"),e.find("#setting-columns").val(Me(c.columns)),e.find("#setting-submit-align").val(wt(c,c.layout||"grid")),Zt(e,c),e.find("#setting-form-class").val(c.class||""),e.find("#setting-submit-class").val(c.submitClass||""),e.find("#columns-group").get(0).style.display=(c.layout||"grid")==="grid"?"":"none",e.find("#setting-honeypot").prop("checked",c.honeypot!==!1),e.find("#setting-rate-limit").val(c.rateLimitPerMinute||3),e.find("#setting-success-redirect").val(c.successRedirect||"");const h=i.actions?.email||{};e.find("#action-email-enabled").prop("checked",h.enabled||!1),e.find("#action-email-recipients").val(h.recipients||""),e.find("#action-email-subject-prefix").val(h.subjectPrefix||"");const g=i.actions?.webhook||{};e.find("#action-webhook-enabled").prop("checked",g.enabled||!1),e.find("#action-webhook-url").val(g.url||""),e.find("#action-webhook-method").val(g.method||"POST")}const d=i?.settings?.actionSlug||"";try{const c=await fe("/actions").catch(()=>[]);if(n())return;const h=e.find("#action-cms-slug").get(0);if(ke=Array.isArray(c)?c:[],(Array.isArray(c)?c:[]).forEach(g=>{const k=document.createElement("option");k.value=g.slug,k.textContent=g.title||g.slug,g.slug===d&&(k.selected=!0),h.appendChild(k)}),!c.length){const g=document.createElement("option");g.value="",g.textContent="No actions available",g.disabled=!0,h.appendChild(g)}}catch{}const r=c=>{e.find("#columns-group").get(0).style.display=c?"":"none",document.querySelectorAll(".fb-grid-row").forEach(h=>{h.style.display=c?"flex":"none"})};e.find("#setting-layout").get(0)?.addEventListener("change",function(){r(this.value==="grid")}),r(e.find("#setting-layout").val()==="grid"),ne(e);const u=e.find("#add-element-menu").get(0);e.find("#add-element-btn").get(0).addEventListener("click",c=>{c.stopPropagation(),u.style.display=u.style.display==="none"?"":"none"}),oe&&document.removeEventListener("click",oe),oe=()=>{u&&(u.style.display="none")},document.addEventListener("click",oe),e.find("#add-field-btn").get(0).addEventListener("click",()=>{u&&(u.style.display="none"),V();const c=T.length;T.push({name:`field_${c+1}`,type:"string",label:"New Field",required:!1,placeholder:""}),ne(e);const h=e.find("#fields-list").get(0)?.lastElementChild;if(h){const g=h.querySelector(".fb-field-body");g&&(g.style.display="")}}),e.find("#add-spacer-btn").get(0).addEventListener("click",()=>{u&&(u.style.display="none"),V(),T.push({type:"spacer"}),ne(e)}),e.find("#add-page-break-btn").get(0).addEventListener("click",()=>{u&&(u.style.display="none"),V();const c=T.filter(h=>h.type==="page-break").length+2;T.push({type:"page-break",label:`Step ${c}`,description:""}),ne(e)}),Le=()=>{const c=Ce(e),h=e.find("#preview-container").get(0);if(!h)return;const g=e.find("#preview-test-result").get(0),k=e.find("#preview-test-badge").get(0);g&&(g.style.display="none",g.textContent=""),k&&(k.style.display=I?"":"none"),e.find("#preview-card").get(0).style.display="",vt(h),h.textContent="";const N=document.createElement("div");N.id="fb-preview-form",h.appendChild(N);const H=new Map;hn(N,c.fields);const K=I?async D=>{g&&(g.style.display="none",g.textContent="");try{const $=new Map(H);yt(N,$);const te=mn(D,c.fields,$),ie=fn(te.values,te.files),G=await fetch(`/api/forms/submit/${I}`,{method:"POST",headers:ie.headers,body:ie.body}),X=await G.json();if(!G.ok)throw new Error(X.error||"Submission failed.");g&&(g.textContent=X.message||c.settings?.successMessage||"Submitted successfully.",g.style.cssText="display:block;margin-top:.75rem;padding:.6rem .9rem;border-radius:6px;font-size:.9rem;background:color-mix(in srgb, var(--dm-success) 12%, transparent);color:var(--dm-success);"),E.toast("Test submission stored.",{type:"success"})}catch($){g&&(g.textContent=$.message,g.style.cssText="display:block;margin-top:.75rem;padding:.6rem .9rem;border-radius:6px;font-size:.9rem;background:color-mix(in srgb, var(--dm-danger) 12%, transparent);color:var(--dm-danger);"),E.toast($.message,{type:"error"})}return!1}:()=>!1,me=c.fields.some(D=>D.type==="page-break");if(N.addEventListener("click",D=>{const $=D.target.closest?.("a.fb-secondary-btn");$&&(D.preventDefault(),E.toast(`Cancel takes the visitor to ${$.getAttribute("href")}`,{type:"info"}))}),N.addEventListener("dm-form-end",D=>{const $=D.detail?.validation;if($&&!$.valid){E.toast(`Still to answer: ${$.missing.map(te=>te.label).concat($.errors).join(", ")}`,{type:"warning"});return}K(D.detail?.values||{})}),typeof F<"u"){const D=Me(c.settings?.columns),$=c.settings?.layout||"grid";if(me&&F.wizard){const te=dn(c.fields,c.title),ie=te.map(se=>({title:se.title,description:se.description,fields:en(se.fields,D)}));let G=0,X=null,_e=null;const ht=se=>{const Pe=(te[G]||{}).fields||[];gn(N,c.settings),xt(N,c.settings,Pe),tn("#fb-preview-form",Pe),bt(N,Pe),pn(N,H),se&&window.FormLogicEngine&&$t(c.fields)&&(X?.destroy?.(),X=new window.FormLogicEngine.FormLogicRuntime({fields:c.fields},N,{baseValues:_e?.getStepData?_e.getStepData():{}}),X.init())};_e=F.wizard("#fb-preview-form",{schema:{steps:ie},layout:$,columns:D,finishText:c.settings?.submitText||"Finish",onStepChange:se=>{yt(N,H),G=se,queueMicrotask(()=>ht(!0))},onSubmit:K}),ht(!1)}else if(F.render){const te=en(c.fields,D),ie={};c.fields.forEach(G=>{if(!(!G.name||G.type==="page-break"||G.type==="spacer")&&(G.type==="select"||G.type==="multiselect")&&G.required){const X=(G.options||[])[0];X&&(ie[G.name]=typeof X=="object"?X.value:X)}}),F.render("#fb-preview-form",te,ie,{submitText:c.settings?.submitText||"Submit",layout:$,columns:D,onSubmit:K}),bn(N,c.settings,$),xt(N,c.settings,c.fields),tn("#fb-preview-form",c.fields),bt(N,c.fields)}window.FormLogicEngine&&$t(c.fields)&&requestAnimationFrame(()=>{new window.FormLogicEngine.FormLogicRuntime({fields:c.fields},N).init()})}else{const D=document.createElement("p");D.textContent=`${c.fields.filter($=>$.type!=="page-break").length} field(s): ${c.fields.filter($=>$.type!=="page-break").map($=>$.label).join(", ")}`,D.style.cssText="color:var(--dm-text-muted);font-style:italic;",N.appendChild(D)}},Le();const p=e.find("#editor-tabs").get(0),m=e.find("#preview-card").get(0),v=c=>{m&&m.contains(c.target)||Ke()};p&&(p.addEventListener("input",v),p.addEventListener("change",v));const f=e.get(0),y=[],C=c=>(y.push(c),c);let x=!1;const b=M.observable(!1),w=M.observable(!1);let q=I,B=null,_=!1,W=!1,j=!1,Y=Ce(e),U=Ee(Y),ae=Y;const le=()=>{x||!f.isConnected||(ae=Ce(e),b.value=Ee(ae)!==U)};let de=null;Ge=()=>{clearTimeout(de),de=setTimeout(le,150)},C(()=>{clearTimeout(de),Ge=null,ve=null}),C(()=>vt(f.querySelector("#preview-container")));const Re=c=>{const h=c.settings||{};e.find("#field-title").val(c.title||""),e.find("#field-slug").val(c.slug||""),e.find("#field-description").val(c.description||""),e.find("#field-bundled").prop("checked",!!c.bundled),Te={...c.meta||{}},e.find("#field-project").val(c.meta?.project||""),e.find("#setting-submit-text").val(h.submitText||"Submit"),e.find("#setting-success-message").val(h.successMessage||""),e.find("#setting-layout").val(h.layout||"grid"),e.find("#setting-columns").val(Me(h.columns)),e.find("#setting-submit-align").val(wt(h,h.layout||"grid")),Zt(e,h),e.find("#setting-form-class").val(h.class||""),e.find("#setting-submit-class").val(h.submitClass||""),e.find("#setting-honeypot").prop("checked",h.honeypot!==!1),e.find("#setting-rate-limit").val(h.rateLimitPerMinute||3),e.find("#setting-success-redirect").val(h.successRedirect||""),e.find("#action-cms-slug").val(h.actionSlug||"");const g=c.actions?.email||{};e.find("#action-email-enabled").prop("checked",!!g.enabled),e.find("#action-email-recipients").val(g.recipients||""),e.find("#action-email-subject-prefix").val(g.subjectPrefix||"");const k=c.actions?.webhook||{};e.find("#action-webhook-enabled").prop("checked",!!k.enabled),e.find("#action-webhook-url").val(k.url||""),e.find("#action-webhook-method").val(k.method||"POST"),T=JSON.parse(JSON.stringify(c.fields||[])),r((h.layout||"grid")==="grid"),ne(e)};async function De(){if(w.peek()||x)return;if(o){E.toast("This form did not load, so saving would replace it with an empty one. Reload the page to try again.",{type:"error"});return}const c=Ce(e);if(!c.title){E.toast("Please enter a form title (Form Details).",{type:"error"});return}const h=yn(c.settings);if(h.length){E.toast(`${h[0]} (Settings > Submit Button)`,{type:"error"});return}const g=vn(c.settings,c.fields);if(g.length){E.toast(g[0],{type:"error"});return}const k=Hn(c.fields);k.length&&E.toast(`Some trigger messages use ${k.map(K=>`{{${K}}}`).join(", ")}, which will show as blank: no field has that name, or it is a password, file or image.`,{type:"warning",duration:9e3}),w.value=!0;const N=I,H=he(q);N||(f.inert=!0);try{if(I?(await fe(`/forms/${I}`,{method:"PUT",body:JSON.stringify(c)}),E.toast("Form saved.",{type:"success"})):(I=(await fe("/forms",{method:"POST",body:JSON.stringify(c)})).slug,E.toast("Form created.",{type:"success"})),x){if(!N)try{S.remove?S.remove(H):S.set(H,null)}catch{}return}q=I,Y=c,U=Ee(c),B=null,W=!1,j=!1,clearTimeout(de),le();for(const K of new Set([H,he(I)]))try{S.remove?S.remove(K):S.set(K,null)}catch{}at(f,c.title),wn("#/forms"),N||R.navigate(`/forms/edit/${I}`)}catch(K){N||(I=null),E.toast(K.message||"Failed to save form.",{type:"error"})}finally{w.value=!1,f.inert=!1}}async function on(){b.peek()&&(!await E.confirm("Throw away the changes you have not saved?",{confirmText:"Throw away",cancelText:"Keep editing"})||x||(Re(Y),le(),pt()))}const it=f.querySelector(".fex-bar");if(it){const c=M.applyBindings({dirty:b,saving:w},it,{methods:{save:De,discard:on}});C(()=>c.dispose())}const ue=new Map,st=()=>{for(const[c,h]of ue)f.contains(c)||(h(),ue.delete(c));for(const c of f.querySelectorAll("[data-help]:not([data-help-ready])"))ue.set(c,xn(c,{position:"bottom"}))};st();const ot=new MutationObserver(st);ot.observe(f,{childList:!0,subtree:!0}),C(()=>{ot.disconnect();for(const c of ue.values())c();ue.clear()}),Ve(e,{id:"form-editor-save",icon:"save",title:"Save (Ctrl+S)",onClick:()=>De()}),Ve(e,{id:"form-editor-back",icon:"arrow-left",title:"All forms",onClick:()=>{location.hash="#/forms"}}),I&&Ve(e,{id:"form-editor-subs",icon:"list",title:"Submissions",onClick:()=>{location.hash=`#/forms/${encodeURIComponent(I)}/submissions`}});const lt=c=>{n()||!(c.ctrlKey||c.metaKey)||String(c.key).toLowerCase()!=="s"||(c.preventDefault(),c.stopImmediatePropagation(),De())};document.addEventListener("keydown",lt,!0),C(()=>document.removeEventListener("keydown",lt,!0));const Z=e.find("#fields-list").get(0),ln=c=>c?.closest?.(".fb-field-card"),rt=c=>Number(c?.dataset.index),ct=c=>c?.querySelector(":scope > .fb-field-body"),dt=c=>{const h=ct(c);h&&(h.style.display=h.style.display==="none"?"":"none")},pe=(c,h)=>{T=c,ne(e),h!=null&&Z?.querySelectorAll(".fb-field-card")[h]?.focus()},rn=async c=>{try{await navigator.clipboard.writeText(c),E.toast(`Copied "${c}".`,{type:"success"})}catch{E.toast(`Copy failed - the name is ${c}.`,{type:"info"})}},cn=c=>{const h=ln(c),g=rt(h);V();const k=T[g];if(!k)return[];const N=k.type!=="spacer"&&k.type!=="page-break",H=ct(h),K=H&&H.style.display!=="none";return[{header:We(k)},...H?[{label:K?"Close":"Edit",icon:K?"chevron-up":"edit",action:()=>dt(h)}]:[],...N?[{label:"Logic & triggers",icon:"zap",action:()=>{H&&(H.style.display=""),(H?.querySelector(".fb-field-logic")||H)?.scrollIntoView({block:"center",behavior:"smooth"})}}]:[],{label:"Duplicate",icon:"copy",action:()=>{V(),pe(Sn(T,g),g+1)}},{type:"divider"},{label:"Move up",icon:"arrow-up",shortcut:"Alt+\u2191",disabled:g===0,action:()=>{V(),pe(St(T,g,-1),g-1)}},{label:"Move down",icon:"arrow-down",shortcut:"Alt+\u2193",disabled:g>=T.length-1,action:()=>{V(),pe(St(T,g,1),g+1)}},...N&&k.name?[{label:"Copy field name",icon:"clipboard",action:()=>rn(k.name)}]:[],{type:"divider"},{label:"Delete\u2026",icon:"trash",danger:!0,action:async()=>{if(k.type!=="spacer"&&!await E.confirm(`Remove "${nn(We(k))}"?`))return;V();const me=[...T];me.splice(g,1),pe(me,Math.min(g,me.length-1))}}]};if(Z&&typeof E.sortable=="function"){const c=E.sortable(Z,{items:".fb-field-card",handle:".fb-drag-grip",key:null,onSort:({from:h,to:g})=>{V();const k=[...T],[N]=k.splice(h,1);k.splice(g,0,N),pe(k,g)}});C(()=>c.destroy())}let ut=null;if(Z&&E.contextMenu&&(ut=E.contextMenu(Z,{cursor:"glyph",match:".fb-field-card > :first-child",inherit:!1,density:"compact",items:c=>cn(c)}),C(()=>ut?.destroy?.())),ve=c=>{for(const h of c.querySelectorAll(".fb-field-card")){h.tabIndex=0;const g=h.firstElementChild;if(!g||g.querySelector(".fex-kebab"))continue;const k=document.createElement("button");k.type="button",k.className="fex-kebab",k.textContent="\u22EE",k.title="Actions",V(),k.setAttribute("aria-label",`Actions for ${We(T[rt(h)])}`),k.addEventListener("click",N=>{N.stopPropagation(),Ct(k)}),g.appendChild(k)}},Z){ve(Z);const c=h=>{const g=h.target?.classList?.contains("fb-field-card")?h.target:null;g&&(h.key==="F10"&&h.shiftKey?(h.preventDefault(),Ct(g.firstElementChild)):h.key==="Enter"&&(h.preventDefault(),dt(g)))};Z.addEventListener("keydown",c),C(()=>Z.removeEventListener("keydown",c))}const pt=()=>{try{const c=he(q);S.remove?S.remove(c):S.set(c,null)}catch{}},mt=c=>{if(!o){if(!b.peek()||!c){!_&&!j&&pt();return}try{S.set(he(q),{data:kn(c),base:Cn(B,Et(U)),...W?{newFrom:!0}:{},at:new Date().toISOString()})}catch{}}},ft=c=>{le(),b.peek()&&(mt(ae),c.preventDefault(),c.returnValue="")};if(window.addEventListener("beforeunload",ft),C(()=>window.removeEventListener("beforeunload",ft)),C(()=>{oe&&(document.removeEventListener("click",oe),oe=null)}),J={path:t,dirty:()=>b.peek(),flush:()=>{clearTimeout(de),le()},dispose(){x||(mt(ae),x=!0,y.splice(0).reverse().forEach(c=>{try{c?.()}catch{}}))}},o){const c=document.createElement("p");c.className="fex-load-error",c.setAttribute("role","alert"),c.textContent=o.missing?"This form no longer exists - it may have been deleted. Nothing here can be saved over it.":`This form could not be loaded (${o.message}). Saving is off so an empty editor cannot replace it - reload the page to try again.`;const h=f.querySelector(".fex-bar");h?h.before(c):f.prepend(c),C(()=>c.remove())}const ze=he(I);let ee=null;try{ee=S.get(ze)}catch{}if(ee?.data&&(!o||o.missing)){const c=!!o?.missing,h=En(ee,{missing:c});_=!0;const g=await E.confirm(nn(Tn(ee,{current:Et(U),missing:c,when:ee.at?Nn(ee.at):""})),{title:"Unsaved changes",confirmText:"Throw away",cancelText:h==="new"?"Start a new form":h==="refuse"?"Not now":"Keep them"});if(_=!1,g)try{S.remove?S.remove(ze):S.set(ze,null)}catch{}else h==="refuse"?j=!0:x||(c?(I=null,o=null,B="",W=!0,Y=Ce(e),U=Ee(Y),at(f,"New form"),f.querySelector(".fex-load-error")?.remove(),Re(ee.data)):(B=ee.base??"",Re(Ln(ee.data,Y))),le())}}};
|
|
7
|
+
maybe:Maybe`,i.value=(e||[]).map(a=>{const o=typeof a=="string"?a:a.value??"",l=typeof a=="string"?a:a.label??o;return o===l?o:`${o}:${l}`}).join(`
|
|
8
|
+
`),i.style.fontFamily="monospace",n.appendChild(B("Enter one choice per line. Use value:Label, for example yes:Yes please. The value is stored and tested by conditions; the label is shown to visitors. A line without a colon uses the same text for both.","Options")),s.appendChild(n),s.appendChild(i),s}function Oe(e){return parseInt(e,10)===3?3:2}const oi=[["#setting-submit-icon-start-slot","setting-submit-icon-start"],["#setting-submit-icon-end-slot","setting-submit-icon-end"],["#setting-second-icon-start-slot","setting-second-icon-start"],["#setting-second-icon-end-slot","setting-second-icon-end"]];function li(e){oi.forEach(([t,s])=>{const n=e.find(t).get(0);if(!n||n.querySelector(`#${s}`))return;const i=je("none","");i.input.id=s,i.input.classList.add("form-input"),n.appendChild(i.el)})}function Qt(e){const t=e.find("#setting-second-kind").val()||"",s=e.find("#second-button-group").get(0);s&&(s.style.display=t?"":"none");const n=e.find("#second-url-group").get(0);n&&(n.style.display=t==="link"?"":"none");const i=e.find("#setting-second-label").get(0);i&&(i.placeholder=t==="link"?"Cancel":"Clear form")}function ri(e){const t=a=>String(e.find(a).val()||"").trim(),s={},n={style:t("#setting-submit-style")||"primary",size:t("#setting-submit-size"),iconStart:t("#setting-submit-icon-start"),iconEnd:t("#setting-submit-icon-end")};(n.style!=="primary"||n.size||n.iconStart||n.iconEnd)&&(s.submitButton=n);const i=t("#setting-second-kind");return(i==="reset"||i==="link")&&(s.secondaryButton={kind:i,label:t("#setting-second-label"),...i==="link"?{url:t("#setting-second-url")}:{},style:t("#setting-second-style")||"secondary",iconStart:t("#setting-second-icon-start"),iconEnd:t("#setting-second-icon-end"),first:t("#setting-second-order")==="first",stack:t("#setting-second-arrange")==="stack"}),s}function Zt(e,t){const s=t&&t.submitButton||{},n=t&&t.secondaryButton||{};e.find("#setting-submit-style").val(s.style||"primary"),e.find("#setting-submit-size").val(s.size||""),e.find("#setting-submit-icon-start").val(s.iconStart||""),e.find("#setting-submit-icon-end").val(s.iconEnd||""),e.find("#setting-second-kind").val(n.kind==="reset"||n.kind==="link"?n.kind:""),e.find("#setting-second-label").val(n.label||""),e.find("#setting-second-url").val(n.url||""),e.find("#setting-second-style").val(n.style||"secondary"),e.find("#setting-second-icon-start").val(n.iconStart||""),e.find("#setting-second-icon-end").val(n.iconEnd||""),e.find("#setting-second-order").val(n.first?"first":"after"),e.find("#setting-second-arrange").val(n.stack?"stack":"row"),Qt(e)}function Ce(e){const t=e.find("#field-project").val()||"";return V(),{title:e.find("#field-title").val().trim(),slug:e.find("#field-slug").val().trim(),description:e.find("#field-description").val().trim(),...e.find("#field-bundled").is(":checked")?{bundled:!0}:{},meta:{...Te||{},project:t||null},fields:T,settings:{submitText:e.find("#setting-submit-text").val().trim()||"Submit",successMessage:e.find("#setting-success-message").val().trim()||"Thank you.",layout:e.find("#setting-layout").val()||"grid",columns:Oe(e.find("#setting-columns").val()),submitAlign:e.find("#setting-submit-align").val()||"",...ri(e),...e.find("#setting-form-class").val().trim()&&{class:e.find("#setting-form-class").val().trim()},...e.find("#setting-submit-class").val().trim()&&{submitClass:e.find("#setting-submit-class").val().trim()},honeypot:e.find("#setting-honeypot").prop("checked"),rateLimitPerMinute:parseInt(e.find("#setting-rate-limit").val(),10)||3,successRedirect:e.find("#setting-success-redirect").val().trim()||null,actionSlug:e.find("#action-cms-slug").val()||null},actions:{email:{enabled:e.find("#action-email-enabled").prop("checked"),recipients:e.find("#action-email-recipients").val().trim(),subjectPrefix:e.find("#action-email-subject-prefix").val().trim()},webhook:{enabled:e.find("#action-webhook-enabled").prop("checked"),url:e.find("#action-webhook-url").val().trim(),method:e.find("#action-webhook-method").val()},...Se!==null&&{collection:Se}}}}function en(e,t){const s={};return e.forEach(n=>{if(n.type==="page-break"||n.type==="spacer")return;const i={...n.formConfig||{}};i.span==="full"&&t&&(i.span=t);const a=n.type==="chooser"||!!(n.variant&&["select","radio","checkbox-group"].includes(n.type)),l={type:n.type==="checkbox"?"boolean":n.type==="date"||n.type==="icon"||n.type==="image"?"string":a?"chooser":n.type,label:n.label,required:n.required,options:n.options,formConfig:{...n.placeholder&&{placeholder:n.placeholder},...n.helper&&{helperText:n.helper},...n.tooltip&&{tooltip:n.tooltip},...i}};a&&(n.variant&&(l.variant=n.variant),(n.multiple||n.type==="checkbox-group")&&(l.multiple=!0),n.density&&(l.density=n.density),n.columns&&(l.columns=n.columns),n.accent&&(l.accent=n.accent),n.accentStyle&&(l.accentStyle=n.accentStyle),n.glow&&(l.glow=!0),n.glowColour&&(l.glowColour=n.glowColour),n.shadow&&(l.shadow=n.shadow),n.shadowColour&&(l.shadowColour=n.shadowColour)),s[n.name]=n.type==="file"?pn(n,l):l}),s}function tn(e,t){const s=typeof e=="string"?document.querySelector(e):e;s&&(t||[]).forEach(n=>{if(n.type!=="date"||!n.name)return;const i=s.querySelector(`[name="${n.name}"]`);i&&i.type!=="date"&&(i.type="date")})}function it(e,t){const s=e?.querySelector?.(".dm-plugin-banner-name .dm-plugin-banner-sep");if(s){for(;s.nextSibling;)s.nextSibling.remove();s.after(document.createTextNode(String(t||"")))}}const nn=e=>String(e??"").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""");let J=null,an=0,sn=!1;const Me=e=>String(e||"").split("?")[0].replace(/\/$/,"");function ci(){sn||!window.R?.use||(sn=!0,R.use((e,t,s)=>!J||!kt(t?.path,e?.path)||J.path!==Me(t?.path)||(J.flush(),!J.dirty())?s():E.confirm("You have unsaved changes to this form. Leave anyway? They are kept as a draft for your next visit to it.").then(n=>{n?s():history.replaceState(null,"",`#${t.path}`)})),M.subscribe("router:afterChange",({to:e,from:t}={})=>{if(J&&J.path===Me(t?.path)&&kt(t?.path,e?.path)){const s=J;J=null,s.dispose()}}))}export const formEditorView={templateUrl:"/admin/js/templates/form-editor.html",async onMount(e){if(ci(),J){const c=J;J=null,c.dispose()}const t=Me(location.hash.replace(/^#/,"")),s=++an,n=()=>s!==an||!e.get(0)?.isConnected||Me(location.hash.replace(/^#/,""))!==t;T=[],I=null,Se=null,li(e),e.find("#setting-second-kind").get(0)?.addEventListener("change",()=>Qt(e));const i=window.location.hash.match(/\/forms\/edit\/([^/?#]+)/);I=i?i[1]:null;let a=null,o=null;const l=E.loader(e.get(0),{type:"dots"});if(I)try{if(a=await fe(`/forms/${I}`),n()){l.destroy();return}T=a.fields||[],Se=a.actions?.collection??null}catch(c){if(n()){l.destroy();return}o={missing:c?.status===404,message:c?.message||String(c)},E.toast(o.missing?"This form no longer exists.":`Could not load the form: ${o.message}`,{type:"error"})}Te={};try{const c=await fe("/projects").catch(()=>[]);if(n()){l.destroy();return}const h=e.find("#field-project").get(0);h&&(Array.isArray(c)?c:[]).forEach(y=>{const k=document.createElement("option");k.value=y.slug,k.textContent=y.name||y.slug,h.appendChild(k)})}catch{}if(l.destroy(),it(e.get(0),a?a.title||a.slug:"New form"),I||e.find("#field-title").get(0).addEventListener("input",function(){e.find("#field-slug").val(Nt(this.value))}),E.tabs(e.find("#editor-tabs").get(0)),a){e.find("#field-title").val(a.title),e.find("#field-slug").val(a.slug),e.find("#field-description").val(a.description||""),e.find("#field-bundled").prop("checked",!!a.bundled),Te=a.meta||{},e.find("#field-project").val(a.meta?.project||"");const c=a.settings||{};e.find("#setting-submit-text").val(c.submitText||"Submit"),e.find("#setting-success-message").val(c.successMessage||""),e.find("#setting-layout").val(c.layout||"grid"),e.find("#setting-columns").val(Oe(c.columns)),e.find("#setting-submit-align").val(wt(c,c.layout||"grid")),Zt(e,c),e.find("#setting-form-class").val(c.class||""),e.find("#setting-submit-class").val(c.submitClass||""),e.find("#columns-group").get(0).style.display=(c.layout||"grid")==="grid"?"":"none",e.find("#setting-honeypot").prop("checked",c.honeypot!==!1),e.find("#setting-rate-limit").val(c.rateLimitPerMinute||3),e.find("#setting-success-redirect").val(c.successRedirect||"");const h=a.actions?.email||{};e.find("#action-email-enabled").prop("checked",h.enabled||!1),e.find("#action-email-recipients").val(h.recipients||""),e.find("#action-email-subject-prefix").val(h.subjectPrefix||"");const y=a.actions?.webhook||{};e.find("#action-webhook-enabled").prop("checked",y.enabled||!1),e.find("#action-webhook-url").val(y.url||""),e.find("#action-webhook-method").val(y.method||"POST")}const p=a?.settings?.actionSlug||"";try{const c=await fe("/actions").catch(()=>[]);if(n())return;const h=e.find("#action-cms-slug").get(0);if(ke=Array.isArray(c)?c:[],(Array.isArray(c)?c:[]).forEach(y=>{const k=document.createElement("option");k.value=y.slug,k.textContent=y.title||y.slug,y.slug===p&&(k.selected=!0),h.appendChild(k)}),!c.length){const y=document.createElement("option");y.value="",y.textContent="No actions available",y.disabled=!0,h.appendChild(y)}}catch{}const r=c=>{e.find("#columns-group").get(0).style.display=c?"":"none",document.querySelectorAll(".fb-grid-row").forEach(h=>{h.style.display=c?"flex":"none"})};e.find("#setting-layout").get(0)?.addEventListener("change",function(){r(this.value==="grid")}),r(e.find("#setting-layout").val()==="grid"),ne(e);const d=e.find("#add-element-menu").get(0);e.find("#add-element-btn").get(0).addEventListener("click",c=>{c.stopPropagation(),d.style.display=d.style.display==="none"?"":"none"}),oe&&document.removeEventListener("click",oe),oe=()=>{d&&(d.style.display="none")},document.addEventListener("click",oe),e.find("#add-field-btn").get(0).addEventListener("click",()=>{d&&(d.style.display="none"),V();const c=T.length;T.push({name:`field_${c+1}`,type:"string",label:"New Field",required:!1,placeholder:""}),ne(e);const h=e.find("#fields-list").get(0)?.lastElementChild;if(h){const y=h.querySelector(".fb-field-body");y&&(y.style.display="")}}),e.find("#add-spacer-btn").get(0).addEventListener("click",()=>{d&&(d.style.display="none"),V(),T.push({type:"spacer"}),ne(e)}),e.find("#add-page-break-btn").get(0).addEventListener("click",()=>{d&&(d.style.display="none"),V();const c=T.filter(h=>h.type==="page-break").length+2;T.push({type:"page-break",label:`Step ${c}`,description:""}),ne(e)}),Le=()=>{const c=Ce(e),h=e.find("#preview-container").get(0);if(!h)return;const y=e.find("#preview-test-result").get(0),k=e.find("#preview-test-badge").get(0);y&&(y.style.display="none",y.textContent=""),k&&(k.style.display=I?"":"none"),e.find("#preview-card").get(0).style.display="",vt(h),h.textContent="";const q=document.createElement("div");q.id="fb-preview-form",h.appendChild(q);const H=new Map;hn(q,c.fields);const K=I?async D=>{y&&(y.style.display="none",y.textContent="");try{const A=new Map(H);yt(q,A);const te=mn(D,c.fields,A),ae=fn(te.values,te.files),G=await fetch(`/api/forms/submit/${I}`,{method:"POST",headers:ae.headers,body:ae.body}),X=await G.json();if(!G.ok)throw new Error(X.error||"Submission failed.");y&&(y.textContent=X.message||c.settings?.successMessage||"Submitted successfully.",y.style.cssText="display:block;margin-top:.75rem;padding:.6rem .9rem;border-radius:6px;font-size:.9rem;background:color-mix(in srgb, var(--dm-success) 12%, transparent);color:var(--dm-success);"),E.toast("Test submission stored.",{type:"success"})}catch(A){y&&(y.textContent=A.message,y.style.cssText="display:block;margin-top:.75rem;padding:.6rem .9rem;border-radius:6px;font-size:.9rem;background:color-mix(in srgb, var(--dm-danger) 12%, transparent);color:var(--dm-danger);"),E.toast(A.message,{type:"error"})}return!1}:()=>!1,me=c.fields.some(D=>D.type==="page-break");if(q.addEventListener("click",D=>{const A=D.target.closest?.("a.fb-secondary-btn");A&&(D.preventDefault(),E.toast(`Cancel takes the visitor to ${A.getAttribute("href")}`,{type:"info"}))}),q.addEventListener("dm-form-end",D=>{const A=D.detail?.validation;if(A&&!A.valid){E.toast(`Still to answer: ${A.missing.map(te=>te.label).concat(A.errors).join(", ")}`,{type:"warning"});return}K(D.detail?.values||{})}),typeof F<"u"){const D=Oe(c.settings?.columns),A=c.settings?.layout||"grid";if(me&&F.wizard){const te=dn(c.fields,c.title),ae=te.map(se=>({title:se.title,description:se.description,fields:en(se.fields,D)}));let G=0,X=null,_e=null;const ht=se=>{const Pe=(te[G]||{}).fields||[];gn(q,c.settings),xt(q,c.settings,Pe),tn("#fb-preview-form",Pe),bt(q,Pe),un(q,H),se&&window.FormLogicEngine&&$t(c.fields)&&(X?.destroy?.(),X=new window.FormLogicEngine.FormLogicRuntime({fields:c.fields},q,{baseValues:_e?.getStepData?_e.getStepData():{}}),X.init())};_e=F.wizard("#fb-preview-form",{schema:{steps:ae},layout:A,columns:D,finishText:c.settings?.submitText||"Finish",onStepChange:se=>{yt(q,H),G=se,queueMicrotask(()=>ht(!0))},onSubmit:K}),ht(!1)}else if(F.render){const te=en(c.fields,D),ae={};c.fields.forEach(G=>{if(!(!G.name||G.type==="page-break"||G.type==="spacer")&&(G.type==="select"||G.type==="multiselect")&&G.required){const X=(G.options||[])[0];X&&(ae[G.name]=typeof X=="object"?X.value:X)}}),F.render("#fb-preview-form",te,ae,{submitText:c.settings?.submitText||"Submit",layout:A,columns:D,onSubmit:K}),bn(q,c.settings,A),xt(q,c.settings,c.fields),tn("#fb-preview-form",c.fields),bt(q,c.fields)}window.FormLogicEngine&&$t(c.fields)&&requestAnimationFrame(()=>{new window.FormLogicEngine.FormLogicRuntime({fields:c.fields},q).init()})}else{const D=document.createElement("p");D.textContent=`${c.fields.filter(A=>A.type!=="page-break").length} field(s): ${c.fields.filter(A=>A.type!=="page-break").map(A=>A.label).join(", ")}`,D.style.cssText="color:var(--dm-text-muted);font-style:italic;",q.appendChild(D)}},Le();const u=e.find("#editor-tabs").get(0),m=e.find("#preview-card").get(0),g=c=>{m&&m.contains(c.target)||Ke()};u&&(u.addEventListener("input",g),u.addEventListener("change",g));const f=e.get(0),v=[],w=c=>(v.push(c),c);let N=!1;const x=M.observable(!1),b=M.observable(!1);let C=I,$=null,_=!1,W=!1,j=!1,Y=Ce(e),U=Ee(Y),ie=Y;const le=()=>{N||!f.isConnected||(ie=Ce(e),x.value=Ee(ie)!==U)};let de=null;Ge=()=>{clearTimeout(de),de=setTimeout(le,150)},w(()=>{clearTimeout(de),Ge=null,ve=null}),w(()=>vt(f.querySelector("#preview-container")));const Re=c=>{const h=c.settings||{};e.find("#field-title").val(c.title||""),e.find("#field-slug").val(c.slug||""),e.find("#field-description").val(c.description||""),e.find("#field-bundled").prop("checked",!!c.bundled),Te={...c.meta||{}},e.find("#field-project").val(c.meta?.project||""),e.find("#setting-submit-text").val(h.submitText||"Submit"),e.find("#setting-success-message").val(h.successMessage||""),e.find("#setting-layout").val(h.layout||"grid"),e.find("#setting-columns").val(Oe(h.columns)),e.find("#setting-submit-align").val(wt(h,h.layout||"grid")),Zt(e,h),e.find("#setting-form-class").val(h.class||""),e.find("#setting-submit-class").val(h.submitClass||""),e.find("#setting-honeypot").prop("checked",h.honeypot!==!1),e.find("#setting-rate-limit").val(h.rateLimitPerMinute||3),e.find("#setting-success-redirect").val(h.successRedirect||""),e.find("#action-cms-slug").val(h.actionSlug||"");const y=c.actions?.email||{};e.find("#action-email-enabled").prop("checked",!!y.enabled),e.find("#action-email-recipients").val(y.recipients||""),e.find("#action-email-subject-prefix").val(y.subjectPrefix||"");const k=c.actions?.webhook||{};e.find("#action-webhook-enabled").prop("checked",!!k.enabled),e.find("#action-webhook-url").val(k.url||""),e.find("#action-webhook-method").val(k.method||"POST"),T=JSON.parse(JSON.stringify(c.fields||[])),r((h.layout||"grid")==="grid"),ne(e)};async function De(){if(b.peek()||N)return;if(o){E.toast("This form did not load, so saving would replace it with an empty one. Reload the page to try again.",{type:"error"});return}const c=Ce(e);if(!c.title){E.toast("Please enter a form title (Form Details).",{type:"error"});return}const h=yn(c.settings);if(h.length){E.toast(`${h[0]} (Settings > Submit Button)`,{type:"error"});return}const y=vn(c.settings,c.fields);if(y.length){E.toast(y[0],{type:"error"});return}const k=Hn(c.fields);k.length&&E.toast(`Some trigger messages use ${k.map(K=>`{{${K}}}`).join(", ")}, which will show as blank: no field has that name, or it is a password, file or image.`,{type:"warning",duration:9e3}),b.value=!0;const q=I,H=he(C);q||(f.inert=!0);try{if(I?(await fe(`/forms/${I}`,{method:"PUT",body:JSON.stringify(c)}),E.toast("Form saved.",{type:"success"})):(I=(await fe("/forms",{method:"POST",body:JSON.stringify(c)})).slug,E.toast("Form created.",{type:"success"})),N){if(!q)try{S.remove?S.remove(H):S.set(H,null)}catch{}return}C=I,Y=c,U=Ee(c),$=null,W=!1,j=!1,clearTimeout(de),le();for(const K of new Set([H,he(I)]))try{S.remove?S.remove(K):S.set(K,null)}catch{}it(f,c.title),wn("#/forms"),q||R.navigate(`/forms/edit/${I}`)}catch(K){q||(I=null),E.toast(K.message||"Failed to save form.",{type:"error"})}finally{b.value=!1,f.inert=!1}}async function on(){x.peek()&&(!await E.confirm("Throw away the changes you have not saved?",{confirmText:"Throw away",cancelText:"Keep editing"})||N||(Re(Y),le(),ut()))}const at=f.querySelector(".fex-bar");if(at){const c=M.applyBindings({dirty:x,saving:b},at,{methods:{save:De,discard:on}});w(()=>c.dispose())}const pe=new Map,st=()=>{for(const[c,h]of pe)f.contains(c)||(h(),pe.delete(c));for(const c of f.querySelectorAll("[data-help]:not([data-help-ready])"))pe.set(c,xn(c,{position:"bottom"}))};st();const ot=new MutationObserver(st);ot.observe(f,{childList:!0,subtree:!0}),w(()=>{ot.disconnect();for(const c of pe.values())c();pe.clear()}),Ve(e,{id:"form-editor-save",icon:"save",title:"Save (Ctrl+S)",onClick:()=>De()}),Ve(e,{id:"form-editor-back",icon:"arrow-left",title:"All forms",onClick:()=>{location.hash="#/forms"}}),I&&Ve(e,{id:"form-editor-subs",icon:"list",title:"Submissions",onClick:()=>{location.hash=`#/forms/${encodeURIComponent(I)}/submissions`}});const lt=c=>{n()||!(c.ctrlKey||c.metaKey)||String(c.key).toLowerCase()!=="s"||(c.preventDefault(),c.stopImmediatePropagation(),De())};document.addEventListener("keydown",lt,!0),w(()=>document.removeEventListener("keydown",lt,!0));const Z=e.find("#fields-list").get(0),ln=c=>c?.closest?.(".fb-field-card"),rt=c=>Number(c?.dataset.index),ct=c=>c?.querySelector(":scope > .fb-field-body"),dt=c=>{const h=ct(c);h&&(h.style.display=h.style.display==="none"?"":"none")},ue=(c,h)=>{T=c,ne(e),h!=null&&Z?.querySelectorAll(".fb-field-card")[h]?.focus()},rn=async c=>{try{await navigator.clipboard.writeText(c),E.toast(`Copied "${c}".`,{type:"success"})}catch{E.toast(`Copy failed - the name is ${c}.`,{type:"info"})}},cn=c=>{const h=ln(c),y=rt(h);V();const k=T[y];if(!k)return[];const q=k.type!=="spacer"&&k.type!=="page-break",H=ct(h),K=H&&H.style.display!=="none";return[{header:We(k)},...H?[{label:K?"Close":"Edit",icon:K?"chevron-up":"edit",action:()=>dt(h)}]:[],...q?[{label:"Logic & triggers",icon:"zap",action:()=>{H&&(H.style.display=""),(H?.querySelector(".fb-field-logic")||H)?.scrollIntoView({block:"center",behavior:"smooth"})}}]:[],{label:"Duplicate",icon:"copy",action:()=>{V(),ue(Sn(T,y),y+1)}},{type:"divider"},{label:"Move up",icon:"arrow-up",shortcut:"Alt+\u2191",disabled:y===0,action:()=>{V(),ue(St(T,y,-1),y-1)}},{label:"Move down",icon:"arrow-down",shortcut:"Alt+\u2193",disabled:y>=T.length-1,action:()=>{V(),ue(St(T,y,1),y+1)}},...q&&k.name?[{label:"Copy field name",icon:"clipboard",action:()=>rn(k.name)}]:[],{type:"divider"},{label:"Delete\u2026",icon:"trash",danger:!0,action:async()=>{if(k.type!=="spacer"&&!await E.confirm(`Remove "${nn(We(k))}"?`))return;V();const me=[...T];me.splice(y,1),ue(me,Math.min(y,me.length-1))}}]};if(Z&&typeof E.sortable=="function"){const c=E.sortable(Z,{items:".fb-field-card",handle:".fb-drag-grip",key:null,onSort:({from:h,to:y})=>{V();const k=[...T],[q]=k.splice(h,1);k.splice(y,0,q),ue(k,y)}});w(()=>c.destroy())}let pt=null;if(Z&&E.contextMenu&&(pt=E.contextMenu(Z,{cursor:"glyph",match:".fb-field-card > :first-child",inherit:!1,density:"compact",items:c=>cn(c)}),w(()=>pt?.destroy?.())),ve=c=>{for(const h of c.querySelectorAll(".fb-field-card")){h.tabIndex=0;const y=h.firstElementChild;if(!y||y.querySelector(".fex-kebab"))continue;const k=document.createElement("button");k.type="button",k.className="fex-kebab",k.textContent="\u22EE",k.title="Actions",V(),k.setAttribute("aria-label",`Actions for ${We(T[rt(h)])}`),k.addEventListener("click",q=>{q.stopPropagation(),Ct(k)}),y.appendChild(k)}},Z){ve(Z);const c=h=>{const y=h.target?.classList?.contains("fb-field-card")?h.target:null;y&&(h.key==="F10"&&h.shiftKey?(h.preventDefault(),Ct(y.firstElementChild)):h.key==="Enter"&&(h.preventDefault(),dt(y)))};Z.addEventListener("keydown",c),w(()=>Z.removeEventListener("keydown",c))}const ut=()=>{try{const c=he(C);S.remove?S.remove(c):S.set(c,null)}catch{}},mt=c=>{if(!o){if(!x.peek()||!c){!_&&!j&&ut();return}try{S.set(he(C),{data:kn(c),base:Cn($,Et(U)),...W?{newFrom:!0}:{},at:new Date().toISOString()})}catch{}}},ft=c=>{le(),x.peek()&&(mt(ie),c.preventDefault(),c.returnValue="")};if(window.addEventListener("beforeunload",ft),w(()=>window.removeEventListener("beforeunload",ft)),w(()=>{oe&&(document.removeEventListener("click",oe),oe=null)}),J={path:t,dirty:()=>x.peek(),flush:()=>{clearTimeout(de),le()},dispose(){N||(mt(ie),N=!0,v.splice(0).reverse().forEach(c=>{try{c?.()}catch{}}))}},o){const c=document.createElement("p");c.className="fex-load-error",c.setAttribute("role","alert"),c.textContent=o.missing?"This form no longer exists - it may have been deleted. Nothing here can be saved over it.":`This form could not be loaded (${o.message}). Saving is off so an empty editor cannot replace it - reload the page to try again.`;const h=f.querySelector(".fex-bar");h?h.before(c):f.prepend(c),w(()=>c.remove())}const ze=he(I);let ee=null;try{ee=S.get(ze)}catch{}if(ee?.data&&(!o||o.missing)){const c=!!o?.missing,h=En(ee,{missing:c});_=!0;const y=await E.confirm(nn(Tn(ee,{current:Et(U),missing:c,when:ee.at?Nn(ee.at):""})),{title:"Unsaved changes",confirmText:"Throw away",cancelText:h==="new"?"Start a new form":h==="refuse"?"Not now":"Keep them"});if(_=!1,y)try{S.remove?S.remove(ze):S.set(ze,null)}catch{}else h==="refuse"?j=!0:N||(c?(I=null,o=null,$="",W=!0,Y=Ce(e),U=Ee(Y),it(f,"New form"),f.querySelector(".fex-load-error")?.remove(),Re(ee.data)):($=ee.base??"",Re(Ln(ee.data,Y))),le())}}};
|