1import{_ as v,r as a,i as h,e as m,h as F,d as S,c as A}from"./app.6-zBPm55.js";import{d as I,y as $,J as E}from"./vendor.kmuNf4Gr.js";/* empty css */A(["XForm.mounted"]);const j={name:"XForm",emits:["submit","success","error"],props:{url:String,method:{type:String,default:"POST"},disableConfirmation:{type:Boolean,default:!1},options:{type:Object,default(){return{}}},preventDb:String,data:{type:[Object,FormData]},className:{type:String,default:""},multiple:Boolean,classNames:Array,attributesMeta:{type:Object},afAttributesMeta:{type:[Object,Array]},beforeSend:{type:Function},beforeSendAdditional:{type:Function},formDataSend:{type:Boolean,default:!1},checkUnsavedDataBeforeLeavePage:{type:Boolean,default:!0},notifyOnInvalid:{type:Boolean,default:!1},formatRequest:{type:Boolean,default:!1},formatExcludes:{type:Object,default:()=>({})}},provide(){return{fieldsNames:this.fieldsNames}},data(){return{dontSaveChangesState:!1,isProcessing:!1}},computed:{fieldsNames(){const i=(t,e)=>Object.fromEntries(Object.entries(e).flatMap(([s,r])=>r===null||typeof r!="object"||Array.isArray(r)?[[s,`${t}[${s}]`]]:Object.keys(r).map(n=>[`${s}[${n}]`,`${t}[${s}][${n}]`])));return this.multiple?Object.fromEntries(Object.entries(this.data).flatMap(([t,e])=>Object.entries(i(t,e)))):this.data?i(this.className,this.data):{}}},mounted(...i){return a("XForm.mounted",()=>{this.validateInputs(),setTimeout(()=>{this.validateInputs()},1e3),this.checkUnsavedDataBeforeLeavePage&&(setTimeout(()=>{const{form:t}=this.$refs;t==null||t.querySelectorAll("input:enabled:not([data-silent-input]), select:enabled:not([data-silent-input]), textarea:enabled:not([data-silent-input])").forEach(e=>{e.addEventListener("change",()=>{if(this.dontSaveChangesState)return!1;t.setAttribute("data-form-changed","1")})})},4e3),this.$refs.form.addEventListener("submit",this.submitAfterEnter))},this,i)},methods:{submitAfterEnter(...i){return a("XForm.submitAfterEnter",t=>{t.preventDefault(),this.submit()},this,i)},submit(...i){return a("XForm.submit",()=>{if(this.isProcessing)return;this.$emit("submit"),this.isProcessing=!0;const t=this.$refs.form.reportValidity();if(window.globalEditor&&window.globalEditor.$oel){const e=window.globalEditor,s=e.$oel[0];if(this.$refs.form&&this.$refs.form.contains(s)&&e.events.trigger("form.submit")===!1){this.isProcessing=!1;return}}if(t){let e=this.getChangedValuesToSend();if(typeof this.beforeSend=="function"&&(e=this.beforeSend(e),e===!1)){this.isProcessing=!1;return}let s=new FormData;if(e===null||typeof e=="object"&&Object.keys(e).length===0){let n=this.getOnlyClassName();s.append(`${n}[]`,"")}e instanceof FormData?s=e:this.objectToFormData(s,e,this.className),Array.from(s.entries()).length===0&&s.append(`${this.className}[]`,""),typeof this.beforeSendAdditional=="function"&&(s=this.beforeSendAdditional(s)),S({url:this.url,method:this.method,data:s}).then(n=>{var o;(n.result||(o=n.data)!=null&&o.result)&&(this.changeDefaultValues(e),this.resetDataFormChanged()),this.isProcessing=!1,this.$emit("success",n)}).catch(n=>{this.$emit("error",n)}).finally(()=>{this.isProcessing=!1})}else return this.isProcessing=!1,!1},this,i)},getOnlyClassName(...i){return a("XForm.getOnlyClassName",()=>this.className.split("[")[0],this,i)},resetDataFormChanged(...i){return a("XForm.resetDataFormChanged",()=>{this.$refs.form.removeAttribute("data-form-changed")},this,i)},changeDefaultValues(...i){return a("XForm.changeDefaultValues",(t,e=this.attributesMeta,s="default")=>{this.attributesMeta!==void 0&&Object.entries(t).forEach(([r,n])=>{r.indexOf("additional_attributes")!==-1?this.changeDefaultValues(n,this.afAttributesMeta,"value"):h(e,r)&&(e[r][s]=this.prepareValueToSet(n,e[r]))})},this,i)},objectToFormData(...i){return a("XForm.objectToFormData",(t,e,s)=>{if(e!=null){if(e instanceof Array&&m(e)&&t.append(s,""),this.formatRequest){e=F(e);for(let r in e)this.formatExcludes[r]&&(e[this.formatExcludes[r]]=e[r],e[r]=void 0)}if(typeof e=="object"&&!(e instanceof Date)&&!(e instanceof File)&&!Array.isArray(e))Object.keys(e).forEach(r=>{let n=s?`${s}[${r}]`:r;this.objectToFormData(t,e[r],n)});else if(Array.isArray(e)){t.append(s,"");
1for(let r of e.keys()){if(typeof e[r]!="object"){t.append(`${s}[]`,e[r]);continue}if(Object.keys(e[r]).length===2&&h(e[r],"id")&&h(e[r],"text")){t.append(`${s}[]`,e[r].id);continue}this.objectToFormData(t,e[r],`${s}[${r}]`)}}else t.append(s,e)}},this,i)},validateInputs(...i){return a("XForm.validateInputs",()=>{var e;const t=(e=this.$refs.form)==null?void 0:e.querySelectorAll("input, select, textarea");t&&(t.forEach(s=>{s.removeEventListener("invalid",this.processInvalidInputs),s.addEventListener("invalid",this.processInvalidInputs)}),typeof window.splynx_event_bus<"u"&&(window.splynx_event_bus.off("invalid",this.processInvalidInputs),window.splynx_event_bus.on("invalid",this.processInvalidInputs)))},this,i)},processInvalidInputs(...i){return a("XForm.processInvalidInputs",t=>{var s;if(!((s=this.$refs.form)!=null&&s.contains(t.target)))return;let e=t.target.closest(".form-group");e||(e=t.target.parentElement),this.notifyOnInvalid&&setTimeout(()=>{const r=t.target.getAttribute("data-message"),n=Array.from(document.querySelectorAll(".toaster-content > div")).find(o=>o.textContent.includes(r));r&&!n&&window.show_error(r,4)},50),e&&e.classList.add("has-error"),t.target.removeEventListener("change",this.processChangeInvalidInput),t.target.addEventListener("change",this.processChangeInvalidInput)},this,i)},processChangeInvalidInput(...i){return a("XForm.processChangeInvalidInput",t=>{const e=t.target.closest(".has-error");e&&e.classList.remove("has-error")},this,i)},getChangedValuesToSend(...i){return a("XForm.getChangedValuesToSend",(t=this.data,e=this.className)=>{let s={};
1if(this.formDataSend)return t;for(let r in t){let n="additional_attributes",o=t[r];if(this.formatRequest&&(n="additionalAttributes"),r.indexOf(n)!==-1){s[r]={};for(let c in o){let f=this.afAttributesMeta[c];this.multiple&&(f=this.afAttributesMeta[e][c]);let b=f.value,g=h(f,"ajax")&&f.ajax,l=o[c];if(g)if(Array.isArray(l))if(f.type==="relation_multiple"){let d=[];l.forEach(y=>{d.push(y.id)}),l=d}else{let d=l[0];typeof d=="object"?l=d.id:l=d}else typeof l=="object"&&(l=l.id);this.valueIsChanged(l,b)&&(s[r][c]=this.prepareValueToSend(l,f))}continue}let u=this.attributesMeta[r];if(this.multiple&&(u=this.attributesMeta[e][r]),m(u)){console.warn(`Empty attribute meta: ${r}`),s[r]=this.prepareValueToSend(o,null);continue}let p=u.default;if(u.forceSend){s[r]=this.prepareValueToSend(o,u);continue}this.valueIsChanged(o,p)&&(s[r]=this.prepareValueToSend(o,u))}return s},this,i)},valueIsChanged(...i){return a("XForm.valueIsChanged",(t,e)=>t instanceof Array?e instanceof Array?!this.arraysIsEqual(t,e):t.length>0:t!==e,this,i)},arraysIsEqual(...i){return a("XForm.arraysIsEqual",(t,e)=>{if(t.length!==e.length)return!1;let s=e.map(r=>r.toString());for(let r=0;r<t.length;r++)if(!s.includes(t[r].toString()))return!1;return!0},this,i)},getTypeFromMeta(...i){return a("XForm.getTypeFromMeta",t=>h(t,["rule","type"])?t.rule.type:h(t,["type"])?t.type:null,this,i)},prepareValueToSend(...i){return a("XForm.prepareValueToSend",(t,e)=>this.getTypeFromMeta(e)==="boolean"?t!="0"&&t?"1":"0":t,this,i)}
1,prepareValueToSet(...i){return a("XForm.prepareValueToSet",(t,e)=>this.getTypeFromMeta(e)==="boolean"?t?1:0:t,this,i)}},watch:{isProcessing(i){let t;this.preventDb?t=document.querySelectorAll(this.preventDb):t=this.$el.querySelectorAll("button"),!(!t||t.length===0)&&t.forEach(e=>{i?e.setAttribute("disabled","disabled"):e.removeAttribute("disabled")})}}},T=["method","action"];function O(i,t,e,s,r,n){return I(),$("form",{ref:"form",method:e.method,action:e.url,role:"form"},[E(i.$slots,"default")],8,T)}const D=v(j,[["render",O]]);export{D as XForm,D as default}; 2//# sourceMappingURL=index.CKGmuCIR.js.map
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.