UNPKG

form-designer-xinyi

Version:

基于vue3的设计器,可视化开发页面表单

2 lines (1 loc) 5.68 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("vue"),j=require("ant-design-vue"),P=require("./revoke-Bbm2vFx2.cjs");function v(f=null){const a=o.reactive({}),u=o.inject("pageManager");let l=[],d={},s={};o.onMounted(()=>{o.watch(()=>a,e=>{o.nextTick(()=>{for(const t in e){let n=!1;Array.isArray(e[t])?n=!k(e[t],d[t]):n=e[t]!==d[t],n&&(l==null||l.forEach(i=>{i.condition.conditions.map(c=>c.field).includes(t)&&(m(i.condition)?i.actions.forEach(c=>{setTimeout(()=>{y(c)},0)}):i.actions.filter(c=>c.reverseExecute).forEach(c=>{setTimeout(()=>{y(c,!0)},0)}))}))}d=P.deepClone(e)})},{deep:!0,immediate:!0})});function k(e,t){if(!e&&!t)return!0;if(!e||!t||e.length!==t.length)return!1;for(let n=0;n<e.length;n++)if(e[n]!==t[n])return!1;return!0}function m(e){return e.operator==="and"?e.conditions.every(t=>{if(!t.field)return!0;const n=a[t.field];return h(n,t)}):e.operator==="or"?e.conditions.some(t=>{if(!t.field)return!0;const n=a[t.field];return h(n,t)}):!0}function h(e,t){if(!t.value)return!1;switch(t.operator){case"eq":return typeof e=="object"?P.deepEqual(e,t.value):e===t.value;case"neq":return typeof e=="object"?!P.deepEqual(e,t.value):e!==t.value;case"gt":return(typeof e=="number"||typeof e=="string")&&e>t.value;case"gte":return(typeof e=="number"||typeof e=="string")&&e>=t.value;case"lt":return(typeof e=="number"||typeof e=="string")&&e<t.value;case"lte":return(typeof e=="number"||typeof e=="string")&&e<=t.value;case"in":return b(e,t.value);case"nin":return!b(e,t.value);case"null":return e==null||e.length===0;case"notNull":return e!=null&&e.length>0;default:return!0}}function y(e,t=!1){var i,c,D,V;if(!e.field)return;const n=u.find(e.field);if(t){if(["setValue"].includes(e.trigger??"")){n.setValue(null);return}if(["addValue"].includes(e.trigger??"")){if(e.field.split("_")[0]=="checkbox"){const p=n.getValue()?n.getValue():[],r=e.value;p.length&&n.setValue(q(p,r))}else{const p=n.getValue()?n.getValue():"",r=e.value;p.length>0&&n.setValue(S(p,r))}return}}if(n)switch(e.trigger){case"hide":n.setAttr("hidden",!t),((i=n.componentSchema)==null?void 0:i.type)=="card"&&((c=n.componentSchema.children)!=null&&c.length)&&(t?n.componentSchema.children.forEach(r=>{s.hasOwnProperty(r.field)&&o.nextTick(()=>{a[r.field]=s[r.field],delete s[r.field]})}):n.componentSchema.children.forEach(r=>{a.hasOwnProperty(r.field)&&(s[r.field]=a[r.field],delete a[r.field],delete d[r.field])}));break;case"show":let E=t;n.setAttr("hidden",E),((D=n.componentSchema)==null?void 0:D.type)=="card"&&((V=n.componentSchema.children)!=null&&V.length)&&(E?n.componentSchema.children.forEach(r=>{a.hasOwnProperty(r.field)&&(s[r.field]=a[r.field],delete a[r.field],delete d[r.field])}):n.componentSchema.children.forEach(r=>{s.hasOwnProperty(r.field)&&o.nextTick(()=>{a[r.field]=s[r.field],delete s[r.field]})}));break;case"required":n.setAttr("required",!t),f==null||f();break;case"notRequired":n.setAttr("required",t),f==null||f();break;case"readonly":n.setAttr("readOnly",!t);break;case"readWrite":n.setAttr("readOnly",t);break;case"disabled":n.setAttr("disabled",!t);break;case"enabled":n.setAttr("disabled",t);break;case"setValue":n.setValue(e.value);break;case"addValue":if(e.field.split("_")[0]=="checkbox"){const r=n.getValue()?n.getValue():[],A=e.value,w=[...r,...A],_=[...new Set(w)];n.setValue(_)}else{const r=n.getValue()?n.getValue():"",A=e.value,w=[...r,...A];[...new Set(w)],n.setValue(r+A)}}}function g(e){l=e}function S(e,t){return e.replace(new RegExp(t,"g"),"")}function q(e,t){return e.filter(n=>!t.includes(n))}function b(e,t){if(Array.isArray(e)&&Array.isArray(t)){const n=new Set(e);return t.every(i=>n.has(i))}else{if(Array.isArray(e)&&typeof t!="object")return new Set(e).has(t);if(typeof e=="string"&&typeof t=="string")return e.includes(t)}return!1}return{formData:a,setLogicRules:g}}const C={class:"form-main",style:{height:"100%"}},F=o.defineComponent({__name:"form",props:{componentSchema:{type:Object,require:!0,default:()=>({})}},setup(f,{expose:a}){const u=f,l=o.ref(null),d=o.inject("forms",{}),{formData:s,setLogicRules:k}=v(y);o.provide("formData",s),o.provide("form",l),o.watch(()=>u.componentSchema.componentProps.logicRules,e=>{k(e)},{deep:!0,immediate:!0});function m(){return s}function h(){var e;return(e=l.value)==null?void 0:e.validateFields()}function y(){var e;return(e=l.value)==null?void 0:e.clearValidate()}function g(e){Object.assign(s,e)}o.onMounted(async()=>{var e,t,n,i;if(((e=u.componentSchema)==null?void 0:e.type)==="form"&&d.value&&l.value){const c=((n=(t=u.componentSchema)==null?void 0:t.componentProps)==null?void 0:n.name)??((i=u.componentSchema)==null?void 0:i.name)??"default";return l.value.validate=h,d.value[c]=l.value,l.value.getData=m,l.value.setData=g,!1}});const S=o.computed(()=>{const e=u.componentSchema.componentProps;let t=e.labelCol,n=e.wrapperCol;return e.layout==="vertical"?t=n={span:24}:e.layout==="inline"&&e.labelLayout==="fixed"?(t={},n={flex:1}):e.labelLayout==="fixed"&&(t={flex:`${typeof e.labelWidth=="number"?e.labelWidth+"px":e.labelWidth}`},n={flex:1}),{...e,labelCol:t,wrapperCol:n}});function q(e){console.log(e)}const b=o.computed(()=>u.componentSchema.children??[]);return a({form:l,getData:m,setData:g,validate:h}),(e,t)=>(o.openBlock(),o.createElementBlock("div",C,[o.createVNode(o.unref(j.Form),o.mergeProps({ref_key:"form",ref:l,model:o.unref(s)},S.value,{style:{height:"100%"},onFinish:q}),{default:o.withCtx(()=>[o.renderSlot(e.$slots,"edit-node",{},()=>[(o.openBlock(!0),o.createElementBlock(o.Fragment,null,o.renderList(b.value,n=>o.renderSlot(e.$slots,"node",{componentSchema:n})),256))])]),_:3},16,["model"])]))}});exports.default=F;