UNPKG

@jecloud/func

Version:

JECloud 功能库

1 lines 3.57 kB
import{ref as e,getCurrentInstance as t,nextTick as n,watch as i}from"vue";import{getFuncCache as o}from"../../../func-util";import{Form as r,Hooks as l}from"@jecloud/ui";import{isBoolean as a,pick as u,isNotEmpty as d}from"@jecloud/utils";import{useInjectFunc as c}from"../../../func/src/context";import{FuncChildrenLayoutEnum as s,FuncButtonTypeEnum as f}from"../../../data/enum";import{getBean as g}from"../../../data/api/common";export function useForm(h){var p,m=h.props,O=h.context,y=O.expose,C=e(),R=c(),b={type:m.type,funcCode:m.funcCode,instance:t(),props:m,context:O,$func:R,getFuncData:function(){return o(m.funcCode)},getPlugin:function(){return C.value},getModel:function(){var e;return null!==(e=null==R?void 0:R.store.activeBean)&&void 0!==e?e:m.model},getValues:function(e){var t=b.getModel();return e?null==t?void 0:t[e]:t},getChanges:function(){return null==R?void 0:R.store.getBeanChanges()},setReadOnly:function(e,t){if(a(e))t=e,Object.values(b.getFields()).forEach((function(e){e.readOnly=t})),b.getButtons().forEach((function(e){e.code===f.FORM_BACK_BUTTON||e.noReadOnly||(e.hidden=t)})),n((function(){Object.values(b.getChilds()).forEach((function(e){e.readOnly=t}))}));else{var i=b.getFields(e);i&&(i.readOnly=t)}},setVisible:function(e,t){var n=b.getFields(e);n&&(n.hidden=!t)},setRequired:function(e,t){var i=b.getFields(e);i&&b.getPlugin()&&(b.getPlugin().clearValidate(e),i.required=t,n((function(){b.getPlugin().validate(e)})))},setChildVisible:function(e,t){var n=b.getChilds(e);n&&(n.hidden=!t)},setChildReadOnly:function(e,t){var n=b.getChilds(e);n&&(n.readOnly=t)},setButtonVisible:function(e,t){var n=b.getButtons(e);n&&(n.hidden=!t)},getChilds:function(e){var t={};return Object.values(R.getFuncChild()).forEach((function(e){var n=e.layout||s.FORM_OUTER_HORIZONTAL;[s.FORM_INNER_HORIZONTAL,s.FORM_INNER_VERTICAL,s.FORM_OUTER_HORIZONTAL,s.FORM_INNER_TREE].includes(n)&&(t[e.code]=e)})),e?t[e]:t},reloadData:function(){if(R){var e=b.getFuncData().info,t=e.productCode,n=e.tableCode,i=e.funcCode,o=e.pkCode,r=e.funcAction,l=R.store.activeBean[o];d(l)&&g({params:{tableCode:n,funcCode:i,pkValue:l},pd:t,action:r}).then((function(e){e&&R.store.commitActiveBean(e)}))}}};Object.assign(b,l.useExtendMethods({plugin:C,keys:r.MethodKeys})),y(b);var F=e(m.fields),E=e([]),_=e(m.cols),w={wrapperStyle:{},style:{},mainStyle:{}};if(m.funcCode){var B=o(m.funcCode),M=B.info.formOptions;if(F.value=B.getData("fields.form"),E.value=B.getData("buttons.form"),_.value=null!==(p=M.cols)&&void 0!==p?p:3,M.labelWidth&&(w.labelCol={style:{width:M.labelWidth+"px"}}),M.width)if(M.width.endsWith("%")){if(w.wrapperStyle.width=M.width,M.minWidth){var N=M.minWidth+"px";w.wrapperStyle.minWidth=N,w.style.minWidth=N}}else{var j=M.width+"px";w.wrapperStyle.width=j,w.style.minWidth=j}else w.style.display="inherit";M.backgroundColor&&(w.mainStyle.backgroundColor=M.backgroundColor),Object.assign(w,u(M,["size"]))}var x=v(F.value),T={};Object.assign(b,{getFields:function(e){return e?x[e]:x},getButtons:function(e){return e?E.value.find((function(t){return t.code===e})):E.value},getFields4Ref:function(e){return e?T[e]:T},addFields4Ref:function(e,t){T[e]=t}}),i((function(){return m.readOnly}),(function(){b.setReadOnly(m.readOnly)}));var W=b.getFuncData(),A=(null==W?void 0:W.info.formOptions.anchor)?W.fields.anchor:[];return{$form:b,$func:R,$plugin:C,fields:F,cols:_,formProps:w,anchorOptions:A}}function v(e){var t,n={};return null===(t=null==e?void 0:e.forEach)||void 0===t||t.call(e,(function(e){e.name&&(n[e.name]=e),Object.assign(n,v(e.children))})),n}