@dmitryvim/form-builder 0.2.5 → 0.2.6
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/README.md +121 -2
- package/dist/browser/formbuilder.min.js +31 -31
- package/dist/browser/formbuilder.v0.2.6.min.js +184 -0
- package/dist/cjs/index.cjs +195 -8
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/index.js +189 -6
- package/dist/esm/index.js.map +1 -1
- package/dist/form-builder.js +31 -31
- package/dist/types/instance/FormBuilderInstance.d.ts +5 -0
- package/dist/types/types/index.d.ts +1 -1
- package/dist/types/types/schema.d.ts +11 -1
- package/dist/types/utils/display-conditions.d.ts +17 -0
- package/package.json +1 -1
- package/dist/browser/formbuilder.v0.2.5.min.js +0 -184
|
@@ -6,6 +6,14 @@ export interface ElementAction {
|
|
|
6
6
|
key: string;
|
|
7
7
|
label: string;
|
|
8
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Conditional display configuration for elements
|
|
11
|
+
* Initially supports equals operator, designed for extensibility
|
|
12
|
+
*/
|
|
13
|
+
export interface DisplayCondition {
|
|
14
|
+
key: string;
|
|
15
|
+
equals?: any;
|
|
16
|
+
}
|
|
9
17
|
export interface BaseElement {
|
|
10
18
|
type: string;
|
|
11
19
|
key: string;
|
|
@@ -16,6 +24,7 @@ export interface BaseElement {
|
|
|
16
24
|
hidden?: boolean;
|
|
17
25
|
default?: any;
|
|
18
26
|
actions?: ElementAction[];
|
|
27
|
+
displayIf?: DisplayCondition;
|
|
19
28
|
}
|
|
20
29
|
export interface TextElement extends BaseElement {
|
|
21
30
|
type: "text";
|
|
@@ -93,7 +102,7 @@ export interface GroupElement extends BaseElement {
|
|
|
93
102
|
}
|
|
94
103
|
export type Element = TextElement | TextareaElement | NumberElement | SelectElement | FileElement | FilesElement | ContainerElement | GroupElement;
|
|
95
104
|
export interface Schema {
|
|
96
|
-
version
|
|
105
|
+
version?: string;
|
|
97
106
|
elements: Element[];
|
|
98
107
|
}
|
|
99
108
|
export interface ExternalAction {
|
|
@@ -110,6 +119,7 @@ export interface FormData {
|
|
|
110
119
|
export interface RenderContext {
|
|
111
120
|
path: string;
|
|
112
121
|
prefill: Record<string, any>;
|
|
122
|
+
formData?: Record<string, any>;
|
|
113
123
|
state: import("./state.js").State;
|
|
114
124
|
instance?: any;
|
|
115
125
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { DisplayCondition } from "../types/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Get value from nested object path
|
|
4
|
+
* Supports nested paths: "address.city"
|
|
5
|
+
* Supports array indices: "items[0].name", "contacts[1].email"
|
|
6
|
+
* @param data - Form data object
|
|
7
|
+
* @param path - Dot notation path with optional array indices
|
|
8
|
+
* @returns The value at the path, or undefined if not found
|
|
9
|
+
*/
|
|
10
|
+
export declare function getValueByPath(data: any, path: string): any;
|
|
11
|
+
/**
|
|
12
|
+
* Evaluate a display condition against form data
|
|
13
|
+
* @param condition - Display condition to evaluate
|
|
14
|
+
* @param formData - Current form data
|
|
15
|
+
* @returns true if condition is met (field should be displayed), false otherwise
|
|
16
|
+
*/
|
|
17
|
+
export declare function evaluateDisplayCondition(condition: DisplayCondition, formData: Record<string, any>): boolean;
|
package/package.json
CHANGED
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
var FormBuilder=(function(F){"use strict";function se(e,t){(e.minLength!==null||e.maxLength!==null)&&(e.minLength!==null&&e.maxLength!==null?t.push(`length=${e.minLength}-${e.maxLength} characters`):e.maxLength!==null?t.push(`max=${e.maxLength} characters`):e.minLength!==null&&t.push(`min=${e.minLength} characters`))}function ce(e,t){(e.min!==null||e.max!==null)&&(e.min!==null&&e.max!==null?t.push(`range=${e.min}-${e.max}`):e.max!==null?t.push(`max=${e.max}`):e.min!==null&&t.push(`min=${e.min}`))}function de(e,t){e.maxSizeMB&&t.push(`max_size=${e.maxSizeMB}MB`)}function ue(e,t){var n;(n=e.accept)!=null&&n.extensions&&t.push(`formats=${e.accept.extensions.map(o=>o.toUpperCase()).join(",")}`)}function me(e,t){var n;e.pattern&&!e.pattern.includes("\u0410-\u042F")?t.push("plain text only"):(n=e.pattern)!=null&&n.includes("\u0410-\u042F")&&t.push("text with punctuation")}function k(e){const t=[];return t.push(e.required?"required":"optional"),se(e,t),ce(e,t),de(e,t),ue(e,t),me(e,t),t.join(" \u2022 ")}function M(e){const t=[];if(!e||typeof e!="object")return t.push("Schema must be an object"),t;if(e.version||t.push("Schema missing version"),!Array.isArray(e.elements))return t.push("Schema missing elements array"),t;function n(o,s){o.forEach((r,l)=>{const a=`${s}[${l}]`;if(r.type||t.push(`${a}: missing type`),r.key||t.push(`${a}: missing key`),r.type==="group"&&"elements"in r&&r.elements&&n(r.elements,`${a}.elements`),r.type==="container"&&r.elements&&n(r.elements,`${a}.elements`),r.type==="select"&&r.options){const i=r.default;i!=null&&i!==""&&(r.options.some(c=>c.value===i)||t.push(`${a}: default "${i}" not in options`))}})}return Array.isArray(e.elements)&&n(e.elements,"elements"),t}function A(e){return e&&typeof e=="object"&&e.constructor===Object}function L(e,t){return e?`${e}.${t}`:t}function T(e){for(;e.firstChild;)e.removeChild(e.firstChild)}function pe(e,t,n,o){const s=t.state,r=document.createElement("input");if(r.type="text",r.className="w-full rounded-lg",r.style.cssText=`
|
|
2
|
-
padding: var(--fb-input-padding-y) var(--fb-input-padding-x);
|
|
3
|
-
border: var(--fb-border-width) solid var(--fb-border-color);
|
|
4
|
-
border-radius: var(--fb-border-radius);
|
|
5
|
-
background-color: ${s.config.readonly?"var(--fb-background-readonly-color)":"var(--fb-background-color)"};
|
|
6
|
-
color: var(--fb-text-color);
|
|
7
|
-
font-size: var(--fb-font-size);
|
|
8
|
-
font-family: var(--fb-font-family);
|
|
9
|
-
transition: all var(--fb-transition-duration) ease-in-out;
|
|
10
|
-
`,r.name=o,r.placeholder=e.placeholder||"\u0412\u0432\u0435\u0434\u0438\u0442\u0435 \u0442\u0435\u043A\u0441\u0442",r.value=t.prefill[e.key]||e.default||"",r.readOnly=s.config.readonly,s.config.readonly||(r.addEventListener("focus",()=>{r.style.borderColor="var(--fb-border-focus-color)",r.style.outline="var(--fb-focus-ring-width) solid var(--fb-focus-ring-color)",r.style.outlineOffset="0"}),r.addEventListener("blur",()=>{r.style.borderColor="var(--fb-border-color)",r.style.outline="none"}),r.addEventListener("mouseenter",()=>{document.activeElement!==r&&(r.style.borderColor="var(--fb-border-hover-color)")}),r.addEventListener("mouseleave",()=>{document.activeElement!==r&&(r.style.borderColor="var(--fb-border-color)")})),!s.config.readonly&&t.instance){const a=()=>{t.instance.triggerOnChange(o,r.value)};r.addEventListener("blur",a),r.addEventListener("input",a)}n.appendChild(r);const l=document.createElement("p");l.className="mt-1",l.style.cssText=`
|
|
11
|
-
font-size: var(--fb-font-size-small);
|
|
12
|
-
color: var(--fb-text-secondary-color);
|
|
13
|
-
`,l.textContent=k(e),n.appendChild(l)}function fe(e,t,n,o){var s,r;const l=t.state,a=t.prefill[e.key]||[],i=Array.isArray(a)?[...a]:[],c=(s=e.minCount)!=null?s:1,u=(r=e.maxCount)!=null?r:1/0;for(;i.length<c;)i.push(e.default||"");const d=document.createElement("div");d.className="space-y-2",n.appendChild(d);function h(){d.querySelectorAll(".multiple-text-item").forEach((x,g)=>{const y=x.querySelector("input");y&&(y.name=`${o}[${g}]`)})}function p(x="",g=-1){const y=document.createElement("div");y.className="multiple-text-item flex items-center gap-2";const f=document.createElement("input");if(f.type="text",f.className="flex-1",f.style.cssText=`
|
|
14
|
-
padding: var(--fb-input-padding-y) var(--fb-input-padding-x);
|
|
15
|
-
border: var(--fb-border-width) solid var(--fb-border-color);
|
|
16
|
-
border-radius: var(--fb-border-radius);
|
|
17
|
-
background-color: ${l.config.readonly?"var(--fb-background-readonly-color)":"var(--fb-background-color)"};
|
|
18
|
-
color: var(--fb-text-color);
|
|
19
|
-
font-size: var(--fb-font-size);
|
|
20
|
-
font-family: var(--fb-font-family);
|
|
21
|
-
transition: all var(--fb-transition-duration) ease-in-out;
|
|
22
|
-
`,f.placeholder=e.placeholder||"Enter text",f.value=x,f.readOnly=l.config.readonly,l.config.readonly||(f.addEventListener("focus",()=>{f.style.borderColor="var(--fb-border-focus-color)",f.style.outline="var(--fb-focus-ring-width) solid var(--fb-focus-ring-color)",f.style.outlineOffset="0"}),f.addEventListener("blur",()=>{f.style.borderColor="var(--fb-border-color)",f.style.outline="none"}),f.addEventListener("mouseenter",()=>{document.activeElement!==f&&(f.style.borderColor="var(--fb-border-hover-color)")}),f.addEventListener("mouseleave",()=>{document.activeElement!==f&&(f.style.borderColor="var(--fb-border-color)")})),!l.config.readonly&&t.instance){const C=()=>{t.instance.triggerOnChange(f.name,f.value)};f.addEventListener("blur",C),f.addEventListener("input",C)}return y.appendChild(f),g===-1?d.appendChild(y):d.insertBefore(y,d.children[g]),h(),y}function m(){if(l.config.readonly)return;const x=d.querySelectorAll(".multiple-text-item"),g=x.length;x.forEach(y=>{let f=y.querySelector(".remove-item-btn");f||(f=document.createElement("button"),f.type="button",f.className="remove-item-btn px-2 py-1 rounded",f.style.cssText=`
|
|
23
|
-
color: var(--fb-error-color);
|
|
24
|
-
background-color: transparent;
|
|
25
|
-
transition: background-color var(--fb-transition-duration);
|
|
26
|
-
`,f.innerHTML="\u2715",f.addEventListener("mouseenter",()=>{f.style.backgroundColor="var(--fb-background-hover-color)"}),f.addEventListener("mouseleave",()=>{f.style.backgroundColor="transparent"}),f.onclick=()=>{const E=Array.from(d.children).indexOf(y);d.children.length>c&&(i.splice(E,1),y.remove(),h(),v(),m())},y.appendChild(f));const C=g<=c;f.disabled=C,f.style.opacity=C?"0.5":"1",f.style.pointerEvents=C?"none":"auto"})}function v(){const x=n.querySelector(".add-text-btn");if(x&&x.remove(),!l.config.readonly&&i.length<u){const g=document.createElement("button");g.type="button",g.className="add-text-btn mt-2 px-3 py-1 rounded",g.style.cssText=`
|
|
27
|
-
color: var(--fb-primary-color);
|
|
28
|
-
border: var(--fb-border-width) solid var(--fb-primary-color);
|
|
29
|
-
background-color: transparent;
|
|
30
|
-
font-size: var(--fb-font-size);
|
|
31
|
-
transition: all var(--fb-transition-duration);
|
|
32
|
-
`,g.textContent=`+ Add ${e.label||"Text"}`,g.addEventListener("mouseenter",()=>{g.style.backgroundColor="var(--fb-background-hover-color)"}),g.addEventListener("mouseleave",()=>{g.style.backgroundColor="transparent"}),g.onclick=()=>{i.push(e.default||""),p(e.default||""),v(),m()},n.appendChild(g)}}i.forEach(x=>p(x)),v(),m();const b=document.createElement("p");b.className="mt-1",b.style.cssText=`
|
|
33
|
-
font-size: var(--fb-font-size-small);
|
|
34
|
-
color: var(--fb-text-secondary-color);
|
|
35
|
-
`,b.textContent=k(e),n.appendChild(b)}function I(e,t,n){var o,s,r;const l=[],{scopeRoot:a,skipValidation:i}=n,c=(d,h)=>{var p,m;if(!d)return;const v=`error-${d.getAttribute("name")||Math.random().toString(36).substring(7)}`;let b=document.getElementById(v);h?(d.classList.add("invalid"),d.title=h,b||(b=document.createElement("div"),b.id=v,b.className="error-message",b.style.cssText=`
|
|
36
|
-
color: var(--fb-error-color);
|
|
37
|
-
font-size: var(--fb-font-size-small);
|
|
38
|
-
margin-top: 0.25rem;
|
|
39
|
-
`,d.nextSibling?(p=d.parentNode)==null||p.insertBefore(b,d.nextSibling):(m=d.parentNode)==null||m.appendChild(b)),b.textContent=h,b.style.display="block"):(d.classList.remove("invalid"),d.title="",b&&b.remove())},u=(d,h,p)=>{let m=!1;if(!i&&h){if(e.minLength!==void 0&&e.minLength!==null&&h.length<e.minLength)l.push(`${p}: minLength=${e.minLength}`),c(d,`minLength=${e.minLength}`),m=!0;else if(e.maxLength!==void 0&&e.maxLength!==null&&h.length>e.maxLength)l.push(`${p}: maxLength=${e.maxLength}`),c(d,`maxLength=${e.maxLength}`),m=!0;else if(e.pattern)try{new RegExp(e.pattern).test(h)||(l.push(`${p}: pattern mismatch`),c(d,"pattern mismatch"),m=!0)}catch(v){l.push(`${p}: invalid pattern`),c(d,"invalid pattern"),m=!0}}m||c(d,null)};if(e.multiple){const d=a.querySelectorAll(`[name^="${t}["]`),h=[];if(d.forEach((p,m)=>{var v;const b=(v=p==null?void 0:p.value)!=null?v:"";h.push(b),u(p,b,`${t}[${m}]`)}),!i){const p=(o=e.minCount)!=null?o:1,m=(s=e.maxCount)!=null?s:1/0,v=h.filter(b=>b.trim()!=="");e.required&&v.length===0&&l.push(`${t}: required`),v.length<p&&l.push(`${t}: minimum ${p} items required`),v.length>m&&l.push(`${t}: maximum ${m} items allowed`)}return{value:h,errors:l}}else{const d=a.querySelector(`[name$="${t}"]`),h=(r=d==null?void 0:d.value)!=null?r:"";return!i&&e.required&&h===""?(l.push(`${t}: required`),c(d,"required"),{value:"",errors:l}):(u(d,h,t),{value:h,errors:l})}}function U(e,t,n,o){const{scopeRoot:s}=o;if(e.multiple){if(!Array.isArray(n)){console.warn(`updateTextField: Expected array for multiple field "${t}", got ${typeof n}`);return}const r=s.querySelectorAll(`[name^="${t}["]`);r.forEach((l,a)=>{a<n.length&&(l.value=n[a]!=null?String(n[a]):"",l.classList.remove("invalid"),l.title="")}),n.length!==r.length&&console.warn(`updateTextField: Multiple field "${t}" has ${r.length} inputs but received ${n.length} values. Consider re-rendering for add/remove.`)}else{const r=s.querySelector(`[name="${t}"]`);r&&(r.value=n!=null?String(n):"",r.classList.remove("invalid"),r.title="")}}function he(e,t,n,o){const s=t.state,r=document.createElement("textarea");if(r.className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 resize-none",r.name=o,r.placeholder=e.placeholder||"\u0412\u0432\u0435\u0434\u0438\u0442\u0435 \u0442\u0435\u043A\u0441\u0442",r.rows=e.rows||4,r.value=t.prefill[e.key]||e.default||"",r.readOnly=s.config.readonly,!s.config.readonly&&t.instance){const a=()=>{t.instance.triggerOnChange(o,r.value)};r.addEventListener("blur",a),r.addEventListener("input",a)}n.appendChild(r);const l=document.createElement("p");l.className="text-xs text-gray-500 mt-1",l.textContent=k(e),n.appendChild(l)}function ve(e,t,n,o){var s,r;const l=t.state,a=t.prefill[e.key]||[],i=Array.isArray(a)?[...a]:[],c=(s=e.minCount)!=null?s:1,u=(r=e.maxCount)!=null?r:1/0;for(;i.length<c;)i.push(e.default||"");const d=document.createElement("div");d.className="space-y-2",n.appendChild(d);function h(){d.querySelectorAll(".multiple-textarea-item").forEach((x,g)=>{const y=x.querySelector("textarea");y&&(y.name=`${o}[${g}]`)})}function p(x="",g=-1){const y=document.createElement("div");y.className="multiple-textarea-item";const f=document.createElement("textarea");if(f.className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 resize-none",f.placeholder=e.placeholder||"Enter text",f.rows=e.rows||4,f.value=x,f.readOnly=l.config.readonly,!l.config.readonly&&t.instance){const C=()=>{t.instance.triggerOnChange(f.name,f.value)};f.addEventListener("blur",C),f.addEventListener("input",C)}return y.appendChild(f),g===-1?d.appendChild(y):d.insertBefore(y,d.children[g]),h(),y}function m(){if(l.config.readonly)return;const x=d.querySelectorAll(".multiple-textarea-item"),g=x.length;x.forEach(y=>{let f=y.querySelector(".remove-item-btn");f||(f=document.createElement("button"),f.type="button",f.className="remove-item-btn mt-1 px-2 py-1 text-red-600 hover:bg-red-50 rounded text-sm",f.innerHTML="\u2715 Remove",f.onclick=()=>{const E=Array.from(d.children).indexOf(y);d.children.length>c&&(i.splice(E,1),y.remove(),h(),v(),m())},y.appendChild(f));const C=g<=c;f.disabled=C,f.style.opacity=C?"0.5":"1",f.style.pointerEvents=C?"none":"auto"})}function v(){const x=n.querySelector(".add-textarea-btn");if(x&&x.remove(),!l.config.readonly&&i.length<u){const g=document.createElement("button");g.type="button",g.className="add-textarea-btn mt-2 px-3 py-1 text-blue-600 border border-blue-300 rounded hover:bg-blue-50 text-sm",g.textContent=`+ Add ${e.label||"Textarea"}`,g.onclick=()=>{i.push(e.default||""),p(e.default||""),v(),m()},n.appendChild(g)}}i.forEach(x=>p(x)),v(),m();const b=document.createElement("p");b.className="text-xs text-gray-500 mt-1",b.textContent=k(e),n.appendChild(b)}function ge(e,t,n){return I(e,t,n)}function ye(e,t,n,o){U(e,t,n,o)}function be(e,t,n,o){const s=t.state,r=document.createElement("input");if(r.type="number",r.className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500",r.name=o,r.placeholder=e.placeholder||"0",e.min!==void 0&&(r.min=e.min.toString()),e.max!==void 0&&(r.max=e.max.toString()),e.step!==void 0&&(r.step=e.step.toString()),r.value=t.prefill[e.key]||e.default||"",r.readOnly=s.config.readonly,!s.config.readonly&&t.instance){const a=()=>{const i=r.value?parseFloat(r.value):null;t.instance.triggerOnChange(o,i)};r.addEventListener("blur",a),r.addEventListener("input",a)}n.appendChild(r);const l=document.createElement("p");l.className="text-xs text-gray-500 mt-1",l.textContent=k(e),n.appendChild(l)}function xe(e,t,n,o){var s,r;const l=t.state,a=t.prefill[e.key]||[],i=Array.isArray(a)?[...a]:[],c=(s=e.minCount)!=null?s:1,u=(r=e.maxCount)!=null?r:1/0;for(;i.length<c;)i.push(e.default||"");const d=document.createElement("div");d.className="space-y-2",n.appendChild(d);function h(){d.querySelectorAll(".multiple-number-item").forEach((x,g)=>{const y=x.querySelector("input");y&&(y.name=`${o}[${g}]`)})}function p(x="",g=-1){const y=document.createElement("div");y.className="multiple-number-item flex items-center gap-2";const f=document.createElement("input");if(f.type="number",f.className="flex-1 px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500",f.placeholder=e.placeholder||"0",e.min!==void 0&&(f.min=e.min.toString()),e.max!==void 0&&(f.max=e.max.toString()),e.step!==void 0&&(f.step=e.step.toString()),f.value=x.toString(),f.readOnly=l.config.readonly,!l.config.readonly&&t.instance){const C=()=>{const E=f.value?parseFloat(f.value):null;t.instance.triggerOnChange(f.name,E)};f.addEventListener("blur",C),f.addEventListener("input",C)}return y.appendChild(f),g===-1?d.appendChild(y):d.insertBefore(y,d.children[g]),h(),y}function m(){if(l.config.readonly)return;const x=d.querySelectorAll(".multiple-number-item"),g=x.length;x.forEach(y=>{let f=y.querySelector(".remove-item-btn");f||(f=document.createElement("button"),f.type="button",f.className="remove-item-btn px-2 py-1 text-red-600 hover:bg-red-50 rounded",f.innerHTML="\u2715",f.onclick=()=>{const E=Array.from(d.children).indexOf(y);d.children.length>c&&(i.splice(E,1),y.remove(),h(),v(),m())},y.appendChild(f));const C=g<=c;f.disabled=C,f.style.opacity=C?"0.5":"1",f.style.pointerEvents=C?"none":"auto"})}function v(){const x=n.querySelector(".add-number-btn");if(x&&x.remove(),!l.config.readonly&&i.length<u){const g=document.createElement("button");g.type="button",g.className="add-number-btn mt-2 px-3 py-1 text-blue-600 border border-blue-300 rounded hover:bg-blue-50 text-sm",g.textContent=`+ Add ${e.label||"Number"}`,g.onclick=()=>{i.push(e.default||""),p(e.default||""),v(),m()},n.appendChild(g)}}i.forEach(x=>p(x)),v(),m();const b=document.createElement("p");b.className="text-xs text-gray-500 mt-1",b.textContent=k(e),n.appendChild(b)}function Ce(e,t,n){var o,s,r,l,a;const i=[],{scopeRoot:c,skipValidation:u}=n,d=(p,m)=>{var v,b;if(!p)return;const x=`error-${p.getAttribute("name")||Math.random().toString(36).substring(7)}`;let g=document.getElementById(x);m?(p.classList.add("invalid"),p.title=m,g||(g=document.createElement("div"),g.id=x,g.className="error-message",g.style.cssText=`
|
|
40
|
-
color: var(--fb-error-color);
|
|
41
|
-
font-size: var(--fb-font-size-small);
|
|
42
|
-
margin-top: 0.25rem;
|
|
43
|
-
`,p.nextSibling?(v=p.parentNode)==null||v.insertBefore(g,p.nextSibling):(b=p.parentNode)==null||b.appendChild(g)),g.textContent=m,g.style.display="block"):(p.classList.remove("invalid"),p.title="",g&&g.remove())},h=(p,m,v)=>{let b=!1;!u&&e.min!==void 0&&e.min!==null&&m<e.min?(i.push(`${v}: < min=${e.min}`),d(p,`< min=${e.min}`),b=!0):!u&&e.max!==void 0&&e.max!==null&&m>e.max&&(i.push(`${v}: > max=${e.max}`),d(p,`> max=${e.max}`),b=!0),b||d(p,null)};if(e.multiple){const p=c.querySelectorAll(`[name^="${t}["]`),m=[];if(p.forEach((v,b)=>{var x,g,y;const f=(x=v==null?void 0:v.value)!=null?x:"";if(f===""){m.push(null),d(v,null);return}const C=parseFloat(f);if(!u&&!Number.isFinite(C)){i.push(`${t}[${b}]: not a number`),d(v,"not a number"),m.push(null);return}h(v,C,`${t}[${b}]`);const E=Number.isInteger((g=e.decimals)!=null?g:0)&&(y=e.decimals)!=null?y:0;m.push(Number(C.toFixed(E)))}),!u){const v=(o=e.minCount)!=null?o:1,b=(s=e.maxCount)!=null?s:1/0,x=m.filter(g=>g!==null);e.required&&x.length===0&&i.push(`${t}: required`),x.length<v&&i.push(`${t}: minimum ${v} items required`),x.length>b&&i.push(`${t}: maximum ${b} items allowed`)}return{value:m,errors:i}}else{const p=c.querySelector(`[name$="${t}"]`),m=(r=p==null?void 0:p.value)!=null?r:"";if(!u&&e.required&&m==="")return i.push(`${t}: required`),d(p,"required"),{value:null,errors:i};if(m==="")return d(p,null),{value:null,errors:i};const v=parseFloat(m);if(!u&&!Number.isFinite(v))return i.push(`${t}: not a number`),d(p,"not a number"),{value:null,errors:i};h(p,v,t);const b=Number.isInteger((l=e.decimals)!=null?l:0)&&(a=e.decimals)!=null?a:0;return{value:Number(v.toFixed(b)),errors:i}}}function Ee(e,t,n,o){const{scopeRoot:s}=o;if(e.multiple){if(!Array.isArray(n)){console.warn(`updateNumberField: Expected array for multiple field "${t}", got ${typeof n}`);return}const r=s.querySelectorAll(`[name^="${t}["]`);r.forEach((l,a)=>{a<n.length&&(l.value=n[a]!=null?String(n[a]):"",l.classList.remove("invalid"),l.title="")}),n.length!==r.length&&console.warn(`updateNumberField: Multiple field "${t}" has ${r.length} inputs but received ${n.length} values. Consider re-rendering for add/remove.`)}else{const r=s.querySelector(`[name="${t}"]`);r&&(r.value=n!=null?String(n):"",r.classList.remove("invalid"),r.title="")}}function we(e,t,n,o){const s=t.state,r=document.createElement("select");if(r.className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500",r.name=o,r.disabled=s.config.readonly,(e.options||[]).forEach(a=>{const i=document.createElement("option");i.value=a.value,i.textContent=a.label,(t.prefill[e.key]||e.default)===a.value&&(i.selected=!0),r.appendChild(i)}),!s.config.readonly&&t.instance){const a=()=>{t.instance.triggerOnChange(o,r.value)};r.addEventListener("change",a)}n.appendChild(r);const l=document.createElement("p");l.className="text-xs text-gray-500 mt-1",l.textContent=k(e),n.appendChild(l)}function $e(e,t,n,o){var s,r,l,a;const i=t.state,c=t.prefill[e.key]||[],u=Array.isArray(c)?[...c]:[],d=(s=e.minCount)!=null?s:1,h=(r=e.maxCount)!=null?r:1/0;for(;u.length<d;)u.push(e.default||((a=(l=e.options)==null?void 0:l[0])==null?void 0:a.value)||"");const p=document.createElement("div");p.className="space-y-2",n.appendChild(p);function m(){p.querySelectorAll(".multiple-select-item").forEach((y,f)=>{const C=y.querySelector("select");C&&(C.name=`${o}[${f}]`)})}function v(y="",f=-1){const C=document.createElement("div");C.className="multiple-select-item flex items-center gap-2";const E=document.createElement("select");if(E.className="flex-1 px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500",E.disabled=i.config.readonly,(e.options||[]).forEach(w=>{const S=document.createElement("option");S.value=w.value,S.textContent=w.label,y===w.value&&(S.selected=!0),E.appendChild(S)}),!i.config.readonly&&t.instance){const w=()=>{t.instance.triggerOnChange(E.name,E.value)};E.addEventListener("change",w)}return C.appendChild(E),f===-1?p.appendChild(C):p.insertBefore(C,p.children[f]),m(),C}function b(){if(i.config.readonly)return;const y=p.querySelectorAll(".multiple-select-item"),f=y.length;y.forEach(C=>{let E=C.querySelector(".remove-item-btn");E||(E=document.createElement("button"),E.type="button",E.className="remove-item-btn px-2 py-1 text-red-600 hover:bg-red-50 rounded",E.innerHTML="\u2715",E.onclick=()=>{const S=Array.from(p.children).indexOf(C);p.children.length>d&&(u.splice(S,1),C.remove(),m(),x(),b())},C.appendChild(E));const w=f<=d;E.disabled=w,E.style.opacity=w?"0.5":"1",E.style.pointerEvents=w?"none":"auto"})}function x(){const y=n.querySelector(".add-select-btn");if(y&&y.remove(),!i.config.readonly&&u.length<h){const f=document.createElement("button");f.type="button",f.className="add-select-btn mt-2 px-3 py-1 text-blue-600 border border-blue-300 rounded hover:bg-blue-50 text-sm",f.textContent=`+ Add ${e.label||"Selection"}`,f.onclick=()=>{var C,E;const w=e.default||((E=(C=e.options)==null?void 0:C[0])==null?void 0:E.value)||"";u.push(w),v(w),x(),b()},n.appendChild(f)}}u.forEach(y=>v(y)),x(),b();const g=document.createElement("p");g.className="text-xs text-gray-500 mt-1",g.textContent=k(e),n.appendChild(g)}function ke(e,t,n){var o;const s=[],{scopeRoot:r,skipValidation:l}=n,a=(c,u)=>{var d,h;if(!c)return;const p=`error-${c.getAttribute("name")||Math.random().toString(36).substring(7)}`;let m=document.getElementById(p);u?(c.classList.add("invalid"),c.title=u,m||(m=document.createElement("div"),m.id=p,m.className="error-message",m.style.cssText=`
|
|
44
|
-
color: var(--fb-error-color);
|
|
45
|
-
font-size: var(--fb-font-size-small);
|
|
46
|
-
margin-top: 0.25rem;
|
|
47
|
-
`,c.nextSibling?(d=c.parentNode)==null||d.insertBefore(m,c.nextSibling):(h=c.parentNode)==null||h.appendChild(m)),m.textContent=u,m.style.display="block"):(c.classList.remove("invalid"),c.title="",m&&m.remove())},i=(c,u,d,h)=>{var p,m;if(l)return;const v=u.filter(h),b="minCount"in d&&(p=d.minCount)!=null?p:1,x="maxCount"in d&&(m=d.maxCount)!=null?m:1/0;d.required&&v.length===0&&s.push(`${c}: required`),v.length<b&&s.push(`${c}: minimum ${b} items required`),v.length>x&&s.push(`${c}: maximum ${x} items allowed`)};if("multiple"in e&&e.multiple){const c=r.querySelectorAll(`[name^="${t}["]`),u=[];return c.forEach(d=>{var h;const p=(h=d==null?void 0:d.value)!=null?h:"";u.push(p),a(d,null)}),i(t,u,e,d=>d!==""),{value:u,errors:s}}else{const c=r.querySelector(`[name$="${t}"]`),u=(o=c==null?void 0:c.value)!=null?o:"";return!l&&e.required&&u===""?(s.push(`${t}: required`),a(c,"required"),{value:null,errors:s}):(a(c,null),{value:u===""?null:u,errors:s})}}function Fe(e,t,n,o){const{scopeRoot:s}=o;if("multiple"in e&&e.multiple){if(!Array.isArray(n)){console.warn(`updateSelectField: Expected array for multiple field "${t}", got ${typeof n}`);return}const r=s.querySelectorAll(`[name^="${t}["]`);r.forEach((l,a)=>{a<n.length&&(l.value=n[a]!=null?String(n[a]):"",l.querySelectorAll("option").forEach(i=>{i.selected=i.value===String(n[a])}),l.classList.remove("invalid"),l.title="")}),n.length!==r.length&&console.warn(`updateSelectField: Multiple field "${t}" has ${r.length} selects but received ${n.length} values. Consider re-rendering for add/remove.`)}else{const r=s.querySelector(`[name="${t}"]`);r&&(r.value=n!=null?String(n):"",r.querySelectorAll("option").forEach(l=>{l.selected=l.value===String(n)}),r.classList.remove("invalid"),r.title="")}}function $(e,t){const n=t.config.locale||"en";return(t.config.translations[n]||t.config.translations.en)[e]||e}async function O(e,t,n,o={}){const{fileName:s="",isReadonly:r=!1,deps:l=null}=o;if(!r&&l&&(!l.picker||!l.fileUploadHandler||!l.dragHandler))throw new Error("renderFilePreview: missing deps {picker, fileUploadHandler, dragHandler}");T(e),r&&e.classList.add("cursor-pointer");const a=document.createElement("img");a.className="w-full h-full object-contain",a.alt=s||"Preview";const i=n.resourceIndex.get(t);if(i&&i.file&&i.file instanceof File){if(i.type&&i.type.startsWith("image/")){const c=new FileReader;c.onload=u=>{var d;a.src=((d=u.target)==null?void 0:d.result)||""},c.readAsDataURL(i.file),e.appendChild(a)}else if(i.type&&i.type.startsWith("video/")){const c=URL.createObjectURL(i.file);e.onclick=null;const u=e.cloneNode(!1);e.parentNode&&e.parentNode.replaceChild(u,e),e=u,e.innerHTML=`
|
|
48
|
-
<div class="relative group h-full">
|
|
49
|
-
<video class="w-full h-full object-contain" controls preload="auto" muted>
|
|
50
|
-
<source src="${c}" type="${i.type}">
|
|
51
|
-
Your browser does not support the video tag.
|
|
52
|
-
</video>
|
|
53
|
-
<div class="absolute top-2 right-2 opacity-0 group-hover:opacity-100 transition-opacity z-10 flex gap-1">
|
|
54
|
-
<button class="bg-red-600 bg-opacity-75 hover:bg-opacity-90 text-white p-1 rounded text-xs delete-file-btn">
|
|
55
|
-
${$("removeElement",n)}
|
|
56
|
-
</button>
|
|
57
|
-
<button class="bg-gray-800 bg-opacity-75 hover:bg-opacity-90 text-white p-1 rounded text-xs change-file-btn">
|
|
58
|
-
Change
|
|
59
|
-
</button>
|
|
60
|
-
</div>
|
|
61
|
-
</div>
|
|
62
|
-
`;const d=e.querySelector(".change-file-btn");d&&(d.onclick=p=>{p.stopPropagation(),l!=null&&l.picker&&l.picker.click()});const h=e.querySelector(".delete-file-btn");h&&(h.onclick=p=>{var m;p.stopPropagation(),n.resourceIndex.delete(t);const v=(m=e.parentElement)==null?void 0:m.querySelector('input[type="hidden"]');v&&(v.value=""),l!=null&&l.fileUploadHandler&&(e.onclick=l.fileUploadHandler),l!=null&&l.dragHandler&&z(e,l.dragHandler),e.innerHTML=`
|
|
63
|
-
<div class="flex flex-col items-center justify-center h-full text-gray-400">
|
|
64
|
-
<svg class="w-6 h-6 mb-2" fill="currentColor" viewBox="0 0 24 24">
|
|
65
|
-
<path d="M21 19V5c0-1.1-.9-2-2-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2zM8.5 13.5l2.5 3.01L14.5 12l4.5 6H5l3.5-4.5z"/>
|
|
66
|
-
</svg>
|
|
67
|
-
<div class="text-sm text-center">${$("clickDragText",n)}</div>
|
|
68
|
-
</div>
|
|
69
|
-
`})}else e.innerHTML=`<div class="flex flex-col items-center justify-center h-full text-gray-400"><div class="text-2xl mb-2">\u{1F4C1}</div><div class="text-sm">${s}</div></div>`;!r&&!(i&&i.type&&i.type.startsWith("video/"))&&Se(e,n,()=>{var c;n.resourceIndex.delete(t);const u=(c=e.parentElement)==null?void 0:c.querySelector('input[type="hidden"]');u&&(u.value=""),e.innerHTML=`
|
|
70
|
-
<div class="flex flex-col items-center justify-center h-full text-gray-400">
|
|
71
|
-
<svg class="w-6 h-6 mb-2" fill="currentColor" viewBox="0 0 24 24">
|
|
72
|
-
<path d="M21 19V5c0-1.1-.9-2-2-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2zM8.5 13.5l2.5 3.01L14.5 12l4.5 6H5l3.5-4.5z"/>
|
|
73
|
-
</svg>
|
|
74
|
-
<div class="text-sm text-center">${$("clickDragText",n)}</div>
|
|
75
|
-
</div>
|
|
76
|
-
`})}else if(n.config.getThumbnail)try{const c=await n.config.getThumbnail(t);if(c)if(T(e),i&&i.type&&i.type.startsWith("video/")){const u=document.createElement("video");u.className="w-full h-full object-contain",u.controls=!0,u.preload="metadata",u.muted=!0;const d=document.createElement("source");d.src=c,d.type=i.type,u.appendChild(d),u.appendChild(document.createTextNode("Your browser does not support the video tag.")),e.appendChild(u)}else a.src=c,e.appendChild(a);else j(e,n)}catch(c){console.error("Failed to get thumbnail:",c),e.innerHTML=`
|
|
77
|
-
<div class="flex flex-col items-center justify-center h-full text-gray-400">
|
|
78
|
-
<svg class="w-6 h-6 mb-2" fill="currentColor" viewBox="0 0 24 24">
|
|
79
|
-
<path d="M21 19V5c0-1.1-.9-2-2-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2zM8.5 13.5l2.5 3.01L14.5 12l4.5 6H5l3.5-4.5z"/>
|
|
80
|
-
</svg>
|
|
81
|
-
<div class="text-sm text-center">${s||"Preview unavailable"}</div>
|
|
82
|
-
</div>
|
|
83
|
-
`}else j(e,n)}async function B(e,t,n){var o,s;const r=t.resourceIndex.get(e),l=(r==null?void 0:r.name)||e.split("/").pop()||"file",a=l.toLowerCase().match(/\.psd$/),i=document.createElement("div");i.className=a?"space-y-2":"space-y-3";const c=document.createElement("div");a?c.className="bg-gray-100 rounded-lg overflow-hidden cursor-pointer hover:opacity-90 transition-opacity flex items-center p-3 max-w-sm":c.className="bg-gray-100 rounded-lg overflow-hidden cursor-pointer hover:opacity-90 transition-opacity";const u=!a&&(((o=r==null?void 0:r.type)==null?void 0:o.startsWith("image/"))||l.toLowerCase().match(/\.(jpg|jpeg|png|gif|webp)$/)),d=((s=r==null?void 0:r.type)==null?void 0:s.startsWith("video/"))||l.toLowerCase().match(/\.(mp4|webm|avi|mov)$/);if(u)if(t.config.getThumbnail)try{const m=await t.config.getThumbnail(e);m?c.innerHTML=`<img src="${m}" alt="${l}" class="w-full h-auto">`:c.innerHTML=`<div class="aspect-video flex items-center justify-center text-gray-400"><div class="text-center"><div class="text-4xl mb-2">\u{1F5BC}\uFE0F</div><div class="text-sm">${l}</div></div></div>`}catch(m){console.warn("getThumbnail failed for",e,m),c.innerHTML=`<div class="aspect-video flex items-center justify-center text-gray-400"><div class="text-center"><div class="text-4xl mb-2">\u{1F5BC}\uFE0F</div><div class="text-sm">${l}</div></div></div>`}else c.innerHTML=`<div class="aspect-video flex items-center justify-center text-gray-400"><div class="text-center"><div class="text-4xl mb-2">\u{1F5BC}\uFE0F</div><div class="text-sm">${l}</div></div></div>`;else if(d)if(t.config.getThumbnail)try{const m=await t.config.getThumbnail(e);m?c.innerHTML=`
|
|
84
|
-
<div class="relative group">
|
|
85
|
-
<video class="w-full h-auto" controls preload="auto" muted>
|
|
86
|
-
<source src="${m}" type="${(r==null?void 0:r.type)||"video/mp4"}">
|
|
87
|
-
\u0412\u0430\u0448 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043D\u0435 \u043F\u043E\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0432\u0438\u0434\u0435\u043E.
|
|
88
|
-
</video>
|
|
89
|
-
<div class="absolute inset-0 bg-black bg-opacity-20 opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center pointer-events-none">
|
|
90
|
-
<div class="bg-white bg-opacity-90 rounded-full p-3">
|
|
91
|
-
<svg class="w-8 h-8 text-gray-800" fill="currentColor" viewBox="0 0 24 24">
|
|
92
|
-
<path d="M8 5v14l11-7z"/>
|
|
93
|
-
</svg>
|
|
94
|
-
</div>
|
|
95
|
-
</div>
|
|
96
|
-
</div>
|
|
97
|
-
`:c.innerHTML=`<div class="aspect-video flex items-center justify-center text-gray-400"><div class="text-center"><div class="text-4xl mb-2">\u{1F3A5}</div><div class="text-sm">${l}</div></div></div>`}catch(m){console.warn("getThumbnail failed for video",e,m),c.innerHTML=`<div class="aspect-video flex items-center justify-center text-gray-400"><div class="text-center"><div class="text-4xl mb-2">\u{1F3A5}</div><div class="text-sm">${l}</div></div></div>`}else c.innerHTML=`<div class="aspect-video flex items-center justify-center text-gray-400"><div class="text-center"><div class="text-4xl mb-2">\u{1F3A5}</div><div class="text-sm">${l}</div></div></div>`;else{const m=a?"\u{1F3A8}":"\u{1F4C1}",v=a?"PSD File":"Document";a?c.innerHTML=`
|
|
98
|
-
<div class="flex items-center space-x-3">
|
|
99
|
-
<div class="text-3xl text-gray-400">${m}</div>
|
|
100
|
-
<div class="flex-1 min-w-0">
|
|
101
|
-
<div class="text-sm font-medium text-gray-900 truncate">${l}</div>
|
|
102
|
-
<div class="text-xs text-gray-500">${v}</div>
|
|
103
|
-
</div>
|
|
104
|
-
</div>
|
|
105
|
-
`:c.innerHTML=`<div class="aspect-video flex items-center justify-center text-gray-400"><div class="text-center"><div class="text-4xl mb-2">${m}</div><div class="text-sm">${l}</div><div class="text-xs text-gray-500 mt-1">${v}</div></div></div>`}const h=document.createElement("p");h.className=a?"hidden":"text-sm font-medium text-gray-900 text-center",h.textContent=l;const p=document.createElement("button");return p.className="w-full px-3 py-2 text-sm bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors",p.textContent=$("downloadButton",t),p.onclick=m=>{m.preventDefault(),m.stopPropagation(),t.config.downloadFile?t.config.downloadFile(e,l):Te(e,l,t)},i.appendChild(c),i.appendChild(h),i.appendChild(p),i}function P(e,t,n,o){T(e);const s=!e.classList.contains("grid");if((!t||t.length===0)&&s){const a=document.createElement("div");a.className="grid grid-cols-4 gap-3 mb-3";for(let u=0;u<4;u++){const d=document.createElement("div");d.className="aspect-square bg-gray-100 border-2 border-dashed border-gray-300 rounded flex items-center justify-center cursor-pointer hover:border-gray-400 transition-colors";const h=document.createElementNS("http://www.w3.org/2000/svg","svg");h.setAttribute("class","w-12 h-12 text-gray-400"),h.setAttribute("fill","currentColor"),h.setAttribute("viewBox","0 0 24 24");const p=document.createElementNS("http://www.w3.org/2000/svg","path");p.setAttribute("d","M21 19V5c0-1.1-.9-2-2-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2zM8.5 13.5l2.5 3.01L14.5 12l4.5 6H5l3.5-4.5z"),h.appendChild(p),d.appendChild(h),d.onclick=()=>{let m=e.parentElement;for(;m&&!m.classList.contains("space-y-2");)m=m.parentElement;!m&&e.classList.contains("space-y-2")&&(m=e);const v=m==null?void 0:m.querySelector('input[type="file"]');v&&v.click()},a.appendChild(d)}const i=document.createElement("div");i.className="text-center text-xs text-gray-600";const c=document.createElement("span");c.className="underline cursor-pointer",c.textContent=$("uploadText",n),c.onclick=u=>{u.stopPropagation();let d=e.parentElement;for(;d&&!d.classList.contains("space-y-2");)d=d.parentElement;!d&&e.classList.contains("space-y-2")&&(d=e);const h=d==null?void 0:d.querySelector('input[type="file"]');h&&h.click()},i.appendChild(c),i.appendChild(document.createTextNode(` ${$("dragDropText",n)}`)),e.appendChild(a),e.appendChild(i);return}e.className="files-list grid grid-cols-4 gap-3 mt-2";const r=t?t.length:0,l=(Math.floor(r/4)+1)*4;for(let a=0;a<l;a++){const i=document.createElement("div");if(t&&a<t.length){const c=t[a],u=n.resourceIndex.get(c);if(i.className="resource-pill aspect-square bg-gray-100 rounded-lg overflow-hidden relative group border border-gray-300",i.dataset.resourceId=c,Le(i,c,u,n).catch(d=>{console.error("Failed to render thumbnail:",d),i.innerHTML=`<div class="flex flex-col items-center justify-center h-full text-gray-400">
|
|
106
|
-
<div class="text-2xl mb-1">\u{1F4C1}</div>
|
|
107
|
-
<div class="text-xs">Preview error</div>
|
|
108
|
-
</div>`}),o){const d=document.createElement("div");d.className="absolute inset-0 bg-black bg-opacity-50 opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center";const h=document.createElement("button");h.className="bg-red-600 text-white px-2 py-1 rounded text-xs",h.textContent=$("removeElement",n),h.onclick=p=>{p.stopPropagation(),o(c)},d.appendChild(h),i.appendChild(d)}}else i.className="aspect-square bg-gray-100 border-2 border-dashed border-gray-300 rounded-lg flex items-center justify-center cursor-pointer hover:border-gray-400 transition-colors",i.innerHTML='<svg class="w-12 h-12 text-gray-400" fill="currentColor" viewBox="0 0 24 24"><path d="M21 19V5c0-1.1-.9-2-2-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2zM8.5 13.5l2.5 3.01L14.5 12l4.5 6H5l3.5-4.5z"/></svg>',i.onclick=()=>{let c=e.parentElement;for(;c&&!c.classList.contains("space-y-2");)c=c.parentElement;!c&&e.classList.contains("space-y-2")&&(c=e);const u=c==null?void 0:c.querySelector('input[type="file"]');u&&u.click()};e.appendChild(i)}}function V(e,t="w-12 h-12"){e.innerHTML=`<div class="flex flex-col items-center justify-center h-full text-gray-400">
|
|
109
|
-
<svg class="${t} text-red-400" fill="currentColor" viewBox="0 0 24 24">
|
|
110
|
-
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"/>
|
|
111
|
-
</svg>
|
|
112
|
-
<div class="text-xs mt-1 text-red-600">Preview error</div>
|
|
113
|
-
</div>`}async function Le(e,t,n,o){var s,r;if(n&&(s=n.type)!=null&&s.startsWith("image/"))if(n.file&&n.file instanceof File){const l=document.createElement("img");l.className="w-full h-full object-contain",l.alt=n.name;const a=new FileReader;a.onload=i=>{var c;l.src=((c=i.target)==null?void 0:c.result)||""},a.readAsDataURL(n.file),e.appendChild(l)}else if(o.config.getThumbnail)try{const l=await o.config.getThumbnail(t);if(l){const a=document.createElement("img");a.className="w-full h-full object-contain",a.alt=n.name,a.src=l,e.appendChild(a)}else e.innerHTML=`<div class="flex flex-col items-center justify-center h-full text-gray-400">
|
|
114
|
-
<svg class="w-12 h-12" fill="currentColor" viewBox="0 0 24 24">
|
|
115
|
-
<path d="M21 19V5c0-1.1-.9-2-2-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2zM8.5 13.5l2.5 3.01L14.5 12l4.5 6H5l3.5-4.5z"/>
|
|
116
|
-
</svg>
|
|
117
|
-
</div>`}catch(l){const a=l instanceof Error?l:new Error(String(l));o.config.onThumbnailError&&o.config.onThumbnailError(a,t),V(e)}else e.innerHTML=`<div class="flex flex-col items-center justify-center h-full text-gray-400">
|
|
118
|
-
<svg class="w-12 h-12" fill="currentColor" viewBox="0 0 24 24">
|
|
119
|
-
<path d="M21 19V5c0-1.1-.9-2-2-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2zM8.5 13.5l2.5 3.01L14.5 12l4.5 6H5l3.5-4.5z"/>
|
|
120
|
-
</svg>
|
|
121
|
-
</div>`;else if(n&&(r=n.type)!=null&&r.startsWith("video/"))if(n.file&&n.file instanceof File){const l=URL.createObjectURL(n.file);e.innerHTML=`
|
|
122
|
-
<div class="relative group h-full w-full">
|
|
123
|
-
<video class="w-full h-full object-contain" preload="metadata" muted>
|
|
124
|
-
<source src="${l}" type="${n.type}">
|
|
125
|
-
</video>
|
|
126
|
-
<div class="absolute inset-0 bg-black bg-opacity-30 flex items-center justify-center">
|
|
127
|
-
<div class="bg-white bg-opacity-90 rounded-full p-1">
|
|
128
|
-
<svg class="w-4 h-4 text-gray-800" fill="currentColor" viewBox="0 0 24 24">
|
|
129
|
-
<path d="M8 5v14l11-7z"/>
|
|
130
|
-
</svg>
|
|
131
|
-
</div>
|
|
132
|
-
</div>
|
|
133
|
-
</div>
|
|
134
|
-
`}else if(o.config.getThumbnail)try{const l=await o.config.getThumbnail(t);l?e.innerHTML=`
|
|
135
|
-
<div class="relative group h-full w-full">
|
|
136
|
-
<video class="w-full h-full object-contain" preload="metadata" muted>
|
|
137
|
-
<source src="${l}" type="${n.type}">
|
|
138
|
-
</video>
|
|
139
|
-
<div class="absolute inset-0 bg-black bg-opacity-30 flex items-center justify-center">
|
|
140
|
-
<div class="bg-white bg-opacity-90 rounded-full p-1">
|
|
141
|
-
<svg class="w-4 h-4 text-gray-800" fill="currentColor" viewBox="0 0 24 24">
|
|
142
|
-
<path d="M8 5v14l11-7z"/>
|
|
143
|
-
</svg>
|
|
144
|
-
</div>
|
|
145
|
-
</div>
|
|
146
|
-
</div>
|
|
147
|
-
`:e.innerHTML=`<div class="flex flex-col items-center justify-center h-full text-gray-400">
|
|
148
|
-
<svg class="w-8 h-8" fill="currentColor" viewBox="0 0 24 24">
|
|
149
|
-
<path d="M8 5v14l11-7z"/>
|
|
150
|
-
</svg>
|
|
151
|
-
<div class="text-xs mt-1">${(n==null?void 0:n.name)||"Video"}</div>
|
|
152
|
-
</div>`}catch(l){const a=l instanceof Error?l:new Error(String(l));o.config.onThumbnailError&&o.config.onThumbnailError(a,t),V(e,"w-8 h-8")}else e.innerHTML=`<div class="flex flex-col items-center justify-center h-full text-gray-400">
|
|
153
|
-
<svg class="w-8 h-8" fill="currentColor" viewBox="0 0 24 24">
|
|
154
|
-
<path d="M8 5v14l11-7z"/>
|
|
155
|
-
</svg>
|
|
156
|
-
<div class="text-xs mt-1">${(n==null?void 0:n.name)||"Video"}</div>
|
|
157
|
-
</div>`;else e.innerHTML=`<div class="flex flex-col items-center justify-center h-full text-gray-400">
|
|
158
|
-
<div class="text-2xl mb-1">\u{1F4C1}</div>
|
|
159
|
-
<div class="text-xs">${(n==null?void 0:n.name)||"File"}</div>
|
|
160
|
-
</div>`}function j(e,t){e.innerHTML=`
|
|
161
|
-
<div class="flex flex-col items-center justify-center h-full text-gray-400">
|
|
162
|
-
<svg class="w-6 h-6 mb-2" fill="currentColor" viewBox="0 0 24 24">
|
|
163
|
-
<path d="M21 19V5c0-1.1-.9-2-2-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2zM8.5 13.5l2.5 3.01L14.5 12l4.5 6H5l3.5-4.5z"/>
|
|
164
|
-
</svg>
|
|
165
|
-
<div class="text-sm text-center">${$("clickDragText",t)}</div>
|
|
166
|
-
</div>
|
|
167
|
-
`}async function _(e,t,n,o,s=null,r){var l,a;let i;if(o.config.uploadFile)try{if(i=await o.config.uploadFile(e),typeof i!="string")throw new Error("Upload handler must return a string resource ID")}catch(u){const d=u instanceof Error?u:new Error(String(u));throw o.config.onUploadError&&o.config.onUploadError(d,e),new Error(`File upload failed: ${d.message}`)}else throw new Error("No upload handler configured. Set uploadHandler via FormBuilder.setUploadHandler()");o.resourceIndex.set(i,{name:e.name,type:e.type,size:e.size,uploadedAt:new Date,file:e});let c=(l=t.parentElement)==null?void 0:l.querySelector('input[type="hidden"]');c||(c=document.createElement("input"),c.type="hidden",c.name=n,(a=t.parentElement)==null||a.appendChild(c)),c.value=i,O(t,i,o,{fileName:e.name,isReadonly:!1,deps:s}).catch(console.error),r&&!o.config.readonly&&r.triggerOnChange(n,i)}function z(e,t){e.addEventListener("dragover",n=>{n.preventDefault(),e.classList.add("border-blue-500","bg-blue-50")}),e.addEventListener("dragleave",n=>{n.preventDefault(),e.classList.remove("border-blue-500","bg-blue-50")}),e.addEventListener("drop",n=>{var o;n.preventDefault(),e.classList.remove("border-blue-500","bg-blue-50"),(o=n.dataTransfer)!=null&&o.files&&t(n.dataTransfer.files)})}function Se(e,t,n){const o=e.querySelector(".delete-overlay");o&&o.remove();const s=document.createElement("div");s.className="delete-overlay absolute inset-0 bg-black bg-opacity-50 opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center";const r=document.createElement("button");r.className="bg-red-600 text-white px-3 py-1 rounded text-sm hover:bg-red-700 transition-colors",r.textContent=$("removeElement",t),r.onclick=l=>{l.stopPropagation(),n()},s.appendChild(r),e.appendChild(s)}async function W(e,t){if(t.config.uploadFile)try{const n=await t.config.uploadFile(e);if(typeof n!="string")throw new Error("Upload handler must return a string resource ID");return n}catch(n){const o=n instanceof Error?n:new Error(String(n));throw t.config.onUploadError&&t.config.onUploadError(o,e),new Error(`File upload failed: ${o.message}`)}else throw new Error("No upload handler configured. Set uploadHandler via FormBuilder.setUploadHandler()")}async function Te(e,t,n){try{let o=null;if(n.config.getDownloadUrl?o=n.config.getDownloadUrl(e):n.config.getThumbnail&&(o=await n.config.getThumbnail(e)),o){const s=o.startsWith("http")?o:new URL(o,window.location.href).href,r=await fetch(s);if(!r.ok)throw new Error(`HTTP error! status: ${r.status}`);const l=await r.blob();Ne(l,t)}else throw new Error("No download URL available for resource")}catch(o){const s=o instanceof Error?o:new Error(String(o));throw n.config.onDownloadError&&n.config.onDownloadError(s,e,t),console.error(`File download failed for ${t}:`,s),s}}function Ne(e,t){try{const n=URL.createObjectURL(e),o=document.createElement("a");o.href=n,o.download=t,o.style.display="none",document.body.appendChild(o),o.click(),document.body.removeChild(o),setTimeout(()=>{URL.revokeObjectURL(n)},100)}catch(n){throw new Error(`Blob download failed: ${n.message}`)}}function G(e,t){e.length>0&&e.forEach(n=>{var o;if(!t.resourceIndex.has(n)){const s=n.split("/").pop()||"file",r=(o=s.split(".").pop())==null?void 0:o.toLowerCase();let l="application/octet-stream";r&&(["jpg","jpeg","png","gif","webp"].includes(r)?l=`image/${r==="jpg"?"jpeg":r}`:["mp4","webm","mov","avi"].includes(r)&&(l=`video/${r==="mov"?"quicktime":r}`)),t.resourceIndex.set(n,{name:s,type:l,size:0,uploadedAt:new Date,file:void 0})}})}function Ae(e,t,n,o,s,r){var l;if(!s.resourceIndex.has(e)){const i=e.split("/").pop()||"file",c=(l=i.split(".").pop())==null?void 0:l.toLowerCase();let u="application/octet-stream";c&&(["jpg","jpeg","png","gif","webp"].includes(c)?u=`image/${c==="jpg"?"jpeg":c}`:["mp4","webm","mov","avi"].includes(c)&&(u=`video/${c==="mov"?"quicktime":c}`)),s.resourceIndex.set(e,{name:i,type:u,size:0,uploadedAt:new Date,file:void 0})}O(t,e,s,{fileName:e,isReadonly:!1,deps:r}).catch(console.error);const a=document.createElement("input");a.type="hidden",a.name=n,a.value=e,o.appendChild(a)}function Y(e,t,n,o,s,r){z(e,async l=>{const a=Array.from(l);for(const i of a){const c=await W(i,n);n.resourceIndex.set(c,{name:i.name,type:i.type,size:i.size,uploadedAt:new Date,file:void 0}),t.push(c)}o(),r&&s&&!n.config.readonly&&r.triggerOnChange(s,t)})}function J(e,t,n,o,s,r){e.onchange=async()=>{if(e.files)for(const l of Array.from(e.files)){const a=await W(l,n);n.resourceIndex.set(a,{name:l.name,type:l.type,size:l.size,uploadedAt:new Date,file:void 0}),t.push(a)}o(),e.value="",r&&s&&!n.config.readonly&&r.triggerOnChange(s,t)}}function qe(e,t,n,o){var s;const r=t.state;if(r.config.readonly){const l=t.prefill[e.key];if(l)B(l,r).then(a=>{n.appendChild(a)}).catch(a=>{console.error("Failed to render file preview:",a);const i=document.createElement("div");i.className="aspect-video bg-gray-100 rounded-lg flex items-center justify-center text-gray-500",i.innerHTML='<div class="text-center">Preview unavailable</div>',n.appendChild(i)});else{const a=document.createElement("div");a.className="aspect-video bg-gray-100 rounded-lg flex items-center justify-center text-gray-500",a.innerHTML=`<div class="text-center">${$("noFileSelected",r)}</div>`,n.appendChild(a)}}else{const l=document.createElement("div");l.className="space-y-2";const a=document.createElement("input");a.type="file",a.name=o,a.style.display="none",e.accept&&(a.accept=typeof e.accept=="string"?e.accept:((s=e.accept.extensions)==null?void 0:s.map(v=>`.${v}`).join(","))||"");const i=document.createElement("div");i.className="file-preview-container w-full aspect-square max-w-xs bg-gray-100 rounded-lg overflow-hidden relative group cursor-pointer";const c=t.prefill[e.key],u=()=>a.click(),d=v=>{if(v.length>0){const b={picker:a,fileUploadHandler:u,dragHandler:d};_(v[0],i,o,r,b,t.instance)}};c?Ae(c,i,o,l,r,{picker:a,fileUploadHandler:u,dragHandler:d}):j(i,r),i.onclick=u,z(i,d),a.onchange=()=>{if(a.files&&a.files.length>0){const v={picker:a,fileUploadHandler:u,dragHandler:d};_(a.files[0],i,o,r,v,t.instance)}},l.appendChild(i),l.appendChild(a);const h=document.createElement("p");h.className="text-xs text-gray-600 mt-2 text-center",h.innerHTML=`<span class="underline cursor-pointer">${$("uploadText",r)}</span> ${$("dragDropTextSingle",r)}`;const p=h.querySelector("span");p&&(p.onclick=()=>a.click()),l.appendChild(h);const m=document.createElement("p");m.className="text-xs text-gray-500 mt-1 text-center",m.textContent=k(e),l.appendChild(m),n.appendChild(l)}}function He(e,t,n,o){var s;const r=t.state;if(r.config.readonly){const l=document.createElement("div");l.className="space-y-4";const a=t.prefill[e.key]||[];a.length>0?a.forEach(i=>{B(i,r).then(c=>{l.appendChild(c)}).catch(c=>{console.error("Failed to render file preview:",c)})}):l.innerHTML=`<div class="aspect-video bg-gray-100 rounded-lg flex items-center justify-center text-gray-500"><div class="text-center">${$("noFilesSelected",r)}</div></div>`,n.appendChild(l)}else{let l=function(){P(u,d,r,p=>{const m=d.indexOf(p);m>-1&&d.splice(m,1),l()})};const a=document.createElement("div");a.className="space-y-2";const i=document.createElement("input");i.type="file",i.name=o,i.multiple=!0,i.style.display="none",e.accept&&(i.accept=typeof e.accept=="string"?e.accept:((s=e.accept.extensions)==null?void 0:s.map(p=>`.${p}`).join(","))||"");const c=document.createElement("div");c.className="border-2 border-dashed border-gray-300 rounded-lg p-3 hover:border-gray-400 transition-colors";const u=document.createElement("div");u.className="files-list";const d=t.prefill[e.key]||[];G(d,r),l(),Y(c,d,r,l,o,t.instance),J(i,d,r,l,o,t.instance),c.appendChild(u),a.appendChild(c),a.appendChild(i);const h=document.createElement("p");h.className="text-xs text-gray-500 mt-1 text-center",h.textContent=k(e),a.appendChild(h),n.appendChild(a)}}function Me(e,t,n,o){var s,r,l;const a=t.state,i=(s=e.minCount)!=null?s:0,c=(r=e.maxCount)!=null?r:1/0;if(a.config.readonly){const u=document.createElement("div");u.className="space-y-4";const d=t.prefill[e.key]||[];d.length>0?d.forEach(h=>{B(h,a).then(p=>{u.appendChild(p)}).catch(p=>{console.error("Failed to render file preview:",p)})}):u.innerHTML=`<div class="aspect-video bg-gray-100 rounded-lg flex items-center justify-center text-gray-500"><div class="text-center">${$("noFilesSelected",a)}</div></div>`,n.appendChild(u)}else{const u=document.createElement("div");u.className="space-y-2";const d=document.createElement("input");d.type="file",d.name=o,d.multiple=!0,d.style.display="none",e.accept&&(d.accept=typeof e.accept=="string"?e.accept:((l=e.accept.extensions)==null?void 0:l.map(v=>`.${v}`).join(","))||"");const h=document.createElement("div");h.className="files-list space-y-2",u.appendChild(d),u.appendChild(h);const p=Array.isArray(t.prefill[e.key])?[...t.prefill[e.key]]:[];G(p,a);const m=()=>{P(h,p,a,y=>{p.splice(p.indexOf(y),1),m()});const v=document.createElement("div");v.className="text-xs text-gray-500 mt-2 file-count-info";const b=`${p.length} file${p.length!==1?"s":""}`,x=i>0||c<1/0?` (${i}-${c} allowed)`:"";v.textContent=b+x;const g=u.querySelector(".file-count-info");g&&g.remove(),u.appendChild(v)};Y(h,p,a,m,o,t.instance),J(d,p,a,m,o,t.instance),m(),n.appendChild(u)}}function X(e,t,n){var o;const s=[],{scopeRoot:r,skipValidation:l,path:a}=n,i=e.type==="files"||"multiple"in e&&!!e.multiple,c=(u,d,h)=>{var p,m;if(l)return;const v="minCount"in h&&(p=h.minCount)!=null?p:0,b="maxCount"in h&&(m=h.maxCount)!=null?m:1/0;h.required&&d.length===0&&s.push(`${u}: required`),d.length<v&&s.push(`${u}: minimum ${v} files required`),d.length>b&&s.push(`${u}: maximum ${b} files allowed`)};if(i){const u=L(a,t),d=r.querySelector(`input[type="file"][name="${u}"]`),h=d==null?void 0:d.closest(".space-y-2"),p=(h==null?void 0:h.querySelector(".files-list"))||null,m=[];return p&&p.querySelectorAll(".resource-pill").forEach(v=>{const b=v.dataset.resourceId;b&&m.push(b)}),c(t,m,e),{value:m,errors:s}}else{const u=r.querySelector(`input[name$="${t}"][type="hidden"]`),d=(o=u==null?void 0:u.value)!=null?o:"";return!l&&e.required&&d===""?(s.push(`${t}: required`),{value:null,errors:s}):{value:d||null,errors:s}}}function Z(e,t,n,o){var s;const{scopeRoot:r,state:l}=o;if("multiple"in e&&e.multiple){if(!Array.isArray(n)){console.warn(`updateFileField: Expected array for multiple file field "${t}", got ${typeof n}`);return}n.forEach(a=>{var i;if(a&&typeof a=="string"&&!l.resourceIndex.has(a)){const c=a.split("/").pop()||"file",u=(i=c.split(".").pop())==null?void 0:i.toLowerCase();let d="application/octet-stream";u&&(["jpg","jpeg","png","gif","webp"].includes(u)?d=`image/${u==="jpg"?"jpeg":u}`:["mp4","webm","mov","avi"].includes(u)&&(d=`video/${u==="mov"?"quicktime":u}`)),l.resourceIndex.set(a,{name:c,type:d,size:0,uploadedAt:new Date,file:void 0})}}),console.info(`updateFileField: Multiple file field "${t}" updated. Preview update requires re-render.`)}else{const a=r.querySelector(`input[name="${t}"][type="hidden"]`);if(!a){console.warn(`updateFileField: Hidden input not found for file field "${t}"`);return}if(a.value=n!=null?String(n):"",n&&typeof n=="string"){if(!l.resourceIndex.has(n)){const i=n.split("/").pop()||"file",c=(s=i.split(".").pop())==null?void 0:s.toLowerCase();let u="application/octet-stream";c&&(["jpg","jpeg","png","gif","webp"].includes(c)?u=`image/${c==="jpg"?"jpeg":c}`:["mp4","webm","mov","avi"].includes(c)&&(u=`video/${c==="mov"?"quicktime":c}`)),l.resourceIndex.set(n,{name:i,type:u,size:0,uploadedAt:new Date,file:void 0})}console.info(`updateFileField: File field "${t}" updated. Preview update requires re-render.`)}}}let R=null;function Be(e){R=e}function q(e,t){if(!R)throw new Error("renderElement not initialized. Import from components/index.ts");return R(e,t)}function K(e,t,n,o){var s;const r=document.createElement("div");r.className="border border-gray-200 rounded-lg p-4 bg-gray-50",r.setAttribute("data-container",o);const l=document.createElement("div");l.className="flex justify-between items-center mb-4";const a=document.createElement("div");a.className="flex-1";const i=document.createElement("div");i.className="space-y-4",r.appendChild(l),l.appendChild(a);const c={path:L(t.path,e.key),prefill:((s=t.prefill)==null?void 0:s[e.key])||{},state:t.state};e.elements.forEach(u=>{u.hidden||i.appendChild(q(u,c))}),r.appendChild(i),a.innerHTML=`<span>${e.label||e.key}</span>`,n.appendChild(r)}function Q(e,t,n,o){var s,r,l;const a=t.state,i=document.createElement("div");i.className="border border-gray-200 rounded-lg p-4 bg-gray-50";const c=document.createElement("div");c.className="flex justify-between items-center mb-4";const u=document.createElement("div");u.className="flex-1";const d=document.createElement("div");d.className="flex gap-2";const h=document.createElement("div");h.className="space-y-4",i.appendChild(c),c.appendChild(u),a.config.readonly||c.appendChild(d);const p=(s=e.minCount)!=null?s:0,m=(r=e.maxCount)!=null?r:1/0,v=Array.isArray((l=t.prefill)==null?void 0:l[e.key])?t.prefill[e.key]:null,b=()=>h.querySelectorAll(":scope > .containerItem").length,x=()=>{const y=document.createElement("button");return y.type="button",y.className="px-3 py-1.5 bg-blue-600 text-white text-sm rounded-lg hover:bg-blue-700 transition-colors",y.textContent=$("addElement",a),y.onclick=()=>{if(b()<m){const f=b(),C={state:t.state,path:L(t.path,`${e.key}[${f}]`),prefill:{}},E=document.createElement("div");if(E.className="containerItem border border-gray-300 rounded-lg p-4 bg-white",E.setAttribute("data-container-item",`${e.key}[${f}]`),e.elements.forEach(w=>{w.hidden||E.appendChild(q(w,C))}),!a.config.readonly){const w=document.createElement("button");w.type="button",w.className="absolute top-2 right-2 w-6 h-6 bg-red-500 text-white rounded-full text-xs hover:bg-red-600 transition-colors",w.textContent="\xD7",w.onclick=()=>{E.remove(),g()},E.style.position="relative",E.appendChild(w)}h.appendChild(E),g()}},y},g=()=>{const y=b(),f=d.querySelector("button");f&&(f.disabled=y>=m,f.style.opacity=y>=m?"0.5":"1"),u.innerHTML=`<span>${e.label||e.key}</span> <span class="text-sm text-gray-500">(${y}/${m===1/0?"\u221E":m})</span>`};if(a.config.readonly||d.appendChild(x()),v&&Array.isArray(v)&&v.forEach((y,f)=>{const C={state:t.state,path:L(t.path,`${e.key}[${f}]`),prefill:y||{}},E=document.createElement("div");if(E.className="containerItem border border-gray-300 rounded-lg p-4 bg-white",E.setAttribute("data-container-item",`${e.key}[${f}]`),e.elements.forEach(w=>{w.hidden||E.appendChild(q(w,C))}),!a.config.readonly){const w=document.createElement("button");w.type="button",w.className="absolute top-2 right-2 w-6 h-6 bg-red-500 text-white rounded-full text-xs hover:bg-red-600 transition-colors",w.textContent="\xD7",w.onclick=()=>{E.remove(),g()},E.style.position="relative",E.appendChild(w)}h.appendChild(E)}),!a.config.readonly)for(;b()<p;){const y=b(),f={state:t.state,path:L(t.path,`${e.key}[${y}]`),prefill:{}},C=document.createElement("div");C.className="containerItem border border-gray-300 rounded-lg p-4 bg-white",C.setAttribute("data-container-item",`${e.key}[${y}]`),e.elements.forEach(w=>{w.hidden||C.appendChild(q(w,f))});const E=document.createElement("button");E.type="button",E.className="absolute top-2 right-2 w-6 h-6 bg-red-500 text-white rounded-full text-xs hover:bg-red-600 transition-colors",E.textContent="\xD7",E.onclick=()=>{b()>p&&(C.remove(),g())},C.style.position="relative",C.appendChild(E),h.appendChild(C)}i.appendChild(h),g(),n.appendChild(i)}let D=null;function je(e){D=e}function ee(e,t,n){if(!D)throw new Error("validateElement not initialized. Should be set from FormBuilderInstance");return D(e,t,n)}function te(e,t,n){const o=[],{scopeRoot:s,skipValidation:r,path:l}=n;if(!("elements"in e))return{value:null,errors:o};const a=(i,c,u)=>{var d,h;if(r)return;const p="minCount"in u&&(d=u.minCount)!=null?d:0,m="maxCount"in u&&(h=u.maxCount)!=null?h:1/0;u.required&&c.length===0&&o.push(`${i}: required`),c.length<p&&o.push(`${i}: minimum ${p} items required`),c.length>m&&o.push(`${i}: maximum ${m} items allowed`)};if("multiple"in e&&e.multiple){const i=[],c=s.querySelectorAll("[data-container-item]"),u=Array.from(c).filter(d=>{const h=d.getAttribute("data-container-item");return h==null?void 0:h.startsWith(`${t}[`)}).length;for(let d=0;d<u;d++){const h={},p=s.querySelector(`[data-container-item="${t}[${d}]"]`)||s;e.elements.forEach(m=>{if(m.hidden||m.type==="hidden")h[m.key]=m.default!==void 0?m.default:null;else{const v=`${t}[${d}].${m.key}`;h[m.key]=ee({...m,key:v},{path:l},p)}}),i.push(h)}return a(t,i,e),{value:i,errors:o}}else{const i={},c=s.querySelector(`[data-container="${t}"]`)||s;return e.elements.forEach(u=>{if(u.hidden||u.type==="hidden")i[u.key]=u.default!==void 0?u.default:null;else{const d=`${t}.${u.key}`;i[u.key]=ee({...u,key:d},{path:l},c)}}),{value:i,errors:o}}}function ne(e,t,n,o){const{instance:s,scopeRoot:r}=o;if("elements"in e)if("multiple"in e&&e.multiple){if(!Array.isArray(n)){console.warn(`updateContainerField: Expected array for multiple container field "${t}", got ${typeof n}`);return}n.forEach((a,i)=>{A(a)&&e.elements.forEach(c=>{const u=c.key,d=`${t}[${i}].${u}`,h=a[u];h!==void 0&&s.updateField(d,h)})});const l=r.querySelectorAll(`[data-container-item^="${t}["]`);n.length!==l.length&&console.warn(`updateContainerField: Multiple container field "${t}" item count mismatch. Consider re-rendering for add/remove.`)}else{if(!A(n)){console.warn(`updateContainerField: Expected object for container field "${t}", got ${typeof n}`);return}e.elements.forEach(l=>{const a=l.key,i=`${t}.${a}`,c=n[a];c!==void 0&&s.updateField(i,c)})}}function ze(e,t,n,o){var s,r;typeof console!="undefined"&&console.warn&&console.warn(`[Form Builder] The "group" field type is deprecated and will be removed in a future version. Please use type: "container" with multiple: true instead. Field key: "${e.key}"`);const l={key:e.key,label:e.label,description:e.description,hint:e.hint,required:e.required,hidden:e.hidden,default:e.default,actions:e.actions,elements:e.elements,multiple:!!(e.repeat&&A(e.repeat)),minCount:(s=e.repeat)==null?void 0:s.min,maxCount:(r=e.repeat)==null?void 0:r.max};l.multiple?Q(l,t,n):K(l,t,n,o)}function re(e){var t,n;const o=e;return{type:"container",key:o.key,label:o.label,description:o.description,hint:o.hint,required:o.required,hidden:o.hidden,default:o.default,actions:o.actions,elements:o.elements,multiple:!!(o.repeat&&A(o.repeat)),minCount:(t=o.repeat)==null?void 0:t.min,maxCount:(n=o.repeat)==null?void 0:n.max}}function Re(e,t,n){typeof console!="undefined"&&console.warn&&console.warn(`[Form Builder] The "group" field type is deprecated. Please use type: "container" instead. Field key: "${t}"`);const o=re(e);return te(o,t,n)}function De(e,t,n,o){typeof console!="undefined"&&console.warn&&console.warn(`[Form Builder] The "group" field type is deprecated. Please use type: "container" instead. Field path: "${t}"`);const s=re(e);return ne(s,t,n,o)}function Ie(e,t){const n=document.getElementById(e);if(!n)return;const o=!n.classList.contains("hidden");if(document.querySelectorAll('[id^="tooltip-"]').forEach(u=>{u.classList.add("hidden")}),o)return;const s=t.getBoundingClientRect(),r=window.innerWidth,l=window.innerHeight;n&&n.parentElement!==document.body&&document.body.appendChild(n),n.style.visibility="hidden",n.style.position="fixed",n.classList.remove("hidden");const a=n.getBoundingClientRect();n.classList.add("hidden"),n.style.visibility="visible";let i=s.left,c=s.bottom+5;i+a.width>r&&(i=s.right-a.width),c+a.height>l&&(c=s.top-a.height-5),i<10&&(i=10),c<10&&(c=s.bottom+5),n.style.left=`${i}px`,n.style.top=`${c}px`,n.classList.remove("hidden"),setTimeout(()=>{n.classList.add("hidden")},25e3)}typeof document!="undefined"&&document.addEventListener("click",e=>{const t=e.target,n=t.closest("button")&&t.closest("button").onclick,o=t.closest('[id^="tooltip-"]');!n&&!o&&document.querySelectorAll('[id^="tooltip-"]').forEach(s=>{s.classList.add("hidden")})});function le(e,t){const n=document.createElement("div");n.className="mb-6 fb-field-wrapper";const o=document.createElement("div");o.className="flex items-center mb-2";const s=document.createElement("label");if(s.className="text-sm font-medium text-gray-900",s.textContent=e.label||e.key,e.required){const l=document.createElement("span");l.className="text-red-500 ml-1",l.textContent="*",s.appendChild(l)}if(o.appendChild(s),e.description||e.hint){const l=document.createElement("button");l.type="button",l.className="ml-2 text-gray-400 hover:text-gray-600",l.innerHTML='<svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-6h2v6zm0-8h-2V7h2v2z"/></svg>';const a=`tooltip-${e.key}-${Math.random().toString(36).substr(2,9)}`,i=document.createElement("div");i.id=a,i.className="hidden absolute z-50 bg-gray-200 text-gray-900 text-sm rounded-lg p-3 max-w-sm border border-gray-300 shadow-lg",i.style.position="fixed",i.textContent=e.description||e.hint||"Field information",document.body.appendChild(i),l.onclick=c=>{c.preventDefault(),c.stopPropagation(),Ie(a,l)},o.appendChild(l)}n.appendChild(o);const r=L(t.path,e.key);switch(e.type){case"text":"multiple"in e&&e.multiple?fe(e,t,n,r):pe(e,t,n,r);break;case"textarea":"multiple"in e&&e.multiple?ve(e,t,n,r):he(e,t,n,r);break;case"number":"multiple"in e&&e.multiple?xe(e,t,n,r):be(e,t,n,r);break;case"select":"multiple"in e&&e.multiple?$e(e,t,n,r):we(e,t,n,r);break;case"file":"multiple"in e&&e.multiple?Me(e,t,n,r):qe(e,t,n,r);break;case"files":He(e,t,n,r);break;case"group":ze(e,t,n,r);break;case"container":"multiple"in e&&e.multiple?Q(e,t,n):K(e,t,n,r);break;default:{const l=document.createElement("div");l.className="text-red-500 text-sm",l.textContent=`Unsupported field type: ${e.type}`,n.appendChild(l)}}return n}Be(le);const Ue={uploadFile:null,downloadFile:null,getThumbnail:null,getDownloadUrl:null,actionHandler:null,onChange:null,onFieldChange:null,onThumbnailError:null,onUploadError:null,onDownloadError:null,debounceMs:300,verboseErrors:!1,enableFilePreview:!0,maxPreviewSize:"200px",readonly:!1,locale:"en",translations:{en:{addElement:"Add Element",removeElement:"Remove",uploadText:"Upload",dragDropText:"or drag and drop files",dragDropTextSingle:"or drag and drop file",clickDragText:"Click or drag file",noFileSelected:"No file selected",noFilesSelected:"No files selected",downloadButton:"Download"},ru:{addElement:"\u0414\u043E\u0431\u0430\u0432\u0438\u0442\u044C \u044D\u043B\u0435\u043C\u0435\u043D\u0442",removeElement:"\u0423\u0434\u0430\u043B\u0438\u0442\u044C",uploadText:"\u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435",dragDropText:"\u0438\u043B\u0438 \u043F\u0435\u0440\u0435\u0442\u0430\u0449\u0438\u0442\u0435 \u0444\u0430\u0439\u043B\u044B",dragDropTextSingle:"\u0438\u043B\u0438 \u043F\u0435\u0440\u0435\u0442\u0430\u0449\u0438\u0442\u0435 \u0444\u0430\u0439\u043B",clickDragText:"\u041D\u0430\u0436\u043C\u0438\u0442\u0435 \u0438\u043B\u0438 \u043F\u0435\u0440\u0435\u0442\u0430\u0449\u0438\u0442\u0435 \u0444\u0430\u0439\u043B",noFileSelected:"\u0424\u0430\u0439\u043B \u043D\u0435 \u0432\u044B\u0431\u0440\u0430\u043D",noFilesSelected:"\u041D\u0435\u0442 \u0444\u0430\u0439\u043B\u043E\u0432",downloadButton:"\u0421\u043A\u0430\u0447\u0430\u0442\u044C"}},theme:{}};function Oe(e){return{schema:null,formRoot:null,resourceIndex:new Map,externalActions:null,version:"1.0.0",config:{...Ue,...e},debounceTimer:null}}function Pe(){const e=Date.now().toString(36),t=Math.random().toString(36).substring(2,9);return`inst-${e}-${t}`}const N={primaryColor:"#3b82f6",primaryHoverColor:"#2563eb",errorColor:"#ef4444",errorHoverColor:"#dc2626",successColor:"#10b981",borderColor:"#d1d5db",borderHoverColor:"#9ca3af",borderFocusColor:"#3b82f6",backgroundColor:"#ffffff",backgroundHoverColor:"#f9fafb",backgroundReadonlyColor:"#f3f4f6",textColor:"#1f2937",textSecondaryColor:"#6b7280",textPlaceholderColor:"#9ca3af",textDisabledColor:"#d1d5db",buttonBgColor:"#3b82f6",buttonTextColor:"#ffffff",buttonBorderColor:"#2563eb",buttonHoverBgColor:"#2563eb",buttonHoverBorderColor:"#1d4ed8",actionBgColor:"#ffffff",actionTextColor:"#374151",actionBorderColor:"#e5e7eb",actionHoverBgColor:"#f9fafb",actionHoverBorderColor:"#d1d5db",fileUploadBgColor:"#f3f4f6",fileUploadBorderColor:"#d1d5db",fileUploadTextColor:"#9ca3af",fileUploadHoverBorderColor:"#3b82f6",inputPaddingX:"0.75rem",inputPaddingY:"0.5rem",borderRadius:"0.5rem",borderWidth:"1px",fontSize:"0.875rem",fontSizeSmall:"0.75rem",fontSizeExtraSmall:"0.625rem",fontFamily:'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif',fontWeightNormal:"400",fontWeightMedium:"500",focusRingWidth:"2px",focusRingColor:"#3b82f6",focusRingOpacity:"0.5",transitionDuration:"200ms"};function Ve(e){const t={...N,...e},n=[];return Object.entries(t).forEach(([o,s])=>{const r=o.replace(/([A-Z])/g,"-$1").toLowerCase();n.push(` --fb-${r}: ${s};`)}),n.join(`
|
|
168
|
-
`)}function _e(e,t){const n=Ve(t);let o=e.querySelector("style[data-fb-theme]");o||(o=document.createElement("style"),o.setAttribute("data-fb-theme","true"),e.appendChild(o)),o.textContent=`
|
|
169
|
-
[data-fb-root="true"] {
|
|
170
|
-
${n}
|
|
171
|
-
}
|
|
172
|
-
`}const We={default:N,dark:{...N,primaryColor:"#60a5fa",primaryHoverColor:"#3b82f6",borderColor:"#4b5563",borderHoverColor:"#6b7280",borderFocusColor:"#60a5fa",backgroundColor:"#1f2937",backgroundHoverColor:"#374151",backgroundReadonlyColor:"#111827",textColor:"#f9fafb",textSecondaryColor:"#9ca3af",textPlaceholderColor:"#6b7280",fileUploadBgColor:"#374151",fileUploadBorderColor:"#4b5563",fileUploadTextColor:"#9ca3af"},klein:{...N,primaryColor:"#0066cc",primaryHoverColor:"#0052a3",errorColor:"#d32f2f",errorHoverColor:"#c62828",successColor:"#388e3c",borderColor:"#e0e0e0",borderHoverColor:"#bdbdbd",borderFocusColor:"#0066cc",borderRadius:"4px",fontSize:"16px",fontSizeSmall:"14px",fontFamily:'"Roboto", "Helvetica", "Arial", sans-serif'}};function oe(e,t=!1){e.style.cssText=`
|
|
173
|
-
background-color: var(--fb-action-bg-color);
|
|
174
|
-
color: var(--fb-action-text-color);
|
|
175
|
-
border: var(--fb-border-width) solid var(--fb-action-border-color);
|
|
176
|
-
padding: ${t?"0.5rem 1rem":"0.5rem 0.75rem"};
|
|
177
|
-
font-size: var(--fb-font-size);
|
|
178
|
-
font-weight: var(--fb-font-weight-medium);
|
|
179
|
-
border-radius: var(--fb-border-radius);
|
|
180
|
-
transition: all var(--fb-transition-duration);
|
|
181
|
-
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
|
|
182
|
-
`,e.addEventListener("mouseenter",()=>{e.style.backgroundColor="var(--fb-action-hover-bg-color)",e.style.borderColor="var(--fb-action-hover-border-color)"}),e.addEventListener("mouseleave",()=>{e.style.backgroundColor="var(--fb-action-bg-color)",e.style.borderColor="var(--fb-action-border-color)"})}const Ge={text:{validate:I,update:U},textarea:{validate:ge,update:ye},number:{validate:Ce,update:Ee},select:{validate:ke,update:Fe},file:{validate:X,update:Z},files:{validate:X,update:Z},container:{validate:te,update:ne},group:{validate:Re,update:De}};function ae(e){return Ge[e]||null}function Ye(e,t,n){const o=ae(e.type);return o&&o.validate?o.validate(e,t,n):null}function Je(e,t,n,o){const s=ae(e.type);return s&&s.update?(s.update(e,t,n,o),!0):!1}class H{constructor(t){this.instanceId=Pe(),this.state=Oe(t),this.state.config.verboseErrors&&(globalThis.__formBuilderInstances||(globalThis.__formBuilderInstances=new Set),globalThis.__formBuilderInstances.add(this.instanceId),globalThis.__formBuilderInstances.size>10&&console.warn(`[form-builder] ${globalThis.__formBuilderInstances.size} instances active. Possible memory leak - ensure you call destroy() when done.`))}getInstanceId(){return this.instanceId}getState(){return this.state}setFormRoot(t){this.state.formRoot=t}configure(t){Object.assign(this.state.config,t)}setUploadHandler(t){this.state.config.uploadFile=t}setDownloadHandler(t){this.state.config.downloadFile=t}setThumbnailHandler(t){this.state.config.getThumbnail=t}setActionHandler(t){this.state.config.actionHandler=t}setMode(t){this.state.config.readonly=t==="readonly"}setLocale(t){this.state.config.translations[t]&&(this.state.config.locale=t)}triggerOnChange(t,n){this.state.config.readonly||(this.state.debounceTimer!==null&&clearTimeout(this.state.debounceTimer),this.state.debounceTimer=setTimeout(()=>{const o=this.validateForm(!0);this.state.config.onChange&&this.state.config.onChange(o),this.state.config.onFieldChange&&t!==void 0&&n!==void 0&&this.state.config.onFieldChange(t,n,o),this.state.debounceTimer=null},this.state.config.debounceMs))}registerAction(t){if(!t||!t.value)throw new Error("Action must have a value property");this.state.externalActions||(this.state.externalActions=[]);const n=this.state.externalActions.findIndex(o=>o.value===t.value&&o.related_field===t.related_field);n>=0?this.state.externalActions[n]=t:this.state.externalActions.push(t)}findFormElementByFieldPath(t){if(!this.state.formRoot)return null;if(!this.state.config.readonly){let s=this.state.formRoot.querySelector(`[name="${t}"]`);if(s)return s;const r=[t,t.replace(/\[(\d+)\]/g,"[$1]"),t.replace(/\./g,"[")+"]".repeat((t.match(/\./g)||[]).length)];for(const l of r)if(s=this.state.formRoot.querySelector(`[name="${l}"]`),s)return s}const n=this.findSchemaElement(t);if(!n)return null;const o=this.state.formRoot.querySelectorAll(".fb-field-wrapper");for(const s of o){const r=n.label||n.key,l=s.querySelector("label");if(l&&(l.textContent===r||l.textContent===`${r}*`)){let a=s.querySelector(".field-placeholder");return a||(a=document.createElement("div"),a.className="field-placeholder",a.style.display="none",s.appendChild(a)),a}}return null}findSchemaElement(t){if(!this.state.schema||!this.state.schema.elements)return null;let n=this.state.schema.elements,o=null;const s=t.replace(/\[\d+\]/g,"").split(".").filter(Boolean);for(const r of s){if(o=n.find(l=>l.key===r)||null,!o)return null;"elements"in o&&o.elements&&(n=o.elements)}return o}resolveActionLabel(t,n,o,s=!1){if(o&&"actions"in o&&o.actions){const r=o.actions.find(l=>l.key===t);if(r&&r.label)return r.label}if(s&&this.state.schema&&"actions"in this.state.schema&&this.state.schema.actions){const r=this.state.schema.actions.find(l=>l.key===t);if(r&&r.label)return r.label}return n||t}renderFormLevelActions(t,n=[]){if(!this.state.formRoot)return;const o=this.state.formRoot.querySelector(".form-level-actions-container");o&&o.remove();const s=document.createElement("div");s.className="form-level-actions-container mt-6 pt-4 flex flex-wrap gap-3 justify-center",s.style.cssText=`
|
|
183
|
-
border-top: var(--fb-border-width) solid var(--fb-border-color);
|
|
184
|
-
`,t.forEach(r=>{const l=document.createElement("button");l.type="button",oe(l,!0);const a=n.includes(r),i=this.resolveActionLabel(r.key,r.label,null,a);l.textContent=i,l.addEventListener("click",c=>{c.preventDefault(),c.stopPropagation(),this.state.config.actionHandler&&typeof this.state.config.actionHandler=="function"&&this.state.config.actionHandler(r.value,r.key,null)}),s.appendChild(l)}),this.state.formRoot.appendChild(s)}renderExternalActions(){if(!this.state.externalActions||!Array.isArray(this.state.externalActions))return;const t=new Map,n=[],o=[];this.state.externalActions.forEach(r=>{!r.key||!r.value||(r.related_field?(t.has(r.related_field)||t.set(r.related_field,[]),t.get(r.related_field).push(r)):n.push(r))}),t.forEach((r,l)=>{const a=this.findFormElementByFieldPath(l);if(!a){console.warn(`External action: Could not find form element for field "${l}", treating as form-level actions`),o.push(...r);return}let i=a.closest(".fb-field-wrapper");if(i||(i=a.parentElement),!i){console.warn(`External action: Could not find wrapper for field "${l}"`);return}const c=i.querySelector(".external-actions-container");c&&c.remove();const u=document.createElement("div");u.className="external-actions-container mt-3 flex flex-wrap gap-2";const d=this.findSchemaElement(l);r.forEach(h=>{const p=document.createElement("button");p.type="button",oe(p,!1);const m=this.resolveActionLabel(h.key,h.label,d);p.textContent=m,p.addEventListener("click",v=>{v.preventDefault(),v.stopPropagation(),this.state.config.actionHandler&&typeof this.state.config.actionHandler=="function"&&this.state.config.actionHandler(h.value,h.key,h.related_field)}),u.appendChild(p)}),i.appendChild(u)});const s=[...n,...o];s.length>0&&this.renderFormLevelActions(s,n)}renderForm(t,n,o,s){const r=M(n);if(r.length>0){console.error("Schema validation errors:",r);return}this.state.formRoot=t,this.state.schema=n,this.state.externalActions=s||null,T(t),t.setAttribute("data-fb-root","true"),_e(t,this.state.config.theme);const l=document.createElement("div");l.className="space-y-6",n.elements.forEach(a=>{if(a.hidden)return;const i=le(a,{path:"",prefill:o||{},state:this.state,instance:this});l.appendChild(i)}),t.appendChild(l),this.state.config.readonly&&this.state.externalActions&&Array.isArray(this.state.externalActions)&&this.renderExternalActions()}validateForm(t=!1){if(!this.state.schema||!this.state.formRoot)return{valid:!0,errors:[],data:{}};const n=[],o={},s=(r,l,a=null)=>{const i=r.key,c={scopeRoot:a||this.state.formRoot,state:this.state,instance:this,path:l.path,skipValidation:t},u=Ye(r,i,c);return u!==null?(n.push(...u.errors),u.value):(console.warn(`Unknown field type "${r.type}" for key "${i}"`),null)};return je(s),this.state.schema.elements.forEach(r=>{r.hidden?o[r.key]=r.default!==void 0?r.default:null:o[r.key]=s(r,{path:""})}),{valid:n.length===0,errors:n,data:o}}getFormData(){return this.validateForm(!1)}submitForm(){const t=this.validateForm(!1);return t.valid&&typeof window!="undefined"&&window.parent&&window.parent.postMessage({type:"formSubmit",data:t.data,schema:this.state.schema},"*"),t}saveDraft(){const t=this.validateForm(!0);return typeof window!="undefined"&&window.parent&&window.parent.postMessage({type:"formDraft",data:t.data,schema:this.state.schema},"*"),t}clearForm(){if(!this.state.schema||!this.state.formRoot){console.warn("clearForm: Form not initialized. Call renderForm() first.");return}const t=this.state.schema,n=this.state.formRoot,o=this.state.externalActions,s=this.buildHiddenFieldsData(t.elements);this.renderForm(n,t,s,o||void 0)}buildHiddenFieldsData(t){const n={};for(const o of t){const s=o.key;if(o.hidden&&o.default!==void 0&&(n[s]=o.default),o.type==="container"||o.type==="group"){const r=o,l=this.buildHiddenFieldsData(r.elements);Object.keys(l).length>0&&(s in n?typeof n[s]=="object"&&n[s]!==null&&!Array.isArray(n[s])&&(n[s]={...n[s],...l}):n[s]=l)}}return n}setFormData(t){if(!this.state.schema||!this.state.formRoot){console.warn("setFormData: Form not initialized. Call renderForm() first.");return}for(const n in t)this.updateField(n,t[n])}updateField(t,n){if(!this.state.schema||!this.state.formRoot){console.warn("updateField: Form not initialized. Call renderForm() first.");return}const o=this.findSchemaElement(t);if(!o){console.warn(`updateField: Schema element not found for path "${t}"`);return}const s=this.findFormElementByFieldPath(t);if(!s){console.warn(`updateField: DOM element not found for path "${t}"`);return}this.updateFieldValue(s,o,t,n),(this.state.config.onChange||this.state.config.onFieldChange)&&this.triggerOnChange(t,n)}updateFieldValue(t,n,o,s){const r={scopeRoot:this.state.formRoot,state:this.state,instance:this,path:""};Je(n,o,s,r)||console.warn(`updateField: No updater found for field type "${n.type}" at path "${o}"`)}destroy(){var t;this.state.debounceTimer!==null&&(clearTimeout(this.state.debounceTimer),this.state.debounceTimer=null),this.state.resourceIndex.clear(),this.state.formRoot&&T(this.state.formRoot),this.state.formRoot=null,this.state.schema=null,this.state.externalActions=null,this.state.config.verboseErrors&&((t=globalThis.__formBuilderInstances)==null||t.delete(this.instanceId))}}function ie(e){return new H(e)}return typeof window!="undefined"&&(window.FormBuilder=H,window.createFormBuilder=ie,window.validateSchema=M),F.FormBuilderInstance=H,F.createFormBuilder=ie,F.default=H,F.defaultTheme=N,F.exampleThemes=We,F.validateSchema=M,Object.defineProperty(F,"__esModule",{value:!0}),F})({});
|