voc-lib-js 1.0.49 → 1.0.51

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.
@@ -1,2 +1,2 @@
1
- "use strict";var VocLibJs=(()=>{var H=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var U=Object.getOwnPropertyNames;var J=Object.prototype.hasOwnProperty;var K=(a,w)=>{for(var v in w)H(a,v,{get:w[v],enumerable:!0})},Q=(a,w,v,E)=>{if(w&&typeof w=="object"||typeof w=="function")for(let y of U(w))!J.call(a,y)&&y!==v&&H(a,y,{get:()=>w[y],enumerable:!(E=G(w,y))||E.enumerable});return a};var X=a=>Q(H({},"__esModule",{value:!0}),a);var ee={};K(ee,{FormsLib:()=>Y,default:()=>Z});function D(a,w,v){let E=w?.style==="bootstrap",y=document.createElement("div");y.className="voc-form-avatar-wrapper",E&&y.classList.add("d-flex","align-items-center","gap-3");let p=document.createElement("img");p.className="voc-form-avatar-preview",p.alt=a.label||"avatar",E&&(p.classList.add("img-thumbnail","rounded-circle"),p.style.width=p.style.height="64px",p.style.objectFit="cover");let C=v!=null?String(v):a.default!==void 0&&a.default!==null?String(a.default):"";C?p.src=C:(p.src="",p.style.display="none");let o=document.createElement("input");o.type="file",o.accept=a.accept||"image/*",o.className="voc-form-avatar-input",E&&o.classList.add("form-control"),(a.key||a.name)&&(o.name=a.name||a.key,a.key&&(o.id=`voc-form-${a.key}`));let h=document.createElement("input");if(h.type="hidden",(a.key||a.name)&&(h.name=(a.name||a.key)+"_url"),h.value=C||"",o.addEventListener("change",b=>{let l=b.target;if(l.files&&l.files.length>0){let L=l.files[0],A=new FileReader;A.onload=()=>{p.src=String(A.result),p.style.display="",h.value=""},A.readAsDataURL(L)}}),E){let b=document.createElement("div");b.style.flex="1",b.appendChild(o),b.appendChild(h),y.appendChild(p),y.appendChild(b)}else y.appendChild(p),y.appendChild(o),y.appendChild(h);return y}function O(a,w,v){let E=w?.style==="bootstrap",y=document.createElement("div");if(y.className="voc-form-audio-select",!a.options||a.options.length===0)return y;let p=a.name||a.key||`audio_select_${Math.random().toString(36).substr(2,6)}`,C;return Array.isArray(v)?C=v[0]:v!==void 0?C=v:a.default!==void 0&&(C=a.default),a.options.forEach((o,h)=>{let b=document.createElement("div");b.className="voc-form-audio-item",E&&b.classList.add("form-check");let l=document.createElement("input");l.type="radio",l.name=p,l.value=o.value,l.className="voc-form-audio-radio",a.key&&(l.id=`voc-form-${a.key}-${h}`),a.required&&h===0&&(l.required=!0),l.style.position="absolute",l.style.width="1px",l.style.height="1px",l.style.margin="0",l.style.padding="0",l.style.clip="rect(0 0 0 0)",l.style.clipPath="inset(50%)",l.style.overflow="hidden",l.style.border="0",C!==void 0&&(Array.isArray(C)?l.checked=C.includes(o.value):l.checked=String(C)===String(o.value));let L=()=>{l.checked?b.classList.add("selected"):b.classList.remove("selected")};l.addEventListener("change",()=>{y.querySelectorAll(".voc-form-audio-item").forEach(n=>n.classList.remove("selected")),L()}),l.checked&&b.classList.add("selected");let A=document.createElement("label");A.className="voc-form-audio-label",E&&A.classList.add("form-check-label"),A.htmlFor=l.id||"",A.textContent=o.label||o.value,A.tabIndex=0;let e=document.createElement("div");e.className="voc-form-audio-control";let d=null;if(o.audio_link){let t=document.createElement("button");t.type="button",t.className="voc-form-audio-play",E&&t.classList.add("btn","btn-sm","btn-outline-secondary"),t.setAttribute("aria-label",`Play ${o.label||o.value}`);let n="http://www.w3.org/2000/svg",r=document.createElementNS(n,"svg");r.setAttribute("viewBox","0 0 24 24"),r.setAttribute("width","16"),r.setAttribute("height","16"),r.setAttribute("aria-hidden","true");let s=document.createElementNS(n,"path");s.setAttribute("d","M8 5v14l11-7z"),s.setAttribute("fill","currentColor"),r.appendChild(s),t.appendChild(r),d=document.createElement("audio"),d.src=o.audio_link,d.preload="none",d.style.display="none";let m=f=>{for(;f.firstChild;)f.removeChild(f.firstChild)},u=()=>{let f="http://www.w3.org/2000/svg",F=document.createElementNS(f,"svg");F.setAttribute("viewBox","0 0 24 24"),F.setAttribute("width","16"),F.setAttribute("height","16"),F.setAttribute("aria-hidden","true");let g=document.createElementNS(f,"path");return g.setAttribute("d","M8 5v14l11-7z"),g.setAttribute("fill","currentColor"),F.appendChild(g),F},R=()=>{let f="http://www.w3.org/2000/svg",F=document.createElementNS(f,"svg");F.setAttribute("viewBox","0 0 24 24"),F.setAttribute("width","16"),F.setAttribute("height","16"),F.setAttribute("aria-hidden","true");let g=document.createElementNS(f,"rect");return g.setAttribute("x","6"),g.setAttribute("y","6"),g.setAttribute("width","12"),g.setAttribute("height","12"),g.setAttribute("fill","currentColor"),F.appendChild(g),F};m(t),t.appendChild(u());let S=f=>{m(t),f?(t.appendChild(R()),t.setAttribute("aria-label",`Stop ${o.label||o.value}`)):(t.appendChild(u()),t.setAttribute("aria-label",`Play ${o.label||o.value}`))};t.addEventListener("click",()=>{try{if(!d)return;let f=y.querySelectorAll("audio");if(!d.paused&&!d.ended){d.pause(),d.currentTime=0;return}f.forEach(g=>{try{g.pause(),g.currentTime=0}catch{}}),y.querySelectorAll(".voc-form-audio-item").forEach(g=>g.classList.remove("is-playing")),d.play()}catch(f){console.warn("audio play failed",f)}}),d.addEventListener("playing",()=>{b.classList.add("is-playing"),S(!0)});let N=()=>{b.classList.remove("is-playing"),S(!1)};d.addEventListener("pause",N),d.addEventListener("ended",N),e.appendChild(t),e.appendChild(d)}let c=document.createElement("div");c.className="voc-form-item-detail",c.style.display="flex",c.style.flexDirection="column",c.style.gap="4px";let i=document.createElement("div");i.className="voc-form-item-top",i.style.display="flex",i.style.alignItems="center",i.style.gap="8px",i.appendChild(l),i.appendChild(A);let x=document.createElement("div");x.className="voc-form-item-description";let k=o.description||o.hint_text||"";k&&(x.textContent=k),c.appendChild(i),c.appendChild(x),e.appendChild(c),b.appendChild(e),i.addEventListener("click",t=>{let n=t.target;n&&n.closest(".voc-form-audio-play")||(l.checked=!0,l.dispatchEvent(new Event("change",{bubbles:!0})))}),b.addEventListener("click",t=>{let n=t.target;n&&n.closest(".voc-form-audio-play")||(l.checked=!0,l.dispatchEvent(new Event("change",{bubbles:!0})))}),y.appendChild(b)}),y}function I(a,w,v){let E=document.createElement("div");E.className="voc-form-table-wrapper";let y=w?.style==="bootstrap",p=document.createElement("input");p.type="hidden",p.name=a.name||a.key,a.key&&(p.id=`voc-form-${a.key}`),E.appendChild(p);let C=document.createElement("table");C.className="voc-form-table",y&&C.classList.add("table","table-bordered");let o=document.createElement("thead"),h=document.createElement("tr");(a.fields||[]).forEach(t=>{let n=document.createElement("th");n.textContent=t.label||t.key,h.appendChild(n)});let b=document.createElement("th");b.textContent="Actions",h.appendChild(b),o.appendChild(h),C.appendChild(o);let l=document.createElement("tbody");C.appendChild(l),E.appendChild(C);let L=document.createElement("button");L.type="button",L.className="voc-form-table-add",y&&L.classList.add("btn","btn-secondary"),L.textContent="+Add";let A=document.createElement("tfoot"),e=document.createElement("tr"),d=document.createElement("td");d.colSpan=(a.fields?a.fields.length:0)+1,d.appendChild(L),e.appendChild(d),A.appendChild(e),C.appendChild(A),E.appendChild(C);let c=[];function i(){p.value=JSON.stringify(c)}function x(t){let n=[];return a.fields.forEach(r=>{let s=t[r.key];if(r.type==="checkbox"){let m=Array.isArray(s)?s:s?[s]:[];r.required&&m.length===0&&n.push(`${r.label||r.key} is required`);return}r.required&&(s==null||String(s).trim()==="")&&n.push(`${r.label||r.key} is required`),r.type==="email"&&s&&(/^\S+@\S+\.\S+$/.test(String(s))||n.push(`${r.label||r.key} must be a valid email`)),r.type==="number"&&s&&isNaN(Number(s))&&n.push(`${r.label||r.key} must be a number`),r.validation&&r.validation.regex&&s&&(new RegExp(r.validation.regex).test(String(s))||n.push(`${r.label||r.key} invalid format`))}),n}function k(){l.innerHTML="",c.forEach((t,n)=>{let r=document.createElement("tr");a.fields.forEach(u=>{let R=document.createElement("td"),S,N=(u.type||"text").toLowerCase();if(N==="select"){let f=document.createElement("select");if(f.className="voc-form-table-cell",y&&f.classList.add("form-control"),u.placeholder){let g=document.createElement("option");g.value="",g.textContent=u.placeholder,g.disabled=!0,(t[u.key]===void 0||t[u.key]===null||t[u.key]==="")&&(g.selected=!0),f.appendChild(g)}(u.options||[]).forEach(g=>{let _=document.createElement("option");_.value=g.value,_.textContent=g.label,f.appendChild(_)}),f.value=t[u.key]!==void 0&&t[u.key]!==null?String(t[u.key]):"",u.required&&(f.required=!0),f.addEventListener("change",g=>{c[n][u.key]=g.target.value,i()}),S=f}else if(N==="checkbox"){let f=document.createElement("div");f.className="voc-form-table-checkboxes",y&&f.classList.add("form-check");let F=u.options||[];Array.isArray(c[n][u.key])||(c[n][u.key]=c[n][u.key]?[c[n][u.key]]:[]),F.forEach(g=>{let _=`voc-form-${a.key}-${u.key}-${n}-${g.value}`,q=document.createElement("div");q.className="voc-form-table-checkbox-item",y&&q.classList.add("form-check");let T=document.createElement("input");T.type="checkbox",T.className="voc-form-table-cell",y&&T.classList.add("form-check-input"),T.id=_,T.value=g.value,T.checked=Array.isArray(c[n][u.key])&&c[n][u.key].includes(g.value),T.addEventListener("change",z=>{let P=z.target.checked;Array.isArray(c[n][u.key])||(c[n][u.key]=[]),P?c[n][u.key].includes(g.value)||c[n][u.key].push(g.value):c[n][u.key]=c[n][u.key].filter(V=>V!==g.value),i()});let M=document.createElement("label");M.htmlFor=_,M.className="voc-form-table-checkbox-label",y&&M.classList.add("form-check-label"),M.textContent=g.label,q.appendChild(T),q.appendChild(M),f.appendChild(q)}),S=f}else{let f=document.createElement("input");N==="number"?f.type="number":N==="email"?f.type="email":N==="tel"?f.type="tel":N==="url"?f.type="url":N==="date"?f.type="date":f.type="text",f.value=t[u.key]!==void 0&&t[u.key]!==null?String(t[u.key]):"",u.placeholder&&(f.placeholder=u.placeholder),u.required&&(f.required=!0),f.className="voc-form-table-cell",y&&f.classList.add("form-control"),f.addEventListener("input",F=>{c[n][u.key]=F.target.value,i()}),S=f}R.appendChild(S),r.appendChild(R)});let s=document.createElement("td"),m=document.createElement("button");m.type="button",m.className="voc-form-table-remove",y&&m.classList.add("btn","btn-danger"),m.textContent="Remove",m.addEventListener("click",()=>{c.splice(n,1),k(),i()}),s.appendChild(m),r.appendChild(s),l.appendChild(r)})}return L.addEventListener("click",()=>{if(a.validation?.max_rows!==void 0&&c.length>=a.validation.max_rows)return;let t={};a.fields.forEach(n=>t[n.key]=""),c.push(t),k(),i()}),Array.isArray(v)&&v.length?c=v.map(t=>({...t})):a.default&&Array.isArray(a.default)&&(c=a.default.map(t=>({...t}))),k(),i(),E.validate=function(){let t=[];return c.forEach((n,r)=>{let s=x(n);s.length&&t.push(`Row ${r+1}: ${s.join("; ")}`)}),a.validation?.min_rows!==void 0&&c.length<a.validation.min_rows&&t.push(`At least ${a.validation.min_rows} rows required`),a.validation?.max_rows!==void 0&&c.length>a.validation.max_rows&&t.push(`No more than ${a.validation.max_rows} rows allowed`),a.validation?.unique_fields&&a.validation.unique_fields.length&&a.validation.unique_fields.forEach(n=>{let r=new Set;c.forEach((s,m)=>{let u=s[n];u!=null&&(r.has(u)&&t.push(`Field ${n} must be unique (duplicate at row ${m+1})`),r.add(u))})}),t},E}function $(a,w=!0){let v=new FormData(a);if(w)return v;let E={};return v.forEach((y,p)=>{let C=(y instanceof File,y);Object.prototype.hasOwnProperty.call(E,p)?Array.isArray(E[p])?E[p].push(C):E[p]=[E[p],C]:E[p]=C}),E}async function W(a,w,v){let E=w.url,y={"Content-Type":"application/json"};w?.api_token&&(y.Authorization=`Bearer ${w.api_token}`);let p=$(a);if(w?.additional_data)for(let[h,b]of Object.entries(w.additional_data))p.append(h,b);let C,o=p instanceof FormData;C={method:"POST",headers:{Authorization:`Bearer ${w.api_token}`,...o?{}:{"Content-Type":"application/json"},Accept:"application/json"},body:o?p:JSON.stringify(Object.fromEntries(p.entries()))};try{fetch(E,C).then(async h=>{let b=h.headers.get("content-type");if(!h.ok)try{let L=(await h.json())?.message;throw new Error(L??"Their was an error submitting the form. Check your form and try again.")}catch(l){throw new Error(l.message)}return b&&b.includes("application/json")?h.json():h.text()}).then(h=>{v&&v(h,null)}).catch(h=>{v&&v(null,h)})}catch(h){v&&v(null,h)}}function j(a,w,v){let E=[];if(typeof a=="string"?E=Array.from(document.querySelectorAll(a)):E=[a],E.length===0)return null;function y(){let o=v?.style==="bootstrap",h=document.createElement("form");h.classList.add("voc-form"),o&&h.classList.add("needs-validation"),h.id=w.id;let b=document.createElement("h2");b.className="voc-form-title",o&&b.classList.add("mb-3"),b.textContent=w.form_name,h.appendChild(b);function l(e,d){d&&(d.min_length!==void 0&&(e.minLength=d.min_length),d.max_length!==void 0&&(e.maxLength=d.max_length),d.regex&&(e instanceof HTMLInputElement?e.pattern=d.regex:e.dataset.pattern=d.regex))}function L(e){if(!e.key&&!e.label)return null;let d=w.default_values||{},c=e.name||e.key,i=c?d[c]:void 0,x=document.createElement("div");if(x.className="voc-form-group",o&&x.classList.add("mb-3"),e.label){let t=document.createElement("label");if(t.classList.add("voc-form-label"),o&&t.classList.add("form-label"),e.key?e.type==="radio"?t.htmlFor="":t.htmlFor=`voc-form-${e.key}`:t.htmlFor="",t.textContent=e.label,e.required){let n=document.createElement("span");n.className="voc-form-required",n.textContent="*",t.appendChild(n)}x.appendChild(t)}let k=null;switch(e.type){case"checkbox":{let t=document.createElement("div");if(t.className="voc-form-checkbox-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options)e.options.forEach((n,r)=>{let s=document.createElement("label");s.classList.add("voc-form-checkbox-label"),o&&s.classList.add("form-check"),s.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(s.style.width=e.width);let m=document.createElement("input");m.type="checkbox",m.classList.add("voc-form-checkbox"),o&&m.classList.add("form-check-input"),m.name=e.name||e.key||"",m.value=n.value,e.key&&(m.id=`voc-form-${e.key}-${r}`),e.required&&(m.required=!0),Array.isArray(i)?i.includes(n.value)&&(m.checked=!0):i!==void 0?i===n.value&&(m.checked=!0):e.default===n.value&&(m.checked=!0),s.appendChild(m),s.appendChild(document.createTextNode(n.label)),t.appendChild(s)});else{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),o&&n.classList.add("form-check"),e.width&&(n.style.width=e.width);let r=document.createElement("input");r.type="checkbox",r.classList.add("voc-form-checkbox"),(e.key||e.name)&&(r.name=e.name||e.key,e.key&&(r.id=`voc-form-${e.key}`)),o&&r.classList.add("form-check-input"),e.required&&(r.required=!0),i!==void 0?r.checked=!!i:e.default&&(r.checked=!!e.default),n.appendChild(r),n.appendChild(document.createTextNode(e.label||"")),t.appendChild(n)}k=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),o&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),i!=null?t.value=String(i):e.default!==void 0&&e.default!==null&&(t.value=String(e.default));let n=e.default||i||"";if(n){let r=(n.match(/\n/g)||[]).length,s=80,m=n.split(`
2
- `).reduce((S,N)=>Math.max(S,N.length),0),u=Math.ceil(m/s)||1,R=Math.max(2,r+u);t.rows=R,t.style.resize="vertical"}e.width&&(t.style.width=e.width),e.required&&(t.required=!0),l(t,e.validation),k=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),o&&t.classList.add("form-select"),o&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder){let n=document.createElement("option");n.value="",n.textContent=e.placeholder,n.disabled=!0,n.selected=!0,t.appendChild(n)}e.required&&(t.required=!0),e.options&&e.options.forEach(n=>{let r=document.createElement("option");r.value=n.value,r.textContent=n.label,Array.isArray(i)?i.includes(n.value)&&(r.selected=!0):i!==void 0?i===n.value&&(r.selected=!0):e.default===n.value&&(r.selected=!0),t.appendChild(r)}),e.width&&(t.style.width=e.width),k=t;break}case"radio":{let t=document.createElement("div");t.className="voc-form-radio-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options&&e.options.forEach((n,r)=>{let s=document.createElement("label");s.classList.add("voc-form-radio-label"),o&&s.classList.add("form-check"),s.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(s.style.width=e.width);let m=document.createElement("input");m.type="radio",m.classList.add("voc-form-radio"),o&&m.classList.add("form-check-input"),m.name=e.name||e.key||"",m.value=n.value,e.key&&(m.id=`voc-form-${e.key}-${r}`),Array.isArray(i)?i.includes(n.value)&&(m.checked=!0):i!==void 0?i===n.value&&(m.checked=!0):e.default===n.value&&(m.checked=!0),s.appendChild(m),s.appendChild(document.createTextNode(n.label)),t.appendChild(s)}),k=t;break}case"file":{let t=document.createElement("div");t.className="voc-form-file-wrapper",e.width&&(t.style.width=e.width);let n=document.createElement("input");n.type="file",n.classList.add("voc-form-file"),o&&n.classList.add("form-control"),(e.key||e.name)&&(n.name=e.name||e.key,e.key&&(n.id=`voc-form-${e.key}`)),e.width&&(n.style.width=e.width),e.required&&(n.required=!0),e.accept&&(n.accept=e.accept),e.multiple&&(n.multiple=!0),t.appendChild(n),k=t;break}case"avatar":{k=D(e,v||{},i);break}case"table":{k=I(e,v||{},i);break}case"audio_select":{k=O(e,v||{},i);break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),o&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),i!=null?t.value=String(i):e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),l(t,e.validation),k=t;break}}if(k&&x.appendChild(k),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,x.appendChild(t)}return x}w.fields.forEach(e=>{if(e.type==="group"&&"items"in e){let d=e;if(o){let c=document.createElement("div");c.className="card voc-form-group-card mb-3";let i=d.label||d.header?.title,x=d.description||d.header?.description;if(i||x){let t=document.createElement("div");if(t.className="card-header voc-form-group-head",i){let n=document.createElement("h5");n.className="mb-0 voc-form-group-label",n.textContent=i,t.appendChild(n)}if(x){let n=document.createElement("p");n.className="mb-0 voc-form-group-description",n.textContent=x,t.appendChild(n)}c.appendChild(t)}let k=document.createElement("div");k.className="card-body voc-form-group-body",d.items.forEach(t=>{if(t.type==="row"&&"items"in t){let n=t,r=document.createElement("div");if(r.className="voc-form-group voc-form-row mb-3",n.label){let m=document.createElement("h6");m.className="voc-form-row-label",m.textContent=n.label,r.appendChild(m)}let s=document.createElement("div");s.className="voc-form-row-items "+(n.direction||"horizontal"),n.items.forEach(m=>{let u=L(m);u&&s.appendChild(u)}),r.appendChild(s),k.appendChild(r)}else{let n=L(t);n&&k.appendChild(n)}}),c.appendChild(k),h.appendChild(c)}else{let c=document.createElement("div");c.className="voc-form-group voc-form-group-wrapper",o&&c.classList.add("mb-3");let i=d.label||d.header?.title,x=d.description||d.header?.description;if(i||x){let k=document.createElement("div");if(k.className="voc-form-group-head",i){let t=document.createElement("h3");t.className="voc-form-group-label",t.textContent=i,k.appendChild(t)}if(x){let t=document.createElement("p");t.className="voc-form-group-description",t.textContent=x,k.appendChild(t)}c.appendChild(k)}d.items.forEach(k=>{if(k.type==="row"&&"items"in k){let t=k,n=document.createElement("div");if(n.className="voc-form-group voc-form-row",o&&n.classList.add("mb-3"),t.label){let s=document.createElement("h4");s.className="voc-form-row-label",s.textContent=t.label,n.appendChild(s)}let r=document.createElement("div");r.className="voc-form-row-items "+(t.direction||"horizontal"),t.items.forEach(s=>{let m=L(s);m&&r.appendChild(m)}),n.appendChild(r),c.appendChild(n)}else{let t=L(k);t&&c.appendChild(t)}}),h.appendChild(c)}return}if(e.type==="row"&&"items"in e){let d=document.createElement("div");if(d.className="voc-form-group voc-form-row",o&&d.classList.add("mb-3"),e.label){let i=document.createElement("h3");i.className="voc-form-row-label",i.textContent=e.label,d.appendChild(i)}if(e.description){let i=document.createElement("p");i.className="voc-form-row-description",i.textContent=e.description,d.appendChild(i)}let c=document.createElement("div");c.className="voc-form-row-items "+(e.direction||"horizontal"),d.appendChild(c),e.items.forEach(i=>{let x=L(i);x&&c.appendChild(x)}),h.appendChild(d)}else{let d=L(e);d&&h.appendChild(d)}});let A=document.createElement("button");return A.type="submit",A.className="voc-form-submit",A.textContent="Submit",o&&A.classList.add("btn","btn-primary"),h.appendChild(A),h}let p=y();E[0].appendChild(p);let C=w?.submit_config??v?.submit_config??null;return C&&p.addEventListener("submit",o=>{if(o.preventDefault(),!p.checkValidity()){p.reportValidity(),v?.onSubmitResult&&v.onSubmitResult(null,new Error("Form validation failed"));return}let h=Array.from(p.querySelectorAll(" .voc-form-table-wrapper, .voc-form-table-wrapper")),b=[];if(h.forEach(l=>{if(l&&typeof l.validate=="function"){let L=l.validate();L&&L.length&&b.push(...L)}}),b.length){v?.onSubmitResult&&v.onSubmitResult(null,new Error(b.join("; ")));let l=h[0];l&&l.scrollIntoView({behavior:"smooth",block:"center"});return}W(p,C,(l,L)=>{v?.onSubmitResult&&v.onSubmitResult(l,L)})}),{element:p,validate:()=>{let o=[];return p.querySelectorAll(":invalid").forEach(b=>{let l=b.validationMessage;o.push({element:b,message:l})}),o},getValues:()=>$(p),onSubmit:o=>{p.addEventListener("submit",h=>{h.preventDefault();let b=$(p);o(b,h)})}}}var B={renderForm:j};var Y=B,Z={FormsLib:B};return X(ee);})();
1
+ "use strict";var VocLibJs=(()=>{var j=Object.defineProperty;var Q=Object.getOwnPropertyDescriptor;var X=Object.getOwnPropertyNames;var Y=Object.prototype.hasOwnProperty;var Z=(s,b)=>{for(var f in b)j(s,f,{get:b[f],enumerable:!0})},ee=(s,b,f,v)=>{if(b&&typeof b=="object"||typeof b=="function")for(let d of X(b))!Y.call(s,d)&&d!==f&&j(s,d,{get:()=>b[d],enumerable:!(v=Q(b,d))||v.enumerable});return s};var te=s=>ee(j({},"__esModule",{value:!0}),s);var oe={};Z(oe,{FormsLib:()=>ne,default:()=>re});function z(s,b,f){let v=b?.style==="bootstrap",d=document.createElement("div");d.className="voc-form-avatar-wrapper",v&&d.classList.add("d-flex","align-items-center","gap-3");let m=document.createElement("img");m.className="voc-form-avatar-preview",m.alt=s.label||"avatar",v&&(m.classList.add("img-thumbnail","rounded-circle"),m.style.width=m.style.height="64px",m.style.objectFit="cover");let E=f!=null?String(f):s.default!==void 0&&s.default!==null?String(s.default):"";E?m.src=E:(m.src="",m.style.display="none");let c=document.createElement("input");c.type="file",c.accept=s.accept||"image/*",c.className="voc-form-avatar-input",v&&c.classList.add("form-control"),(s.key||s.name)&&(c.name=s.name||s.key,s.key&&(c.id=`voc-form-${s.key}`));let h=document.createElement("input");if(h.type="hidden",(s.key||s.name)&&(h.name=(s.name||s.key)+"_url"),h.value=E||"",c.addEventListener("change",y=>{let u=y.target;if(u.files&&u.files.length>0){let N=u.files[0],C=new FileReader;C.onload=()=>{m.src=String(C.result),m.style.display="",h.value=""},C.readAsDataURL(N)}}),v){let y=document.createElement("div");y.style.flex="1",y.appendChild(c),y.appendChild(h),d.appendChild(m),d.appendChild(y)}else d.appendChild(m),d.appendChild(c),d.appendChild(h);return d}function P(s,b,f){let v=b?.style==="bootstrap",d=document.createElement("div");if(d.className="voc-form-audio-select",!s.options||s.options.length===0)return d;let m=s.name||s.key||`audio_select_${Math.random().toString(36).substr(2,6)}`,E;return Array.isArray(f)?E=f[0]:f!==void 0?E=f:s.default!==void 0&&(E=s.default),s.options.forEach((c,h)=>{let y=document.createElement("div");y.className="voc-form-audio-item",v&&y.classList.add("form-check");let u=document.createElement("input");u.type="radio",u.name=m,u.value=c.value,u.className="voc-form-audio-radio",s.key&&(u.id=`voc-form-${s.key}-${h}`),s.required&&h===0&&(u.required=!0),u.style.position="absolute",u.style.width="1px",u.style.height="1px",u.style.margin="0",u.style.padding="0",u.style.clip="rect(0 0 0 0)",u.style.clipPath="inset(50%)",u.style.overflow="hidden",u.style.border="0",E!==void 0&&(Array.isArray(E)?u.checked=E.includes(c.value):u.checked=String(E)===String(c.value));let N=()=>{u.checked?y.classList.add("selected"):y.classList.remove("selected")};u.addEventListener("change",()=>{d.querySelectorAll(".voc-form-audio-item").forEach(a=>a.classList.remove("selected")),N()}),u.checked&&y.classList.add("selected");let C=document.createElement("label");C.className="voc-form-audio-label",v&&C.classList.add("form-check-label"),C.htmlFor=u.id||"",C.textContent=c.label||c.value,C.tabIndex=0;let e=document.createElement("div");e.className="voc-form-audio-control";let p=null;if(c.audio_link){let t=document.createElement("button");t.type="button",t.className="voc-form-audio-play",v&&t.classList.add("btn","btn-sm","btn-outline-secondary"),t.setAttribute("aria-label",`Play ${c.label||c.value}`);let a="http://www.w3.org/2000/svg",i=document.createElementNS(a,"svg");i.setAttribute("viewBox","0 0 24 24"),i.setAttribute("width","16"),i.setAttribute("height","16"),i.setAttribute("aria-hidden","true");let n=document.createElementNS(a,"path");n.setAttribute("d","M8 5v14l11-7z"),n.setAttribute("fill","currentColor"),i.appendChild(n),t.appendChild(i),p=document.createElement("audio"),p.src=c.audio_link,p.preload="none",p.style.display="none";let o=l=>{for(;l.firstChild;)l.removeChild(l.firstChild)},x=()=>{let l="http://www.w3.org/2000/svg",M=document.createElementNS(l,"svg");M.setAttribute("viewBox","0 0 24 24"),M.setAttribute("width","16"),M.setAttribute("height","16"),M.setAttribute("aria-hidden","true");let T=document.createElementNS(l,"path");return T.setAttribute("d","M8 5v14l11-7z"),T.setAttribute("fill","currentColor"),M.appendChild(T),M},A=()=>{let l="http://www.w3.org/2000/svg",M=document.createElementNS(l,"svg");M.setAttribute("viewBox","0 0 24 24"),M.setAttribute("width","16"),M.setAttribute("height","16"),M.setAttribute("aria-hidden","true");let T=document.createElementNS(l,"rect");return T.setAttribute("x","6"),T.setAttribute("y","6"),T.setAttribute("width","12"),T.setAttribute("height","12"),T.setAttribute("fill","currentColor"),M.appendChild(T),M};o(t),t.appendChild(x());let $=l=>{o(t),l?(t.appendChild(A()),t.setAttribute("aria-label",`Stop ${c.label||c.value}`)):(t.appendChild(x()),t.setAttribute("aria-label",`Play ${c.label||c.value}`))};t.addEventListener("click",()=>{try{if(!p)return;let l=d.querySelectorAll("audio");if(!p.paused&&!p.ended){p.pause(),p.currentTime=0;return}l.forEach(T=>{try{T.pause(),T.currentTime=0}catch{}}),d.querySelectorAll(".voc-form-audio-item").forEach(T=>T.classList.remove("is-playing")),p.play()}catch(l){console.warn("audio play failed",l)}}),p.addEventListener("playing",()=>{y.classList.add("is-playing"),$(!0)});let R=()=>{y.classList.remove("is-playing"),$(!1)};p.addEventListener("pause",R),p.addEventListener("ended",R),e.appendChild(t),e.appendChild(p)}let w=document.createElement("div");w.className="voc-form-item-detail",w.style.display="flex",w.style.flexDirection="column",w.style.gap="4px";let r=document.createElement("div");r.className="voc-form-item-top",r.style.display="flex",r.style.alignItems="center",r.style.gap="8px",r.appendChild(u),r.appendChild(C);let L=document.createElement("div");L.className="voc-form-item-description";let k=c.description||c.hint_text||"";k&&(L.textContent=k),w.appendChild(r),w.appendChild(L),e.appendChild(w),y.appendChild(e),r.addEventListener("click",t=>{let a=t.target;a&&a.closest(".voc-form-audio-play")||(u.checked=!0,u.dispatchEvent(new Event("change",{bubbles:!0})))}),y.addEventListener("click",t=>{let a=t.target;a&&a.closest(".voc-form-audio-play")||(u.checked=!0,u.dispatchEvent(new Event("change",{bubbles:!0})))}),d.appendChild(y)}),d}function V(s,b,f){let v=document.createElement("div");v.className="voc-form-table-wrapper";let d=b?.style==="bootstrap",m=document.createElement("input");m.type="hidden",m.name=s.name||s.key,s.key&&(m.id=`voc-form-${s.key}`),v.appendChild(m);let E=document.createElement("table");E.className="voc-form-table",d&&E.classList.add("table","table-bordered");let c=document.createElement("thead"),h=document.createElement("tr"),y=document.createElement("th");y.className="voc-form-table-drag-col",y.textContent="",h.appendChild(y),(s.fields||[]).forEach(a=>{let i=document.createElement("th");i.textContent=a.label||a.key,h.appendChild(i)});let u=document.createElement("th");u.textContent="Actions",h.appendChild(u),c.appendChild(h),E.appendChild(c);let N=document.createElement("tbody");E.appendChild(N),v.appendChild(E);let C=document.createElement("button");C.type="button",C.className="voc-form-table-add",d&&C.classList.add("btn","btn-secondary"),C.textContent="+Add";let e=document.createElement("tfoot"),p=document.createElement("tr"),w=document.createElement("td");w.colSpan=(s.fields?s.fields.length:0)+1,w.appendChild(C),p.appendChild(w),e.appendChild(p),E.appendChild(e),v.appendChild(E);let r=[];function L(){m.value=JSON.stringify(r)}function k(a){let i=[];return s.fields.forEach(n=>{let o=a[n.key];if(n.type==="checkbox"){let x=Array.isArray(o)?o:o?[o]:[];n.required&&x.length===0&&i.push(`${n.label||n.key} is required`);return}n.required&&(o==null||String(o).trim()==="")&&i.push(`${n.label||n.key} is required`),n.type==="email"&&o&&(/^\S+@\S+\.\S+$/.test(String(o))||i.push(`${n.label||n.key} must be a valid email`)),n.type==="number"&&o&&isNaN(Number(o))&&i.push(`${n.label||n.key} must be a number`),n.validation&&n.validation.regex&&o&&(new RegExp(n.validation.regex).test(String(o))||i.push(`${n.label||n.key} invalid format`))}),i}function t(){N.innerHTML="";let a=(i,n)=>{n.draggable=!0,n.addEventListener("mousedown",()=>{n.style.cursor="grabbing"}),n.addEventListener("mouseup",()=>{n.style.cursor="grab"}),n.addEventListener("dragstart",o=>{let x=Number(i.dataset.rowIndex);o.dataTransfer.setData("text/plain",String(x)),i.classList.add("dragging");try{o.dataTransfer?.setDragImage(n,10,10)}catch{}}),n.addEventListener("dragend",()=>{i.classList.remove("dragging"),n.style.cursor="grab"}),i.addEventListener("dragover",o=>{o.preventDefault(),o.currentTarget.classList.add("drag-over")}),i.addEventListener("dragleave",o=>{o.currentTarget.classList.remove("drag-over")}),i.addEventListener("drop",o=>{o.preventDefault();let x=o.dataTransfer.getData("text/plain"),A=Number(o.currentTarget.dataset.rowIndex),$=Number(x);if(!isNaN($)&&!isNaN(A)&&$!==A){let[R]=r.splice($,1);r.splice(A,0,R),t(),L()}})};r.forEach((i,n)=>{let o=document.createElement("tr");o.dataset.rowIndex=String(n),o.classList.add("voc-form-table-row");let x=document.createElement("td");x.className="voc-form-table-drag-handle";let A=document.createElement("span");A.className="voc-form-drag-handle-icon",A.textContent="\u2630",A.style.cursor="grab",x.appendChild(A),o.appendChild(x),a(o,A),s.fields.forEach(l=>{let M=document.createElement("td"),T,q=(l.type||"text").toLowerCase();if(q==="select"){let g=document.createElement("select");if(g.className="voc-form-table-cell",d&&g.classList.add("form-control"),l.placeholder){let F=document.createElement("option");F.value="",F.textContent=l.placeholder,F.disabled=!0,(i[l.key]===void 0||i[l.key]===null||i[l.key]==="")&&(F.selected=!0),g.appendChild(F)}(l.options||[]).forEach(F=>{let D=document.createElement("option");D.value=F.value,D.textContent=F.label,g.appendChild(D)}),g.value=i[l.key]!==void 0&&i[l.key]!==null?String(i[l.key]):"",l.required&&(g.required=!0),g.addEventListener("change",F=>{r[n][l.key]=F.target.value,L()}),T=g}else if(q==="checkbox"){let g=document.createElement("div");g.className="voc-form-table-checkboxes",d&&g.classList.add("form-check");let B=l.options||[];Array.isArray(r[n][l.key])||(r[n][l.key]=r[n][l.key]?[r[n][l.key]]:[]),B.forEach(F=>{let D=`voc-form-${s.key}-${l.key}-${n}-${F.value}`,_=document.createElement("div");_.className="voc-form-table-checkbox-item",d&&_.classList.add("form-check");let S=document.createElement("input");S.type="checkbox",S.className="voc-form-table-cell",d&&S.classList.add("form-check-input"),S.id=D,S.value=F.value,S.checked=Array.isArray(r[n][l.key])&&r[n][l.key].includes(F.value),S.addEventListener("change",O=>{let U=O.target.checked;Array.isArray(r[n][l.key])||(r[n][l.key]=[]),U?r[n][l.key].includes(F.value)||r[n][l.key].push(F.value):r[n][l.key]=r[n][l.key].filter(K=>K!==F.value),L()});let H=document.createElement("label");H.htmlFor=D,H.className="voc-form-table-checkbox-label",d&&H.classList.add("form-check-label"),H.textContent=F.label,_.appendChild(S),_.appendChild(H),g.appendChild(_)}),T=g}else if(q==="radio"){let g=document.createElement("div");g.className="voc-form-table-radios",d&&g.classList.add("form-check");let B=l.options||[];r[n][l.key]===void 0&&(r[n][l.key]=""),B.forEach(F=>{let D=`voc-form-${s.key}-${l.key}-${n}-${F.value}`,_=document.createElement("div");_.className="voc-form-table-radio-item",d&&_.classList.add("form-check");let S=document.createElement("input");S.type="radio",S.className="voc-form-table-cell",d&&S.classList.add("form-check-input"),S.id=D,S.name=`voc-form-${s.key}-${l.key}-${n}`,S.value=F.value,S.checked=r[n][l.key]===F.value,S.addEventListener("change",O=>{O.target.checked&&(r[n][l.key]=O.target.value,L())});let H=document.createElement("label");H.htmlFor=D,H.className="voc-form-table-radio-label",d&&H.classList.add("form-check-label"),H.textContent=F.label,_.appendChild(S),_.appendChild(H),g.appendChild(_)}),T=g}else{let g=document.createElement("input");q==="number"?g.type="number":q==="email"?g.type="email":q==="tel"?g.type="tel":q==="url"?g.type="url":q==="date"?g.type="date":g.type="text",g.value=i[l.key]!==void 0&&i[l.key]!==null?String(i[l.key]):"",l.placeholder&&(g.placeholder=l.placeholder),l.required&&(g.required=!0),g.className="voc-form-table-cell",d&&g.classList.add("form-control"),g.addEventListener("input",B=>{r[n][l.key]=B.target.value,L()}),T=g}M.appendChild(T),o.appendChild(M)});let $=document.createElement("td"),R=document.createElement("button");R.type="button",R.className="voc-form-table-remove",d&&R.classList.add("btn","btn-danger"),R.textContent="Remove",R.addEventListener("click",()=>{r.splice(n,1),t(),L()}),$.appendChild(R),o.appendChild($),N.appendChild(o)})}return C.addEventListener("click",()=>{if(s.validation?.max_rows!==void 0&&r.length>=s.validation.max_rows)return;let a={};s.fields.forEach(i=>a[i.key]=""),r.push(a),t(),L()}),Array.isArray(f)&&f.length?r=f.map(a=>({...a})):s.default&&Array.isArray(s.default)&&(r=s.default.map(a=>({...a}))),t(),L(),v.validate=function(){let a=[];return r.forEach((i,n)=>{let o=k(i);o.length&&a.push(`Row ${n+1}: ${o.join("; ")}`)}),s.validation?.min_rows!==void 0&&r.length<s.validation.min_rows&&a.push(`At least ${s.validation.min_rows} rows required`),s.validation?.max_rows!==void 0&&r.length>s.validation.max_rows&&a.push(`No more than ${s.validation.max_rows} rows allowed`),s.validation?.unique_fields&&s.validation.unique_fields.length&&s.validation.unique_fields.forEach(i=>{let n=new Set;r.forEach((o,x)=>{let A=o[i];A!=null&&(n.has(A)&&a.push(`Field ${i} must be unique (duplicate at row ${x+1})`),n.add(A))})}),a},v}function I(s,b=!0){let f=new FormData(s);if(b)return ae(f);let v={};return f.forEach((d,m)=>{let E=(d instanceof File,d);Object.prototype.hasOwnProperty.call(v,m)?Array.isArray(v[m])?v[m].push(E):v[m]=[v[m],E]:v[m]=E}),v}function ae(s){let b=new FormData;for(let[f,v]of s.entries()){if(v instanceof File){b.append(f,v);continue}let d;try{d=JSON.parse(v)}catch{d=null}Array.isArray(d)&&d.every(m=>typeof m=="object"&&m!==null)?d.forEach((m,E)=>{for(let[c,h]of Object.entries(m))b.append(`${f}[${E}][${c}]`,h)}):b.append(f,v)}return b}async function G(s,b,f){let v=b.url,d={"Content-Type":"application/json"};b?.api_token&&(d.Authorization=`Bearer ${b.api_token}`);let m=I(s);if(b?.additional_data)for(let[h,y]of Object.entries(b.additional_data))m.append(h,y);let E,c=m instanceof FormData;E={method:"POST",headers:{Authorization:`Bearer ${b.api_token}`,...c?{}:{"Content-Type":"application/json"},Accept:"application/json"},body:c?m:JSON.stringify(Object.fromEntries(m.entries()))};try{fetch(v,E).then(async h=>{let y=h.headers.get("content-type");if(!h.ok)try{let N=(await h.json())?.message;throw new Error(N??"Their was an error submitting the form. Check your form and try again.")}catch(u){throw new Error(u.message)}return y&&y.includes("application/json")?h.json():h.text()}).then(h=>{f&&f(h,null)}).catch(h=>{f&&f(null,h)})}catch(h){f&&f(null,h)}}function J(s,b,f){let v=[];if(typeof s=="string"?v=Array.from(document.querySelectorAll(s)):v=[s],v.length===0)return null;function d(){let c=f?.style==="bootstrap",h=document.createElement("form");h.classList.add("voc-form"),c&&h.classList.add("needs-validation"),h.id=b.id;let y=document.createElement("h2");y.className="voc-form-title",c&&y.classList.add("mb-3"),y.textContent=b.form_name,h.appendChild(y);function u(e,p){p&&(p.min_length!==void 0&&(e.minLength=p.min_length),p.max_length!==void 0&&(e.maxLength=p.max_length),p.regex&&(e instanceof HTMLInputElement?e.pattern=p.regex:e.dataset.pattern=p.regex))}function N(e){if(!e.key&&!e.label)return null;let p=b.default_values||{},w=e.name||e.key,r=w?p[w]:void 0,L=document.createElement("div");if(L.className="voc-form-group",c&&L.classList.add("mb-3"),e.label){let t=document.createElement("label");if(t.classList.add("voc-form-label"),c&&t.classList.add("form-label"),e.key?e.type==="radio"?t.htmlFor="":t.htmlFor=`voc-form-${e.key}`:t.htmlFor="",t.textContent=e.label,e.required){let a=document.createElement("span");a.className="voc-form-required",a.textContent="*",t.appendChild(a)}L.appendChild(t)}let k=null;switch(e.type){case"checkbox":{let t=document.createElement("div");if(t.className="voc-form-checkbox-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options)e.options.forEach((a,i)=>{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),c&&n.classList.add("form-check"),n.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(n.style.width=e.width);let o=document.createElement("input");o.type="checkbox",o.classList.add("voc-form-checkbox"),c&&o.classList.add("form-check-input"),o.name=e.name||e.key||"",o.value=a.value,e.key&&(o.id=`voc-form-${e.key}-${i}`),e.required&&(o.required=!0),Array.isArray(r)?r.includes(a.value)&&(o.checked=!0):r!==void 0?r===a.value&&(o.checked=!0):e.default===a.value&&(o.checked=!0),n.appendChild(o),n.appendChild(document.createTextNode(a.label)),t.appendChild(n)});else{let a=document.createElement("label");a.classList.add("voc-form-checkbox-label"),c&&a.classList.add("form-check"),e.width&&(a.style.width=e.width);let i=document.createElement("input");i.type="checkbox",i.classList.add("voc-form-checkbox"),(e.key||e.name)&&(i.name=e.name||e.key,e.key&&(i.id=`voc-form-${e.key}`)),c&&i.classList.add("form-check-input"),e.required&&(i.required=!0),r!==void 0?i.checked=!!r:e.default&&(i.checked=!!e.default),a.appendChild(i),a.appendChild(document.createTextNode(e.label||"")),t.appendChild(a)}k=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),c&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),r!=null?t.value=String(r):e.default!==void 0&&e.default!==null&&(t.value=String(e.default));let a=e.default||r||"";if(a){let i=(a.match(/\n/g)||[]).length,n=80,o=a.split(`
2
+ `).reduce(($,R)=>Math.max($,R.length),0),x=Math.ceil(o/n)||1,A=Math.max(2,i+x);t.rows=A,t.style.resize="vertical"}e.width&&(t.style.width=e.width),e.required&&(t.required=!0),u(t,e.validation),k=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),c&&t.classList.add("form-select"),c&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder){let a=document.createElement("option");a.value="",a.textContent=e.placeholder,a.disabled=!0,a.selected=!0,t.appendChild(a)}e.required&&(t.required=!0),e.options&&e.options.forEach(a=>{let i=document.createElement("option");i.value=a.value,i.textContent=a.label,Array.isArray(r)?r.includes(a.value)&&(i.selected=!0):r!==void 0?r===a.value&&(i.selected=!0):e.default===a.value&&(i.selected=!0),t.appendChild(i)}),e.width&&(t.style.width=e.width),k=t;break}case"radio":{let t=document.createElement("div");t.className="voc-form-radio-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options&&e.options.forEach((a,i)=>{let n=document.createElement("label");n.classList.add("voc-form-radio-label"),c&&n.classList.add("form-check"),n.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(n.style.width=e.width);let o=document.createElement("input");o.type="radio",o.classList.add("voc-form-radio"),c&&o.classList.add("form-check-input"),o.name=e.name||e.key||"",o.value=a.value,e.key&&(o.id=`voc-form-${e.key}-${i}`),Array.isArray(r)?r.includes(a.value)&&(o.checked=!0):r!==void 0?r===a.value&&(o.checked=!0):e.default===a.value&&(o.checked=!0),n.appendChild(o),n.appendChild(document.createTextNode(a.label)),t.appendChild(n)}),k=t;break}case"file":{let t=document.createElement("div");t.className="voc-form-file-wrapper",e.width&&(t.style.width=e.width);let a=document.createElement("input");a.type="file",a.classList.add("voc-form-file"),c&&a.classList.add("form-control"),(e.key||e.name)&&(a.name=e.name||e.key,e.key&&(a.id=`voc-form-${e.key}`)),e.width&&(a.style.width=e.width),e.required&&(a.required=!0),e.accept&&(a.accept=e.accept),e.multiple&&(a.multiple=!0),t.appendChild(a),k=t;break}case"avatar":{k=z(e,f||{},r);break}case"table":{k=V(e,f||{},r);break}case"audio_select":{k=P(e,f||{},r);break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),c&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),r!=null?t.value=String(r):e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),u(t,e.validation),k=t;break}}if(k&&L.appendChild(k),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,L.appendChild(t)}return L}b.fields.forEach(e=>{if(e.type==="group"&&"items"in e){let p=e;if(c){let w=document.createElement("div");w.className="card voc-form-group-card mb-3";let r=p.label||p.header?.title,L=p.description||p.header?.description;if(r||L){let t=document.createElement("div");if(t.className="card-header voc-form-group-head",r){let a=document.createElement("h5");a.className="mb-0 voc-form-group-label",a.textContent=r,t.appendChild(a)}if(L){let a=document.createElement("p");a.className="mb-0 voc-form-group-description",a.textContent=L,t.appendChild(a)}w.appendChild(t)}let k=document.createElement("div");k.className="card-body voc-form-group-body",p.items.forEach(t=>{if(t.type==="row"&&"items"in t){let a=t,i=document.createElement("div");if(i.className="voc-form-group voc-form-row mb-3",a.label){let o=document.createElement("h6");o.className="voc-form-row-label",o.textContent=a.label,i.appendChild(o)}let n=document.createElement("div");n.className="voc-form-row-items "+(a.direction||"horizontal"),a.items.forEach(o=>{let x=N(o);x&&n.appendChild(x)}),i.appendChild(n),k.appendChild(i)}else{let a=N(t);a&&k.appendChild(a)}}),w.appendChild(k),h.appendChild(w)}else{let w=document.createElement("div");w.className="voc-form-group voc-form-group-wrapper",c&&w.classList.add("mb-3");let r=p.label||p.header?.title,L=p.description||p.header?.description;if(r||L){let k=document.createElement("div");if(k.className="voc-form-group-head",r){let t=document.createElement("h3");t.className="voc-form-group-label",t.textContent=r,k.appendChild(t)}if(L){let t=document.createElement("p");t.className="voc-form-group-description",t.textContent=L,k.appendChild(t)}w.appendChild(k)}p.items.forEach(k=>{if(k.type==="row"&&"items"in k){let t=k,a=document.createElement("div");if(a.className="voc-form-group voc-form-row",c&&a.classList.add("mb-3"),t.label){let n=document.createElement("h4");n.className="voc-form-row-label",n.textContent=t.label,a.appendChild(n)}let i=document.createElement("div");i.className="voc-form-row-items "+(t.direction||"horizontal"),t.items.forEach(n=>{let o=N(n);o&&i.appendChild(o)}),a.appendChild(i),w.appendChild(a)}else{let t=N(k);t&&w.appendChild(t)}}),h.appendChild(w)}return}if(e.type==="row"&&"items"in e){let p=document.createElement("div");if(p.className="voc-form-group voc-form-row",c&&p.classList.add("mb-3"),e.label){let r=document.createElement("h3");r.className="voc-form-row-label",r.textContent=e.label,p.appendChild(r)}if(e.description){let r=document.createElement("p");r.className="voc-form-row-description",r.textContent=e.description,p.appendChild(r)}let w=document.createElement("div");w.className="voc-form-row-items "+(e.direction||"horizontal"),p.appendChild(w),e.items.forEach(r=>{let L=N(r);L&&w.appendChild(L)}),h.appendChild(p)}else{let p=N(e);p&&h.appendChild(p)}});let C=document.createElement("button");return C.type="submit",C.className="voc-form-submit",C.textContent="Submit",c&&C.classList.add("btn","btn-primary"),h.appendChild(C),h}let m=d();v[0].appendChild(m);let E=b?.submit_config??f?.submit_config??null;return E&&m.addEventListener("submit",c=>{if(c.preventDefault(),!m.checkValidity()){m.reportValidity(),f?.onSubmitResult&&f.onSubmitResult(null,new Error("Form validation failed"));return}let h=Array.from(m.querySelectorAll(" .voc-form-table-wrapper, .voc-form-table-wrapper")),y=[];if(h.forEach(u=>{if(u&&typeof u.validate=="function"){let N=u.validate();N&&N.length&&y.push(...N)}}),y.length){f?.onSubmitResult&&f.onSubmitResult(null,new Error(y.join("; ")));let u=h[0];u&&u.scrollIntoView({behavior:"smooth",block:"center"});return}G(m,E,(u,N)=>{f?.onSubmitResult&&f.onSubmitResult(u,N)})}),{element:m,validate:()=>{let c=[];return m.querySelectorAll(":invalid").forEach(y=>{let u=y.validationMessage;c.push({element:y,message:u})}),c},getValues:()=>I(m),onSubmit:c=>{m.addEventListener("submit",h=>{h.preventDefault();let y=I(m);c(y,h)})}}}var W={renderForm:J};var ne=W,re={FormsLib:W};return te(oe);})();
package/dist/main.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var H=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var U=Object.getOwnPropertyNames;var J=Object.prototype.hasOwnProperty;var K=(a,w)=>{for(var v in w)H(a,v,{get:w[v],enumerable:!0})},Q=(a,w,v,E)=>{if(w&&typeof w=="object"||typeof w=="function")for(let y of U(w))!J.call(a,y)&&y!==v&&H(a,y,{get:()=>w[y],enumerable:!(E=G(w,y))||E.enumerable});return a};var X=a=>Q(H({},"__esModule",{value:!0}),a);var ee={};K(ee,{FormsLib:()=>Y,default:()=>Z});module.exports=X(ee);function D(a,w,v){let E=w?.style==="bootstrap",y=document.createElement("div");y.className="voc-form-avatar-wrapper",E&&y.classList.add("d-flex","align-items-center","gap-3");let p=document.createElement("img");p.className="voc-form-avatar-preview",p.alt=a.label||"avatar",E&&(p.classList.add("img-thumbnail","rounded-circle"),p.style.width=p.style.height="64px",p.style.objectFit="cover");let C=v!=null?String(v):a.default!==void 0&&a.default!==null?String(a.default):"";C?p.src=C:(p.src="",p.style.display="none");let o=document.createElement("input");o.type="file",o.accept=a.accept||"image/*",o.className="voc-form-avatar-input",E&&o.classList.add("form-control"),(a.key||a.name)&&(o.name=a.name||a.key,a.key&&(o.id=`voc-form-${a.key}`));let h=document.createElement("input");if(h.type="hidden",(a.key||a.name)&&(h.name=(a.name||a.key)+"_url"),h.value=C||"",o.addEventListener("change",b=>{let l=b.target;if(l.files&&l.files.length>0){let L=l.files[0],A=new FileReader;A.onload=()=>{p.src=String(A.result),p.style.display="",h.value=""},A.readAsDataURL(L)}}),E){let b=document.createElement("div");b.style.flex="1",b.appendChild(o),b.appendChild(h),y.appendChild(p),y.appendChild(b)}else y.appendChild(p),y.appendChild(o),y.appendChild(h);return y}function O(a,w,v){let E=w?.style==="bootstrap",y=document.createElement("div");if(y.className="voc-form-audio-select",!a.options||a.options.length===0)return y;let p=a.name||a.key||`audio_select_${Math.random().toString(36).substr(2,6)}`,C;return Array.isArray(v)?C=v[0]:v!==void 0?C=v:a.default!==void 0&&(C=a.default),a.options.forEach((o,h)=>{let b=document.createElement("div");b.className="voc-form-audio-item",E&&b.classList.add("form-check");let l=document.createElement("input");l.type="radio",l.name=p,l.value=o.value,l.className="voc-form-audio-radio",a.key&&(l.id=`voc-form-${a.key}-${h}`),a.required&&h===0&&(l.required=!0),l.style.position="absolute",l.style.width="1px",l.style.height="1px",l.style.margin="0",l.style.padding="0",l.style.clip="rect(0 0 0 0)",l.style.clipPath="inset(50%)",l.style.overflow="hidden",l.style.border="0",C!==void 0&&(Array.isArray(C)?l.checked=C.includes(o.value):l.checked=String(C)===String(o.value));let L=()=>{l.checked?b.classList.add("selected"):b.classList.remove("selected")};l.addEventListener("change",()=>{y.querySelectorAll(".voc-form-audio-item").forEach(n=>n.classList.remove("selected")),L()}),l.checked&&b.classList.add("selected");let A=document.createElement("label");A.className="voc-form-audio-label",E&&A.classList.add("form-check-label"),A.htmlFor=l.id||"",A.textContent=o.label||o.value,A.tabIndex=0;let e=document.createElement("div");e.className="voc-form-audio-control";let d=null;if(o.audio_link){let t=document.createElement("button");t.type="button",t.className="voc-form-audio-play",E&&t.classList.add("btn","btn-sm","btn-outline-secondary"),t.setAttribute("aria-label",`Play ${o.label||o.value}`);let n="http://www.w3.org/2000/svg",r=document.createElementNS(n,"svg");r.setAttribute("viewBox","0 0 24 24"),r.setAttribute("width","16"),r.setAttribute("height","16"),r.setAttribute("aria-hidden","true");let s=document.createElementNS(n,"path");s.setAttribute("d","M8 5v14l11-7z"),s.setAttribute("fill","currentColor"),r.appendChild(s),t.appendChild(r),d=document.createElement("audio"),d.src=o.audio_link,d.preload="none",d.style.display="none";let m=f=>{for(;f.firstChild;)f.removeChild(f.firstChild)},u=()=>{let f="http://www.w3.org/2000/svg",F=document.createElementNS(f,"svg");F.setAttribute("viewBox","0 0 24 24"),F.setAttribute("width","16"),F.setAttribute("height","16"),F.setAttribute("aria-hidden","true");let g=document.createElementNS(f,"path");return g.setAttribute("d","M8 5v14l11-7z"),g.setAttribute("fill","currentColor"),F.appendChild(g),F},R=()=>{let f="http://www.w3.org/2000/svg",F=document.createElementNS(f,"svg");F.setAttribute("viewBox","0 0 24 24"),F.setAttribute("width","16"),F.setAttribute("height","16"),F.setAttribute("aria-hidden","true");let g=document.createElementNS(f,"rect");return g.setAttribute("x","6"),g.setAttribute("y","6"),g.setAttribute("width","12"),g.setAttribute("height","12"),g.setAttribute("fill","currentColor"),F.appendChild(g),F};m(t),t.appendChild(u());let S=f=>{m(t),f?(t.appendChild(R()),t.setAttribute("aria-label",`Stop ${o.label||o.value}`)):(t.appendChild(u()),t.setAttribute("aria-label",`Play ${o.label||o.value}`))};t.addEventListener("click",()=>{try{if(!d)return;let f=y.querySelectorAll("audio");if(!d.paused&&!d.ended){d.pause(),d.currentTime=0;return}f.forEach(g=>{try{g.pause(),g.currentTime=0}catch{}}),y.querySelectorAll(".voc-form-audio-item").forEach(g=>g.classList.remove("is-playing")),d.play()}catch(f){console.warn("audio play failed",f)}}),d.addEventListener("playing",()=>{b.classList.add("is-playing"),S(!0)});let N=()=>{b.classList.remove("is-playing"),S(!1)};d.addEventListener("pause",N),d.addEventListener("ended",N),e.appendChild(t),e.appendChild(d)}let c=document.createElement("div");c.className="voc-form-item-detail",c.style.display="flex",c.style.flexDirection="column",c.style.gap="4px";let i=document.createElement("div");i.className="voc-form-item-top",i.style.display="flex",i.style.alignItems="center",i.style.gap="8px",i.appendChild(l),i.appendChild(A);let x=document.createElement("div");x.className="voc-form-item-description";let k=o.description||o.hint_text||"";k&&(x.textContent=k),c.appendChild(i),c.appendChild(x),e.appendChild(c),b.appendChild(e),i.addEventListener("click",t=>{let n=t.target;n&&n.closest(".voc-form-audio-play")||(l.checked=!0,l.dispatchEvent(new Event("change",{bubbles:!0})))}),b.addEventListener("click",t=>{let n=t.target;n&&n.closest(".voc-form-audio-play")||(l.checked=!0,l.dispatchEvent(new Event("change",{bubbles:!0})))}),y.appendChild(b)}),y}function I(a,w,v){let E=document.createElement("div");E.className="voc-form-table-wrapper";let y=w?.style==="bootstrap",p=document.createElement("input");p.type="hidden",p.name=a.name||a.key,a.key&&(p.id=`voc-form-${a.key}`),E.appendChild(p);let C=document.createElement("table");C.className="voc-form-table",y&&C.classList.add("table","table-bordered");let o=document.createElement("thead"),h=document.createElement("tr");(a.fields||[]).forEach(t=>{let n=document.createElement("th");n.textContent=t.label||t.key,h.appendChild(n)});let b=document.createElement("th");b.textContent="Actions",h.appendChild(b),o.appendChild(h),C.appendChild(o);let l=document.createElement("tbody");C.appendChild(l),E.appendChild(C);let L=document.createElement("button");L.type="button",L.className="voc-form-table-add",y&&L.classList.add("btn","btn-secondary"),L.textContent="+Add";let A=document.createElement("tfoot"),e=document.createElement("tr"),d=document.createElement("td");d.colSpan=(a.fields?a.fields.length:0)+1,d.appendChild(L),e.appendChild(d),A.appendChild(e),C.appendChild(A),E.appendChild(C);let c=[];function i(){p.value=JSON.stringify(c)}function x(t){let n=[];return a.fields.forEach(r=>{let s=t[r.key];if(r.type==="checkbox"){let m=Array.isArray(s)?s:s?[s]:[];r.required&&m.length===0&&n.push(`${r.label||r.key} is required`);return}r.required&&(s==null||String(s).trim()==="")&&n.push(`${r.label||r.key} is required`),r.type==="email"&&s&&(/^\S+@\S+\.\S+$/.test(String(s))||n.push(`${r.label||r.key} must be a valid email`)),r.type==="number"&&s&&isNaN(Number(s))&&n.push(`${r.label||r.key} must be a number`),r.validation&&r.validation.regex&&s&&(new RegExp(r.validation.regex).test(String(s))||n.push(`${r.label||r.key} invalid format`))}),n}function k(){l.innerHTML="",c.forEach((t,n)=>{let r=document.createElement("tr");a.fields.forEach(u=>{let R=document.createElement("td"),S,N=(u.type||"text").toLowerCase();if(N==="select"){let f=document.createElement("select");if(f.className="voc-form-table-cell",y&&f.classList.add("form-control"),u.placeholder){let g=document.createElement("option");g.value="",g.textContent=u.placeholder,g.disabled=!0,(t[u.key]===void 0||t[u.key]===null||t[u.key]==="")&&(g.selected=!0),f.appendChild(g)}(u.options||[]).forEach(g=>{let _=document.createElement("option");_.value=g.value,_.textContent=g.label,f.appendChild(_)}),f.value=t[u.key]!==void 0&&t[u.key]!==null?String(t[u.key]):"",u.required&&(f.required=!0),f.addEventListener("change",g=>{c[n][u.key]=g.target.value,i()}),S=f}else if(N==="checkbox"){let f=document.createElement("div");f.className="voc-form-table-checkboxes",y&&f.classList.add("form-check");let F=u.options||[];Array.isArray(c[n][u.key])||(c[n][u.key]=c[n][u.key]?[c[n][u.key]]:[]),F.forEach(g=>{let _=`voc-form-${a.key}-${u.key}-${n}-${g.value}`,q=document.createElement("div");q.className="voc-form-table-checkbox-item",y&&q.classList.add("form-check");let T=document.createElement("input");T.type="checkbox",T.className="voc-form-table-cell",y&&T.classList.add("form-check-input"),T.id=_,T.value=g.value,T.checked=Array.isArray(c[n][u.key])&&c[n][u.key].includes(g.value),T.addEventListener("change",z=>{let P=z.target.checked;Array.isArray(c[n][u.key])||(c[n][u.key]=[]),P?c[n][u.key].includes(g.value)||c[n][u.key].push(g.value):c[n][u.key]=c[n][u.key].filter(V=>V!==g.value),i()});let M=document.createElement("label");M.htmlFor=_,M.className="voc-form-table-checkbox-label",y&&M.classList.add("form-check-label"),M.textContent=g.label,q.appendChild(T),q.appendChild(M),f.appendChild(q)}),S=f}else{let f=document.createElement("input");N==="number"?f.type="number":N==="email"?f.type="email":N==="tel"?f.type="tel":N==="url"?f.type="url":N==="date"?f.type="date":f.type="text",f.value=t[u.key]!==void 0&&t[u.key]!==null?String(t[u.key]):"",u.placeholder&&(f.placeholder=u.placeholder),u.required&&(f.required=!0),f.className="voc-form-table-cell",y&&f.classList.add("form-control"),f.addEventListener("input",F=>{c[n][u.key]=F.target.value,i()}),S=f}R.appendChild(S),r.appendChild(R)});let s=document.createElement("td"),m=document.createElement("button");m.type="button",m.className="voc-form-table-remove",y&&m.classList.add("btn","btn-danger"),m.textContent="Remove",m.addEventListener("click",()=>{c.splice(n,1),k(),i()}),s.appendChild(m),r.appendChild(s),l.appendChild(r)})}return L.addEventListener("click",()=>{if(a.validation?.max_rows!==void 0&&c.length>=a.validation.max_rows)return;let t={};a.fields.forEach(n=>t[n.key]=""),c.push(t),k(),i()}),Array.isArray(v)&&v.length?c=v.map(t=>({...t})):a.default&&Array.isArray(a.default)&&(c=a.default.map(t=>({...t}))),k(),i(),E.validate=function(){let t=[];return c.forEach((n,r)=>{let s=x(n);s.length&&t.push(`Row ${r+1}: ${s.join("; ")}`)}),a.validation?.min_rows!==void 0&&c.length<a.validation.min_rows&&t.push(`At least ${a.validation.min_rows} rows required`),a.validation?.max_rows!==void 0&&c.length>a.validation.max_rows&&t.push(`No more than ${a.validation.max_rows} rows allowed`),a.validation?.unique_fields&&a.validation.unique_fields.length&&a.validation.unique_fields.forEach(n=>{let r=new Set;c.forEach((s,m)=>{let u=s[n];u!=null&&(r.has(u)&&t.push(`Field ${n} must be unique (duplicate at row ${m+1})`),r.add(u))})}),t},E}function $(a,w=!0){let v=new FormData(a);if(w)return v;let E={};return v.forEach((y,p)=>{let C=(y instanceof File,y);Object.prototype.hasOwnProperty.call(E,p)?Array.isArray(E[p])?E[p].push(C):E[p]=[E[p],C]:E[p]=C}),E}async function W(a,w,v){let E=w.url,y={"Content-Type":"application/json"};w?.api_token&&(y.Authorization=`Bearer ${w.api_token}`);let p=$(a);if(w?.additional_data)for(let[h,b]of Object.entries(w.additional_data))p.append(h,b);let C,o=p instanceof FormData;C={method:"POST",headers:{Authorization:`Bearer ${w.api_token}`,...o?{}:{"Content-Type":"application/json"},Accept:"application/json"},body:o?p:JSON.stringify(Object.fromEntries(p.entries()))};try{fetch(E,C).then(async h=>{let b=h.headers.get("content-type");if(!h.ok)try{let L=(await h.json())?.message;throw new Error(L??"Their was an error submitting the form. Check your form and try again.")}catch(l){throw new Error(l.message)}return b&&b.includes("application/json")?h.json():h.text()}).then(h=>{v&&v(h,null)}).catch(h=>{v&&v(null,h)})}catch(h){v&&v(null,h)}}function j(a,w,v){let E=[];if(typeof a=="string"?E=Array.from(document.querySelectorAll(a)):E=[a],E.length===0)return null;function y(){let o=v?.style==="bootstrap",h=document.createElement("form");h.classList.add("voc-form"),o&&h.classList.add("needs-validation"),h.id=w.id;let b=document.createElement("h2");b.className="voc-form-title",o&&b.classList.add("mb-3"),b.textContent=w.form_name,h.appendChild(b);function l(e,d){d&&(d.min_length!==void 0&&(e.minLength=d.min_length),d.max_length!==void 0&&(e.maxLength=d.max_length),d.regex&&(e instanceof HTMLInputElement?e.pattern=d.regex:e.dataset.pattern=d.regex))}function L(e){if(!e.key&&!e.label)return null;let d=w.default_values||{},c=e.name||e.key,i=c?d[c]:void 0,x=document.createElement("div");if(x.className="voc-form-group",o&&x.classList.add("mb-3"),e.label){let t=document.createElement("label");if(t.classList.add("voc-form-label"),o&&t.classList.add("form-label"),e.key?e.type==="radio"?t.htmlFor="":t.htmlFor=`voc-form-${e.key}`:t.htmlFor="",t.textContent=e.label,e.required){let n=document.createElement("span");n.className="voc-form-required",n.textContent="*",t.appendChild(n)}x.appendChild(t)}let k=null;switch(e.type){case"checkbox":{let t=document.createElement("div");if(t.className="voc-form-checkbox-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options)e.options.forEach((n,r)=>{let s=document.createElement("label");s.classList.add("voc-form-checkbox-label"),o&&s.classList.add("form-check"),s.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(s.style.width=e.width);let m=document.createElement("input");m.type="checkbox",m.classList.add("voc-form-checkbox"),o&&m.classList.add("form-check-input"),m.name=e.name||e.key||"",m.value=n.value,e.key&&(m.id=`voc-form-${e.key}-${r}`),e.required&&(m.required=!0),Array.isArray(i)?i.includes(n.value)&&(m.checked=!0):i!==void 0?i===n.value&&(m.checked=!0):e.default===n.value&&(m.checked=!0),s.appendChild(m),s.appendChild(document.createTextNode(n.label)),t.appendChild(s)});else{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),o&&n.classList.add("form-check"),e.width&&(n.style.width=e.width);let r=document.createElement("input");r.type="checkbox",r.classList.add("voc-form-checkbox"),(e.key||e.name)&&(r.name=e.name||e.key,e.key&&(r.id=`voc-form-${e.key}`)),o&&r.classList.add("form-check-input"),e.required&&(r.required=!0),i!==void 0?r.checked=!!i:e.default&&(r.checked=!!e.default),n.appendChild(r),n.appendChild(document.createTextNode(e.label||"")),t.appendChild(n)}k=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),o&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),i!=null?t.value=String(i):e.default!==void 0&&e.default!==null&&(t.value=String(e.default));let n=e.default||i||"";if(n){let r=(n.match(/\n/g)||[]).length,s=80,m=n.split(`
2
- `).reduce((S,N)=>Math.max(S,N.length),0),u=Math.ceil(m/s)||1,R=Math.max(2,r+u);t.rows=R,t.style.resize="vertical"}e.width&&(t.style.width=e.width),e.required&&(t.required=!0),l(t,e.validation),k=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),o&&t.classList.add("form-select"),o&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder){let n=document.createElement("option");n.value="",n.textContent=e.placeholder,n.disabled=!0,n.selected=!0,t.appendChild(n)}e.required&&(t.required=!0),e.options&&e.options.forEach(n=>{let r=document.createElement("option");r.value=n.value,r.textContent=n.label,Array.isArray(i)?i.includes(n.value)&&(r.selected=!0):i!==void 0?i===n.value&&(r.selected=!0):e.default===n.value&&(r.selected=!0),t.appendChild(r)}),e.width&&(t.style.width=e.width),k=t;break}case"radio":{let t=document.createElement("div");t.className="voc-form-radio-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options&&e.options.forEach((n,r)=>{let s=document.createElement("label");s.classList.add("voc-form-radio-label"),o&&s.classList.add("form-check"),s.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(s.style.width=e.width);let m=document.createElement("input");m.type="radio",m.classList.add("voc-form-radio"),o&&m.classList.add("form-check-input"),m.name=e.name||e.key||"",m.value=n.value,e.key&&(m.id=`voc-form-${e.key}-${r}`),Array.isArray(i)?i.includes(n.value)&&(m.checked=!0):i!==void 0?i===n.value&&(m.checked=!0):e.default===n.value&&(m.checked=!0),s.appendChild(m),s.appendChild(document.createTextNode(n.label)),t.appendChild(s)}),k=t;break}case"file":{let t=document.createElement("div");t.className="voc-form-file-wrapper",e.width&&(t.style.width=e.width);let n=document.createElement("input");n.type="file",n.classList.add("voc-form-file"),o&&n.classList.add("form-control"),(e.key||e.name)&&(n.name=e.name||e.key,e.key&&(n.id=`voc-form-${e.key}`)),e.width&&(n.style.width=e.width),e.required&&(n.required=!0),e.accept&&(n.accept=e.accept),e.multiple&&(n.multiple=!0),t.appendChild(n),k=t;break}case"avatar":{k=D(e,v||{},i);break}case"table":{k=I(e,v||{},i);break}case"audio_select":{k=O(e,v||{},i);break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),o&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),i!=null?t.value=String(i):e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),l(t,e.validation),k=t;break}}if(k&&x.appendChild(k),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,x.appendChild(t)}return x}w.fields.forEach(e=>{if(e.type==="group"&&"items"in e){let d=e;if(o){let c=document.createElement("div");c.className="card voc-form-group-card mb-3";let i=d.label||d.header?.title,x=d.description||d.header?.description;if(i||x){let t=document.createElement("div");if(t.className="card-header voc-form-group-head",i){let n=document.createElement("h5");n.className="mb-0 voc-form-group-label",n.textContent=i,t.appendChild(n)}if(x){let n=document.createElement("p");n.className="mb-0 voc-form-group-description",n.textContent=x,t.appendChild(n)}c.appendChild(t)}let k=document.createElement("div");k.className="card-body voc-form-group-body",d.items.forEach(t=>{if(t.type==="row"&&"items"in t){let n=t,r=document.createElement("div");if(r.className="voc-form-group voc-form-row mb-3",n.label){let m=document.createElement("h6");m.className="voc-form-row-label",m.textContent=n.label,r.appendChild(m)}let s=document.createElement("div");s.className="voc-form-row-items "+(n.direction||"horizontal"),n.items.forEach(m=>{let u=L(m);u&&s.appendChild(u)}),r.appendChild(s),k.appendChild(r)}else{let n=L(t);n&&k.appendChild(n)}}),c.appendChild(k),h.appendChild(c)}else{let c=document.createElement("div");c.className="voc-form-group voc-form-group-wrapper",o&&c.classList.add("mb-3");let i=d.label||d.header?.title,x=d.description||d.header?.description;if(i||x){let k=document.createElement("div");if(k.className="voc-form-group-head",i){let t=document.createElement("h3");t.className="voc-form-group-label",t.textContent=i,k.appendChild(t)}if(x){let t=document.createElement("p");t.className="voc-form-group-description",t.textContent=x,k.appendChild(t)}c.appendChild(k)}d.items.forEach(k=>{if(k.type==="row"&&"items"in k){let t=k,n=document.createElement("div");if(n.className="voc-form-group voc-form-row",o&&n.classList.add("mb-3"),t.label){let s=document.createElement("h4");s.className="voc-form-row-label",s.textContent=t.label,n.appendChild(s)}let r=document.createElement("div");r.className="voc-form-row-items "+(t.direction||"horizontal"),t.items.forEach(s=>{let m=L(s);m&&r.appendChild(m)}),n.appendChild(r),c.appendChild(n)}else{let t=L(k);t&&c.appendChild(t)}}),h.appendChild(c)}return}if(e.type==="row"&&"items"in e){let d=document.createElement("div");if(d.className="voc-form-group voc-form-row",o&&d.classList.add("mb-3"),e.label){let i=document.createElement("h3");i.className="voc-form-row-label",i.textContent=e.label,d.appendChild(i)}if(e.description){let i=document.createElement("p");i.className="voc-form-row-description",i.textContent=e.description,d.appendChild(i)}let c=document.createElement("div");c.className="voc-form-row-items "+(e.direction||"horizontal"),d.appendChild(c),e.items.forEach(i=>{let x=L(i);x&&c.appendChild(x)}),h.appendChild(d)}else{let d=L(e);d&&h.appendChild(d)}});let A=document.createElement("button");return A.type="submit",A.className="voc-form-submit",A.textContent="Submit",o&&A.classList.add("btn","btn-primary"),h.appendChild(A),h}let p=y();E[0].appendChild(p);let C=w?.submit_config??v?.submit_config??null;return C&&p.addEventListener("submit",o=>{if(o.preventDefault(),!p.checkValidity()){p.reportValidity(),v?.onSubmitResult&&v.onSubmitResult(null,new Error("Form validation failed"));return}let h=Array.from(p.querySelectorAll(" .voc-form-table-wrapper, .voc-form-table-wrapper")),b=[];if(h.forEach(l=>{if(l&&typeof l.validate=="function"){let L=l.validate();L&&L.length&&b.push(...L)}}),b.length){v?.onSubmitResult&&v.onSubmitResult(null,new Error(b.join("; ")));let l=h[0];l&&l.scrollIntoView({behavior:"smooth",block:"center"});return}W(p,C,(l,L)=>{v?.onSubmitResult&&v.onSubmitResult(l,L)})}),{element:p,validate:()=>{let o=[];return p.querySelectorAll(":invalid").forEach(b=>{let l=b.validationMessage;o.push({element:b,message:l})}),o},getValues:()=>$(p),onSubmit:o=>{p.addEventListener("submit",h=>{h.preventDefault();let b=$(p);o(b,h)})}}}var B={renderForm:j};var Y=B,Z={FormsLib:B};0&&(module.exports={FormsLib});
1
+ "use strict";var j=Object.defineProperty;var Q=Object.getOwnPropertyDescriptor;var X=Object.getOwnPropertyNames;var Y=Object.prototype.hasOwnProperty;var Z=(s,b)=>{for(var f in b)j(s,f,{get:b[f],enumerable:!0})},ee=(s,b,f,v)=>{if(b&&typeof b=="object"||typeof b=="function")for(let d of X(b))!Y.call(s,d)&&d!==f&&j(s,d,{get:()=>b[d],enumerable:!(v=Q(b,d))||v.enumerable});return s};var te=s=>ee(j({},"__esModule",{value:!0}),s);var oe={};Z(oe,{FormsLib:()=>ne,default:()=>re});module.exports=te(oe);function z(s,b,f){let v=b?.style==="bootstrap",d=document.createElement("div");d.className="voc-form-avatar-wrapper",v&&d.classList.add("d-flex","align-items-center","gap-3");let m=document.createElement("img");m.className="voc-form-avatar-preview",m.alt=s.label||"avatar",v&&(m.classList.add("img-thumbnail","rounded-circle"),m.style.width=m.style.height="64px",m.style.objectFit="cover");let E=f!=null?String(f):s.default!==void 0&&s.default!==null?String(s.default):"";E?m.src=E:(m.src="",m.style.display="none");let c=document.createElement("input");c.type="file",c.accept=s.accept||"image/*",c.className="voc-form-avatar-input",v&&c.classList.add("form-control"),(s.key||s.name)&&(c.name=s.name||s.key,s.key&&(c.id=`voc-form-${s.key}`));let h=document.createElement("input");if(h.type="hidden",(s.key||s.name)&&(h.name=(s.name||s.key)+"_url"),h.value=E||"",c.addEventListener("change",y=>{let u=y.target;if(u.files&&u.files.length>0){let N=u.files[0],C=new FileReader;C.onload=()=>{m.src=String(C.result),m.style.display="",h.value=""},C.readAsDataURL(N)}}),v){let y=document.createElement("div");y.style.flex="1",y.appendChild(c),y.appendChild(h),d.appendChild(m),d.appendChild(y)}else d.appendChild(m),d.appendChild(c),d.appendChild(h);return d}function P(s,b,f){let v=b?.style==="bootstrap",d=document.createElement("div");if(d.className="voc-form-audio-select",!s.options||s.options.length===0)return d;let m=s.name||s.key||`audio_select_${Math.random().toString(36).substr(2,6)}`,E;return Array.isArray(f)?E=f[0]:f!==void 0?E=f:s.default!==void 0&&(E=s.default),s.options.forEach((c,h)=>{let y=document.createElement("div");y.className="voc-form-audio-item",v&&y.classList.add("form-check");let u=document.createElement("input");u.type="radio",u.name=m,u.value=c.value,u.className="voc-form-audio-radio",s.key&&(u.id=`voc-form-${s.key}-${h}`),s.required&&h===0&&(u.required=!0),u.style.position="absolute",u.style.width="1px",u.style.height="1px",u.style.margin="0",u.style.padding="0",u.style.clip="rect(0 0 0 0)",u.style.clipPath="inset(50%)",u.style.overflow="hidden",u.style.border="0",E!==void 0&&(Array.isArray(E)?u.checked=E.includes(c.value):u.checked=String(E)===String(c.value));let N=()=>{u.checked?y.classList.add("selected"):y.classList.remove("selected")};u.addEventListener("change",()=>{d.querySelectorAll(".voc-form-audio-item").forEach(a=>a.classList.remove("selected")),N()}),u.checked&&y.classList.add("selected");let C=document.createElement("label");C.className="voc-form-audio-label",v&&C.classList.add("form-check-label"),C.htmlFor=u.id||"",C.textContent=c.label||c.value,C.tabIndex=0;let e=document.createElement("div");e.className="voc-form-audio-control";let p=null;if(c.audio_link){let t=document.createElement("button");t.type="button",t.className="voc-form-audio-play",v&&t.classList.add("btn","btn-sm","btn-outline-secondary"),t.setAttribute("aria-label",`Play ${c.label||c.value}`);let a="http://www.w3.org/2000/svg",i=document.createElementNS(a,"svg");i.setAttribute("viewBox","0 0 24 24"),i.setAttribute("width","16"),i.setAttribute("height","16"),i.setAttribute("aria-hidden","true");let n=document.createElementNS(a,"path");n.setAttribute("d","M8 5v14l11-7z"),n.setAttribute("fill","currentColor"),i.appendChild(n),t.appendChild(i),p=document.createElement("audio"),p.src=c.audio_link,p.preload="none",p.style.display="none";let o=l=>{for(;l.firstChild;)l.removeChild(l.firstChild)},x=()=>{let l="http://www.w3.org/2000/svg",M=document.createElementNS(l,"svg");M.setAttribute("viewBox","0 0 24 24"),M.setAttribute("width","16"),M.setAttribute("height","16"),M.setAttribute("aria-hidden","true");let T=document.createElementNS(l,"path");return T.setAttribute("d","M8 5v14l11-7z"),T.setAttribute("fill","currentColor"),M.appendChild(T),M},A=()=>{let l="http://www.w3.org/2000/svg",M=document.createElementNS(l,"svg");M.setAttribute("viewBox","0 0 24 24"),M.setAttribute("width","16"),M.setAttribute("height","16"),M.setAttribute("aria-hidden","true");let T=document.createElementNS(l,"rect");return T.setAttribute("x","6"),T.setAttribute("y","6"),T.setAttribute("width","12"),T.setAttribute("height","12"),T.setAttribute("fill","currentColor"),M.appendChild(T),M};o(t),t.appendChild(x());let $=l=>{o(t),l?(t.appendChild(A()),t.setAttribute("aria-label",`Stop ${c.label||c.value}`)):(t.appendChild(x()),t.setAttribute("aria-label",`Play ${c.label||c.value}`))};t.addEventListener("click",()=>{try{if(!p)return;let l=d.querySelectorAll("audio");if(!p.paused&&!p.ended){p.pause(),p.currentTime=0;return}l.forEach(T=>{try{T.pause(),T.currentTime=0}catch{}}),d.querySelectorAll(".voc-form-audio-item").forEach(T=>T.classList.remove("is-playing")),p.play()}catch(l){console.warn("audio play failed",l)}}),p.addEventListener("playing",()=>{y.classList.add("is-playing"),$(!0)});let R=()=>{y.classList.remove("is-playing"),$(!1)};p.addEventListener("pause",R),p.addEventListener("ended",R),e.appendChild(t),e.appendChild(p)}let w=document.createElement("div");w.className="voc-form-item-detail",w.style.display="flex",w.style.flexDirection="column",w.style.gap="4px";let r=document.createElement("div");r.className="voc-form-item-top",r.style.display="flex",r.style.alignItems="center",r.style.gap="8px",r.appendChild(u),r.appendChild(C);let L=document.createElement("div");L.className="voc-form-item-description";let k=c.description||c.hint_text||"";k&&(L.textContent=k),w.appendChild(r),w.appendChild(L),e.appendChild(w),y.appendChild(e),r.addEventListener("click",t=>{let a=t.target;a&&a.closest(".voc-form-audio-play")||(u.checked=!0,u.dispatchEvent(new Event("change",{bubbles:!0})))}),y.addEventListener("click",t=>{let a=t.target;a&&a.closest(".voc-form-audio-play")||(u.checked=!0,u.dispatchEvent(new Event("change",{bubbles:!0})))}),d.appendChild(y)}),d}function V(s,b,f){let v=document.createElement("div");v.className="voc-form-table-wrapper";let d=b?.style==="bootstrap",m=document.createElement("input");m.type="hidden",m.name=s.name||s.key,s.key&&(m.id=`voc-form-${s.key}`),v.appendChild(m);let E=document.createElement("table");E.className="voc-form-table",d&&E.classList.add("table","table-bordered");let c=document.createElement("thead"),h=document.createElement("tr"),y=document.createElement("th");y.className="voc-form-table-drag-col",y.textContent="",h.appendChild(y),(s.fields||[]).forEach(a=>{let i=document.createElement("th");i.textContent=a.label||a.key,h.appendChild(i)});let u=document.createElement("th");u.textContent="Actions",h.appendChild(u),c.appendChild(h),E.appendChild(c);let N=document.createElement("tbody");E.appendChild(N),v.appendChild(E);let C=document.createElement("button");C.type="button",C.className="voc-form-table-add",d&&C.classList.add("btn","btn-secondary"),C.textContent="+Add";let e=document.createElement("tfoot"),p=document.createElement("tr"),w=document.createElement("td");w.colSpan=(s.fields?s.fields.length:0)+1,w.appendChild(C),p.appendChild(w),e.appendChild(p),E.appendChild(e),v.appendChild(E);let r=[];function L(){m.value=JSON.stringify(r)}function k(a){let i=[];return s.fields.forEach(n=>{let o=a[n.key];if(n.type==="checkbox"){let x=Array.isArray(o)?o:o?[o]:[];n.required&&x.length===0&&i.push(`${n.label||n.key} is required`);return}n.required&&(o==null||String(o).trim()==="")&&i.push(`${n.label||n.key} is required`),n.type==="email"&&o&&(/^\S+@\S+\.\S+$/.test(String(o))||i.push(`${n.label||n.key} must be a valid email`)),n.type==="number"&&o&&isNaN(Number(o))&&i.push(`${n.label||n.key} must be a number`),n.validation&&n.validation.regex&&o&&(new RegExp(n.validation.regex).test(String(o))||i.push(`${n.label||n.key} invalid format`))}),i}function t(){N.innerHTML="";let a=(i,n)=>{n.draggable=!0,n.addEventListener("mousedown",()=>{n.style.cursor="grabbing"}),n.addEventListener("mouseup",()=>{n.style.cursor="grab"}),n.addEventListener("dragstart",o=>{let x=Number(i.dataset.rowIndex);o.dataTransfer.setData("text/plain",String(x)),i.classList.add("dragging");try{o.dataTransfer?.setDragImage(n,10,10)}catch{}}),n.addEventListener("dragend",()=>{i.classList.remove("dragging"),n.style.cursor="grab"}),i.addEventListener("dragover",o=>{o.preventDefault(),o.currentTarget.classList.add("drag-over")}),i.addEventListener("dragleave",o=>{o.currentTarget.classList.remove("drag-over")}),i.addEventListener("drop",o=>{o.preventDefault();let x=o.dataTransfer.getData("text/plain"),A=Number(o.currentTarget.dataset.rowIndex),$=Number(x);if(!isNaN($)&&!isNaN(A)&&$!==A){let[R]=r.splice($,1);r.splice(A,0,R),t(),L()}})};r.forEach((i,n)=>{let o=document.createElement("tr");o.dataset.rowIndex=String(n),o.classList.add("voc-form-table-row");let x=document.createElement("td");x.className="voc-form-table-drag-handle";let A=document.createElement("span");A.className="voc-form-drag-handle-icon",A.textContent="\u2630",A.style.cursor="grab",x.appendChild(A),o.appendChild(x),a(o,A),s.fields.forEach(l=>{let M=document.createElement("td"),T,q=(l.type||"text").toLowerCase();if(q==="select"){let g=document.createElement("select");if(g.className="voc-form-table-cell",d&&g.classList.add("form-control"),l.placeholder){let F=document.createElement("option");F.value="",F.textContent=l.placeholder,F.disabled=!0,(i[l.key]===void 0||i[l.key]===null||i[l.key]==="")&&(F.selected=!0),g.appendChild(F)}(l.options||[]).forEach(F=>{let D=document.createElement("option");D.value=F.value,D.textContent=F.label,g.appendChild(D)}),g.value=i[l.key]!==void 0&&i[l.key]!==null?String(i[l.key]):"",l.required&&(g.required=!0),g.addEventListener("change",F=>{r[n][l.key]=F.target.value,L()}),T=g}else if(q==="checkbox"){let g=document.createElement("div");g.className="voc-form-table-checkboxes",d&&g.classList.add("form-check");let B=l.options||[];Array.isArray(r[n][l.key])||(r[n][l.key]=r[n][l.key]?[r[n][l.key]]:[]),B.forEach(F=>{let D=`voc-form-${s.key}-${l.key}-${n}-${F.value}`,_=document.createElement("div");_.className="voc-form-table-checkbox-item",d&&_.classList.add("form-check");let S=document.createElement("input");S.type="checkbox",S.className="voc-form-table-cell",d&&S.classList.add("form-check-input"),S.id=D,S.value=F.value,S.checked=Array.isArray(r[n][l.key])&&r[n][l.key].includes(F.value),S.addEventListener("change",O=>{let U=O.target.checked;Array.isArray(r[n][l.key])||(r[n][l.key]=[]),U?r[n][l.key].includes(F.value)||r[n][l.key].push(F.value):r[n][l.key]=r[n][l.key].filter(K=>K!==F.value),L()});let H=document.createElement("label");H.htmlFor=D,H.className="voc-form-table-checkbox-label",d&&H.classList.add("form-check-label"),H.textContent=F.label,_.appendChild(S),_.appendChild(H),g.appendChild(_)}),T=g}else if(q==="radio"){let g=document.createElement("div");g.className="voc-form-table-radios",d&&g.classList.add("form-check");let B=l.options||[];r[n][l.key]===void 0&&(r[n][l.key]=""),B.forEach(F=>{let D=`voc-form-${s.key}-${l.key}-${n}-${F.value}`,_=document.createElement("div");_.className="voc-form-table-radio-item",d&&_.classList.add("form-check");let S=document.createElement("input");S.type="radio",S.className="voc-form-table-cell",d&&S.classList.add("form-check-input"),S.id=D,S.name=`voc-form-${s.key}-${l.key}-${n}`,S.value=F.value,S.checked=r[n][l.key]===F.value,S.addEventListener("change",O=>{O.target.checked&&(r[n][l.key]=O.target.value,L())});let H=document.createElement("label");H.htmlFor=D,H.className="voc-form-table-radio-label",d&&H.classList.add("form-check-label"),H.textContent=F.label,_.appendChild(S),_.appendChild(H),g.appendChild(_)}),T=g}else{let g=document.createElement("input");q==="number"?g.type="number":q==="email"?g.type="email":q==="tel"?g.type="tel":q==="url"?g.type="url":q==="date"?g.type="date":g.type="text",g.value=i[l.key]!==void 0&&i[l.key]!==null?String(i[l.key]):"",l.placeholder&&(g.placeholder=l.placeholder),l.required&&(g.required=!0),g.className="voc-form-table-cell",d&&g.classList.add("form-control"),g.addEventListener("input",B=>{r[n][l.key]=B.target.value,L()}),T=g}M.appendChild(T),o.appendChild(M)});let $=document.createElement("td"),R=document.createElement("button");R.type="button",R.className="voc-form-table-remove",d&&R.classList.add("btn","btn-danger"),R.textContent="Remove",R.addEventListener("click",()=>{r.splice(n,1),t(),L()}),$.appendChild(R),o.appendChild($),N.appendChild(o)})}return C.addEventListener("click",()=>{if(s.validation?.max_rows!==void 0&&r.length>=s.validation.max_rows)return;let a={};s.fields.forEach(i=>a[i.key]=""),r.push(a),t(),L()}),Array.isArray(f)&&f.length?r=f.map(a=>({...a})):s.default&&Array.isArray(s.default)&&(r=s.default.map(a=>({...a}))),t(),L(),v.validate=function(){let a=[];return r.forEach((i,n)=>{let o=k(i);o.length&&a.push(`Row ${n+1}: ${o.join("; ")}`)}),s.validation?.min_rows!==void 0&&r.length<s.validation.min_rows&&a.push(`At least ${s.validation.min_rows} rows required`),s.validation?.max_rows!==void 0&&r.length>s.validation.max_rows&&a.push(`No more than ${s.validation.max_rows} rows allowed`),s.validation?.unique_fields&&s.validation.unique_fields.length&&s.validation.unique_fields.forEach(i=>{let n=new Set;r.forEach((o,x)=>{let A=o[i];A!=null&&(n.has(A)&&a.push(`Field ${i} must be unique (duplicate at row ${x+1})`),n.add(A))})}),a},v}function I(s,b=!0){let f=new FormData(s);if(b)return ae(f);let v={};return f.forEach((d,m)=>{let E=(d instanceof File,d);Object.prototype.hasOwnProperty.call(v,m)?Array.isArray(v[m])?v[m].push(E):v[m]=[v[m],E]:v[m]=E}),v}function ae(s){let b=new FormData;for(let[f,v]of s.entries()){if(v instanceof File){b.append(f,v);continue}let d;try{d=JSON.parse(v)}catch{d=null}Array.isArray(d)&&d.every(m=>typeof m=="object"&&m!==null)?d.forEach((m,E)=>{for(let[c,h]of Object.entries(m))b.append(`${f}[${E}][${c}]`,h)}):b.append(f,v)}return b}async function G(s,b,f){let v=b.url,d={"Content-Type":"application/json"};b?.api_token&&(d.Authorization=`Bearer ${b.api_token}`);let m=I(s);if(b?.additional_data)for(let[h,y]of Object.entries(b.additional_data))m.append(h,y);let E,c=m instanceof FormData;E={method:"POST",headers:{Authorization:`Bearer ${b.api_token}`,...c?{}:{"Content-Type":"application/json"},Accept:"application/json"},body:c?m:JSON.stringify(Object.fromEntries(m.entries()))};try{fetch(v,E).then(async h=>{let y=h.headers.get("content-type");if(!h.ok)try{let N=(await h.json())?.message;throw new Error(N??"Their was an error submitting the form. Check your form and try again.")}catch(u){throw new Error(u.message)}return y&&y.includes("application/json")?h.json():h.text()}).then(h=>{f&&f(h,null)}).catch(h=>{f&&f(null,h)})}catch(h){f&&f(null,h)}}function J(s,b,f){let v=[];if(typeof s=="string"?v=Array.from(document.querySelectorAll(s)):v=[s],v.length===0)return null;function d(){let c=f?.style==="bootstrap",h=document.createElement("form");h.classList.add("voc-form"),c&&h.classList.add("needs-validation"),h.id=b.id;let y=document.createElement("h2");y.className="voc-form-title",c&&y.classList.add("mb-3"),y.textContent=b.form_name,h.appendChild(y);function u(e,p){p&&(p.min_length!==void 0&&(e.minLength=p.min_length),p.max_length!==void 0&&(e.maxLength=p.max_length),p.regex&&(e instanceof HTMLInputElement?e.pattern=p.regex:e.dataset.pattern=p.regex))}function N(e){if(!e.key&&!e.label)return null;let p=b.default_values||{},w=e.name||e.key,r=w?p[w]:void 0,L=document.createElement("div");if(L.className="voc-form-group",c&&L.classList.add("mb-3"),e.label){let t=document.createElement("label");if(t.classList.add("voc-form-label"),c&&t.classList.add("form-label"),e.key?e.type==="radio"?t.htmlFor="":t.htmlFor=`voc-form-${e.key}`:t.htmlFor="",t.textContent=e.label,e.required){let a=document.createElement("span");a.className="voc-form-required",a.textContent="*",t.appendChild(a)}L.appendChild(t)}let k=null;switch(e.type){case"checkbox":{let t=document.createElement("div");if(t.className="voc-form-checkbox-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options)e.options.forEach((a,i)=>{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),c&&n.classList.add("form-check"),n.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(n.style.width=e.width);let o=document.createElement("input");o.type="checkbox",o.classList.add("voc-form-checkbox"),c&&o.classList.add("form-check-input"),o.name=e.name||e.key||"",o.value=a.value,e.key&&(o.id=`voc-form-${e.key}-${i}`),e.required&&(o.required=!0),Array.isArray(r)?r.includes(a.value)&&(o.checked=!0):r!==void 0?r===a.value&&(o.checked=!0):e.default===a.value&&(o.checked=!0),n.appendChild(o),n.appendChild(document.createTextNode(a.label)),t.appendChild(n)});else{let a=document.createElement("label");a.classList.add("voc-form-checkbox-label"),c&&a.classList.add("form-check"),e.width&&(a.style.width=e.width);let i=document.createElement("input");i.type="checkbox",i.classList.add("voc-form-checkbox"),(e.key||e.name)&&(i.name=e.name||e.key,e.key&&(i.id=`voc-form-${e.key}`)),c&&i.classList.add("form-check-input"),e.required&&(i.required=!0),r!==void 0?i.checked=!!r:e.default&&(i.checked=!!e.default),a.appendChild(i),a.appendChild(document.createTextNode(e.label||"")),t.appendChild(a)}k=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),c&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),r!=null?t.value=String(r):e.default!==void 0&&e.default!==null&&(t.value=String(e.default));let a=e.default||r||"";if(a){let i=(a.match(/\n/g)||[]).length,n=80,o=a.split(`
2
+ `).reduce(($,R)=>Math.max($,R.length),0),x=Math.ceil(o/n)||1,A=Math.max(2,i+x);t.rows=A,t.style.resize="vertical"}e.width&&(t.style.width=e.width),e.required&&(t.required=!0),u(t,e.validation),k=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),c&&t.classList.add("form-select"),c&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder){let a=document.createElement("option");a.value="",a.textContent=e.placeholder,a.disabled=!0,a.selected=!0,t.appendChild(a)}e.required&&(t.required=!0),e.options&&e.options.forEach(a=>{let i=document.createElement("option");i.value=a.value,i.textContent=a.label,Array.isArray(r)?r.includes(a.value)&&(i.selected=!0):r!==void 0?r===a.value&&(i.selected=!0):e.default===a.value&&(i.selected=!0),t.appendChild(i)}),e.width&&(t.style.width=e.width),k=t;break}case"radio":{let t=document.createElement("div");t.className="voc-form-radio-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options&&e.options.forEach((a,i)=>{let n=document.createElement("label");n.classList.add("voc-form-radio-label"),c&&n.classList.add("form-check"),n.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(n.style.width=e.width);let o=document.createElement("input");o.type="radio",o.classList.add("voc-form-radio"),c&&o.classList.add("form-check-input"),o.name=e.name||e.key||"",o.value=a.value,e.key&&(o.id=`voc-form-${e.key}-${i}`),Array.isArray(r)?r.includes(a.value)&&(o.checked=!0):r!==void 0?r===a.value&&(o.checked=!0):e.default===a.value&&(o.checked=!0),n.appendChild(o),n.appendChild(document.createTextNode(a.label)),t.appendChild(n)}),k=t;break}case"file":{let t=document.createElement("div");t.className="voc-form-file-wrapper",e.width&&(t.style.width=e.width);let a=document.createElement("input");a.type="file",a.classList.add("voc-form-file"),c&&a.classList.add("form-control"),(e.key||e.name)&&(a.name=e.name||e.key,e.key&&(a.id=`voc-form-${e.key}`)),e.width&&(a.style.width=e.width),e.required&&(a.required=!0),e.accept&&(a.accept=e.accept),e.multiple&&(a.multiple=!0),t.appendChild(a),k=t;break}case"avatar":{k=z(e,f||{},r);break}case"table":{k=V(e,f||{},r);break}case"audio_select":{k=P(e,f||{},r);break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),c&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),r!=null?t.value=String(r):e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),u(t,e.validation),k=t;break}}if(k&&L.appendChild(k),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,L.appendChild(t)}return L}b.fields.forEach(e=>{if(e.type==="group"&&"items"in e){let p=e;if(c){let w=document.createElement("div");w.className="card voc-form-group-card mb-3";let r=p.label||p.header?.title,L=p.description||p.header?.description;if(r||L){let t=document.createElement("div");if(t.className="card-header voc-form-group-head",r){let a=document.createElement("h5");a.className="mb-0 voc-form-group-label",a.textContent=r,t.appendChild(a)}if(L){let a=document.createElement("p");a.className="mb-0 voc-form-group-description",a.textContent=L,t.appendChild(a)}w.appendChild(t)}let k=document.createElement("div");k.className="card-body voc-form-group-body",p.items.forEach(t=>{if(t.type==="row"&&"items"in t){let a=t,i=document.createElement("div");if(i.className="voc-form-group voc-form-row mb-3",a.label){let o=document.createElement("h6");o.className="voc-form-row-label",o.textContent=a.label,i.appendChild(o)}let n=document.createElement("div");n.className="voc-form-row-items "+(a.direction||"horizontal"),a.items.forEach(o=>{let x=N(o);x&&n.appendChild(x)}),i.appendChild(n),k.appendChild(i)}else{let a=N(t);a&&k.appendChild(a)}}),w.appendChild(k),h.appendChild(w)}else{let w=document.createElement("div");w.className="voc-form-group voc-form-group-wrapper",c&&w.classList.add("mb-3");let r=p.label||p.header?.title,L=p.description||p.header?.description;if(r||L){let k=document.createElement("div");if(k.className="voc-form-group-head",r){let t=document.createElement("h3");t.className="voc-form-group-label",t.textContent=r,k.appendChild(t)}if(L){let t=document.createElement("p");t.className="voc-form-group-description",t.textContent=L,k.appendChild(t)}w.appendChild(k)}p.items.forEach(k=>{if(k.type==="row"&&"items"in k){let t=k,a=document.createElement("div");if(a.className="voc-form-group voc-form-row",c&&a.classList.add("mb-3"),t.label){let n=document.createElement("h4");n.className="voc-form-row-label",n.textContent=t.label,a.appendChild(n)}let i=document.createElement("div");i.className="voc-form-row-items "+(t.direction||"horizontal"),t.items.forEach(n=>{let o=N(n);o&&i.appendChild(o)}),a.appendChild(i),w.appendChild(a)}else{let t=N(k);t&&w.appendChild(t)}}),h.appendChild(w)}return}if(e.type==="row"&&"items"in e){let p=document.createElement("div");if(p.className="voc-form-group voc-form-row",c&&p.classList.add("mb-3"),e.label){let r=document.createElement("h3");r.className="voc-form-row-label",r.textContent=e.label,p.appendChild(r)}if(e.description){let r=document.createElement("p");r.className="voc-form-row-description",r.textContent=e.description,p.appendChild(r)}let w=document.createElement("div");w.className="voc-form-row-items "+(e.direction||"horizontal"),p.appendChild(w),e.items.forEach(r=>{let L=N(r);L&&w.appendChild(L)}),h.appendChild(p)}else{let p=N(e);p&&h.appendChild(p)}});let C=document.createElement("button");return C.type="submit",C.className="voc-form-submit",C.textContent="Submit",c&&C.classList.add("btn","btn-primary"),h.appendChild(C),h}let m=d();v[0].appendChild(m);let E=b?.submit_config??f?.submit_config??null;return E&&m.addEventListener("submit",c=>{if(c.preventDefault(),!m.checkValidity()){m.reportValidity(),f?.onSubmitResult&&f.onSubmitResult(null,new Error("Form validation failed"));return}let h=Array.from(m.querySelectorAll(" .voc-form-table-wrapper, .voc-form-table-wrapper")),y=[];if(h.forEach(u=>{if(u&&typeof u.validate=="function"){let N=u.validate();N&&N.length&&y.push(...N)}}),y.length){f?.onSubmitResult&&f.onSubmitResult(null,new Error(y.join("; ")));let u=h[0];u&&u.scrollIntoView({behavior:"smooth",block:"center"});return}G(m,E,(u,N)=>{f?.onSubmitResult&&f.onSubmitResult(u,N)})}),{element:m,validate:()=>{let c=[];return m.querySelectorAll(":invalid").forEach(y=>{let u=y.validationMessage;c.push({element:y,message:u})}),c},getValues:()=>I(m),onSubmit:c=>{m.addEventListener("submit",h=>{h.preventDefault();let y=I(m);c(y,h)})}}}var W={renderForm:J};var ne=W,re={FormsLib:W};0&&(module.exports={FormsLib});
package/dist/main.mjs CHANGED
@@ -1,2 +1,2 @@
1
- function B(r,x,v){let k=x?.style==="bootstrap",b=document.createElement("div");b.className="voc-form-avatar-wrapper",k&&b.classList.add("d-flex","align-items-center","gap-3");let p=document.createElement("img");p.className="voc-form-avatar-preview",p.alt=r.label||"avatar",k&&(p.classList.add("img-thumbnail","rounded-circle"),p.style.width=p.style.height="64px",p.style.objectFit="cover");let w=v!=null?String(v):r.default!==void 0&&r.default!==null?String(r.default):"";w?p.src=w:(p.src="",p.style.display="none");let o=document.createElement("input");o.type="file",o.accept=r.accept||"image/*",o.className="voc-form-avatar-input",k&&o.classList.add("form-control"),(r.key||r.name)&&(o.name=r.name||r.key,r.key&&(o.id=`voc-form-${r.key}`));let h=document.createElement("input");if(h.type="hidden",(r.key||r.name)&&(h.name=(r.name||r.key)+"_url"),h.value=w||"",o.addEventListener("change",y=>{let l=y.target;if(l.files&&l.files.length>0){let C=l.files[0],A=new FileReader;A.onload=()=>{p.src=String(A.result),p.style.display="",h.value=""},A.readAsDataURL(C)}}),k){let y=document.createElement("div");y.style.flex="1",y.appendChild(o),y.appendChild(h),b.appendChild(p),b.appendChild(y)}else b.appendChild(p),b.appendChild(o),b.appendChild(h);return b}function D(r,x,v){let k=x?.style==="bootstrap",b=document.createElement("div");if(b.className="voc-form-audio-select",!r.options||r.options.length===0)return b;let p=r.name||r.key||`audio_select_${Math.random().toString(36).substr(2,6)}`,w;return Array.isArray(v)?w=v[0]:v!==void 0?w=v:r.default!==void 0&&(w=r.default),r.options.forEach((o,h)=>{let y=document.createElement("div");y.className="voc-form-audio-item",k&&y.classList.add("form-check");let l=document.createElement("input");l.type="radio",l.name=p,l.value=o.value,l.className="voc-form-audio-radio",r.key&&(l.id=`voc-form-${r.key}-${h}`),r.required&&h===0&&(l.required=!0),l.style.position="absolute",l.style.width="1px",l.style.height="1px",l.style.margin="0",l.style.padding="0",l.style.clip="rect(0 0 0 0)",l.style.clipPath="inset(50%)",l.style.overflow="hidden",l.style.border="0",w!==void 0&&(Array.isArray(w)?l.checked=w.includes(o.value):l.checked=String(w)===String(o.value));let C=()=>{l.checked?y.classList.add("selected"):y.classList.remove("selected")};l.addEventListener("change",()=>{b.querySelectorAll(".voc-form-audio-item").forEach(n=>n.classList.remove("selected")),C()}),l.checked&&y.classList.add("selected");let A=document.createElement("label");A.className="voc-form-audio-label",k&&A.classList.add("form-check-label"),A.htmlFor=l.id||"",A.textContent=o.label||o.value,A.tabIndex=0;let e=document.createElement("div");e.className="voc-form-audio-control";let d=null;if(o.audio_link){let t=document.createElement("button");t.type="button",t.className="voc-form-audio-play",k&&t.classList.add("btn","btn-sm","btn-outline-secondary"),t.setAttribute("aria-label",`Play ${o.label||o.value}`);let n="http://www.w3.org/2000/svg",a=document.createElementNS(n,"svg");a.setAttribute("viewBox","0 0 24 24"),a.setAttribute("width","16"),a.setAttribute("height","16"),a.setAttribute("aria-hidden","true");let s=document.createElementNS(n,"path");s.setAttribute("d","M8 5v14l11-7z"),s.setAttribute("fill","currentColor"),a.appendChild(s),t.appendChild(a),d=document.createElement("audio"),d.src=o.audio_link,d.preload="none",d.style.display="none";let m=f=>{for(;f.firstChild;)f.removeChild(f.firstChild)},u=()=>{let f="http://www.w3.org/2000/svg",F=document.createElementNS(f,"svg");F.setAttribute("viewBox","0 0 24 24"),F.setAttribute("width","16"),F.setAttribute("height","16"),F.setAttribute("aria-hidden","true");let g=document.createElementNS(f,"path");return g.setAttribute("d","M8 5v14l11-7z"),g.setAttribute("fill","currentColor"),F.appendChild(g),F},R=()=>{let f="http://www.w3.org/2000/svg",F=document.createElementNS(f,"svg");F.setAttribute("viewBox","0 0 24 24"),F.setAttribute("width","16"),F.setAttribute("height","16"),F.setAttribute("aria-hidden","true");let g=document.createElementNS(f,"rect");return g.setAttribute("x","6"),g.setAttribute("y","6"),g.setAttribute("width","12"),g.setAttribute("height","12"),g.setAttribute("fill","currentColor"),F.appendChild(g),F};m(t),t.appendChild(u());let S=f=>{m(t),f?(t.appendChild(R()),t.setAttribute("aria-label",`Stop ${o.label||o.value}`)):(t.appendChild(u()),t.setAttribute("aria-label",`Play ${o.label||o.value}`))};t.addEventListener("click",()=>{try{if(!d)return;let f=b.querySelectorAll("audio");if(!d.paused&&!d.ended){d.pause(),d.currentTime=0;return}f.forEach(g=>{try{g.pause(),g.currentTime=0}catch{}}),b.querySelectorAll(".voc-form-audio-item").forEach(g=>g.classList.remove("is-playing")),d.play()}catch(f){console.warn("audio play failed",f)}}),d.addEventListener("playing",()=>{y.classList.add("is-playing"),S(!0)});let N=()=>{y.classList.remove("is-playing"),S(!1)};d.addEventListener("pause",N),d.addEventListener("ended",N),e.appendChild(t),e.appendChild(d)}let c=document.createElement("div");c.className="voc-form-item-detail",c.style.display="flex",c.style.flexDirection="column",c.style.gap="4px";let i=document.createElement("div");i.className="voc-form-item-top",i.style.display="flex",i.style.alignItems="center",i.style.gap="8px",i.appendChild(l),i.appendChild(A);let L=document.createElement("div");L.className="voc-form-item-description";let E=o.description||o.hint_text||"";E&&(L.textContent=E),c.appendChild(i),c.appendChild(L),e.appendChild(c),y.appendChild(e),i.addEventListener("click",t=>{let n=t.target;n&&n.closest(".voc-form-audio-play")||(l.checked=!0,l.dispatchEvent(new Event("change",{bubbles:!0})))}),y.addEventListener("click",t=>{let n=t.target;n&&n.closest(".voc-form-audio-play")||(l.checked=!0,l.dispatchEvent(new Event("change",{bubbles:!0})))}),b.appendChild(y)}),b}function O(r,x,v){let k=document.createElement("div");k.className="voc-form-table-wrapper";let b=x?.style==="bootstrap",p=document.createElement("input");p.type="hidden",p.name=r.name||r.key,r.key&&(p.id=`voc-form-${r.key}`),k.appendChild(p);let w=document.createElement("table");w.className="voc-form-table",b&&w.classList.add("table","table-bordered");let o=document.createElement("thead"),h=document.createElement("tr");(r.fields||[]).forEach(t=>{let n=document.createElement("th");n.textContent=t.label||t.key,h.appendChild(n)});let y=document.createElement("th");y.textContent="Actions",h.appendChild(y),o.appendChild(h),w.appendChild(o);let l=document.createElement("tbody");w.appendChild(l),k.appendChild(w);let C=document.createElement("button");C.type="button",C.className="voc-form-table-add",b&&C.classList.add("btn","btn-secondary"),C.textContent="+Add";let A=document.createElement("tfoot"),e=document.createElement("tr"),d=document.createElement("td");d.colSpan=(r.fields?r.fields.length:0)+1,d.appendChild(C),e.appendChild(d),A.appendChild(e),w.appendChild(A),k.appendChild(w);let c=[];function i(){p.value=JSON.stringify(c)}function L(t){let n=[];return r.fields.forEach(a=>{let s=t[a.key];if(a.type==="checkbox"){let m=Array.isArray(s)?s:s?[s]:[];a.required&&m.length===0&&n.push(`${a.label||a.key} is required`);return}a.required&&(s==null||String(s).trim()==="")&&n.push(`${a.label||a.key} is required`),a.type==="email"&&s&&(/^\S+@\S+\.\S+$/.test(String(s))||n.push(`${a.label||a.key} must be a valid email`)),a.type==="number"&&s&&isNaN(Number(s))&&n.push(`${a.label||a.key} must be a number`),a.validation&&a.validation.regex&&s&&(new RegExp(a.validation.regex).test(String(s))||n.push(`${a.label||a.key} invalid format`))}),n}function E(){l.innerHTML="",c.forEach((t,n)=>{let a=document.createElement("tr");r.fields.forEach(u=>{let R=document.createElement("td"),S,N=(u.type||"text").toLowerCase();if(N==="select"){let f=document.createElement("select");if(f.className="voc-form-table-cell",b&&f.classList.add("form-control"),u.placeholder){let g=document.createElement("option");g.value="",g.textContent=u.placeholder,g.disabled=!0,(t[u.key]===void 0||t[u.key]===null||t[u.key]==="")&&(g.selected=!0),f.appendChild(g)}(u.options||[]).forEach(g=>{let _=document.createElement("option");_.value=g.value,_.textContent=g.label,f.appendChild(_)}),f.value=t[u.key]!==void 0&&t[u.key]!==null?String(t[u.key]):"",u.required&&(f.required=!0),f.addEventListener("change",g=>{c[n][u.key]=g.target.value,i()}),S=f}else if(N==="checkbox"){let f=document.createElement("div");f.className="voc-form-table-checkboxes",b&&f.classList.add("form-check");let F=u.options||[];Array.isArray(c[n][u.key])||(c[n][u.key]=c[n][u.key]?[c[n][u.key]]:[]),F.forEach(g=>{let _=`voc-form-${r.key}-${u.key}-${n}-${g.value}`,q=document.createElement("div");q.className="voc-form-table-checkbox-item",b&&q.classList.add("form-check");let T=document.createElement("input");T.type="checkbox",T.className="voc-form-table-cell",b&&T.classList.add("form-check-input"),T.id=_,T.value=g.value,T.checked=Array.isArray(c[n][u.key])&&c[n][u.key].includes(g.value),T.addEventListener("change",j=>{let z=j.target.checked;Array.isArray(c[n][u.key])||(c[n][u.key]=[]),z?c[n][u.key].includes(g.value)||c[n][u.key].push(g.value):c[n][u.key]=c[n][u.key].filter(P=>P!==g.value),i()});let M=document.createElement("label");M.htmlFor=_,M.className="voc-form-table-checkbox-label",b&&M.classList.add("form-check-label"),M.textContent=g.label,q.appendChild(T),q.appendChild(M),f.appendChild(q)}),S=f}else{let f=document.createElement("input");N==="number"?f.type="number":N==="email"?f.type="email":N==="tel"?f.type="tel":N==="url"?f.type="url":N==="date"?f.type="date":f.type="text",f.value=t[u.key]!==void 0&&t[u.key]!==null?String(t[u.key]):"",u.placeholder&&(f.placeholder=u.placeholder),u.required&&(f.required=!0),f.className="voc-form-table-cell",b&&f.classList.add("form-control"),f.addEventListener("input",F=>{c[n][u.key]=F.target.value,i()}),S=f}R.appendChild(S),a.appendChild(R)});let s=document.createElement("td"),m=document.createElement("button");m.type="button",m.className="voc-form-table-remove",b&&m.classList.add("btn","btn-danger"),m.textContent="Remove",m.addEventListener("click",()=>{c.splice(n,1),E(),i()}),s.appendChild(m),a.appendChild(s),l.appendChild(a)})}return C.addEventListener("click",()=>{if(r.validation?.max_rows!==void 0&&c.length>=r.validation.max_rows)return;let t={};r.fields.forEach(n=>t[n.key]=""),c.push(t),E(),i()}),Array.isArray(v)&&v.length?c=v.map(t=>({...t})):r.default&&Array.isArray(r.default)&&(c=r.default.map(t=>({...t}))),E(),i(),k.validate=function(){let t=[];return c.forEach((n,a)=>{let s=L(n);s.length&&t.push(`Row ${a+1}: ${s.join("; ")}`)}),r.validation?.min_rows!==void 0&&c.length<r.validation.min_rows&&t.push(`At least ${r.validation.min_rows} rows required`),r.validation?.max_rows!==void 0&&c.length>r.validation.max_rows&&t.push(`No more than ${r.validation.max_rows} rows allowed`),r.validation?.unique_fields&&r.validation.unique_fields.length&&r.validation.unique_fields.forEach(n=>{let a=new Set;c.forEach((s,m)=>{let u=s[n];u!=null&&(a.has(u)&&t.push(`Field ${n} must be unique (duplicate at row ${m+1})`),a.add(u))})}),t},k}function $(r,x=!0){let v=new FormData(r);if(x)return v;let k={};return v.forEach((b,p)=>{let w=(b instanceof File,b);Object.prototype.hasOwnProperty.call(k,p)?Array.isArray(k[p])?k[p].push(w):k[p]=[k[p],w]:k[p]=w}),k}async function I(r,x,v){let k=x.url,b={"Content-Type":"application/json"};x?.api_token&&(b.Authorization=`Bearer ${x.api_token}`);let p=$(r);if(x?.additional_data)for(let[h,y]of Object.entries(x.additional_data))p.append(h,y);let w,o=p instanceof FormData;w={method:"POST",headers:{Authorization:`Bearer ${x.api_token}`,...o?{}:{"Content-Type":"application/json"},Accept:"application/json"},body:o?p:JSON.stringify(Object.fromEntries(p.entries()))};try{fetch(k,w).then(async h=>{let y=h.headers.get("content-type");if(!h.ok)try{let C=(await h.json())?.message;throw new Error(C??"Their was an error submitting the form. Check your form and try again.")}catch(l){throw new Error(l.message)}return y&&y.includes("application/json")?h.json():h.text()}).then(h=>{v&&v(h,null)}).catch(h=>{v&&v(null,h)})}catch(h){v&&v(null,h)}}function W(r,x,v){let k=[];if(typeof r=="string"?k=Array.from(document.querySelectorAll(r)):k=[r],k.length===0)return null;function b(){let o=v?.style==="bootstrap",h=document.createElement("form");h.classList.add("voc-form"),o&&h.classList.add("needs-validation"),h.id=x.id;let y=document.createElement("h2");y.className="voc-form-title",o&&y.classList.add("mb-3"),y.textContent=x.form_name,h.appendChild(y);function l(e,d){d&&(d.min_length!==void 0&&(e.minLength=d.min_length),d.max_length!==void 0&&(e.maxLength=d.max_length),d.regex&&(e instanceof HTMLInputElement?e.pattern=d.regex:e.dataset.pattern=d.regex))}function C(e){if(!e.key&&!e.label)return null;let d=x.default_values||{},c=e.name||e.key,i=c?d[c]:void 0,L=document.createElement("div");if(L.className="voc-form-group",o&&L.classList.add("mb-3"),e.label){let t=document.createElement("label");if(t.classList.add("voc-form-label"),o&&t.classList.add("form-label"),e.key?e.type==="radio"?t.htmlFor="":t.htmlFor=`voc-form-${e.key}`:t.htmlFor="",t.textContent=e.label,e.required){let n=document.createElement("span");n.className="voc-form-required",n.textContent="*",t.appendChild(n)}L.appendChild(t)}let E=null;switch(e.type){case"checkbox":{let t=document.createElement("div");if(t.className="voc-form-checkbox-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options)e.options.forEach((n,a)=>{let s=document.createElement("label");s.classList.add("voc-form-checkbox-label"),o&&s.classList.add("form-check"),s.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(s.style.width=e.width);let m=document.createElement("input");m.type="checkbox",m.classList.add("voc-form-checkbox"),o&&m.classList.add("form-check-input"),m.name=e.name||e.key||"",m.value=n.value,e.key&&(m.id=`voc-form-${e.key}-${a}`),e.required&&(m.required=!0),Array.isArray(i)?i.includes(n.value)&&(m.checked=!0):i!==void 0?i===n.value&&(m.checked=!0):e.default===n.value&&(m.checked=!0),s.appendChild(m),s.appendChild(document.createTextNode(n.label)),t.appendChild(s)});else{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),o&&n.classList.add("form-check"),e.width&&(n.style.width=e.width);let a=document.createElement("input");a.type="checkbox",a.classList.add("voc-form-checkbox"),(e.key||e.name)&&(a.name=e.name||e.key,e.key&&(a.id=`voc-form-${e.key}`)),o&&a.classList.add("form-check-input"),e.required&&(a.required=!0),i!==void 0?a.checked=!!i:e.default&&(a.checked=!!e.default),n.appendChild(a),n.appendChild(document.createTextNode(e.label||"")),t.appendChild(n)}E=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),o&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),i!=null?t.value=String(i):e.default!==void 0&&e.default!==null&&(t.value=String(e.default));let n=e.default||i||"";if(n){let a=(n.match(/\n/g)||[]).length,s=80,m=n.split(`
2
- `).reduce((S,N)=>Math.max(S,N.length),0),u=Math.ceil(m/s)||1,R=Math.max(2,a+u);t.rows=R,t.style.resize="vertical"}e.width&&(t.style.width=e.width),e.required&&(t.required=!0),l(t,e.validation),E=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),o&&t.classList.add("form-select"),o&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder){let n=document.createElement("option");n.value="",n.textContent=e.placeholder,n.disabled=!0,n.selected=!0,t.appendChild(n)}e.required&&(t.required=!0),e.options&&e.options.forEach(n=>{let a=document.createElement("option");a.value=n.value,a.textContent=n.label,Array.isArray(i)?i.includes(n.value)&&(a.selected=!0):i!==void 0?i===n.value&&(a.selected=!0):e.default===n.value&&(a.selected=!0),t.appendChild(a)}),e.width&&(t.style.width=e.width),E=t;break}case"radio":{let t=document.createElement("div");t.className="voc-form-radio-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options&&e.options.forEach((n,a)=>{let s=document.createElement("label");s.classList.add("voc-form-radio-label"),o&&s.classList.add("form-check"),s.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(s.style.width=e.width);let m=document.createElement("input");m.type="radio",m.classList.add("voc-form-radio"),o&&m.classList.add("form-check-input"),m.name=e.name||e.key||"",m.value=n.value,e.key&&(m.id=`voc-form-${e.key}-${a}`),Array.isArray(i)?i.includes(n.value)&&(m.checked=!0):i!==void 0?i===n.value&&(m.checked=!0):e.default===n.value&&(m.checked=!0),s.appendChild(m),s.appendChild(document.createTextNode(n.label)),t.appendChild(s)}),E=t;break}case"file":{let t=document.createElement("div");t.className="voc-form-file-wrapper",e.width&&(t.style.width=e.width);let n=document.createElement("input");n.type="file",n.classList.add("voc-form-file"),o&&n.classList.add("form-control"),(e.key||e.name)&&(n.name=e.name||e.key,e.key&&(n.id=`voc-form-${e.key}`)),e.width&&(n.style.width=e.width),e.required&&(n.required=!0),e.accept&&(n.accept=e.accept),e.multiple&&(n.multiple=!0),t.appendChild(n),E=t;break}case"avatar":{E=B(e,v||{},i);break}case"table":{E=O(e,v||{},i);break}case"audio_select":{E=D(e,v||{},i);break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),o&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),i!=null?t.value=String(i):e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),l(t,e.validation),E=t;break}}if(E&&L.appendChild(E),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,L.appendChild(t)}return L}x.fields.forEach(e=>{if(e.type==="group"&&"items"in e){let d=e;if(o){let c=document.createElement("div");c.className="card voc-form-group-card mb-3";let i=d.label||d.header?.title,L=d.description||d.header?.description;if(i||L){let t=document.createElement("div");if(t.className="card-header voc-form-group-head",i){let n=document.createElement("h5");n.className="mb-0 voc-form-group-label",n.textContent=i,t.appendChild(n)}if(L){let n=document.createElement("p");n.className="mb-0 voc-form-group-description",n.textContent=L,t.appendChild(n)}c.appendChild(t)}let E=document.createElement("div");E.className="card-body voc-form-group-body",d.items.forEach(t=>{if(t.type==="row"&&"items"in t){let n=t,a=document.createElement("div");if(a.className="voc-form-group voc-form-row mb-3",n.label){let m=document.createElement("h6");m.className="voc-form-row-label",m.textContent=n.label,a.appendChild(m)}let s=document.createElement("div");s.className="voc-form-row-items "+(n.direction||"horizontal"),n.items.forEach(m=>{let u=C(m);u&&s.appendChild(u)}),a.appendChild(s),E.appendChild(a)}else{let n=C(t);n&&E.appendChild(n)}}),c.appendChild(E),h.appendChild(c)}else{let c=document.createElement("div");c.className="voc-form-group voc-form-group-wrapper",o&&c.classList.add("mb-3");let i=d.label||d.header?.title,L=d.description||d.header?.description;if(i||L){let E=document.createElement("div");if(E.className="voc-form-group-head",i){let t=document.createElement("h3");t.className="voc-form-group-label",t.textContent=i,E.appendChild(t)}if(L){let t=document.createElement("p");t.className="voc-form-group-description",t.textContent=L,E.appendChild(t)}c.appendChild(E)}d.items.forEach(E=>{if(E.type==="row"&&"items"in E){let t=E,n=document.createElement("div");if(n.className="voc-form-group voc-form-row",o&&n.classList.add("mb-3"),t.label){let s=document.createElement("h4");s.className="voc-form-row-label",s.textContent=t.label,n.appendChild(s)}let a=document.createElement("div");a.className="voc-form-row-items "+(t.direction||"horizontal"),t.items.forEach(s=>{let m=C(s);m&&a.appendChild(m)}),n.appendChild(a),c.appendChild(n)}else{let t=C(E);t&&c.appendChild(t)}}),h.appendChild(c)}return}if(e.type==="row"&&"items"in e){let d=document.createElement("div");if(d.className="voc-form-group voc-form-row",o&&d.classList.add("mb-3"),e.label){let i=document.createElement("h3");i.className="voc-form-row-label",i.textContent=e.label,d.appendChild(i)}if(e.description){let i=document.createElement("p");i.className="voc-form-row-description",i.textContent=e.description,d.appendChild(i)}let c=document.createElement("div");c.className="voc-form-row-items "+(e.direction||"horizontal"),d.appendChild(c),e.items.forEach(i=>{let L=C(i);L&&c.appendChild(L)}),h.appendChild(d)}else{let d=C(e);d&&h.appendChild(d)}});let A=document.createElement("button");return A.type="submit",A.className="voc-form-submit",A.textContent="Submit",o&&A.classList.add("btn","btn-primary"),h.appendChild(A),h}let p=b();k[0].appendChild(p);let w=x?.submit_config??v?.submit_config??null;return w&&p.addEventListener("submit",o=>{if(o.preventDefault(),!p.checkValidity()){p.reportValidity(),v?.onSubmitResult&&v.onSubmitResult(null,new Error("Form validation failed"));return}let h=Array.from(p.querySelectorAll(" .voc-form-table-wrapper, .voc-form-table-wrapper")),y=[];if(h.forEach(l=>{if(l&&typeof l.validate=="function"){let C=l.validate();C&&C.length&&y.push(...C)}}),y.length){v?.onSubmitResult&&v.onSubmitResult(null,new Error(y.join("; ")));let l=h[0];l&&l.scrollIntoView({behavior:"smooth",block:"center"});return}I(p,w,(l,C)=>{v?.onSubmitResult&&v.onSubmitResult(l,C)})}),{element:p,validate:()=>{let o=[];return p.querySelectorAll(":invalid").forEach(y=>{let l=y.validationMessage;o.push({element:y,message:l})}),o},getValues:()=>$(p),onSubmit:o=>{p.addEventListener("submit",h=>{h.preventDefault();let y=$(p);o(y,h)})}}}var H={renderForm:W};var ie=H,se={FormsLib:H};export{ie as FormsLib,se as default};
1
+ function W(i,L,y){let v=L?.style==="bootstrap",h=document.createElement("div");h.className="voc-form-avatar-wrapper",v&&h.classList.add("d-flex","align-items-center","gap-3");let d=document.createElement("img");d.className="voc-form-avatar-preview",d.alt=i.label||"avatar",v&&(d.classList.add("img-thumbnail","rounded-circle"),d.style.width=d.style.height="64px",d.style.objectFit="cover");let g=y!=null?String(y):i.default!==void 0&&i.default!==null?String(i.default):"";g?d.src=g:(d.src="",d.style.display="none");let c=document.createElement("input");c.type="file",c.accept=i.accept||"image/*",c.className="voc-form-avatar-input",v&&c.classList.add("form-control"),(i.key||i.name)&&(c.name=i.name||i.key,i.key&&(c.id=`voc-form-${i.key}`));let p=document.createElement("input");if(p.type="hidden",(i.key||i.name)&&(p.name=(i.name||i.key)+"_url"),p.value=g||"",c.addEventListener("change",f=>{let m=f.target;if(m.files&&m.files.length>0){let N=m.files[0],C=new FileReader;C.onload=()=>{d.src=String(C.result),d.style.display="",p.value=""},C.readAsDataURL(N)}}),v){let f=document.createElement("div");f.style.flex="1",f.appendChild(c),f.appendChild(p),h.appendChild(d),h.appendChild(f)}else h.appendChild(d),h.appendChild(c),h.appendChild(p);return h}function z(i,L,y){let v=L?.style==="bootstrap",h=document.createElement("div");if(h.className="voc-form-audio-select",!i.options||i.options.length===0)return h;let d=i.name||i.key||`audio_select_${Math.random().toString(36).substr(2,6)}`,g;return Array.isArray(y)?g=y[0]:y!==void 0?g=y:i.default!==void 0&&(g=i.default),i.options.forEach((c,p)=>{let f=document.createElement("div");f.className="voc-form-audio-item",v&&f.classList.add("form-check");let m=document.createElement("input");m.type="radio",m.name=d,m.value=c.value,m.className="voc-form-audio-radio",i.key&&(m.id=`voc-form-${i.key}-${p}`),i.required&&p===0&&(m.required=!0),m.style.position="absolute",m.style.width="1px",m.style.height="1px",m.style.margin="0",m.style.padding="0",m.style.clip="rect(0 0 0 0)",m.style.clipPath="inset(50%)",m.style.overflow="hidden",m.style.border="0",g!==void 0&&(Array.isArray(g)?m.checked=g.includes(c.value):m.checked=String(g)===String(c.value));let N=()=>{m.checked?f.classList.add("selected"):f.classList.remove("selected")};m.addEventListener("change",()=>{h.querySelectorAll(".voc-form-audio-item").forEach(a=>a.classList.remove("selected")),N()}),m.checked&&f.classList.add("selected");let C=document.createElement("label");C.className="voc-form-audio-label",v&&C.classList.add("form-check-label"),C.htmlFor=m.id||"",C.textContent=c.label||c.value,C.tabIndex=0;let e=document.createElement("div");e.className="voc-form-audio-control";let u=null;if(c.audio_link){let t=document.createElement("button");t.type="button",t.className="voc-form-audio-play",v&&t.classList.add("btn","btn-sm","btn-outline-secondary"),t.setAttribute("aria-label",`Play ${c.label||c.value}`);let a="http://www.w3.org/2000/svg",s=document.createElementNS(a,"svg");s.setAttribute("viewBox","0 0 24 24"),s.setAttribute("width","16"),s.setAttribute("height","16"),s.setAttribute("aria-hidden","true");let n=document.createElementNS(a,"path");n.setAttribute("d","M8 5v14l11-7z"),n.setAttribute("fill","currentColor"),s.appendChild(n),t.appendChild(s),u=document.createElement("audio"),u.src=c.audio_link,u.preload="none",u.style.display="none";let o=l=>{for(;l.firstChild;)l.removeChild(l.firstChild)},x=()=>{let l="http://www.w3.org/2000/svg",M=document.createElementNS(l,"svg");M.setAttribute("viewBox","0 0 24 24"),M.setAttribute("width","16"),M.setAttribute("height","16"),M.setAttribute("aria-hidden","true");let T=document.createElementNS(l,"path");return T.setAttribute("d","M8 5v14l11-7z"),T.setAttribute("fill","currentColor"),M.appendChild(T),M},A=()=>{let l="http://www.w3.org/2000/svg",M=document.createElementNS(l,"svg");M.setAttribute("viewBox","0 0 24 24"),M.setAttribute("width","16"),M.setAttribute("height","16"),M.setAttribute("aria-hidden","true");let T=document.createElementNS(l,"rect");return T.setAttribute("x","6"),T.setAttribute("y","6"),T.setAttribute("width","12"),T.setAttribute("height","12"),T.setAttribute("fill","currentColor"),M.appendChild(T),M};o(t),t.appendChild(x());let $=l=>{o(t),l?(t.appendChild(A()),t.setAttribute("aria-label",`Stop ${c.label||c.value}`)):(t.appendChild(x()),t.setAttribute("aria-label",`Play ${c.label||c.value}`))};t.addEventListener("click",()=>{try{if(!u)return;let l=h.querySelectorAll("audio");if(!u.paused&&!u.ended){u.pause(),u.currentTime=0;return}l.forEach(T=>{try{T.pause(),T.currentTime=0}catch{}}),h.querySelectorAll(".voc-form-audio-item").forEach(T=>T.classList.remove("is-playing")),u.play()}catch(l){console.warn("audio play failed",l)}}),u.addEventListener("playing",()=>{f.classList.add("is-playing"),$(!0)});let R=()=>{f.classList.remove("is-playing"),$(!1)};u.addEventListener("pause",R),u.addEventListener("ended",R),e.appendChild(t),e.appendChild(u)}let k=document.createElement("div");k.className="voc-form-item-detail",k.style.display="flex",k.style.flexDirection="column",k.style.gap="4px";let r=document.createElement("div");r.className="voc-form-item-top",r.style.display="flex",r.style.alignItems="center",r.style.gap="8px",r.appendChild(m),r.appendChild(C);let w=document.createElement("div");w.className="voc-form-item-description";let E=c.description||c.hint_text||"";E&&(w.textContent=E),k.appendChild(r),k.appendChild(w),e.appendChild(k),f.appendChild(e),r.addEventListener("click",t=>{let a=t.target;a&&a.closest(".voc-form-audio-play")||(m.checked=!0,m.dispatchEvent(new Event("change",{bubbles:!0})))}),f.addEventListener("click",t=>{let a=t.target;a&&a.closest(".voc-form-audio-play")||(m.checked=!0,m.dispatchEvent(new Event("change",{bubbles:!0})))}),h.appendChild(f)}),h}function P(i,L,y){let v=document.createElement("div");v.className="voc-form-table-wrapper";let h=L?.style==="bootstrap",d=document.createElement("input");d.type="hidden",d.name=i.name||i.key,i.key&&(d.id=`voc-form-${i.key}`),v.appendChild(d);let g=document.createElement("table");g.className="voc-form-table",h&&g.classList.add("table","table-bordered");let c=document.createElement("thead"),p=document.createElement("tr"),f=document.createElement("th");f.className="voc-form-table-drag-col",f.textContent="",p.appendChild(f),(i.fields||[]).forEach(a=>{let s=document.createElement("th");s.textContent=a.label||a.key,p.appendChild(s)});let m=document.createElement("th");m.textContent="Actions",p.appendChild(m),c.appendChild(p),g.appendChild(c);let N=document.createElement("tbody");g.appendChild(N),v.appendChild(g);let C=document.createElement("button");C.type="button",C.className="voc-form-table-add",h&&C.classList.add("btn","btn-secondary"),C.textContent="+Add";let e=document.createElement("tfoot"),u=document.createElement("tr"),k=document.createElement("td");k.colSpan=(i.fields?i.fields.length:0)+1,k.appendChild(C),u.appendChild(k),e.appendChild(u),g.appendChild(e),v.appendChild(g);let r=[];function w(){d.value=JSON.stringify(r)}function E(a){let s=[];return i.fields.forEach(n=>{let o=a[n.key];if(n.type==="checkbox"){let x=Array.isArray(o)?o:o?[o]:[];n.required&&x.length===0&&s.push(`${n.label||n.key} is required`);return}n.required&&(o==null||String(o).trim()==="")&&s.push(`${n.label||n.key} is required`),n.type==="email"&&o&&(/^\S+@\S+\.\S+$/.test(String(o))||s.push(`${n.label||n.key} must be a valid email`)),n.type==="number"&&o&&isNaN(Number(o))&&s.push(`${n.label||n.key} must be a number`),n.validation&&n.validation.regex&&o&&(new RegExp(n.validation.regex).test(String(o))||s.push(`${n.label||n.key} invalid format`))}),s}function t(){N.innerHTML="";let a=(s,n)=>{n.draggable=!0,n.addEventListener("mousedown",()=>{n.style.cursor="grabbing"}),n.addEventListener("mouseup",()=>{n.style.cursor="grab"}),n.addEventListener("dragstart",o=>{let x=Number(s.dataset.rowIndex);o.dataTransfer.setData("text/plain",String(x)),s.classList.add("dragging");try{o.dataTransfer?.setDragImage(n,10,10)}catch{}}),n.addEventListener("dragend",()=>{s.classList.remove("dragging"),n.style.cursor="grab"}),s.addEventListener("dragover",o=>{o.preventDefault(),o.currentTarget.classList.add("drag-over")}),s.addEventListener("dragleave",o=>{o.currentTarget.classList.remove("drag-over")}),s.addEventListener("drop",o=>{o.preventDefault();let x=o.dataTransfer.getData("text/plain"),A=Number(o.currentTarget.dataset.rowIndex),$=Number(x);if(!isNaN($)&&!isNaN(A)&&$!==A){let[R]=r.splice($,1);r.splice(A,0,R),t(),w()}})};r.forEach((s,n)=>{let o=document.createElement("tr");o.dataset.rowIndex=String(n),o.classList.add("voc-form-table-row");let x=document.createElement("td");x.className="voc-form-table-drag-handle";let A=document.createElement("span");A.className="voc-form-drag-handle-icon",A.textContent="\u2630",A.style.cursor="grab",x.appendChild(A),o.appendChild(x),a(o,A),i.fields.forEach(l=>{let M=document.createElement("td"),T,q=(l.type||"text").toLowerCase();if(q==="select"){let b=document.createElement("select");if(b.className="voc-form-table-cell",h&&b.classList.add("form-control"),l.placeholder){let F=document.createElement("option");F.value="",F.textContent=l.placeholder,F.disabled=!0,(s[l.key]===void 0||s[l.key]===null||s[l.key]==="")&&(F.selected=!0),b.appendChild(F)}(l.options||[]).forEach(F=>{let D=document.createElement("option");D.value=F.value,D.textContent=F.label,b.appendChild(D)}),b.value=s[l.key]!==void 0&&s[l.key]!==null?String(s[l.key]):"",l.required&&(b.required=!0),b.addEventListener("change",F=>{r[n][l.key]=F.target.value,w()}),T=b}else if(q==="checkbox"){let b=document.createElement("div");b.className="voc-form-table-checkboxes",h&&b.classList.add("form-check");let B=l.options||[];Array.isArray(r[n][l.key])||(r[n][l.key]=r[n][l.key]?[r[n][l.key]]:[]),B.forEach(F=>{let D=`voc-form-${i.key}-${l.key}-${n}-${F.value}`,_=document.createElement("div");_.className="voc-form-table-checkbox-item",h&&_.classList.add("form-check");let S=document.createElement("input");S.type="checkbox",S.className="voc-form-table-cell",h&&S.classList.add("form-check-input"),S.id=D,S.value=F.value,S.checked=Array.isArray(r[n][l.key])&&r[n][l.key].includes(F.value),S.addEventListener("change",O=>{let J=O.target.checked;Array.isArray(r[n][l.key])||(r[n][l.key]=[]),J?r[n][l.key].includes(F.value)||r[n][l.key].push(F.value):r[n][l.key]=r[n][l.key].filter(U=>U!==F.value),w()});let H=document.createElement("label");H.htmlFor=D,H.className="voc-form-table-checkbox-label",h&&H.classList.add("form-check-label"),H.textContent=F.label,_.appendChild(S),_.appendChild(H),b.appendChild(_)}),T=b}else if(q==="radio"){let b=document.createElement("div");b.className="voc-form-table-radios",h&&b.classList.add("form-check");let B=l.options||[];r[n][l.key]===void 0&&(r[n][l.key]=""),B.forEach(F=>{let D=`voc-form-${i.key}-${l.key}-${n}-${F.value}`,_=document.createElement("div");_.className="voc-form-table-radio-item",h&&_.classList.add("form-check");let S=document.createElement("input");S.type="radio",S.className="voc-form-table-cell",h&&S.classList.add("form-check-input"),S.id=D,S.name=`voc-form-${i.key}-${l.key}-${n}`,S.value=F.value,S.checked=r[n][l.key]===F.value,S.addEventListener("change",O=>{O.target.checked&&(r[n][l.key]=O.target.value,w())});let H=document.createElement("label");H.htmlFor=D,H.className="voc-form-table-radio-label",h&&H.classList.add("form-check-label"),H.textContent=F.label,_.appendChild(S),_.appendChild(H),b.appendChild(_)}),T=b}else{let b=document.createElement("input");q==="number"?b.type="number":q==="email"?b.type="email":q==="tel"?b.type="tel":q==="url"?b.type="url":q==="date"?b.type="date":b.type="text",b.value=s[l.key]!==void 0&&s[l.key]!==null?String(s[l.key]):"",l.placeholder&&(b.placeholder=l.placeholder),l.required&&(b.required=!0),b.className="voc-form-table-cell",h&&b.classList.add("form-control"),b.addEventListener("input",B=>{r[n][l.key]=B.target.value,w()}),T=b}M.appendChild(T),o.appendChild(M)});let $=document.createElement("td"),R=document.createElement("button");R.type="button",R.className="voc-form-table-remove",h&&R.classList.add("btn","btn-danger"),R.textContent="Remove",R.addEventListener("click",()=>{r.splice(n,1),t(),w()}),$.appendChild(R),o.appendChild($),N.appendChild(o)})}return C.addEventListener("click",()=>{if(i.validation?.max_rows!==void 0&&r.length>=i.validation.max_rows)return;let a={};i.fields.forEach(s=>a[s.key]=""),r.push(a),t(),w()}),Array.isArray(y)&&y.length?r=y.map(a=>({...a})):i.default&&Array.isArray(i.default)&&(r=i.default.map(a=>({...a}))),t(),w(),v.validate=function(){let a=[];return r.forEach((s,n)=>{let o=E(s);o.length&&a.push(`Row ${n+1}: ${o.join("; ")}`)}),i.validation?.min_rows!==void 0&&r.length<i.validation.min_rows&&a.push(`At least ${i.validation.min_rows} rows required`),i.validation?.max_rows!==void 0&&r.length>i.validation.max_rows&&a.push(`No more than ${i.validation.max_rows} rows allowed`),i.validation?.unique_fields&&i.validation.unique_fields.length&&i.validation.unique_fields.forEach(s=>{let n=new Set;r.forEach((o,x)=>{let A=o[s];A!=null&&(n.has(A)&&a.push(`Field ${s} must be unique (duplicate at row ${x+1})`),n.add(A))})}),a},v}function I(i,L=!0){let y=new FormData(i);if(L)return K(y);let v={};return y.forEach((h,d)=>{let g=(h instanceof File,h);Object.prototype.hasOwnProperty.call(v,d)?Array.isArray(v[d])?v[d].push(g):v[d]=[v[d],g]:v[d]=g}),v}function K(i){let L=new FormData;for(let[y,v]of i.entries()){if(v instanceof File){L.append(y,v);continue}let h;try{h=JSON.parse(v)}catch{h=null}Array.isArray(h)&&h.every(d=>typeof d=="object"&&d!==null)?h.forEach((d,g)=>{for(let[c,p]of Object.entries(d))L.append(`${y}[${g}][${c}]`,p)}):L.append(y,v)}return L}async function V(i,L,y){let v=L.url,h={"Content-Type":"application/json"};L?.api_token&&(h.Authorization=`Bearer ${L.api_token}`);let d=I(i);if(L?.additional_data)for(let[p,f]of Object.entries(L.additional_data))d.append(p,f);let g,c=d instanceof FormData;g={method:"POST",headers:{Authorization:`Bearer ${L.api_token}`,...c?{}:{"Content-Type":"application/json"},Accept:"application/json"},body:c?d:JSON.stringify(Object.fromEntries(d.entries()))};try{fetch(v,g).then(async p=>{let f=p.headers.get("content-type");if(!p.ok)try{let N=(await p.json())?.message;throw new Error(N??"Their was an error submitting the form. Check your form and try again.")}catch(m){throw new Error(m.message)}return f&&f.includes("application/json")?p.json():p.text()}).then(p=>{y&&y(p,null)}).catch(p=>{y&&y(null,p)})}catch(p){y&&y(null,p)}}function G(i,L,y){let v=[];if(typeof i=="string"?v=Array.from(document.querySelectorAll(i)):v=[i],v.length===0)return null;function h(){let c=y?.style==="bootstrap",p=document.createElement("form");p.classList.add("voc-form"),c&&p.classList.add("needs-validation"),p.id=L.id;let f=document.createElement("h2");f.className="voc-form-title",c&&f.classList.add("mb-3"),f.textContent=L.form_name,p.appendChild(f);function m(e,u){u&&(u.min_length!==void 0&&(e.minLength=u.min_length),u.max_length!==void 0&&(e.maxLength=u.max_length),u.regex&&(e instanceof HTMLInputElement?e.pattern=u.regex:e.dataset.pattern=u.regex))}function N(e){if(!e.key&&!e.label)return null;let u=L.default_values||{},k=e.name||e.key,r=k?u[k]:void 0,w=document.createElement("div");if(w.className="voc-form-group",c&&w.classList.add("mb-3"),e.label){let t=document.createElement("label");if(t.classList.add("voc-form-label"),c&&t.classList.add("form-label"),e.key?e.type==="radio"?t.htmlFor="":t.htmlFor=`voc-form-${e.key}`:t.htmlFor="",t.textContent=e.label,e.required){let a=document.createElement("span");a.className="voc-form-required",a.textContent="*",t.appendChild(a)}w.appendChild(t)}let E=null;switch(e.type){case"checkbox":{let t=document.createElement("div");if(t.className="voc-form-checkbox-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options)e.options.forEach((a,s)=>{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),c&&n.classList.add("form-check"),n.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(n.style.width=e.width);let o=document.createElement("input");o.type="checkbox",o.classList.add("voc-form-checkbox"),c&&o.classList.add("form-check-input"),o.name=e.name||e.key||"",o.value=a.value,e.key&&(o.id=`voc-form-${e.key}-${s}`),e.required&&(o.required=!0),Array.isArray(r)?r.includes(a.value)&&(o.checked=!0):r!==void 0?r===a.value&&(o.checked=!0):e.default===a.value&&(o.checked=!0),n.appendChild(o),n.appendChild(document.createTextNode(a.label)),t.appendChild(n)});else{let a=document.createElement("label");a.classList.add("voc-form-checkbox-label"),c&&a.classList.add("form-check"),e.width&&(a.style.width=e.width);let s=document.createElement("input");s.type="checkbox",s.classList.add("voc-form-checkbox"),(e.key||e.name)&&(s.name=e.name||e.key,e.key&&(s.id=`voc-form-${e.key}`)),c&&s.classList.add("form-check-input"),e.required&&(s.required=!0),r!==void 0?s.checked=!!r:e.default&&(s.checked=!!e.default),a.appendChild(s),a.appendChild(document.createTextNode(e.label||"")),t.appendChild(a)}E=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),c&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),r!=null?t.value=String(r):e.default!==void 0&&e.default!==null&&(t.value=String(e.default));let a=e.default||r||"";if(a){let s=(a.match(/\n/g)||[]).length,n=80,o=a.split(`
2
+ `).reduce(($,R)=>Math.max($,R.length),0),x=Math.ceil(o/n)||1,A=Math.max(2,s+x);t.rows=A,t.style.resize="vertical"}e.width&&(t.style.width=e.width),e.required&&(t.required=!0),m(t,e.validation),E=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),c&&t.classList.add("form-select"),c&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder){let a=document.createElement("option");a.value="",a.textContent=e.placeholder,a.disabled=!0,a.selected=!0,t.appendChild(a)}e.required&&(t.required=!0),e.options&&e.options.forEach(a=>{let s=document.createElement("option");s.value=a.value,s.textContent=a.label,Array.isArray(r)?r.includes(a.value)&&(s.selected=!0):r!==void 0?r===a.value&&(s.selected=!0):e.default===a.value&&(s.selected=!0),t.appendChild(s)}),e.width&&(t.style.width=e.width),E=t;break}case"radio":{let t=document.createElement("div");t.className="voc-form-radio-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options&&e.options.forEach((a,s)=>{let n=document.createElement("label");n.classList.add("voc-form-radio-label"),c&&n.classList.add("form-check"),n.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(n.style.width=e.width);let o=document.createElement("input");o.type="radio",o.classList.add("voc-form-radio"),c&&o.classList.add("form-check-input"),o.name=e.name||e.key||"",o.value=a.value,e.key&&(o.id=`voc-form-${e.key}-${s}`),Array.isArray(r)?r.includes(a.value)&&(o.checked=!0):r!==void 0?r===a.value&&(o.checked=!0):e.default===a.value&&(o.checked=!0),n.appendChild(o),n.appendChild(document.createTextNode(a.label)),t.appendChild(n)}),E=t;break}case"file":{let t=document.createElement("div");t.className="voc-form-file-wrapper",e.width&&(t.style.width=e.width);let a=document.createElement("input");a.type="file",a.classList.add("voc-form-file"),c&&a.classList.add("form-control"),(e.key||e.name)&&(a.name=e.name||e.key,e.key&&(a.id=`voc-form-${e.key}`)),e.width&&(a.style.width=e.width),e.required&&(a.required=!0),e.accept&&(a.accept=e.accept),e.multiple&&(a.multiple=!0),t.appendChild(a),E=t;break}case"avatar":{E=W(e,y||{},r);break}case"table":{E=P(e,y||{},r);break}case"audio_select":{E=z(e,y||{},r);break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),c&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),r!=null?t.value=String(r):e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),m(t,e.validation),E=t;break}}if(E&&w.appendChild(E),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,w.appendChild(t)}return w}L.fields.forEach(e=>{if(e.type==="group"&&"items"in e){let u=e;if(c){let k=document.createElement("div");k.className="card voc-form-group-card mb-3";let r=u.label||u.header?.title,w=u.description||u.header?.description;if(r||w){let t=document.createElement("div");if(t.className="card-header voc-form-group-head",r){let a=document.createElement("h5");a.className="mb-0 voc-form-group-label",a.textContent=r,t.appendChild(a)}if(w){let a=document.createElement("p");a.className="mb-0 voc-form-group-description",a.textContent=w,t.appendChild(a)}k.appendChild(t)}let E=document.createElement("div");E.className="card-body voc-form-group-body",u.items.forEach(t=>{if(t.type==="row"&&"items"in t){let a=t,s=document.createElement("div");if(s.className="voc-form-group voc-form-row mb-3",a.label){let o=document.createElement("h6");o.className="voc-form-row-label",o.textContent=a.label,s.appendChild(o)}let n=document.createElement("div");n.className="voc-form-row-items "+(a.direction||"horizontal"),a.items.forEach(o=>{let x=N(o);x&&n.appendChild(x)}),s.appendChild(n),E.appendChild(s)}else{let a=N(t);a&&E.appendChild(a)}}),k.appendChild(E),p.appendChild(k)}else{let k=document.createElement("div");k.className="voc-form-group voc-form-group-wrapper",c&&k.classList.add("mb-3");let r=u.label||u.header?.title,w=u.description||u.header?.description;if(r||w){let E=document.createElement("div");if(E.className="voc-form-group-head",r){let t=document.createElement("h3");t.className="voc-form-group-label",t.textContent=r,E.appendChild(t)}if(w){let t=document.createElement("p");t.className="voc-form-group-description",t.textContent=w,E.appendChild(t)}k.appendChild(E)}u.items.forEach(E=>{if(E.type==="row"&&"items"in E){let t=E,a=document.createElement("div");if(a.className="voc-form-group voc-form-row",c&&a.classList.add("mb-3"),t.label){let n=document.createElement("h4");n.className="voc-form-row-label",n.textContent=t.label,a.appendChild(n)}let s=document.createElement("div");s.className="voc-form-row-items "+(t.direction||"horizontal"),t.items.forEach(n=>{let o=N(n);o&&s.appendChild(o)}),a.appendChild(s),k.appendChild(a)}else{let t=N(E);t&&k.appendChild(t)}}),p.appendChild(k)}return}if(e.type==="row"&&"items"in e){let u=document.createElement("div");if(u.className="voc-form-group voc-form-row",c&&u.classList.add("mb-3"),e.label){let r=document.createElement("h3");r.className="voc-form-row-label",r.textContent=e.label,u.appendChild(r)}if(e.description){let r=document.createElement("p");r.className="voc-form-row-description",r.textContent=e.description,u.appendChild(r)}let k=document.createElement("div");k.className="voc-form-row-items "+(e.direction||"horizontal"),u.appendChild(k),e.items.forEach(r=>{let w=N(r);w&&k.appendChild(w)}),p.appendChild(u)}else{let u=N(e);u&&p.appendChild(u)}});let C=document.createElement("button");return C.type="submit",C.className="voc-form-submit",C.textContent="Submit",c&&C.classList.add("btn","btn-primary"),p.appendChild(C),p}let d=h();v[0].appendChild(d);let g=L?.submit_config??y?.submit_config??null;return g&&d.addEventListener("submit",c=>{if(c.preventDefault(),!d.checkValidity()){d.reportValidity(),y?.onSubmitResult&&y.onSubmitResult(null,new Error("Form validation failed"));return}let p=Array.from(d.querySelectorAll(" .voc-form-table-wrapper, .voc-form-table-wrapper")),f=[];if(p.forEach(m=>{if(m&&typeof m.validate=="function"){let N=m.validate();N&&N.length&&f.push(...N)}}),f.length){y?.onSubmitResult&&y.onSubmitResult(null,new Error(f.join("; ")));let m=p[0];m&&m.scrollIntoView({behavior:"smooth",block:"center"});return}V(d,g,(m,N)=>{y?.onSubmitResult&&y.onSubmitResult(m,N)})}),{element:d,validate:()=>{let c=[];return d.querySelectorAll(":invalid").forEach(f=>{let m=f.validationMessage;c.push({element:f,message:m})}),c},getValues:()=>I(d),onSubmit:c=>{d.addEventListener("submit",p=>{p.preventDefault();let f=I(d);c(f,p)})}}}var j={renderForm:G};var me=j,ue={FormsLib:j};export{me as FormsLib,ue as default};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "voc-lib-js",
3
- "version": "1.0.49",
3
+ "version": "1.0.51",
4
4
  "description": "A JavaScript library for VocPhone",
5
5
  "main": "./dist/main.cjs",
6
6
  "module": "./dist/main.mjs",