voc-lib-js 1.0.48 → 1.0.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.global.js +2 -2
- package/dist/main.js +2 -2
- package/dist/main.mjs +2 -2
- package/package.json +1 -1
package/dist/main.global.js
CHANGED
|
@@ -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.message)}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((
|
|
1
|
+
"use strict";var VocLibJs=(()=>{var H=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var J=Object.getOwnPropertyNames;var U=Object.prototype.hasOwnProperty;var K=(a,E)=>{for(var y in E)H(a,y,{get:E[y],enumerable:!0})},Q=(a,E,y,g)=>{if(E&&typeof E=="object"||typeof E=="function")for(let h of J(E))!U.call(a,h)&&h!==y&&H(a,h,{get:()=>E[h],enumerable:!(g=G(E,h))||g.enumerable});return a};var X=a=>Q(H({},"__esModule",{value:!0}),a);var te={};K(te,{FormsLib:()=>Z,default:()=>ee});function D(a,E,y){let g=E?.style==="bootstrap",h=document.createElement("div");h.className="voc-form-avatar-wrapper",g&&h.classList.add("d-flex","align-items-center","gap-3");let d=document.createElement("img");d.className="voc-form-avatar-preview",d.alt=a.label||"avatar",g&&(d.classList.add("img-thumbnail","rounded-circle"),d.style.width=d.style.height="64px",d.style.objectFit="cover");let C=y!=null?String(y):a.default!==void 0&&a.default!==null?String(a.default):"";C?d.src=C:(d.src="",d.style.display="none");let o=document.createElement("input");o.type="file",o.accept=a.accept||"image/*",o.className="voc-form-avatar-input",g&&o.classList.add("form-control"),(a.key||a.name)&&(o.name=a.name||a.key,a.key&&(o.id=`voc-form-${a.key}`));let p=document.createElement("input");if(p.type="hidden",(a.key||a.name)&&(p.name=(a.name||a.key)+"_url"),p.value=C||"",o.addEventListener("change",b=>{let l=b.target;if(l.files&&l.files.length>0){let L=l.files[0],F=new FileReader;F.onload=()=>{d.src=String(F.result),d.style.display="",p.value=""},F.readAsDataURL(L)}}),g){let b=document.createElement("div");b.style.flex="1",b.appendChild(o),b.appendChild(p),h.appendChild(d),h.appendChild(b)}else h.appendChild(d),h.appendChild(o),h.appendChild(p);return h}function O(a,E,y){let g=E?.style==="bootstrap",h=document.createElement("div");if(h.className="voc-form-audio-select",!a.options||a.options.length===0)return h;let d=a.name||a.key||`audio_select_${Math.random().toString(36).substr(2,6)}`,C;return Array.isArray(y)?C=y[0]:y!==void 0?C=y:a.default!==void 0&&(C=a.default),a.options.forEach((o,p)=>{let b=document.createElement("div");b.className="voc-form-audio-item",g&&b.classList.add("form-check");let l=document.createElement("input");l.type="radio",l.name=d,l.value=o.value,l.className="voc-form-audio-radio",a.key&&(l.id=`voc-form-${a.key}-${p}`),a.required&&p===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",()=>{h.querySelectorAll(".voc-form-audio-item").forEach(n=>n.classList.remove("selected")),L()}),l.checked&&b.classList.add("selected");let F=document.createElement("label");F.className="voc-form-audio-label",g&&F.classList.add("form-check-label"),F.htmlFor=l.id||"",F.textContent=o.label||o.value,F.tabIndex=0;let e=document.createElement("div");e.className="voc-form-audio-control";let m=null;if(o.audio_link){let t=document.createElement("button");t.type="button",t.className="voc-form-audio-play",g&&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),m=document.createElement("audio"),m.src=o.audio_link,m.preload="none",m.style.display="none";let u=v=>{for(;v.firstChild;)v.removeChild(v.firstChild)},f=()=>{let v="http://www.w3.org/2000/svg",A=document.createElementNS(v,"svg");A.setAttribute("viewBox","0 0 24 24"),A.setAttribute("width","16"),A.setAttribute("height","16"),A.setAttribute("aria-hidden","true");let k=document.createElementNS(v,"path");return k.setAttribute("d","M8 5v14l11-7z"),k.setAttribute("fill","currentColor"),A.appendChild(k),A},R=()=>{let v="http://www.w3.org/2000/svg",A=document.createElementNS(v,"svg");A.setAttribute("viewBox","0 0 24 24"),A.setAttribute("width","16"),A.setAttribute("height","16"),A.setAttribute("aria-hidden","true");let k=document.createElementNS(v,"rect");return k.setAttribute("x","6"),k.setAttribute("y","6"),k.setAttribute("width","12"),k.setAttribute("height","12"),k.setAttribute("fill","currentColor"),A.appendChild(k),A};u(t),t.appendChild(f());let S=v=>{u(t),v?(t.appendChild(R()),t.setAttribute("aria-label",`Stop ${o.label||o.value}`)):(t.appendChild(f()),t.setAttribute("aria-label",`Play ${o.label||o.value}`))};t.addEventListener("click",()=>{try{if(!m)return;let v=h.querySelectorAll("audio");if(!m.paused&&!m.ended){m.pause(),m.currentTime=0;return}v.forEach(k=>{try{k.pause(),k.currentTime=0}catch{}}),h.querySelectorAll(".voc-form-audio-item").forEach(k=>k.classList.remove("is-playing")),m.play()}catch(v){console.warn("audio play failed",v)}}),m.addEventListener("playing",()=>{b.classList.add("is-playing"),S(!0)});let N=()=>{b.classList.remove("is-playing"),S(!1)};m.addEventListener("pause",N),m.addEventListener("ended",N),e.appendChild(t),e.appendChild(m)}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(F);let x=document.createElement("div");x.className="voc-form-item-description";let w=o.description||o.hint_text||"";w&&(x.textContent=w),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})))}),h.appendChild(b)}),h}function I(a,E,y){let g=document.createElement("div");g.className="voc-form-table-wrapper";let h=E?.style==="bootstrap",d=document.createElement("input");d.type="hidden",d.name=a.name||a.key,a.key&&(d.id=`voc-form-${a.key}`),g.appendChild(d);let C=document.createElement("table");C.className="voc-form-table",h&&C.classList.add("table","table-bordered");let o=document.createElement("thead"),p=document.createElement("tr");(a.fields||[]).forEach(t=>{let n=document.createElement("th");n.textContent=t.label||t.key,p.appendChild(n)});let b=document.createElement("th");b.textContent="Actions",p.appendChild(b),o.appendChild(p),C.appendChild(o);let l=document.createElement("tbody");C.appendChild(l),g.appendChild(C);let L=document.createElement("button");L.type="button",L.className="voc-form-table-add",h&&L.classList.add("btn","btn-secondary"),L.textContent="+Add";let F=document.createElement("tfoot"),e=document.createElement("tr"),m=document.createElement("td");m.colSpan=(a.fields?a.fields.length:0)+1,m.appendChild(L),e.appendChild(m),F.appendChild(e),C.appendChild(F),g.appendChild(C);let c=[];function i(){d.value=JSON.stringify(c)}function x(t){let n=[];return a.fields.forEach(r=>{let s=t[r.key];if(r.type==="checkbox"){let u=Array.isArray(s)?s:s?[s]:[];r.required&&u.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 w(){l.innerHTML="",c.forEach((t,n)=>{let r=document.createElement("tr");a.fields.forEach(f=>{let R=document.createElement("td"),S,N=(f.type||"text").toLowerCase();if(N==="select"){let v=document.createElement("select");if(v.className="voc-form-table-cell",h&&v.classList.add("form-control"),f.placeholder){let k=document.createElement("option");k.value="",k.textContent=f.placeholder,k.disabled=!0,(t[f.key]===void 0||t[f.key]===null||t[f.key]==="")&&(k.selected=!0),v.appendChild(k)}(f.options||[]).forEach(k=>{let _=document.createElement("option");_.value=k.value,_.textContent=k.label,v.appendChild(_)}),v.value=t[f.key]!==void 0&&t[f.key]!==null?String(t[f.key]):"",f.required&&(v.required=!0),v.addEventListener("change",k=>{c[n][f.key]=k.target.value,i()}),S=v}else if(N==="checkbox"){let v=document.createElement("div");v.className="voc-form-table-checkboxes",h&&v.classList.add("form-check");let A=f.options||[];Array.isArray(c[n][f.key])||(c[n][f.key]=c[n][f.key]?[c[n][f.key]]:[]),A.forEach(k=>{let _=`voc-form-${a.key}-${f.key}-${n}-${k.value}`,q=document.createElement("div");q.className="voc-form-table-checkbox-item",h&&q.classList.add("form-check");let T=document.createElement("input");T.type="checkbox",T.className="voc-form-table-cell",h&&T.classList.add("form-check-input"),T.id=_,T.value=k.value,T.checked=Array.isArray(c[n][f.key])&&c[n][f.key].includes(k.value),T.addEventListener("change",z=>{let P=z.target.checked;Array.isArray(c[n][f.key])||(c[n][f.key]=[]),P?c[n][f.key].includes(k.value)||c[n][f.key].push(k.value):c[n][f.key]=c[n][f.key].filter(V=>V!==k.value),i()});let M=document.createElement("label");M.htmlFor=_,M.className="voc-form-table-checkbox-label",h&&M.classList.add("form-check-label"),M.textContent=k.label,q.appendChild(T),q.appendChild(M),v.appendChild(q)}),S=v}else{let v=document.createElement("input");N==="number"?v.type="number":N==="email"?v.type="email":N==="tel"?v.type="tel":N==="url"?v.type="url":N==="date"?v.type="date":v.type="text",v.value=t[f.key]!==void 0&&t[f.key]!==null?String(t[f.key]):"",f.placeholder&&(v.placeholder=f.placeholder),f.required&&(v.required=!0),v.className="voc-form-table-cell",h&&v.classList.add("form-control"),v.addEventListener("input",A=>{c[n][f.key]=A.target.value,i()}),S=v}R.appendChild(S),r.appendChild(R)});let s=document.createElement("td"),u=document.createElement("button");u.type="button",u.className="voc-form-table-remove",h&&u.classList.add("btn","btn-danger"),u.textContent="Remove",u.addEventListener("click",()=>{c.splice(n,1),w(),i()}),s.appendChild(u),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),w(),i()}),Array.isArray(y)&&y.length?c=y.map(t=>({...t})):a.default&&Array.isArray(a.default)&&(c=a.default.map(t=>({...t}))),w(),i(),g.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,u)=>{let f=s[n];f!=null&&(r.has(f)&&t.push(`Field ${n} must be unique (duplicate at row ${u+1})`),r.add(f))})}),t},g}function $(a,E=!0){let y=new FormData(a);if(E)return Y(y);let g={};return y.forEach((h,d)=>{let C=(h instanceof File,h);Object.prototype.hasOwnProperty.call(g,d)?Array.isArray(g[d])?g[d].push(C):g[d]=[g[d],C]:g[d]=C}),g}function Y(a){let E=new FormData;for(let[y,g]of a.entries()){if(g instanceof File){E.append(y,g);continue}let h;try{h=JSON.parse(g)}catch{h=null}Array.isArray(h)&&h.every(d=>typeof d=="object"&&d!==null)?h.forEach((d,C)=>{for(let[o,p]of Object.entries(d))E.append(`${y}[${C}][${o}]`,p)}):E.append(y,g)}return E}async function j(a,E,y){let g=E.url,h={"Content-Type":"application/json"};E?.api_token&&(h.Authorization=`Bearer ${E.api_token}`);let d=$(a);if(E?.additional_data)for(let[p,b]of Object.entries(E.additional_data))d.append(p,b);let C,o=d instanceof FormData;C={method:"POST",headers:{Authorization:`Bearer ${E.api_token}`,...o?{}:{"Content-Type":"application/json"},Accept:"application/json"},body:o?d:JSON.stringify(Object.fromEntries(d.entries()))};try{fetch(g,C).then(async p=>{let b=p.headers.get("content-type");if(!p.ok)try{let L=(await p.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")?p.json():p.text()}).then(p=>{y&&y(p,null)}).catch(p=>{y&&y(null,p)})}catch(p){y&&y(null,p)}}function W(a,E,y){let g=[];if(typeof a=="string"?g=Array.from(document.querySelectorAll(a)):g=[a],g.length===0)return null;function h(){let o=y?.style==="bootstrap",p=document.createElement("form");p.classList.add("voc-form"),o&&p.classList.add("needs-validation"),p.id=E.id;let b=document.createElement("h2");b.className="voc-form-title",o&&b.classList.add("mb-3"),b.textContent=E.form_name,p.appendChild(b);function l(e,m){m&&(m.min_length!==void 0&&(e.minLength=m.min_length),m.max_length!==void 0&&(e.maxLength=m.max_length),m.regex&&(e instanceof HTMLInputElement?e.pattern=m.regex:e.dataset.pattern=m.regex))}function L(e){if(!e.key&&!e.label)return null;let m=E.default_values||{},c=e.name||e.key,i=c?m[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 w=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 u=document.createElement("input");u.type="checkbox",u.classList.add("voc-form-checkbox"),o&&u.classList.add("form-check-input"),u.name=e.name||e.key||"",u.value=n.value,e.key&&(u.id=`voc-form-${e.key}-${r}`),e.required&&(u.required=!0),Array.isArray(i)?i.includes(n.value)&&(u.checked=!0):i!==void 0?i===n.value&&(u.checked=!0):e.default===n.value&&(u.checked=!0),s.appendChild(u),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)}w=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,u=n.split(`
|
|
2
|
+
`).reduce((S,N)=>Math.max(S,N.length),0),f=Math.ceil(u/s)||1,R=Math.max(2,r+f);t.rows=R,t.style.resize="vertical"}e.width&&(t.style.width=e.width),e.required&&(t.required=!0),l(t,e.validation),w=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),w=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 u=document.createElement("input");u.type="radio",u.classList.add("voc-form-radio"),o&&u.classList.add("form-check-input"),u.name=e.name||e.key||"",u.value=n.value,e.key&&(u.id=`voc-form-${e.key}-${r}`),Array.isArray(i)?i.includes(n.value)&&(u.checked=!0):i!==void 0?i===n.value&&(u.checked=!0):e.default===n.value&&(u.checked=!0),s.appendChild(u),s.appendChild(document.createTextNode(n.label)),t.appendChild(s)}),w=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),w=t;break}case"avatar":{w=D(e,y||{},i);break}case"table":{w=I(e,y||{},i);break}case"audio_select":{w=O(e,y||{},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),w=t;break}}if(w&&x.appendChild(w),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,x.appendChild(t)}return x}E.fields.forEach(e=>{if(e.type==="group"&&"items"in e){let m=e;if(o){let c=document.createElement("div");c.className="card voc-form-group-card mb-3";let i=m.label||m.header?.title,x=m.description||m.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 w=document.createElement("div");w.className="card-body voc-form-group-body",m.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 u=document.createElement("h6");u.className="voc-form-row-label",u.textContent=n.label,r.appendChild(u)}let s=document.createElement("div");s.className="voc-form-row-items "+(n.direction||"horizontal"),n.items.forEach(u=>{let f=L(u);f&&s.appendChild(f)}),r.appendChild(s),w.appendChild(r)}else{let n=L(t);n&&w.appendChild(n)}}),c.appendChild(w),p.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=m.label||m.header?.title,x=m.description||m.header?.description;if(i||x){let w=document.createElement("div");if(w.className="voc-form-group-head",i){let t=document.createElement("h3");t.className="voc-form-group-label",t.textContent=i,w.appendChild(t)}if(x){let t=document.createElement("p");t.className="voc-form-group-description",t.textContent=x,w.appendChild(t)}c.appendChild(w)}m.items.forEach(w=>{if(w.type==="row"&&"items"in w){let t=w,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 u=L(s);u&&r.appendChild(u)}),n.appendChild(r),c.appendChild(n)}else{let t=L(w);t&&c.appendChild(t)}}),p.appendChild(c)}return}if(e.type==="row"&&"items"in e){let m=document.createElement("div");if(m.className="voc-form-group voc-form-row",o&&m.classList.add("mb-3"),e.label){let i=document.createElement("h3");i.className="voc-form-row-label",i.textContent=e.label,m.appendChild(i)}if(e.description){let i=document.createElement("p");i.className="voc-form-row-description",i.textContent=e.description,m.appendChild(i)}let c=document.createElement("div");c.className="voc-form-row-items "+(e.direction||"horizontal"),m.appendChild(c),e.items.forEach(i=>{let x=L(i);x&&c.appendChild(x)}),p.appendChild(m)}else{let m=L(e);m&&p.appendChild(m)}});let F=document.createElement("button");return F.type="submit",F.className="voc-form-submit",F.textContent="Submit",o&&F.classList.add("btn","btn-primary"),p.appendChild(F),p}let d=h();g[0].appendChild(d);let C=E?.submit_config??y?.submit_config??null;return C&&d.addEventListener("submit",o=>{if(o.preventDefault(),!d.checkValidity()){d.reportValidity(),y?.onSubmitResult&&y.onSubmitResult(null,new Error("Form validation failed"));return}let p=Array.from(d.querySelectorAll(" .voc-form-table-wrapper, .voc-form-table-wrapper")),b=[];if(p.forEach(l=>{if(l&&typeof l.validate=="function"){let L=l.validate();L&&L.length&&b.push(...L)}}),b.length){y?.onSubmitResult&&y.onSubmitResult(null,new Error(b.join("; ")));let l=p[0];l&&l.scrollIntoView({behavior:"smooth",block:"center"});return}j(d,C,(l,L)=>{y?.onSubmitResult&&y.onSubmitResult(l,L)})}),{element:d,validate:()=>{let o=[];return d.querySelectorAll(":invalid").forEach(b=>{let l=b.validationMessage;o.push({element:b,message:l})}),o},getValues:()=>$(d),onSubmit:o=>{d.addEventListener("submit",p=>{p.preventDefault();let b=$(d);o(b,p)})}}}var B={renderForm:W};var Z=B,ee={FormsLib:B};return X(te);})();
|
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.message)}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((
|
|
1
|
+
"use strict";var H=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var J=Object.getOwnPropertyNames;var U=Object.prototype.hasOwnProperty;var K=(a,E)=>{for(var y in E)H(a,y,{get:E[y],enumerable:!0})},Q=(a,E,y,g)=>{if(E&&typeof E=="object"||typeof E=="function")for(let h of J(E))!U.call(a,h)&&h!==y&&H(a,h,{get:()=>E[h],enumerable:!(g=G(E,h))||g.enumerable});return a};var X=a=>Q(H({},"__esModule",{value:!0}),a);var te={};K(te,{FormsLib:()=>Z,default:()=>ee});module.exports=X(te);function D(a,E,y){let g=E?.style==="bootstrap",h=document.createElement("div");h.className="voc-form-avatar-wrapper",g&&h.classList.add("d-flex","align-items-center","gap-3");let d=document.createElement("img");d.className="voc-form-avatar-preview",d.alt=a.label||"avatar",g&&(d.classList.add("img-thumbnail","rounded-circle"),d.style.width=d.style.height="64px",d.style.objectFit="cover");let C=y!=null?String(y):a.default!==void 0&&a.default!==null?String(a.default):"";C?d.src=C:(d.src="",d.style.display="none");let o=document.createElement("input");o.type="file",o.accept=a.accept||"image/*",o.className="voc-form-avatar-input",g&&o.classList.add("form-control"),(a.key||a.name)&&(o.name=a.name||a.key,a.key&&(o.id=`voc-form-${a.key}`));let p=document.createElement("input");if(p.type="hidden",(a.key||a.name)&&(p.name=(a.name||a.key)+"_url"),p.value=C||"",o.addEventListener("change",b=>{let l=b.target;if(l.files&&l.files.length>0){let L=l.files[0],F=new FileReader;F.onload=()=>{d.src=String(F.result),d.style.display="",p.value=""},F.readAsDataURL(L)}}),g){let b=document.createElement("div");b.style.flex="1",b.appendChild(o),b.appendChild(p),h.appendChild(d),h.appendChild(b)}else h.appendChild(d),h.appendChild(o),h.appendChild(p);return h}function O(a,E,y){let g=E?.style==="bootstrap",h=document.createElement("div");if(h.className="voc-form-audio-select",!a.options||a.options.length===0)return h;let d=a.name||a.key||`audio_select_${Math.random().toString(36).substr(2,6)}`,C;return Array.isArray(y)?C=y[0]:y!==void 0?C=y:a.default!==void 0&&(C=a.default),a.options.forEach((o,p)=>{let b=document.createElement("div");b.className="voc-form-audio-item",g&&b.classList.add("form-check");let l=document.createElement("input");l.type="radio",l.name=d,l.value=o.value,l.className="voc-form-audio-radio",a.key&&(l.id=`voc-form-${a.key}-${p}`),a.required&&p===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",()=>{h.querySelectorAll(".voc-form-audio-item").forEach(n=>n.classList.remove("selected")),L()}),l.checked&&b.classList.add("selected");let F=document.createElement("label");F.className="voc-form-audio-label",g&&F.classList.add("form-check-label"),F.htmlFor=l.id||"",F.textContent=o.label||o.value,F.tabIndex=0;let e=document.createElement("div");e.className="voc-form-audio-control";let m=null;if(o.audio_link){let t=document.createElement("button");t.type="button",t.className="voc-form-audio-play",g&&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),m=document.createElement("audio"),m.src=o.audio_link,m.preload="none",m.style.display="none";let u=v=>{for(;v.firstChild;)v.removeChild(v.firstChild)},f=()=>{let v="http://www.w3.org/2000/svg",A=document.createElementNS(v,"svg");A.setAttribute("viewBox","0 0 24 24"),A.setAttribute("width","16"),A.setAttribute("height","16"),A.setAttribute("aria-hidden","true");let k=document.createElementNS(v,"path");return k.setAttribute("d","M8 5v14l11-7z"),k.setAttribute("fill","currentColor"),A.appendChild(k),A},R=()=>{let v="http://www.w3.org/2000/svg",A=document.createElementNS(v,"svg");A.setAttribute("viewBox","0 0 24 24"),A.setAttribute("width","16"),A.setAttribute("height","16"),A.setAttribute("aria-hidden","true");let k=document.createElementNS(v,"rect");return k.setAttribute("x","6"),k.setAttribute("y","6"),k.setAttribute("width","12"),k.setAttribute("height","12"),k.setAttribute("fill","currentColor"),A.appendChild(k),A};u(t),t.appendChild(f());let S=v=>{u(t),v?(t.appendChild(R()),t.setAttribute("aria-label",`Stop ${o.label||o.value}`)):(t.appendChild(f()),t.setAttribute("aria-label",`Play ${o.label||o.value}`))};t.addEventListener("click",()=>{try{if(!m)return;let v=h.querySelectorAll("audio");if(!m.paused&&!m.ended){m.pause(),m.currentTime=0;return}v.forEach(k=>{try{k.pause(),k.currentTime=0}catch{}}),h.querySelectorAll(".voc-form-audio-item").forEach(k=>k.classList.remove("is-playing")),m.play()}catch(v){console.warn("audio play failed",v)}}),m.addEventListener("playing",()=>{b.classList.add("is-playing"),S(!0)});let N=()=>{b.classList.remove("is-playing"),S(!1)};m.addEventListener("pause",N),m.addEventListener("ended",N),e.appendChild(t),e.appendChild(m)}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(F);let x=document.createElement("div");x.className="voc-form-item-description";let w=o.description||o.hint_text||"";w&&(x.textContent=w),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})))}),h.appendChild(b)}),h}function I(a,E,y){let g=document.createElement("div");g.className="voc-form-table-wrapper";let h=E?.style==="bootstrap",d=document.createElement("input");d.type="hidden",d.name=a.name||a.key,a.key&&(d.id=`voc-form-${a.key}`),g.appendChild(d);let C=document.createElement("table");C.className="voc-form-table",h&&C.classList.add("table","table-bordered");let o=document.createElement("thead"),p=document.createElement("tr");(a.fields||[]).forEach(t=>{let n=document.createElement("th");n.textContent=t.label||t.key,p.appendChild(n)});let b=document.createElement("th");b.textContent="Actions",p.appendChild(b),o.appendChild(p),C.appendChild(o);let l=document.createElement("tbody");C.appendChild(l),g.appendChild(C);let L=document.createElement("button");L.type="button",L.className="voc-form-table-add",h&&L.classList.add("btn","btn-secondary"),L.textContent="+Add";let F=document.createElement("tfoot"),e=document.createElement("tr"),m=document.createElement("td");m.colSpan=(a.fields?a.fields.length:0)+1,m.appendChild(L),e.appendChild(m),F.appendChild(e),C.appendChild(F),g.appendChild(C);let c=[];function i(){d.value=JSON.stringify(c)}function x(t){let n=[];return a.fields.forEach(r=>{let s=t[r.key];if(r.type==="checkbox"){let u=Array.isArray(s)?s:s?[s]:[];r.required&&u.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 w(){l.innerHTML="",c.forEach((t,n)=>{let r=document.createElement("tr");a.fields.forEach(f=>{let R=document.createElement("td"),S,N=(f.type||"text").toLowerCase();if(N==="select"){let v=document.createElement("select");if(v.className="voc-form-table-cell",h&&v.classList.add("form-control"),f.placeholder){let k=document.createElement("option");k.value="",k.textContent=f.placeholder,k.disabled=!0,(t[f.key]===void 0||t[f.key]===null||t[f.key]==="")&&(k.selected=!0),v.appendChild(k)}(f.options||[]).forEach(k=>{let _=document.createElement("option");_.value=k.value,_.textContent=k.label,v.appendChild(_)}),v.value=t[f.key]!==void 0&&t[f.key]!==null?String(t[f.key]):"",f.required&&(v.required=!0),v.addEventListener("change",k=>{c[n][f.key]=k.target.value,i()}),S=v}else if(N==="checkbox"){let v=document.createElement("div");v.className="voc-form-table-checkboxes",h&&v.classList.add("form-check");let A=f.options||[];Array.isArray(c[n][f.key])||(c[n][f.key]=c[n][f.key]?[c[n][f.key]]:[]),A.forEach(k=>{let _=`voc-form-${a.key}-${f.key}-${n}-${k.value}`,q=document.createElement("div");q.className="voc-form-table-checkbox-item",h&&q.classList.add("form-check");let T=document.createElement("input");T.type="checkbox",T.className="voc-form-table-cell",h&&T.classList.add("form-check-input"),T.id=_,T.value=k.value,T.checked=Array.isArray(c[n][f.key])&&c[n][f.key].includes(k.value),T.addEventListener("change",z=>{let P=z.target.checked;Array.isArray(c[n][f.key])||(c[n][f.key]=[]),P?c[n][f.key].includes(k.value)||c[n][f.key].push(k.value):c[n][f.key]=c[n][f.key].filter(V=>V!==k.value),i()});let M=document.createElement("label");M.htmlFor=_,M.className="voc-form-table-checkbox-label",h&&M.classList.add("form-check-label"),M.textContent=k.label,q.appendChild(T),q.appendChild(M),v.appendChild(q)}),S=v}else{let v=document.createElement("input");N==="number"?v.type="number":N==="email"?v.type="email":N==="tel"?v.type="tel":N==="url"?v.type="url":N==="date"?v.type="date":v.type="text",v.value=t[f.key]!==void 0&&t[f.key]!==null?String(t[f.key]):"",f.placeholder&&(v.placeholder=f.placeholder),f.required&&(v.required=!0),v.className="voc-form-table-cell",h&&v.classList.add("form-control"),v.addEventListener("input",A=>{c[n][f.key]=A.target.value,i()}),S=v}R.appendChild(S),r.appendChild(R)});let s=document.createElement("td"),u=document.createElement("button");u.type="button",u.className="voc-form-table-remove",h&&u.classList.add("btn","btn-danger"),u.textContent="Remove",u.addEventListener("click",()=>{c.splice(n,1),w(),i()}),s.appendChild(u),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),w(),i()}),Array.isArray(y)&&y.length?c=y.map(t=>({...t})):a.default&&Array.isArray(a.default)&&(c=a.default.map(t=>({...t}))),w(),i(),g.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,u)=>{let f=s[n];f!=null&&(r.has(f)&&t.push(`Field ${n} must be unique (duplicate at row ${u+1})`),r.add(f))})}),t},g}function $(a,E=!0){let y=new FormData(a);if(E)return Y(y);let g={};return y.forEach((h,d)=>{let C=(h instanceof File,h);Object.prototype.hasOwnProperty.call(g,d)?Array.isArray(g[d])?g[d].push(C):g[d]=[g[d],C]:g[d]=C}),g}function Y(a){let E=new FormData;for(let[y,g]of a.entries()){if(g instanceof File){E.append(y,g);continue}let h;try{h=JSON.parse(g)}catch{h=null}Array.isArray(h)&&h.every(d=>typeof d=="object"&&d!==null)?h.forEach((d,C)=>{for(let[o,p]of Object.entries(d))E.append(`${y}[${C}][${o}]`,p)}):E.append(y,g)}return E}async function j(a,E,y){let g=E.url,h={"Content-Type":"application/json"};E?.api_token&&(h.Authorization=`Bearer ${E.api_token}`);let d=$(a);if(E?.additional_data)for(let[p,b]of Object.entries(E.additional_data))d.append(p,b);let C,o=d instanceof FormData;C={method:"POST",headers:{Authorization:`Bearer ${E.api_token}`,...o?{}:{"Content-Type":"application/json"},Accept:"application/json"},body:o?d:JSON.stringify(Object.fromEntries(d.entries()))};try{fetch(g,C).then(async p=>{let b=p.headers.get("content-type");if(!p.ok)try{let L=(await p.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")?p.json():p.text()}).then(p=>{y&&y(p,null)}).catch(p=>{y&&y(null,p)})}catch(p){y&&y(null,p)}}function W(a,E,y){let g=[];if(typeof a=="string"?g=Array.from(document.querySelectorAll(a)):g=[a],g.length===0)return null;function h(){let o=y?.style==="bootstrap",p=document.createElement("form");p.classList.add("voc-form"),o&&p.classList.add("needs-validation"),p.id=E.id;let b=document.createElement("h2");b.className="voc-form-title",o&&b.classList.add("mb-3"),b.textContent=E.form_name,p.appendChild(b);function l(e,m){m&&(m.min_length!==void 0&&(e.minLength=m.min_length),m.max_length!==void 0&&(e.maxLength=m.max_length),m.regex&&(e instanceof HTMLInputElement?e.pattern=m.regex:e.dataset.pattern=m.regex))}function L(e){if(!e.key&&!e.label)return null;let m=E.default_values||{},c=e.name||e.key,i=c?m[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 w=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 u=document.createElement("input");u.type="checkbox",u.classList.add("voc-form-checkbox"),o&&u.classList.add("form-check-input"),u.name=e.name||e.key||"",u.value=n.value,e.key&&(u.id=`voc-form-${e.key}-${r}`),e.required&&(u.required=!0),Array.isArray(i)?i.includes(n.value)&&(u.checked=!0):i!==void 0?i===n.value&&(u.checked=!0):e.default===n.value&&(u.checked=!0),s.appendChild(u),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)}w=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,u=n.split(`
|
|
2
|
+
`).reduce((S,N)=>Math.max(S,N.length),0),f=Math.ceil(u/s)||1,R=Math.max(2,r+f);t.rows=R,t.style.resize="vertical"}e.width&&(t.style.width=e.width),e.required&&(t.required=!0),l(t,e.validation),w=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),w=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 u=document.createElement("input");u.type="radio",u.classList.add("voc-form-radio"),o&&u.classList.add("form-check-input"),u.name=e.name||e.key||"",u.value=n.value,e.key&&(u.id=`voc-form-${e.key}-${r}`),Array.isArray(i)?i.includes(n.value)&&(u.checked=!0):i!==void 0?i===n.value&&(u.checked=!0):e.default===n.value&&(u.checked=!0),s.appendChild(u),s.appendChild(document.createTextNode(n.label)),t.appendChild(s)}),w=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),w=t;break}case"avatar":{w=D(e,y||{},i);break}case"table":{w=I(e,y||{},i);break}case"audio_select":{w=O(e,y||{},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),w=t;break}}if(w&&x.appendChild(w),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,x.appendChild(t)}return x}E.fields.forEach(e=>{if(e.type==="group"&&"items"in e){let m=e;if(o){let c=document.createElement("div");c.className="card voc-form-group-card mb-3";let i=m.label||m.header?.title,x=m.description||m.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 w=document.createElement("div");w.className="card-body voc-form-group-body",m.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 u=document.createElement("h6");u.className="voc-form-row-label",u.textContent=n.label,r.appendChild(u)}let s=document.createElement("div");s.className="voc-form-row-items "+(n.direction||"horizontal"),n.items.forEach(u=>{let f=L(u);f&&s.appendChild(f)}),r.appendChild(s),w.appendChild(r)}else{let n=L(t);n&&w.appendChild(n)}}),c.appendChild(w),p.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=m.label||m.header?.title,x=m.description||m.header?.description;if(i||x){let w=document.createElement("div");if(w.className="voc-form-group-head",i){let t=document.createElement("h3");t.className="voc-form-group-label",t.textContent=i,w.appendChild(t)}if(x){let t=document.createElement("p");t.className="voc-form-group-description",t.textContent=x,w.appendChild(t)}c.appendChild(w)}m.items.forEach(w=>{if(w.type==="row"&&"items"in w){let t=w,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 u=L(s);u&&r.appendChild(u)}),n.appendChild(r),c.appendChild(n)}else{let t=L(w);t&&c.appendChild(t)}}),p.appendChild(c)}return}if(e.type==="row"&&"items"in e){let m=document.createElement("div");if(m.className="voc-form-group voc-form-row",o&&m.classList.add("mb-3"),e.label){let i=document.createElement("h3");i.className="voc-form-row-label",i.textContent=e.label,m.appendChild(i)}if(e.description){let i=document.createElement("p");i.className="voc-form-row-description",i.textContent=e.description,m.appendChild(i)}let c=document.createElement("div");c.className="voc-form-row-items "+(e.direction||"horizontal"),m.appendChild(c),e.items.forEach(i=>{let x=L(i);x&&c.appendChild(x)}),p.appendChild(m)}else{let m=L(e);m&&p.appendChild(m)}});let F=document.createElement("button");return F.type="submit",F.className="voc-form-submit",F.textContent="Submit",o&&F.classList.add("btn","btn-primary"),p.appendChild(F),p}let d=h();g[0].appendChild(d);let C=E?.submit_config??y?.submit_config??null;return C&&d.addEventListener("submit",o=>{if(o.preventDefault(),!d.checkValidity()){d.reportValidity(),y?.onSubmitResult&&y.onSubmitResult(null,new Error("Form validation failed"));return}let p=Array.from(d.querySelectorAll(" .voc-form-table-wrapper, .voc-form-table-wrapper")),b=[];if(p.forEach(l=>{if(l&&typeof l.validate=="function"){let L=l.validate();L&&L.length&&b.push(...L)}}),b.length){y?.onSubmitResult&&y.onSubmitResult(null,new Error(b.join("; ")));let l=p[0];l&&l.scrollIntoView({behavior:"smooth",block:"center"});return}j(d,C,(l,L)=>{y?.onSubmitResult&&y.onSubmitResult(l,L)})}),{element:d,validate:()=>{let o=[];return d.querySelectorAll(":invalid").forEach(b=>{let l=b.validationMessage;o.push({element:b,message:l})}),o},getValues:()=>$(d),onSubmit:o=>{d.addEventListener("submit",p=>{p.preventDefault();let b=$(d);o(b,p)})}}}var B={renderForm:W};var Z=B,ee={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.message)}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((
|
|
1
|
+
function B(r,C,b){let g=C?.style==="bootstrap",y=document.createElement("div");y.className="voc-form-avatar-wrapper",g&&y.classList.add("d-flex","align-items-center","gap-3");let d=document.createElement("img");d.className="voc-form-avatar-preview",d.alt=r.label||"avatar",g&&(d.classList.add("img-thumbnail","rounded-circle"),d.style.width=d.style.height="64px",d.style.objectFit="cover");let w=b!=null?String(b):r.default!==void 0&&r.default!==null?String(r.default):"";w?d.src=w:(d.src="",d.style.display="none");let o=document.createElement("input");o.type="file",o.accept=r.accept||"image/*",o.className="voc-form-avatar-input",g&&o.classList.add("form-control"),(r.key||r.name)&&(o.name=r.name||r.key,r.key&&(o.id=`voc-form-${r.key}`));let p=document.createElement("input");if(p.type="hidden",(r.key||r.name)&&(p.name=(r.name||r.key)+"_url"),p.value=w||"",o.addEventListener("change",v=>{let l=v.target;if(l.files&&l.files.length>0){let L=l.files[0],F=new FileReader;F.onload=()=>{d.src=String(F.result),d.style.display="",p.value=""},F.readAsDataURL(L)}}),g){let v=document.createElement("div");v.style.flex="1",v.appendChild(o),v.appendChild(p),y.appendChild(d),y.appendChild(v)}else y.appendChild(d),y.appendChild(o),y.appendChild(p);return y}function D(r,C,b){let g=C?.style==="bootstrap",y=document.createElement("div");if(y.className="voc-form-audio-select",!r.options||r.options.length===0)return y;let d=r.name||r.key||`audio_select_${Math.random().toString(36).substr(2,6)}`,w;return Array.isArray(b)?w=b[0]:b!==void 0?w=b:r.default!==void 0&&(w=r.default),r.options.forEach((o,p)=>{let v=document.createElement("div");v.className="voc-form-audio-item",g&&v.classList.add("form-check");let l=document.createElement("input");l.type="radio",l.name=d,l.value=o.value,l.className="voc-form-audio-radio",r.key&&(l.id=`voc-form-${r.key}-${p}`),r.required&&p===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 L=()=>{l.checked?v.classList.add("selected"):v.classList.remove("selected")};l.addEventListener("change",()=>{y.querySelectorAll(".voc-form-audio-item").forEach(n=>n.classList.remove("selected")),L()}),l.checked&&v.classList.add("selected");let F=document.createElement("label");F.className="voc-form-audio-label",g&&F.classList.add("form-check-label"),F.htmlFor=l.id||"",F.textContent=o.label||o.value,F.tabIndex=0;let e=document.createElement("div");e.className="voc-form-audio-control";let m=null;if(o.audio_link){let t=document.createElement("button");t.type="button",t.className="voc-form-audio-play",g&&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),m=document.createElement("audio"),m.src=o.audio_link,m.preload="none",m.style.display="none";let u=f=>{for(;f.firstChild;)f.removeChild(f.firstChild)},h=()=>{let f="http://www.w3.org/2000/svg",A=document.createElementNS(f,"svg");A.setAttribute("viewBox","0 0 24 24"),A.setAttribute("width","16"),A.setAttribute("height","16"),A.setAttribute("aria-hidden","true");let E=document.createElementNS(f,"path");return E.setAttribute("d","M8 5v14l11-7z"),E.setAttribute("fill","currentColor"),A.appendChild(E),A},R=()=>{let f="http://www.w3.org/2000/svg",A=document.createElementNS(f,"svg");A.setAttribute("viewBox","0 0 24 24"),A.setAttribute("width","16"),A.setAttribute("height","16"),A.setAttribute("aria-hidden","true");let E=document.createElementNS(f,"rect");return E.setAttribute("x","6"),E.setAttribute("y","6"),E.setAttribute("width","12"),E.setAttribute("height","12"),E.setAttribute("fill","currentColor"),A.appendChild(E),A};u(t),t.appendChild(h());let S=f=>{u(t),f?(t.appendChild(R()),t.setAttribute("aria-label",`Stop ${o.label||o.value}`)):(t.appendChild(h()),t.setAttribute("aria-label",`Play ${o.label||o.value}`))};t.addEventListener("click",()=>{try{if(!m)return;let f=y.querySelectorAll("audio");if(!m.paused&&!m.ended){m.pause(),m.currentTime=0;return}f.forEach(E=>{try{E.pause(),E.currentTime=0}catch{}}),y.querySelectorAll(".voc-form-audio-item").forEach(E=>E.classList.remove("is-playing")),m.play()}catch(f){console.warn("audio play failed",f)}}),m.addEventListener("playing",()=>{v.classList.add("is-playing"),S(!0)});let N=()=>{v.classList.remove("is-playing"),S(!1)};m.addEventListener("pause",N),m.addEventListener("ended",N),e.appendChild(t),e.appendChild(m)}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(F);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),v.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})))}),v.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(v)}),y}function O(r,C,b){let g=document.createElement("div");g.className="voc-form-table-wrapper";let y=C?.style==="bootstrap",d=document.createElement("input");d.type="hidden",d.name=r.name||r.key,r.key&&(d.id=`voc-form-${r.key}`),g.appendChild(d);let w=document.createElement("table");w.className="voc-form-table",y&&w.classList.add("table","table-bordered");let o=document.createElement("thead"),p=document.createElement("tr");(r.fields||[]).forEach(t=>{let n=document.createElement("th");n.textContent=t.label||t.key,p.appendChild(n)});let v=document.createElement("th");v.textContent="Actions",p.appendChild(v),o.appendChild(p),w.appendChild(o);let l=document.createElement("tbody");w.appendChild(l),g.appendChild(w);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 F=document.createElement("tfoot"),e=document.createElement("tr"),m=document.createElement("td");m.colSpan=(r.fields?r.fields.length:0)+1,m.appendChild(L),e.appendChild(m),F.appendChild(e),w.appendChild(F),g.appendChild(w);let c=[];function i(){d.value=JSON.stringify(c)}function x(t){let n=[];return r.fields.forEach(a=>{let s=t[a.key];if(a.type==="checkbox"){let u=Array.isArray(s)?s:s?[s]:[];a.required&&u.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 k(){l.innerHTML="",c.forEach((t,n)=>{let a=document.createElement("tr");r.fields.forEach(h=>{let R=document.createElement("td"),S,N=(h.type||"text").toLowerCase();if(N==="select"){let f=document.createElement("select");if(f.className="voc-form-table-cell",y&&f.classList.add("form-control"),h.placeholder){let E=document.createElement("option");E.value="",E.textContent=h.placeholder,E.disabled=!0,(t[h.key]===void 0||t[h.key]===null||t[h.key]==="")&&(E.selected=!0),f.appendChild(E)}(h.options||[]).forEach(E=>{let _=document.createElement("option");_.value=E.value,_.textContent=E.label,f.appendChild(_)}),f.value=t[h.key]!==void 0&&t[h.key]!==null?String(t[h.key]):"",h.required&&(f.required=!0),f.addEventListener("change",E=>{c[n][h.key]=E.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 A=h.options||[];Array.isArray(c[n][h.key])||(c[n][h.key]=c[n][h.key]?[c[n][h.key]]:[]),A.forEach(E=>{let _=`voc-form-${r.key}-${h.key}-${n}-${E.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=E.value,T.checked=Array.isArray(c[n][h.key])&&c[n][h.key].includes(E.value),T.addEventListener("change",W=>{let z=W.target.checked;Array.isArray(c[n][h.key])||(c[n][h.key]=[]),z?c[n][h.key].includes(E.value)||c[n][h.key].push(E.value):c[n][h.key]=c[n][h.key].filter(P=>P!==E.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=E.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[h.key]!==void 0&&t[h.key]!==null?String(t[h.key]):"",h.placeholder&&(f.placeholder=h.placeholder),h.required&&(f.required=!0),f.className="voc-form-table-cell",y&&f.classList.add("form-control"),f.addEventListener("input",A=>{c[n][h.key]=A.target.value,i()}),S=f}R.appendChild(S),a.appendChild(R)});let s=document.createElement("td"),u=document.createElement("button");u.type="button",u.className="voc-form-table-remove",y&&u.classList.add("btn","btn-danger"),u.textContent="Remove",u.addEventListener("click",()=>{c.splice(n,1),k(),i()}),s.appendChild(u),a.appendChild(s),l.appendChild(a)})}return L.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),k(),i()}),Array.isArray(b)&&b.length?c=b.map(t=>({...t})):r.default&&Array.isArray(r.default)&&(c=r.default.map(t=>({...t}))),k(),i(),g.validate=function(){let t=[];return c.forEach((n,a)=>{let s=x(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,u)=>{let h=s[n];h!=null&&(a.has(h)&&t.push(`Field ${n} must be unique (duplicate at row ${u+1})`),a.add(h))})}),t},g}function $(r,C=!0){let b=new FormData(r);if(C)return V(b);let g={};return b.forEach((y,d)=>{let w=(y instanceof File,y);Object.prototype.hasOwnProperty.call(g,d)?Array.isArray(g[d])?g[d].push(w):g[d]=[g[d],w]:g[d]=w}),g}function V(r){let C=new FormData;for(let[b,g]of r.entries()){if(g instanceof File){C.append(b,g);continue}let y;try{y=JSON.parse(g)}catch{y=null}Array.isArray(y)&&y.every(d=>typeof d=="object"&&d!==null)?y.forEach((d,w)=>{for(let[o,p]of Object.entries(d))C.append(`${b}[${w}][${o}]`,p)}):C.append(b,g)}return C}async function I(r,C,b){let g=C.url,y={"Content-Type":"application/json"};C?.api_token&&(y.Authorization=`Bearer ${C.api_token}`);let d=$(r);if(C?.additional_data)for(let[p,v]of Object.entries(C.additional_data))d.append(p,v);let w,o=d instanceof FormData;w={method:"POST",headers:{Authorization:`Bearer ${C.api_token}`,...o?{}:{"Content-Type":"application/json"},Accept:"application/json"},body:o?d:JSON.stringify(Object.fromEntries(d.entries()))};try{fetch(g,w).then(async p=>{let v=p.headers.get("content-type");if(!p.ok)try{let L=(await p.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 v&&v.includes("application/json")?p.json():p.text()}).then(p=>{b&&b(p,null)}).catch(p=>{b&&b(null,p)})}catch(p){b&&b(null,p)}}function j(r,C,b){let g=[];if(typeof r=="string"?g=Array.from(document.querySelectorAll(r)):g=[r],g.length===0)return null;function y(){let o=b?.style==="bootstrap",p=document.createElement("form");p.classList.add("voc-form"),o&&p.classList.add("needs-validation"),p.id=C.id;let v=document.createElement("h2");v.className="voc-form-title",o&&v.classList.add("mb-3"),v.textContent=C.form_name,p.appendChild(v);function l(e,m){m&&(m.min_length!==void 0&&(e.minLength=m.min_length),m.max_length!==void 0&&(e.maxLength=m.max_length),m.regex&&(e instanceof HTMLInputElement?e.pattern=m.regex:e.dataset.pattern=m.regex))}function L(e){if(!e.key&&!e.label)return null;let m=C.default_values||{},c=e.name||e.key,i=c?m[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,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 u=document.createElement("input");u.type="checkbox",u.classList.add("voc-form-checkbox"),o&&u.classList.add("form-check-input"),u.name=e.name||e.key||"",u.value=n.value,e.key&&(u.id=`voc-form-${e.key}-${a}`),e.required&&(u.required=!0),Array.isArray(i)?i.includes(n.value)&&(u.checked=!0):i!==void 0?i===n.value&&(u.checked=!0):e.default===n.value&&(u.checked=!0),s.appendChild(u),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)}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 a=(n.match(/\n/g)||[]).length,s=80,u=n.split(`
|
|
2
|
+
`).reduce((S,N)=>Math.max(S,N.length),0),h=Math.ceil(u/s)||1,R=Math.max(2,a+h);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 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),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,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 u=document.createElement("input");u.type="radio",u.classList.add("voc-form-radio"),o&&u.classList.add("form-check-input"),u.name=e.name||e.key||"",u.value=n.value,e.key&&(u.id=`voc-form-${e.key}-${a}`),Array.isArray(i)?i.includes(n.value)&&(u.checked=!0):i!==void 0?i===n.value&&(u.checked=!0):e.default===n.value&&(u.checked=!0),s.appendChild(u),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=B(e,b||{},i);break}case"table":{k=O(e,b||{},i);break}case"audio_select":{k=D(e,b||{},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}C.fields.forEach(e=>{if(e.type==="group"&&"items"in e){let m=e;if(o){let c=document.createElement("div");c.className="card voc-form-group-card mb-3";let i=m.label||m.header?.title,x=m.description||m.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",m.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 u=document.createElement("h6");u.className="voc-form-row-label",u.textContent=n.label,a.appendChild(u)}let s=document.createElement("div");s.className="voc-form-row-items "+(n.direction||"horizontal"),n.items.forEach(u=>{let h=L(u);h&&s.appendChild(h)}),a.appendChild(s),k.appendChild(a)}else{let n=L(t);n&&k.appendChild(n)}}),c.appendChild(k),p.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=m.label||m.header?.title,x=m.description||m.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)}m.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 a=document.createElement("div");a.className="voc-form-row-items "+(t.direction||"horizontal"),t.items.forEach(s=>{let u=L(s);u&&a.appendChild(u)}),n.appendChild(a),c.appendChild(n)}else{let t=L(k);t&&c.appendChild(t)}}),p.appendChild(c)}return}if(e.type==="row"&&"items"in e){let m=document.createElement("div");if(m.className="voc-form-group voc-form-row",o&&m.classList.add("mb-3"),e.label){let i=document.createElement("h3");i.className="voc-form-row-label",i.textContent=e.label,m.appendChild(i)}if(e.description){let i=document.createElement("p");i.className="voc-form-row-description",i.textContent=e.description,m.appendChild(i)}let c=document.createElement("div");c.className="voc-form-row-items "+(e.direction||"horizontal"),m.appendChild(c),e.items.forEach(i=>{let x=L(i);x&&c.appendChild(x)}),p.appendChild(m)}else{let m=L(e);m&&p.appendChild(m)}});let F=document.createElement("button");return F.type="submit",F.className="voc-form-submit",F.textContent="Submit",o&&F.classList.add("btn","btn-primary"),p.appendChild(F),p}let d=y();g[0].appendChild(d);let w=C?.submit_config??b?.submit_config??null;return w&&d.addEventListener("submit",o=>{if(o.preventDefault(),!d.checkValidity()){d.reportValidity(),b?.onSubmitResult&&b.onSubmitResult(null,new Error("Form validation failed"));return}let p=Array.from(d.querySelectorAll(" .voc-form-table-wrapper, .voc-form-table-wrapper")),v=[];if(p.forEach(l=>{if(l&&typeof l.validate=="function"){let L=l.validate();L&&L.length&&v.push(...L)}}),v.length){b?.onSubmitResult&&b.onSubmitResult(null,new Error(v.join("; ")));let l=p[0];l&&l.scrollIntoView({behavior:"smooth",block:"center"});return}I(d,w,(l,L)=>{b?.onSubmitResult&&b.onSubmitResult(l,L)})}),{element:d,validate:()=>{let o=[];return d.querySelectorAll(":invalid").forEach(v=>{let l=v.validationMessage;o.push({element:v,message:l})}),o},getValues:()=>$(d),onSubmit:o=>{d.addEventListener("submit",p=>{p.preventDefault();let v=$(d);o(v,p)})}}}var H={renderForm:j};var se=H,ce={FormsLib:H};export{se as FormsLib,ce as default};
|