voc-lib-js 1.0.47 → 1.0.49

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 R=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var W=(i,v)=>{for(var h in v)R(i,h,{get:v[h],enumerable:!0})},I=(i,v,h,b)=>{if(v&&typeof v=="object"||typeof v=="function")for(let f of O(v))!z.call(i,f)&&f!==h&&R(i,f,{get:()=>v[f],enumerable:!(b=$(v,f))||b.enumerable});return i};var j=i=>I(R({},"__esModule",{value:!0}),i);var V={};W(V,{FormsLib:()=>P,default:()=>G});function H(i,v,h){let b=v?.style==="bootstrap",f=document.createElement("div");f.className="voc-form-avatar-wrapper",b&&f.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",b&&(d.classList.add("img-thumbnail","rounded-circle"),d.style.width=d.style.height="64px",d.style.objectFit="cover");let E=h!=null?String(h):i.default!==void 0&&i.default!==null?String(i.default):"";E?d.src=E:(d.src="",d.style.display="none");let a=document.createElement("input");a.type="file",a.accept=i.accept||"image/*",a.className="voc-form-avatar-input",b&&a.classList.add("form-control"),(i.key||i.name)&&(a.name=i.name||i.key,i.key&&(a.id=`voc-form-${i.key}`));let s=document.createElement("input");if(s.type="hidden",(i.key||i.name)&&(s.name=(i.name||i.key)+"_url"),s.value=E||"",a.addEventListener("change",u=>{let c=u.target;if(c.files&&c.files.length>0){let F=c.files[0],C=new FileReader;C.onload=()=>{d.src=String(C.result),d.style.display="",s.value=""},C.readAsDataURL(F)}}),b){let u=document.createElement("div");u.style.flex="1",u.appendChild(a),u.appendChild(s),f.appendChild(d),f.appendChild(u)}else f.appendChild(d),f.appendChild(a),f.appendChild(s);return f}function B(i,v,h){let b=v?.style==="bootstrap",f=document.createElement("div");if(f.className="voc-form-audio-select",!i.options||i.options.length===0)return f;let d=i.name||i.key||`audio_select_${Math.random().toString(36).substr(2,6)}`,E;return Array.isArray(h)?E=h[0]:h!==void 0?E=h:i.default!==void 0&&(E=i.default),i.options.forEach((a,s)=>{let u=document.createElement("div");u.className="voc-form-audio-item",b&&u.classList.add("form-check");let c=document.createElement("input");c.type="radio",c.name=d,c.value=a.value,c.className="voc-form-audio-radio",i.key&&(c.id=`voc-form-${i.key}-${s}`),i.required&&s===0&&(c.required=!0),c.style.position="absolute",c.style.width="1px",c.style.height="1px",c.style.margin="0",c.style.padding="0",c.style.clip="rect(0 0 0 0)",c.style.clipPath="inset(50%)",c.style.overflow="hidden",c.style.border="0",E!==void 0&&(Array.isArray(E)?c.checked=E.includes(a.value):c.checked=String(E)===String(a.value));let F=()=>{c.checked?u.classList.add("selected"):u.classList.remove("selected")};c.addEventListener("change",()=>{f.querySelectorAll(".voc-form-audio-item").forEach(n=>n.classList.remove("selected")),F()}),c.checked&&u.classList.add("selected");let C=document.createElement("label");C.className="voc-form-audio-label",b&&C.classList.add("form-check-label"),C.htmlFor=c.id||"",C.textContent=a.label||a.value,C.tabIndex=0;let e=document.createElement("div");e.className="voc-form-audio-control";let o=null;if(a.audio_link){let t=document.createElement("button");t.type="button",t.className="voc-form-audio-play",b&&t.classList.add("btn","btn-sm","btn-outline-secondary"),t.setAttribute("aria-label",`Play ${a.label||a.value}`);let n="http://www.w3.org/2000/svg",l=document.createElementNS(n,"svg");l.setAttribute("viewBox","0 0 24 24"),l.setAttribute("width","16"),l.setAttribute("height","16"),l.setAttribute("aria-hidden","true");let p=document.createElementNS(n,"path");p.setAttribute("d","M8 5v14l11-7z"),p.setAttribute("fill","currentColor"),l.appendChild(p),t.appendChild(l),o=document.createElement("audio"),o.src=a.audio_link,o.preload="none",o.style.display="none";let m=L=>{for(;L.firstChild;)L.removeChild(L.firstChild)},A=()=>{let L="http://www.w3.org/2000/svg",x=document.createElementNS(L,"svg");x.setAttribute("viewBox","0 0 24 24"),x.setAttribute("width","16"),x.setAttribute("height","16"),x.setAttribute("aria-hidden","true");let k=document.createElementNS(L,"path");return k.setAttribute("d","M8 5v14l11-7z"),k.setAttribute("fill","currentColor"),x.appendChild(k),x},M=()=>{let L="http://www.w3.org/2000/svg",x=document.createElementNS(L,"svg");x.setAttribute("viewBox","0 0 24 24"),x.setAttribute("width","16"),x.setAttribute("height","16"),x.setAttribute("aria-hidden","true");let k=document.createElementNS(L,"rect");return k.setAttribute("x","6"),k.setAttribute("y","6"),k.setAttribute("width","12"),k.setAttribute("height","12"),k.setAttribute("fill","currentColor"),x.appendChild(k),x};m(t),t.appendChild(A());let T=L=>{m(t),L?(t.appendChild(M()),t.setAttribute("aria-label",`Stop ${a.label||a.value}`)):(t.appendChild(A()),t.setAttribute("aria-label",`Play ${a.label||a.value}`))};t.addEventListener("click",()=>{try{if(!o)return;let L=f.querySelectorAll("audio");if(!o.paused&&!o.ended){o.pause(),o.currentTime=0;return}L.forEach(k=>{try{k.pause(),k.currentTime=0}catch{}}),f.querySelectorAll(".voc-form-audio-item").forEach(k=>k.classList.remove("is-playing")),o.play()}catch(L){console.warn("audio play failed",L)}}),o.addEventListener("playing",()=>{u.classList.add("is-playing"),T(!0)});let S=()=>{u.classList.remove("is-playing"),T(!1)};o.addEventListener("pause",S),o.addEventListener("ended",S),e.appendChild(t),e.appendChild(o)}let g=document.createElement("div");g.className="voc-form-item-detail",g.style.display="flex",g.style.flexDirection="column",g.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(c),r.appendChild(C);let w=document.createElement("div");w.className="voc-form-item-description";let y=a.description||a.hint_text||"";y&&(w.textContent=y),g.appendChild(r),g.appendChild(w),e.appendChild(g),u.appendChild(e),r.addEventListener("click",t=>{let n=t.target;n&&n.closest(".voc-form-audio-play")||(c.checked=!0,c.dispatchEvent(new Event("change",{bubbles:!0})))}),u.addEventListener("click",t=>{let n=t.target;n&&n.closest(".voc-form-audio-play")||(c.checked=!0,c.dispatchEvent(new Event("change",{bubbles:!0})))}),f.appendChild(u)}),f}function N(i,v=!0){let h=new FormData(i);if(v)return h;let b={};return h.forEach((f,d)=>{let E=(f instanceof File,f);Object.prototype.hasOwnProperty.call(b,d)?Array.isArray(b[d])?b[d].push(E):b[d]=[b[d],E]:b[d]=E}),b}async function q(i,v,h){let b=v.url,f={"Content-Type":"application/json"};v?.api_token&&(f.Authorization=`Bearer ${v.api_token}`);let d=N(i);if(v?.additional_data)for(let[s,u]of Object.entries(v.additional_data))d.append(s,u);let E,a=d instanceof FormData;E={method:"POST",headers:{Authorization:`Bearer ${v.api_token}`,...a?{}:{"Content-Type":"application/json"},Accept:"application/json"},body:a?d:JSON.stringify(Object.fromEntries(d.entries()))};try{fetch(b,E).then(async s=>{let u=s.headers.get("content-type");if(!s.ok)try{let F=(await s.json())?.message;throw new Error(F??"Their was an error submitting the form. Check your form and try again.")}catch(c){throw new Error(c)}return u&&u.includes("application/json")?s.json():s.text()}).then(s=>{h&&h(s,null)}).catch(s=>{h&&h(null,s)})}catch(s){h&&h(null,s)}}function D(i,v,h){let b=[];if(typeof i=="string"?b=Array.from(document.querySelectorAll(i)):b=[i],b.length===0)return null;function f(){let a=h?.style==="bootstrap",s=document.createElement("form");s.classList.add("voc-form"),a&&s.classList.add("needs-validation"),s.id=v.id;let u=document.createElement("h2");u.className="voc-form-title",a&&u.classList.add("mb-3"),u.textContent=v.form_name,s.appendChild(u);function c(e,o){o&&(o.min_length!==void 0&&(e.minLength=o.min_length),o.max_length!==void 0&&(e.maxLength=o.max_length),o.regex&&(e instanceof HTMLInputElement?e.pattern=o.regex:e.dataset.pattern=o.regex))}function F(e){if(!e.key&&!e.label)return null;let o=v.default_values||{},g=e.name||e.key,r=g?o[g]:void 0,w=document.createElement("div");if(w.className="voc-form-group",a&&w.classList.add("mb-3"),e.label){let t=document.createElement("label");if(t.classList.add("voc-form-label"),a&&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)}w.appendChild(t)}let y=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,l)=>{let p=document.createElement("label");p.classList.add("voc-form-checkbox-label"),a&&p.classList.add("form-check"),p.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(p.style.width=e.width);let m=document.createElement("input");m.type="checkbox",m.classList.add("voc-form-checkbox"),a&&m.classList.add("form-check-input"),m.name=e.name||e.key||"",m.value=n.value,e.key&&(m.id=`voc-form-${e.key}-${l}`),e.required&&(m.required=!0),Array.isArray(r)?r.includes(n.value)&&(m.checked=!0):r!==void 0?r===n.value&&(m.checked=!0):e.default===n.value&&(m.checked=!0),p.appendChild(m),p.appendChild(document.createTextNode(n.label)),t.appendChild(p)});else{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),a&&n.classList.add("form-check"),e.width&&(n.style.width=e.width);let l=document.createElement("input");l.type="checkbox",l.classList.add("voc-form-checkbox"),(e.key||e.name)&&(l.name=e.name||e.key,e.key&&(l.id=`voc-form-${e.key}`)),a&&l.classList.add("form-check-input"),e.required&&(l.required=!0),r!==void 0?l.checked=!!r:e.default&&(l.checked=!!e.default),n.appendChild(l),n.appendChild(document.createTextNode(e.label||"")),t.appendChild(n)}y=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),a&&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 n=e.default||r||"";if(n){let l=(n.match(/\n/g)||[]).length,p=80,m=n.split(`
2
- `).reduce((T,S)=>Math.max(T,S.length),0),A=Math.ceil(m/p)||1,M=Math.max(2,l+A);t.rows=M,t.style.resize="vertical"}e.width&&(t.style.width=e.width),e.required&&(t.required=!0),c(t,e.validation),y=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),a&&t.classList.add("form-select"),a&&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 l=document.createElement("option");l.value=n.value,l.textContent=n.label,Array.isArray(r)?r.includes(n.value)&&(l.selected=!0):r!==void 0?r===n.value&&(l.selected=!0):e.default===n.value&&(l.selected=!0),t.appendChild(l)}),e.width&&(t.style.width=e.width),y=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,l)=>{let p=document.createElement("label");p.classList.add("voc-form-radio-label"),a&&p.classList.add("form-check"),p.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(p.style.width=e.width);let m=document.createElement("input");m.type="radio",m.classList.add("voc-form-radio"),a&&m.classList.add("form-check-input"),m.name=e.name||e.key||"",m.value=n.value,e.key&&(m.id=`voc-form-${e.key}-${l}`),Array.isArray(r)?r.includes(n.value)&&(m.checked=!0):r!==void 0?r===n.value&&(m.checked=!0):e.default===n.value&&(m.checked=!0),p.appendChild(m),p.appendChild(document.createTextNode(n.label)),t.appendChild(p)}),y=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"),a&&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),y=t;break}case"avatar":{y=H(e,h||{},r);break}case"audio_select":{y=B(e,h||{},r);break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),a&&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),c(t,e.validation),y=t;break}}if(y&&w.appendChild(y),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,w.appendChild(t)}return w}v.fields.forEach(e=>{if(e.type==="group"&&"items"in e){let o=e;if(a){let g=document.createElement("div");g.className="card voc-form-group-card mb-3";let r=o.label||o.header?.title,w=o.description||o.header?.description;if(r||w){let t=document.createElement("div");if(t.className="card-header voc-form-group-head",r){let n=document.createElement("h5");n.className="mb-0 voc-form-group-label",n.textContent=r,t.appendChild(n)}if(w){let n=document.createElement("p");n.className="mb-0 voc-form-group-description",n.textContent=w,t.appendChild(n)}g.appendChild(t)}let y=document.createElement("div");y.className="card-body voc-form-group-body",o.items.forEach(t=>{if(t.type==="row"&&"items"in t){let n=t,l=document.createElement("div");if(l.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,l.appendChild(m)}let p=document.createElement("div");p.className="voc-form-row-items "+(n.direction||"horizontal"),n.items.forEach(m=>{let A=F(m);A&&p.appendChild(A)}),l.appendChild(p),y.appendChild(l)}else{let n=F(t);n&&y.appendChild(n)}}),g.appendChild(y),s.appendChild(g)}else{let g=document.createElement("div");g.className="voc-form-group voc-form-group-wrapper",a&&g.classList.add("mb-3");let r=o.label||o.header?.title,w=o.description||o.header?.description;if(r||w){let y=document.createElement("div");if(y.className="voc-form-group-head",r){let t=document.createElement("h3");t.className="voc-form-group-label",t.textContent=r,y.appendChild(t)}if(w){let t=document.createElement("p");t.className="voc-form-group-description",t.textContent=w,y.appendChild(t)}g.appendChild(y)}o.items.forEach(y=>{if(y.type==="row"&&"items"in y){let t=y,n=document.createElement("div");if(n.className="voc-form-group voc-form-row",a&&n.classList.add("mb-3"),t.label){let p=document.createElement("h4");p.className="voc-form-row-label",p.textContent=t.label,n.appendChild(p)}let l=document.createElement("div");l.className="voc-form-row-items "+(t.direction||"horizontal"),t.items.forEach(p=>{let m=F(p);m&&l.appendChild(m)}),n.appendChild(l),g.appendChild(n)}else{let t=F(y);t&&g.appendChild(t)}}),s.appendChild(g)}return}if(e.type==="row"&&"items"in e){let o=document.createElement("div");if(o.className="voc-form-group voc-form-row",a&&o.classList.add("mb-3"),e.label){let r=document.createElement("h3");r.className="voc-form-row-label",r.textContent=e.label,o.appendChild(r)}if(e.description){let r=document.createElement("p");r.className="voc-form-row-description",r.textContent=e.description,o.appendChild(r)}let g=document.createElement("div");g.className="voc-form-row-items "+(e.direction||"horizontal"),o.appendChild(g),e.items.forEach(r=>{let w=F(r);w&&g.appendChild(w)}),s.appendChild(o)}else{let o=F(e);o&&s.appendChild(o)}});let C=document.createElement("button");return C.type="submit",C.className="voc-form-submit",C.textContent="Submit",a&&C.classList.add("btn","btn-primary"),s.appendChild(C),s}let d=f();b[0].appendChild(d);let E=v?.submit_config??h?.submit_config??null;return E&&d.addEventListener("submit",a=>{a.preventDefault(),q(d,E,(s,u)=>{h?.onSubmitResult&&h.onSubmitResult(s,u)})}),{element:d,validate:()=>{let a=[];return d.querySelectorAll(":invalid").forEach(u=>{let c=u.validationMessage;a.push({element:u,message:c})}),a},getValues:()=>N(d),onSubmit:a=>{d.addEventListener("submit",s=>{s.preventDefault();let u=N(d);a(u,s)})}}}var _={renderForm:D};var P=_,G={FormsLib:_};return j(V);})();
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);})();
package/dist/main.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var R=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var W=(i,v)=>{for(var h in v)R(i,h,{get:v[h],enumerable:!0})},I=(i,v,h,b)=>{if(v&&typeof v=="object"||typeof v=="function")for(let f of O(v))!z.call(i,f)&&f!==h&&R(i,f,{get:()=>v[f],enumerable:!(b=$(v,f))||b.enumerable});return i};var j=i=>I(R({},"__esModule",{value:!0}),i);var V={};W(V,{FormsLib:()=>P,default:()=>G});module.exports=j(V);function H(i,v,h){let b=v?.style==="bootstrap",f=document.createElement("div");f.className="voc-form-avatar-wrapper",b&&f.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",b&&(d.classList.add("img-thumbnail","rounded-circle"),d.style.width=d.style.height="64px",d.style.objectFit="cover");let E=h!=null?String(h):i.default!==void 0&&i.default!==null?String(i.default):"";E?d.src=E:(d.src="",d.style.display="none");let a=document.createElement("input");a.type="file",a.accept=i.accept||"image/*",a.className="voc-form-avatar-input",b&&a.classList.add("form-control"),(i.key||i.name)&&(a.name=i.name||i.key,i.key&&(a.id=`voc-form-${i.key}`));let s=document.createElement("input");if(s.type="hidden",(i.key||i.name)&&(s.name=(i.name||i.key)+"_url"),s.value=E||"",a.addEventListener("change",u=>{let c=u.target;if(c.files&&c.files.length>0){let F=c.files[0],C=new FileReader;C.onload=()=>{d.src=String(C.result),d.style.display="",s.value=""},C.readAsDataURL(F)}}),b){let u=document.createElement("div");u.style.flex="1",u.appendChild(a),u.appendChild(s),f.appendChild(d),f.appendChild(u)}else f.appendChild(d),f.appendChild(a),f.appendChild(s);return f}function B(i,v,h){let b=v?.style==="bootstrap",f=document.createElement("div");if(f.className="voc-form-audio-select",!i.options||i.options.length===0)return f;let d=i.name||i.key||`audio_select_${Math.random().toString(36).substr(2,6)}`,E;return Array.isArray(h)?E=h[0]:h!==void 0?E=h:i.default!==void 0&&(E=i.default),i.options.forEach((a,s)=>{let u=document.createElement("div");u.className="voc-form-audio-item",b&&u.classList.add("form-check");let c=document.createElement("input");c.type="radio",c.name=d,c.value=a.value,c.className="voc-form-audio-radio",i.key&&(c.id=`voc-form-${i.key}-${s}`),i.required&&s===0&&(c.required=!0),c.style.position="absolute",c.style.width="1px",c.style.height="1px",c.style.margin="0",c.style.padding="0",c.style.clip="rect(0 0 0 0)",c.style.clipPath="inset(50%)",c.style.overflow="hidden",c.style.border="0",E!==void 0&&(Array.isArray(E)?c.checked=E.includes(a.value):c.checked=String(E)===String(a.value));let F=()=>{c.checked?u.classList.add("selected"):u.classList.remove("selected")};c.addEventListener("change",()=>{f.querySelectorAll(".voc-form-audio-item").forEach(n=>n.classList.remove("selected")),F()}),c.checked&&u.classList.add("selected");let C=document.createElement("label");C.className="voc-form-audio-label",b&&C.classList.add("form-check-label"),C.htmlFor=c.id||"",C.textContent=a.label||a.value,C.tabIndex=0;let e=document.createElement("div");e.className="voc-form-audio-control";let o=null;if(a.audio_link){let t=document.createElement("button");t.type="button",t.className="voc-form-audio-play",b&&t.classList.add("btn","btn-sm","btn-outline-secondary"),t.setAttribute("aria-label",`Play ${a.label||a.value}`);let n="http://www.w3.org/2000/svg",l=document.createElementNS(n,"svg");l.setAttribute("viewBox","0 0 24 24"),l.setAttribute("width","16"),l.setAttribute("height","16"),l.setAttribute("aria-hidden","true");let p=document.createElementNS(n,"path");p.setAttribute("d","M8 5v14l11-7z"),p.setAttribute("fill","currentColor"),l.appendChild(p),t.appendChild(l),o=document.createElement("audio"),o.src=a.audio_link,o.preload="none",o.style.display="none";let m=L=>{for(;L.firstChild;)L.removeChild(L.firstChild)},A=()=>{let L="http://www.w3.org/2000/svg",x=document.createElementNS(L,"svg");x.setAttribute("viewBox","0 0 24 24"),x.setAttribute("width","16"),x.setAttribute("height","16"),x.setAttribute("aria-hidden","true");let k=document.createElementNS(L,"path");return k.setAttribute("d","M8 5v14l11-7z"),k.setAttribute("fill","currentColor"),x.appendChild(k),x},M=()=>{let L="http://www.w3.org/2000/svg",x=document.createElementNS(L,"svg");x.setAttribute("viewBox","0 0 24 24"),x.setAttribute("width","16"),x.setAttribute("height","16"),x.setAttribute("aria-hidden","true");let k=document.createElementNS(L,"rect");return k.setAttribute("x","6"),k.setAttribute("y","6"),k.setAttribute("width","12"),k.setAttribute("height","12"),k.setAttribute("fill","currentColor"),x.appendChild(k),x};m(t),t.appendChild(A());let T=L=>{m(t),L?(t.appendChild(M()),t.setAttribute("aria-label",`Stop ${a.label||a.value}`)):(t.appendChild(A()),t.setAttribute("aria-label",`Play ${a.label||a.value}`))};t.addEventListener("click",()=>{try{if(!o)return;let L=f.querySelectorAll("audio");if(!o.paused&&!o.ended){o.pause(),o.currentTime=0;return}L.forEach(k=>{try{k.pause(),k.currentTime=0}catch{}}),f.querySelectorAll(".voc-form-audio-item").forEach(k=>k.classList.remove("is-playing")),o.play()}catch(L){console.warn("audio play failed",L)}}),o.addEventListener("playing",()=>{u.classList.add("is-playing"),T(!0)});let S=()=>{u.classList.remove("is-playing"),T(!1)};o.addEventListener("pause",S),o.addEventListener("ended",S),e.appendChild(t),e.appendChild(o)}let g=document.createElement("div");g.className="voc-form-item-detail",g.style.display="flex",g.style.flexDirection="column",g.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(c),r.appendChild(C);let w=document.createElement("div");w.className="voc-form-item-description";let y=a.description||a.hint_text||"";y&&(w.textContent=y),g.appendChild(r),g.appendChild(w),e.appendChild(g),u.appendChild(e),r.addEventListener("click",t=>{let n=t.target;n&&n.closest(".voc-form-audio-play")||(c.checked=!0,c.dispatchEvent(new Event("change",{bubbles:!0})))}),u.addEventListener("click",t=>{let n=t.target;n&&n.closest(".voc-form-audio-play")||(c.checked=!0,c.dispatchEvent(new Event("change",{bubbles:!0})))}),f.appendChild(u)}),f}function N(i,v=!0){let h=new FormData(i);if(v)return h;let b={};return h.forEach((f,d)=>{let E=(f instanceof File,f);Object.prototype.hasOwnProperty.call(b,d)?Array.isArray(b[d])?b[d].push(E):b[d]=[b[d],E]:b[d]=E}),b}async function q(i,v,h){let b=v.url,f={"Content-Type":"application/json"};v?.api_token&&(f.Authorization=`Bearer ${v.api_token}`);let d=N(i);if(v?.additional_data)for(let[s,u]of Object.entries(v.additional_data))d.append(s,u);let E,a=d instanceof FormData;E={method:"POST",headers:{Authorization:`Bearer ${v.api_token}`,...a?{}:{"Content-Type":"application/json"},Accept:"application/json"},body:a?d:JSON.stringify(Object.fromEntries(d.entries()))};try{fetch(b,E).then(async s=>{let u=s.headers.get("content-type");if(!s.ok)try{let F=(await s.json())?.message;throw new Error(F??"Their was an error submitting the form. Check your form and try again.")}catch(c){throw new Error(c)}return u&&u.includes("application/json")?s.json():s.text()}).then(s=>{h&&h(s,null)}).catch(s=>{h&&h(null,s)})}catch(s){h&&h(null,s)}}function D(i,v,h){let b=[];if(typeof i=="string"?b=Array.from(document.querySelectorAll(i)):b=[i],b.length===0)return null;function f(){let a=h?.style==="bootstrap",s=document.createElement("form");s.classList.add("voc-form"),a&&s.classList.add("needs-validation"),s.id=v.id;let u=document.createElement("h2");u.className="voc-form-title",a&&u.classList.add("mb-3"),u.textContent=v.form_name,s.appendChild(u);function c(e,o){o&&(o.min_length!==void 0&&(e.minLength=o.min_length),o.max_length!==void 0&&(e.maxLength=o.max_length),o.regex&&(e instanceof HTMLInputElement?e.pattern=o.regex:e.dataset.pattern=o.regex))}function F(e){if(!e.key&&!e.label)return null;let o=v.default_values||{},g=e.name||e.key,r=g?o[g]:void 0,w=document.createElement("div");if(w.className="voc-form-group",a&&w.classList.add("mb-3"),e.label){let t=document.createElement("label");if(t.classList.add("voc-form-label"),a&&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)}w.appendChild(t)}let y=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,l)=>{let p=document.createElement("label");p.classList.add("voc-form-checkbox-label"),a&&p.classList.add("form-check"),p.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(p.style.width=e.width);let m=document.createElement("input");m.type="checkbox",m.classList.add("voc-form-checkbox"),a&&m.classList.add("form-check-input"),m.name=e.name||e.key||"",m.value=n.value,e.key&&(m.id=`voc-form-${e.key}-${l}`),e.required&&(m.required=!0),Array.isArray(r)?r.includes(n.value)&&(m.checked=!0):r!==void 0?r===n.value&&(m.checked=!0):e.default===n.value&&(m.checked=!0),p.appendChild(m),p.appendChild(document.createTextNode(n.label)),t.appendChild(p)});else{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),a&&n.classList.add("form-check"),e.width&&(n.style.width=e.width);let l=document.createElement("input");l.type="checkbox",l.classList.add("voc-form-checkbox"),(e.key||e.name)&&(l.name=e.name||e.key,e.key&&(l.id=`voc-form-${e.key}`)),a&&l.classList.add("form-check-input"),e.required&&(l.required=!0),r!==void 0?l.checked=!!r:e.default&&(l.checked=!!e.default),n.appendChild(l),n.appendChild(document.createTextNode(e.label||"")),t.appendChild(n)}y=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),a&&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 n=e.default||r||"";if(n){let l=(n.match(/\n/g)||[]).length,p=80,m=n.split(`
2
- `).reduce((T,S)=>Math.max(T,S.length),0),A=Math.ceil(m/p)||1,M=Math.max(2,l+A);t.rows=M,t.style.resize="vertical"}e.width&&(t.style.width=e.width),e.required&&(t.required=!0),c(t,e.validation),y=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),a&&t.classList.add("form-select"),a&&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 l=document.createElement("option");l.value=n.value,l.textContent=n.label,Array.isArray(r)?r.includes(n.value)&&(l.selected=!0):r!==void 0?r===n.value&&(l.selected=!0):e.default===n.value&&(l.selected=!0),t.appendChild(l)}),e.width&&(t.style.width=e.width),y=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,l)=>{let p=document.createElement("label");p.classList.add("voc-form-radio-label"),a&&p.classList.add("form-check"),p.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(p.style.width=e.width);let m=document.createElement("input");m.type="radio",m.classList.add("voc-form-radio"),a&&m.classList.add("form-check-input"),m.name=e.name||e.key||"",m.value=n.value,e.key&&(m.id=`voc-form-${e.key}-${l}`),Array.isArray(r)?r.includes(n.value)&&(m.checked=!0):r!==void 0?r===n.value&&(m.checked=!0):e.default===n.value&&(m.checked=!0),p.appendChild(m),p.appendChild(document.createTextNode(n.label)),t.appendChild(p)}),y=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"),a&&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),y=t;break}case"avatar":{y=H(e,h||{},r);break}case"audio_select":{y=B(e,h||{},r);break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),a&&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),c(t,e.validation),y=t;break}}if(y&&w.appendChild(y),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,w.appendChild(t)}return w}v.fields.forEach(e=>{if(e.type==="group"&&"items"in e){let o=e;if(a){let g=document.createElement("div");g.className="card voc-form-group-card mb-3";let r=o.label||o.header?.title,w=o.description||o.header?.description;if(r||w){let t=document.createElement("div");if(t.className="card-header voc-form-group-head",r){let n=document.createElement("h5");n.className="mb-0 voc-form-group-label",n.textContent=r,t.appendChild(n)}if(w){let n=document.createElement("p");n.className="mb-0 voc-form-group-description",n.textContent=w,t.appendChild(n)}g.appendChild(t)}let y=document.createElement("div");y.className="card-body voc-form-group-body",o.items.forEach(t=>{if(t.type==="row"&&"items"in t){let n=t,l=document.createElement("div");if(l.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,l.appendChild(m)}let p=document.createElement("div");p.className="voc-form-row-items "+(n.direction||"horizontal"),n.items.forEach(m=>{let A=F(m);A&&p.appendChild(A)}),l.appendChild(p),y.appendChild(l)}else{let n=F(t);n&&y.appendChild(n)}}),g.appendChild(y),s.appendChild(g)}else{let g=document.createElement("div");g.className="voc-form-group voc-form-group-wrapper",a&&g.classList.add("mb-3");let r=o.label||o.header?.title,w=o.description||o.header?.description;if(r||w){let y=document.createElement("div");if(y.className="voc-form-group-head",r){let t=document.createElement("h3");t.className="voc-form-group-label",t.textContent=r,y.appendChild(t)}if(w){let t=document.createElement("p");t.className="voc-form-group-description",t.textContent=w,y.appendChild(t)}g.appendChild(y)}o.items.forEach(y=>{if(y.type==="row"&&"items"in y){let t=y,n=document.createElement("div");if(n.className="voc-form-group voc-form-row",a&&n.classList.add("mb-3"),t.label){let p=document.createElement("h4");p.className="voc-form-row-label",p.textContent=t.label,n.appendChild(p)}let l=document.createElement("div");l.className="voc-form-row-items "+(t.direction||"horizontal"),t.items.forEach(p=>{let m=F(p);m&&l.appendChild(m)}),n.appendChild(l),g.appendChild(n)}else{let t=F(y);t&&g.appendChild(t)}}),s.appendChild(g)}return}if(e.type==="row"&&"items"in e){let o=document.createElement("div");if(o.className="voc-form-group voc-form-row",a&&o.classList.add("mb-3"),e.label){let r=document.createElement("h3");r.className="voc-form-row-label",r.textContent=e.label,o.appendChild(r)}if(e.description){let r=document.createElement("p");r.className="voc-form-row-description",r.textContent=e.description,o.appendChild(r)}let g=document.createElement("div");g.className="voc-form-row-items "+(e.direction||"horizontal"),o.appendChild(g),e.items.forEach(r=>{let w=F(r);w&&g.appendChild(w)}),s.appendChild(o)}else{let o=F(e);o&&s.appendChild(o)}});let C=document.createElement("button");return C.type="submit",C.className="voc-form-submit",C.textContent="Submit",a&&C.classList.add("btn","btn-primary"),s.appendChild(C),s}let d=f();b[0].appendChild(d);let E=v?.submit_config??h?.submit_config??null;return E&&d.addEventListener("submit",a=>{a.preventDefault(),q(d,E,(s,u)=>{h?.onSubmitResult&&h.onSubmitResult(s,u)})}),{element:d,validate:()=>{let a=[];return d.querySelectorAll(":invalid").forEach(u=>{let c=u.validationMessage;a.push({element:u,message:c})}),a},getValues:()=>N(d),onSubmit:a=>{d.addEventListener("submit",s=>{s.preventDefault();let u=N(d);a(u,s)})}}}var _={renderForm:D};var P=_,G={FormsLib:_};0&&(module.exports={FormsLib});
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});
package/dist/main.mjs CHANGED
@@ -1,2 +1,2 @@
1
- function _(m,w,v){let b=w?.style==="bootstrap",y=document.createElement("div");y.className="voc-form-avatar-wrapper",b&&y.classList.add("d-flex","align-items-center","gap-3");let l=document.createElement("img");l.className="voc-form-avatar-preview",l.alt=m.label||"avatar",b&&(l.classList.add("img-thumbnail","rounded-circle"),l.style.width=l.style.height="64px",l.style.objectFit="cover");let g=v!=null?String(v):m.default!==void 0&&m.default!==null?String(m.default):"";g?l.src=g:(l.src="",l.style.display="none");let a=document.createElement("input");a.type="file",a.accept=m.accept||"image/*",a.className="voc-form-avatar-input",b&&a.classList.add("form-control"),(m.key||m.name)&&(a.name=m.name||m.key,m.key&&(a.id=`voc-form-${m.key}`));let i=document.createElement("input");if(i.type="hidden",(m.key||m.name)&&(i.name=(m.name||m.key)+"_url"),i.value=g||"",a.addEventListener("change",u=>{let s=u.target;if(s.files&&s.files.length>0){let F=s.files[0],C=new FileReader;C.onload=()=>{l.src=String(C.result),l.style.display="",i.value=""},C.readAsDataURL(F)}}),b){let u=document.createElement("div");u.style.flex="1",u.appendChild(a),u.appendChild(i),y.appendChild(l),y.appendChild(u)}else y.appendChild(l),y.appendChild(a),y.appendChild(i);return y}function H(m,w,v){let b=w?.style==="bootstrap",y=document.createElement("div");if(y.className="voc-form-audio-select",!m.options||m.options.length===0)return y;let l=m.name||m.key||`audio_select_${Math.random().toString(36).substr(2,6)}`,g;return Array.isArray(v)?g=v[0]:v!==void 0?g=v:m.default!==void 0&&(g=m.default),m.options.forEach((a,i)=>{let u=document.createElement("div");u.className="voc-form-audio-item",b&&u.classList.add("form-check");let s=document.createElement("input");s.type="radio",s.name=l,s.value=a.value,s.className="voc-form-audio-radio",m.key&&(s.id=`voc-form-${m.key}-${i}`),m.required&&i===0&&(s.required=!0),s.style.position="absolute",s.style.width="1px",s.style.height="1px",s.style.margin="0",s.style.padding="0",s.style.clip="rect(0 0 0 0)",s.style.clipPath="inset(50%)",s.style.overflow="hidden",s.style.border="0",g!==void 0&&(Array.isArray(g)?s.checked=g.includes(a.value):s.checked=String(g)===String(a.value));let F=()=>{s.checked?u.classList.add("selected"):u.classList.remove("selected")};s.addEventListener("change",()=>{y.querySelectorAll(".voc-form-audio-item").forEach(n=>n.classList.remove("selected")),F()}),s.checked&&u.classList.add("selected");let C=document.createElement("label");C.className="voc-form-audio-label",b&&C.classList.add("form-check-label"),C.htmlFor=s.id||"",C.textContent=a.label||a.value,C.tabIndex=0;let e=document.createElement("div");e.className="voc-form-audio-control";let o=null;if(a.audio_link){let t=document.createElement("button");t.type="button",t.className="voc-form-audio-play",b&&t.classList.add("btn","btn-sm","btn-outline-secondary"),t.setAttribute("aria-label",`Play ${a.label||a.value}`);let n="http://www.w3.org/2000/svg",c=document.createElementNS(n,"svg");c.setAttribute("viewBox","0 0 24 24"),c.setAttribute("width","16"),c.setAttribute("height","16"),c.setAttribute("aria-hidden","true");let p=document.createElementNS(n,"path");p.setAttribute("d","M8 5v14l11-7z"),p.setAttribute("fill","currentColor"),c.appendChild(p),t.appendChild(c),o=document.createElement("audio"),o.src=a.audio_link,o.preload="none",o.style.display="none";let d=L=>{for(;L.firstChild;)L.removeChild(L.firstChild)},A=()=>{let L="http://www.w3.org/2000/svg",x=document.createElementNS(L,"svg");x.setAttribute("viewBox","0 0 24 24"),x.setAttribute("width","16"),x.setAttribute("height","16"),x.setAttribute("aria-hidden","true");let k=document.createElementNS(L,"path");return k.setAttribute("d","M8 5v14l11-7z"),k.setAttribute("fill","currentColor"),x.appendChild(k),x},M=()=>{let L="http://www.w3.org/2000/svg",x=document.createElementNS(L,"svg");x.setAttribute("viewBox","0 0 24 24"),x.setAttribute("width","16"),x.setAttribute("height","16"),x.setAttribute("aria-hidden","true");let k=document.createElementNS(L,"rect");return k.setAttribute("x","6"),k.setAttribute("y","6"),k.setAttribute("width","12"),k.setAttribute("height","12"),k.setAttribute("fill","currentColor"),x.appendChild(k),x};d(t),t.appendChild(A());let T=L=>{d(t),L?(t.appendChild(M()),t.setAttribute("aria-label",`Stop ${a.label||a.value}`)):(t.appendChild(A()),t.setAttribute("aria-label",`Play ${a.label||a.value}`))};t.addEventListener("click",()=>{try{if(!o)return;let L=y.querySelectorAll("audio");if(!o.paused&&!o.ended){o.pause(),o.currentTime=0;return}L.forEach(k=>{try{k.pause(),k.currentTime=0}catch{}}),y.querySelectorAll(".voc-form-audio-item").forEach(k=>k.classList.remove("is-playing")),o.play()}catch(L){console.warn("audio play failed",L)}}),o.addEventListener("playing",()=>{u.classList.add("is-playing"),T(!0)});let S=()=>{u.classList.remove("is-playing"),T(!1)};o.addEventListener("pause",S),o.addEventListener("ended",S),e.appendChild(t),e.appendChild(o)}let f=document.createElement("div");f.className="voc-form-item-detail",f.style.display="flex",f.style.flexDirection="column",f.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(s),r.appendChild(C);let E=document.createElement("div");E.className="voc-form-item-description";let h=a.description||a.hint_text||"";h&&(E.textContent=h),f.appendChild(r),f.appendChild(E),e.appendChild(f),u.appendChild(e),r.addEventListener("click",t=>{let n=t.target;n&&n.closest(".voc-form-audio-play")||(s.checked=!0,s.dispatchEvent(new Event("change",{bubbles:!0})))}),u.addEventListener("click",t=>{let n=t.target;n&&n.closest(".voc-form-audio-play")||(s.checked=!0,s.dispatchEvent(new Event("change",{bubbles:!0})))}),y.appendChild(u)}),y}function N(m,w=!0){let v=new FormData(m);if(w)return v;let b={};return v.forEach((y,l)=>{let g=(y instanceof File,y);Object.prototype.hasOwnProperty.call(b,l)?Array.isArray(b[l])?b[l].push(g):b[l]=[b[l],g]:b[l]=g}),b}async function B(m,w,v){let b=w.url,y={"Content-Type":"application/json"};w?.api_token&&(y.Authorization=`Bearer ${w.api_token}`);let l=N(m);if(w?.additional_data)for(let[i,u]of Object.entries(w.additional_data))l.append(i,u);let g,a=l instanceof FormData;g={method:"POST",headers:{Authorization:`Bearer ${w.api_token}`,...a?{}:{"Content-Type":"application/json"},Accept:"application/json"},body:a?l:JSON.stringify(Object.fromEntries(l.entries()))};try{fetch(b,g).then(async i=>{let u=i.headers.get("content-type");if(!i.ok)try{let F=(await i.json())?.message;throw new Error(F??"Their was an error submitting the form. Check your form and try again.")}catch(s){throw new Error(s)}return u&&u.includes("application/json")?i.json():i.text()}).then(i=>{v&&v(i,null)}).catch(i=>{v&&v(null,i)})}catch(i){v&&v(null,i)}}function q(m,w,v){let b=[];if(typeof m=="string"?b=Array.from(document.querySelectorAll(m)):b=[m],b.length===0)return null;function y(){let a=v?.style==="bootstrap",i=document.createElement("form");i.classList.add("voc-form"),a&&i.classList.add("needs-validation"),i.id=w.id;let u=document.createElement("h2");u.className="voc-form-title",a&&u.classList.add("mb-3"),u.textContent=w.form_name,i.appendChild(u);function s(e,o){o&&(o.min_length!==void 0&&(e.minLength=o.min_length),o.max_length!==void 0&&(e.maxLength=o.max_length),o.regex&&(e instanceof HTMLInputElement?e.pattern=o.regex:e.dataset.pattern=o.regex))}function F(e){if(!e.key&&!e.label)return null;let o=w.default_values||{},f=e.name||e.key,r=f?o[f]:void 0,E=document.createElement("div");if(E.className="voc-form-group",a&&E.classList.add("mb-3"),e.label){let t=document.createElement("label");if(t.classList.add("voc-form-label"),a&&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)}E.appendChild(t)}let h=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,c)=>{let p=document.createElement("label");p.classList.add("voc-form-checkbox-label"),a&&p.classList.add("form-check"),p.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(p.style.width=e.width);let d=document.createElement("input");d.type="checkbox",d.classList.add("voc-form-checkbox"),a&&d.classList.add("form-check-input"),d.name=e.name||e.key||"",d.value=n.value,e.key&&(d.id=`voc-form-${e.key}-${c}`),e.required&&(d.required=!0),Array.isArray(r)?r.includes(n.value)&&(d.checked=!0):r!==void 0?r===n.value&&(d.checked=!0):e.default===n.value&&(d.checked=!0),p.appendChild(d),p.appendChild(document.createTextNode(n.label)),t.appendChild(p)});else{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),a&&n.classList.add("form-check"),e.width&&(n.style.width=e.width);let c=document.createElement("input");c.type="checkbox",c.classList.add("voc-form-checkbox"),(e.key||e.name)&&(c.name=e.name||e.key,e.key&&(c.id=`voc-form-${e.key}`)),a&&c.classList.add("form-check-input"),e.required&&(c.required=!0),r!==void 0?c.checked=!!r:e.default&&(c.checked=!!e.default),n.appendChild(c),n.appendChild(document.createTextNode(e.label||"")),t.appendChild(n)}h=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),a&&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 n=e.default||r||"";if(n){let c=(n.match(/\n/g)||[]).length,p=80,d=n.split(`
2
- `).reduce((T,S)=>Math.max(T,S.length),0),A=Math.ceil(d/p)||1,M=Math.max(2,c+A);t.rows=M,t.style.resize="vertical"}e.width&&(t.style.width=e.width),e.required&&(t.required=!0),s(t,e.validation),h=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),a&&t.classList.add("form-select"),a&&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 c=document.createElement("option");c.value=n.value,c.textContent=n.label,Array.isArray(r)?r.includes(n.value)&&(c.selected=!0):r!==void 0?r===n.value&&(c.selected=!0):e.default===n.value&&(c.selected=!0),t.appendChild(c)}),e.width&&(t.style.width=e.width),h=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,c)=>{let p=document.createElement("label");p.classList.add("voc-form-radio-label"),a&&p.classList.add("form-check"),p.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(p.style.width=e.width);let d=document.createElement("input");d.type="radio",d.classList.add("voc-form-radio"),a&&d.classList.add("form-check-input"),d.name=e.name||e.key||"",d.value=n.value,e.key&&(d.id=`voc-form-${e.key}-${c}`),Array.isArray(r)?r.includes(n.value)&&(d.checked=!0):r!==void 0?r===n.value&&(d.checked=!0):e.default===n.value&&(d.checked=!0),p.appendChild(d),p.appendChild(document.createTextNode(n.label)),t.appendChild(p)}),h=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"),a&&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),h=t;break}case"avatar":{h=_(e,v||{},r);break}case"audio_select":{h=H(e,v||{},r);break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),a&&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),s(t,e.validation),h=t;break}}if(h&&E.appendChild(h),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,E.appendChild(t)}return E}w.fields.forEach(e=>{if(e.type==="group"&&"items"in e){let o=e;if(a){let f=document.createElement("div");f.className="card voc-form-group-card mb-3";let r=o.label||o.header?.title,E=o.description||o.header?.description;if(r||E){let t=document.createElement("div");if(t.className="card-header voc-form-group-head",r){let n=document.createElement("h5");n.className="mb-0 voc-form-group-label",n.textContent=r,t.appendChild(n)}if(E){let n=document.createElement("p");n.className="mb-0 voc-form-group-description",n.textContent=E,t.appendChild(n)}f.appendChild(t)}let h=document.createElement("div");h.className="card-body voc-form-group-body",o.items.forEach(t=>{if(t.type==="row"&&"items"in t){let n=t,c=document.createElement("div");if(c.className="voc-form-group voc-form-row mb-3",n.label){let d=document.createElement("h6");d.className="voc-form-row-label",d.textContent=n.label,c.appendChild(d)}let p=document.createElement("div");p.className="voc-form-row-items "+(n.direction||"horizontal"),n.items.forEach(d=>{let A=F(d);A&&p.appendChild(A)}),c.appendChild(p),h.appendChild(c)}else{let n=F(t);n&&h.appendChild(n)}}),f.appendChild(h),i.appendChild(f)}else{let f=document.createElement("div");f.className="voc-form-group voc-form-group-wrapper",a&&f.classList.add("mb-3");let r=o.label||o.header?.title,E=o.description||o.header?.description;if(r||E){let h=document.createElement("div");if(h.className="voc-form-group-head",r){let t=document.createElement("h3");t.className="voc-form-group-label",t.textContent=r,h.appendChild(t)}if(E){let t=document.createElement("p");t.className="voc-form-group-description",t.textContent=E,h.appendChild(t)}f.appendChild(h)}o.items.forEach(h=>{if(h.type==="row"&&"items"in h){let t=h,n=document.createElement("div");if(n.className="voc-form-group voc-form-row",a&&n.classList.add("mb-3"),t.label){let p=document.createElement("h4");p.className="voc-form-row-label",p.textContent=t.label,n.appendChild(p)}let c=document.createElement("div");c.className="voc-form-row-items "+(t.direction||"horizontal"),t.items.forEach(p=>{let d=F(p);d&&c.appendChild(d)}),n.appendChild(c),f.appendChild(n)}else{let t=F(h);t&&f.appendChild(t)}}),i.appendChild(f)}return}if(e.type==="row"&&"items"in e){let o=document.createElement("div");if(o.className="voc-form-group voc-form-row",a&&o.classList.add("mb-3"),e.label){let r=document.createElement("h3");r.className="voc-form-row-label",r.textContent=e.label,o.appendChild(r)}if(e.description){let r=document.createElement("p");r.className="voc-form-row-description",r.textContent=e.description,o.appendChild(r)}let f=document.createElement("div");f.className="voc-form-row-items "+(e.direction||"horizontal"),o.appendChild(f),e.items.forEach(r=>{let E=F(r);E&&f.appendChild(E)}),i.appendChild(o)}else{let o=F(e);o&&i.appendChild(o)}});let C=document.createElement("button");return C.type="submit",C.className="voc-form-submit",C.textContent="Submit",a&&C.classList.add("btn","btn-primary"),i.appendChild(C),i}let l=y();b[0].appendChild(l);let g=w?.submit_config??v?.submit_config??null;return g&&l.addEventListener("submit",a=>{a.preventDefault(),B(l,g,(i,u)=>{v?.onSubmitResult&&v.onSubmitResult(i,u)})}),{element:l,validate:()=>{let a=[];return l.querySelectorAll(":invalid").forEach(u=>{let s=u.validationMessage;a.push({element:u,message:s})}),a},getValues:()=>N(l),onSubmit:a=>{l.addEventListener("submit",i=>{i.preventDefault();let u=N(l);a(u,i)})}}}var R={renderForm:q};var X=R,Y={FormsLib:R};export{X as FormsLib,Y as default};
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};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "voc-lib-js",
3
- "version": "1.0.47",
3
+ "version": "1.0.49",
4
4
  "description": "A JavaScript library for VocPhone",
5
5
  "main": "./dist/main.cjs",
6
6
  "module": "./dist/main.mjs",