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