@fluig-kit/ecm 1.0.13 → 1.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("react/jsx-runtime"),l=require("react"),E=require("react-hook-form"),N=require("@hookform/resolvers"),W=require("zod"),A=require("@fluig-kit/core");var _=function(e,r){for(var t={};e.length;){var o=e[0],i=o.code,s=o.message,c=o.path.join(".");if(!t[c])if("unionErrors"in o){var a=o.unionErrors[0].errors[0];t[c]={message:a.message,type:a.code}}else t[c]={message:s,type:i};if("unionErrors"in o&&o.unionErrors.forEach(function(y){return y.errors.forEach(function(d){return e.push(d)})}),r){var u=t[c].types,n=u&&u[o.code];t[c]=E.appendErrors(c,r,t,i,n?[].concat(n,o.message):o.message)}e.shift()}return t},B=function(e,r,t){return t===void 0&&(t={}),function(o,i,s){try{return Promise.resolve(function(c,a){try{var u=Promise.resolve(e[t.mode==="sync"?"parse":"parseAsync"](o,r)).then(function(n){return s.shouldUseNativeValidation&&N.validateFieldsNatively({},s),{errors:{},values:t.raw?o:n}})}catch(n){return a(n)}return u&&u.then?u.then(void 0,a):u}(0,function(c){if(function(a){return Array.isArray(a==null?void 0:a.errors)}(c))return{values:{},errors:N.toNestErrors(_(c.errors,!s.shouldUseNativeValidation&&s.criteriaMode==="all"),s)};throw c}))}catch(c){return Promise.reject(c)}}};const M=l.createContext(null);function G({children:e,baseSchema:r}){const[t,o]=l.useState([]),i=l.useMemo(()=>r??W.z.object({}),[r]),s=l.useCallback((n,y)=>{o(d=>d.some(b=>b.id===n)?d:[...d,{id:n,schema:y}])},[]),c=l.useCallback(n=>{o(y=>y.some(d=>d.id===n)?y.filter(d=>d.id!==n):y)},[]),a=l.useMemo(()=>t.length?t.map(n=>n.schema).reduce((n,y)=>n.and(y),i):i,[t,i]),u=l.useMemo(()=>({registerSchema:s,unregisterSchema:c,mergedSchema:a}),[s,c,a]);return g.jsx(M.Provider,{value:u,children:e})}function P(){const e=l.useContext(M);if(!e)throw new Error("useSchemaRegistry deve ser usado dentro de <SchemaRegistryProvider>");return e}function j(e){return typeof e=="number"?e:isNaN(Number(e))?null:Number(e)}function L(e,r,t){if(!e)return!0;const o=Array.isArray(r)?r[0]:r,i=e.field??o,s=t==null?void 0:t[i];return e.hasValue!==void 0?e.hasValue?s!=null&&s!=="":s==null||s==="":e.equals!==void 0?s===e.equals:e.notEquals!==void 0?s!==e.notEquals:!0}function z(e,r,t,o,i){const s=new Set(e),c=new Set(r);return t==null||t.forEach(a=>{if(a.from!==void 0){const n=Array.isArray(a.from)?a.from:[a.from];if(o===null||!n.includes(o))return}if(!L(a.when,a.target,i))return;const u=Array.isArray(a.target)?a.target:[a.target];switch(a.type){case"readonly":u.forEach(n=>{c.add(n),s.delete(n)});break;case"active":u.forEach(n=>{s.add(n),c.delete(n)});break;case"hidden":u.forEach(n=>{s.delete(n),c.delete(n)});break}}),{active:Array.from(s),readonly:Array.from(c)}}function H(e,r,t,o){const i=new Set(e.active),s=new Set(e.readonly),c=new Set(e.hidden??[]);return r==null||r.forEach(a=>{if(a.from!==void 0){const n=Array.isArray(a.from)?a.from:[a.from];if(t===null||!n.includes(t))return}if(!L(a.when,a.target,o))return;const u=Array.isArray(a.target)?a.target:[a.target];switch(a.type){case"hidden":u.forEach(n=>{c.add(n),i.delete(n),s.delete(n)});break;case"active":u.forEach(n=>{i.add(n),s.delete(n),c.delete(n)});break;case"readonly":u.forEach(n=>{s.add(n),i.delete(n),c.delete(n)});break}}),{active:Array.from(i),readonly:Array.from(s),hidden:Array.from(c)}}function $(e,r){if(!(e!=null&&e.length))return r;const t=new Map(r.map(i=>[i.name,i])),o=[];return e.forEach(i=>{t.has(i)&&(o.push(t.get(i)),t.delete(i))}),t.forEach(i=>o.push(i)),o}function K(e){var o,i;const r=new Set,t=(s=[])=>{s.forEach(c=>{var a;c.target&&(Array.isArray(c.target)?c.target:[c.target]).forEach(n=>r.add(n)),(a=c.when)!=null&&a.field&&r.add(c.when.field)})};return t((o=e==null?void 0:e.fields)==null?void 0:o.rules),t((i=e==null?void 0:e.sections)==null?void 0:i.rules),Array.from(r)}function J({activityId:e,previousActivityId:r,control:t,workflowStructure:o,sectionsRegistry:i}){const{registerSchema:s,unregisterSchema:c}=P(),a=j(e),u=j(r),n=a!==null?o[a]:null,y=l.useMemo(()=>K(n),[a,n]),d=E.useWatch({control:t,name:y,defaultValue:{}}),b=l.useMemo(()=>{const v={};return y.forEach((m,w)=>{v[m]=Array.isArray(d)?d[w]:d}),v},[d,y]),f=l.useMemo(()=>{if(!n)return{sections:[],fields:{active:[],readonly:[],hidden:[]}};const v=z(n.sections.active,n.sections.readonly,n.sections.rules,u,b);let m=[...v.readonly.map(h=>({name:h,mode:"readonly"})),...v.active.map(h=>({name:h,mode:"active"}))];m=$(n.sections.order,m);const w=H({active:n.fields.active,readonly:n.fields.readonly,hidden:n.fields.hidden},n.fields.rules,u,b);return{sections:m,fields:w}},[n,u,b,i]);return l.useEffect(()=>{const v=f.sections.filter(m=>m.mode==="active").map(m=>m.name);v.forEach(m=>{var h;const w=(h=i==null?void 0:i[m])==null?void 0:h.schema;w&&s(m,w)}),Object.keys(i??{}).filter(m=>!v.includes(m)).forEach(c)},[f.sections,i,s,c]),f}function Q(){if(typeof document>"u")return{};const e={};return document.querySelectorAll("input[type='radio'][name]").forEach(r=>{const t=r,o=t.getAttribute("name");o&&t.checked&&(e[o]=t.value)}),document.querySelectorAll("input[name], textarea[name], select[name]").forEach(r=>{const t=r,o=t.getAttribute("name");if(!o||e[o]!==void 0)return;const i=t.getAttribute("value")??t.value??t.textContent??"";e[o]=String(i).trim()}),document.querySelectorAll("span[name]").forEach(r=>{var i;const t=r,o=t.getAttribute("name");if(o){const s=((i=t.textContent)==null?void 0:i.trim())??"";s!==""&&(e[o]=s)}}),e}const X=()=>{var d;const e={isCreation:!1,isEdit:!1,isView:!0};if(typeof window>"u"||!window.top)return e;const r=new URLSearchParams(window.location.search),t=window.top,o=window.top.document,i=((d=t.ECM)==null?void 0:d.workflowView)||t.ECM_WKFView,s=(i==null?void 0:i.newRequest)===!0||(i==null?void 0:i.isNewRequest)===!0,c=r.get("edit")==="true"||r.get("mode")==="edit",a=r.has("taskUserId"),u=r.get("view")==="true"||r.get("publicView")==="true",n=!!o.getElementById("sendModal")||!!o.querySelector(".fluig-style-guide.modal-dialog"),y=s||c||a&&!u&&n;return{isCreation:s,isEdit:y,isView:!y}},q=l.createContext(null);function Z({children:e,devConfig:r}){const[t,o]=l.useState(null),i=l.useMemo(()=>Q()??{},[]);return l.useEffect(()=>{let s=!0;const c=async()=>{let u=(t==null?void 0:t.activityId)??null;try{const h=await A.parentProxy.top.ECM.workflowView.sequence;h!=null&&(u=Number(h))}catch{}const{isEdit:n,isView:y}=X(),d=typeof window<"u"&&window.location.hostname==="localhost",b=typeof window<"u"&&window.self!==window.top,f=!d&&b?y:!1,v=i.LASTACTIVITYID?Number(i.LASTACTIVITYID):null,m={isView:f,isReadOnly:f,activityId:u,previousActivityId:v,prefilledValues:i,showDebugSubmit:(r==null?void 0:r.showDebugSubmit)??!1,showDebugLogs:(r==null?void 0:r.showDebugLogs)??!1,isDev:!1};let w=m;if(r!=null&&r.enabled){const h=r.isView??m.isView;w={...m,...r,isView:h,isReadOnly:h,isDev:!0,activityId:r.activityId??u}}s&&o(h=>h&&h.isView===w.isView&&h.activityId===w.activityId?h:w),r!=null&&r.showDebugLogs&&n&&console.log("[FluigRuntime] Modo Edição Detectado via Modal/URL")};c();const a=setInterval(c,800);return()=>{s=!1,clearInterval(a)}},[r,i]),t?g.jsx(q.Provider,{value:t,children:e}):null}function F(){const e=l.useContext(q);if(!e)throw new Error("useFluigRuntime deve ser usado dentro de FluigRuntimeProvider");return e}function ee(e){const{activityId:r,previousActivityId:t}=F();l.useEffect(()=>{r!=null&&e.setValue("CURRENTACTIVITYID",r),t!=null&&e.setValue("LASTACTIVITYID",t)},[r,t,e]),l.useEffect(()=>{var i,s;if(typeof window>"u")return;const o=((i=window.top)==null?void 0:i.document.getElementById("send-process-button"))||((s=window.parent)==null?void 0:s.document.getElementById("workflow-actions-send"))||document.getElementById("send-process-button");if(!o){console.warn("[FluigWorkflowReact] Botão de envio do Fluig não encontrado. A validação automática pode falhar.");return}return window.beforeSendValidate=()=>(e.handleSubmit(()=>{window.top.__FLUIG_REACT_FORM_DATA__=e.getValues(),window.beforeSendValidate=()=>!0;try{o.dispatchEvent(new MouseEvent("click",{view:window,bubbles:!0,cancelable:!0}))}catch{o.click()}},c=>{var a,u,n;console.error("[Validation Errors]",c),(u=(a=window.top)==null?void 0:a.FLUIGC)!=null&&u.toast?(n=window.top)==null||n.FLUIGC.toast({title:"Atenção",message:"Verifique os campos obrigatórios destacados em vermelho.",type:"danger"}):alert("Verifique os campos obrigatórios.")})(),!1),()=>{}},[e])}const te=({workflowStructure:e,sectionsRegistry:r,className:t})=>{const{control:o,handleSubmit:i,register:s,setValue:c,getValues:a}=E.useFormContext(),u=F(),n=l.useRef(!1);l.useEffect(()=>{n.current||(u.activityId!=null&&c("CURRENTACTIVITYID",u.activityId),u.previousActivityId!=null&&c("LASTACTIVITYID",u.previousActivityId),n.current=!0)},[u,c]);const{sections:y,fields:d}=J({control:o,activityId:u.activityId,previousActivityId:u.previousActivityId,workflowStructure:e,sectionsRegistry:r}),b=f=>{console.log("✅ [FluigWorkflowForm] Dados válidos:",f)};return g.jsxs("form",{id:"fluig-react-form",className:t,onSubmit:i(b),noValidate:!0,children:[g.jsxs("div",{className:"hidden",style:{display:"none"},children:[g.jsx("input",{type:"hidden",...s("CURRENTACTIVITYID")}),g.jsx("input",{type:"hidden",...s("LASTACTIVITYID")})]}),y.length===0&&u.activityId!==null&&g.jsxs("p",{children:["Não foi possível encontrar a atividade: ",String(u.activityId)]}),y.map(({name:f,mode:v})=>{if(v==="hidden")return null;const m=r[f];if(!m)return null;const w=m.Component;return g.jsx(w,{id:f,fields:d,readOnly:v==="readonly"},f)}),u.isDev&&u.showDebugSubmit&&g.jsx("div",{style:{width:"100%",padding:"16px",paddingTop:"0"},children:g.jsx("button",{type:"submit",className:"btn btn-primary btn-lg btn-block",children:"Submit (Debug)"})})]})},re=e=>{const{mergedSchema:r}=P(),t=F(),o=E.useForm({resolver:r?B(r):void 0,defaultValues:t.prefilledValues,mode:"onBlur"});return ee(o),l.useEffect(()=>{t.showDebugLogs&&(A.DebugLogger.header("Form Initialized"),A.DebugLogger.table("Prefilled Values (DOM Capture)",t.prefilledValues),A.DebugLogger.info("RHF Current Values",o.getValues()),A.DebugLogger.end())},[t.showDebugLogs,t.prefilledValues,o]),g.jsx(E.FormProvider,{...o,children:g.jsx(te,{...e})})};async function ne(e){if(!e)return console.warn("[setFluigNextActivity] Nenhuma atividade informada."),!1;const{wfView:r,wkfView:t}=await oe();if(!r||!t)return!1;ie(r);const o=Number(e),i=se(r,o);return i?(ae(r,t,i,o),console.log(`[setFluigNextActivity] Próxima atividade definida: ${o}`),!0):(console.warn(`[setFluigNextActivity] Estado ${o} não encontrado nas próximas atividades disponíveis.`),console.log("Estados disponíveis:",r.availableStatesCOPY||r.availableStates),!1)}async function oe(){const e=await A.parentProxy.top.ECM.workflowView,r=await A.parentProxy.top.ECM_WKFView;return!e||!r?(console.warn("[setFluigNextActivity] Contexto Fluig inválido ou não carregado."),{}):{wfView:e,wkfView:r}}function ie(e){e.availableStatesCOPY||(e.availableStatesCOPY=e.availableStates)}function se(e,r){const t=e.availableStatesCOPY||e.availableStates;return t==null?void 0:t.find(o=>o.sequence===r)}function ae(e,r,t,o){e.availableUsers.users=[],e.availableStates=[t],r.selectActivity=()=>o}const O=l.createContext(null);function ce(){const e=l.useContext(O);if(!e)throw new Error("useSection deve ser usado dentro de um componente <Section>");return e}function ue({id:e,className:r,schema:t,children:o,form:i,prefilledValues:s,readOnly:c,fields:a={},hidden:u=!1}){const{isView:n,activityId:y}=F(),d=y,b=E.useFormContext(),f=i||b;if(!f)return console.error(`[FluigWorkflowReact] Section "${e}" não encontrou o 'form'.`),null;if(u)return null;const v=l.useCallback(p=>{var I,V,C;return(I=a.hidden)!=null&&I.includes(p)?"hidden":(V=a.readonly)!=null&&V.includes(p)?"readonly":(C=a.active)!=null&&C.includes(p)?"active":c?"readonly":"active"},[a,c]),m=l.useCallback(p=>v(p)==="hidden",[v]),w=l.useCallback(p=>v(p)==="readonly",[v]),h=l.useCallback(p=>{if(!p)return;const I=f.getValues("CURRENTACTIVITYID");f.setValue("LASTACTIVITYID",I),f.setValue("CURRENTACTIVITYID",d),ne(Number(p))},[f,d]);l.useEffect(()=>{if(!n||!s||!t||!f)return;const p=f.getValues(),I=t.shape;I&&Object.entries(I).forEach(([V,C])=>{var D,k;let S=C;for(;(D=S==null?void 0:S._def)!=null&&D.schema;)S=S._def.schema;const x=(k=S==null?void 0:S._def)==null?void 0:k.fluig;if((x==null?void 0:x.type)!=="options"||!Array.isArray(x.values)||p[V]!==void 0)return;const T={};x.values.forEach(R=>{T[R]=s[R]==="on"}),f.setValue(V,T,{shouldDirty:!1,shouldValidate:!1})})},[n,s,t,f]);const Y=l.useMemo(()=>({id:e,form:f,isHidden:m,isReadOnly:w,isView:n,next:h,wf_activity_id:d}),[e,f,m,w,n,h,d]);return g.jsx(O.Provider,{value:Y,children:g.jsx("section",{id:e,className:[e,r].filter(Boolean).join(" "),children:o})})}const U=l.createContext(null);function le({children:e,config:r}){const t=l.useMemo(()=>A.createFluigClient(r),[r]);return g.jsx(U.Provider,{value:t,children:e})}function de(){const e=l.useContext(U);if(!e)throw new Error("useFluigApi deve ser usado dentro de um <FluigApiProvider>");return e}Object.defineProperty(exports,"DebugLogger",{enumerable:!0,get:()=>A.DebugLogger});Object.defineProperty(exports,"createFluigClient",{enumerable:!0,get:()=>A.createFluigClient});Object.defineProperty(exports,"parentProxy",{enumerable:!0,get:()=>A.parentProxy});exports.FluigApiProvider=le;exports.FluigRuntimeProvider=Z;exports.FluigWorkflowForm=re;exports.SchemaRegistryProvider=G;exports.Section=ue;exports.useFluigApi=de;exports.useFluigRuntime=F;exports.useSchemaRegistry=P;exports.useSection=ce;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("react/jsx-runtime"),l=require("react"),E=require("react-hook-form"),N=require("@hookform/resolvers"),W=require("zod"),A=require("@fluig-kit/core");var _=function(e,r){for(var t={};e.length;){var o=e[0],i=o.code,s=o.message,c=o.path.join(".");if(!t[c])if("unionErrors"in o){var a=o.unionErrors[0].errors[0];t[c]={message:a.message,type:a.code}}else t[c]={message:s,type:i};if("unionErrors"in o&&o.unionErrors.forEach(function(y){return y.errors.forEach(function(d){return e.push(d)})}),r){var u=t[c].types,n=u&&u[o.code];t[c]=E.appendErrors(c,r,t,i,n?[].concat(n,o.message):o.message)}e.shift()}return t},B=function(e,r,t){return t===void 0&&(t={}),function(o,i,s){try{return Promise.resolve(function(c,a){try{var u=Promise.resolve(e[t.mode==="sync"?"parse":"parseAsync"](o,r)).then(function(n){return s.shouldUseNativeValidation&&N.validateFieldsNatively({},s),{errors:{},values:t.raw?o:n}})}catch(n){return a(n)}return u&&u.then?u.then(void 0,a):u}(0,function(c){if(function(a){return Array.isArray(a==null?void 0:a.errors)}(c))return{values:{},errors:N.toNestErrors(_(c.errors,!s.shouldUseNativeValidation&&s.criteriaMode==="all"),s)};throw c}))}catch(c){return Promise.reject(c)}}};const M=l.createContext(null);function G({children:e,baseSchema:r}){const[t,o]=l.useState([]),i=l.useMemo(()=>r??W.z.object({}),[r]),s=l.useCallback((n,y)=>{o(d=>d.some(b=>b.id===n)?d:[...d,{id:n,schema:y}])},[]),c=l.useCallback(n=>{o(y=>y.some(d=>d.id===n)?y.filter(d=>d.id!==n):y)},[]),a=l.useMemo(()=>t.length?t.map(n=>n.schema).reduce((n,y)=>n.and(y),i):i,[t,i]),u=l.useMemo(()=>({registerSchema:s,unregisterSchema:c,mergedSchema:a}),[s,c,a]);return g.jsx(M.Provider,{value:u,children:e})}function P(){const e=l.useContext(M);if(!e)throw new Error("useSchemaRegistry deve ser usado dentro de <SchemaRegistryProvider>");return e}function j(e){return typeof e=="number"?e:isNaN(Number(e))?null:Number(e)}function L(e,r,t){if(!e)return!0;const o=Array.isArray(r)?r[0]:r,i=e.field??o,s=t==null?void 0:t[i];return e.hasValue!==void 0?e.hasValue?s!=null&&s!=="":s==null||s==="":e.equals!==void 0?s===e.equals:e.notEquals!==void 0?s!==e.notEquals:!0}function z(e,r,t,o,i){const s=new Set(e),c=new Set(r);return t==null||t.forEach(a=>{if(a.from!==void 0){const n=Array.isArray(a.from)?a.from:[a.from];if(o===null||!n.includes(o))return}if(!L(a.when,a.target,i))return;const u=Array.isArray(a.target)?a.target:[a.target];switch(a.type){case"readonly":u.forEach(n=>{c.add(n),s.delete(n)});break;case"active":u.forEach(n=>{s.add(n),c.delete(n)});break;case"hidden":u.forEach(n=>{s.delete(n),c.delete(n)});break}}),{active:Array.from(s),readonly:Array.from(c)}}function H(e,r,t,o){const i=new Set(e.active),s=new Set(e.readonly),c=new Set(e.hidden??[]);return r==null||r.forEach(a=>{if(a.from!==void 0){const n=Array.isArray(a.from)?a.from:[a.from];if(t===null||!n.includes(t))return}if(!L(a.when,a.target,o))return;const u=Array.isArray(a.target)?a.target:[a.target];switch(a.type){case"hidden":u.forEach(n=>{c.add(n),i.delete(n),s.delete(n)});break;case"active":u.forEach(n=>{i.add(n),s.delete(n),c.delete(n)});break;case"readonly":u.forEach(n=>{s.add(n),i.delete(n),c.delete(n)});break}}),{active:Array.from(i),readonly:Array.from(s),hidden:Array.from(c)}}function $(e,r){if(!(e!=null&&e.length))return r;const t=new Map(r.map(i=>[i.name,i])),o=[];return e.forEach(i=>{t.has(i)&&(o.push(t.get(i)),t.delete(i))}),t.forEach(i=>o.push(i)),o}function K(e){var o,i;const r=new Set,t=(s=[])=>{s.forEach(c=>{var a;c.target&&(Array.isArray(c.target)?c.target:[c.target]).forEach(n=>r.add(n)),(a=c.when)!=null&&a.field&&r.add(c.when.field)})};return t((o=e==null?void 0:e.fields)==null?void 0:o.rules),t((i=e==null?void 0:e.sections)==null?void 0:i.rules),Array.from(r)}function J({activityId:e,previousActivityId:r,control:t,workflowStructure:o,sectionsRegistry:i}){const{registerSchema:s,unregisterSchema:c}=P(),a=j(e),u=j(r),n=a!==null?o[a]:null,y=l.useMemo(()=>K(n),[a,n]),d=E.useWatch({control:t,name:y,defaultValue:{}}),b=l.useMemo(()=>{const v={};return y.forEach((m,w)=>{v[m]=Array.isArray(d)?d[w]:d}),v},[d,y]),f=l.useMemo(()=>{if(!n)return{sections:[],fields:{active:[],readonly:[],hidden:[]}};const v=z(n.sections.active,n.sections.readonly,n.sections.rules,u,b);let m=[...v.readonly.map(h=>({name:h,mode:"readonly"})),...v.active.map(h=>({name:h,mode:"active"}))];m=$(n.sections.order,m);const w=H({active:n.fields.active,readonly:n.fields.readonly,hidden:n.fields.hidden},n.fields.rules,u,b);return{sections:m,fields:w}},[n,u,b,i]);return l.useEffect(()=>{const v=f.sections.filter(m=>m.mode==="active").map(m=>m.name);v.forEach(m=>{var h;const w=(h=i==null?void 0:i[m])==null?void 0:h.schema;w&&s(m,w)}),Object.keys(i??{}).filter(m=>!v.includes(m)).forEach(c)},[f.sections,i,s,c]),f}function Q(){if(typeof document>"u")return{};const e={};return document.querySelectorAll("input[type='radio'][name]").forEach(r=>{const t=r,o=t.getAttribute("name");o&&t.checked&&(e[o]=t.value)}),document.querySelectorAll("input[name], textarea[name], select[name]").forEach(r=>{const t=r,o=t.getAttribute("name");if(!o||e[o]!==void 0)return;const i=t.getAttribute("value")??t.value??t.textContent??"";e[o]=String(i).trim()}),document.querySelectorAll("span[name]").forEach(r=>{var i;const t=r,o=t.getAttribute("name");if(o){const s=((i=t.textContent)==null?void 0:i.trim())??"";s!==""&&(e[o]=s)}}),e}const X=()=>{var d;const e={isCreation:!1,isEdit:!1,isView:!0};if(typeof window>"u"||!window.top)return e;const r=new URLSearchParams(window.location.search),t=window.top,o=window.top.document,i=((d=t.ECM)==null?void 0:d.workflowView)||t.ECM_WKFView,s=(i==null?void 0:i.newRequest)===!0||(i==null?void 0:i.isNewRequest)===!0,c=r.get("edit")==="true"||r.get("mode")==="edit",a=r.has("taskUserId"),u=r.get("view")==="true"||r.get("publicView")==="true",n=!!o.getElementById("sendModal")||!!o.querySelector(".fluig-style-guide.modal-dialog"),y=s||c||a&&!u&&n;return{isCreation:s,isEdit:y,isView:!y}},q=l.createContext(null);function Z({children:e,devConfig:r}){const[t,o]=l.useState(null),i=l.useMemo(()=>Q()??{},[]);return l.useEffect(()=>{let s=!0;const c=async()=>{let u=(t==null?void 0:t.activityId)??null;try{const h=await A.parentProxy.top.ECM.workflowView.sequence;h!=null&&(u=Number(h))}catch{}const{isEdit:n,isView:y}=X(),d=typeof window<"u"&&window.location.hostname==="localhost",b=typeof window<"u"&&window.self!==window.top,f=!d&&b?y:!1,v=i.LASTACTIVITYID?Number(i.LASTACTIVITYID):null,m={isView:f,isReadOnly:f,activityId:u,previousActivityId:v,prefilledValues:i,showDebugSubmit:(r==null?void 0:r.showDebugSubmit)??!1,showDebugLogs:(r==null?void 0:r.showDebugLogs)??!1,isDev:!1};let w=m;if(r!=null&&r.enabled){const h=r.isView??m.isView;w={...m,...r,isView:h,isReadOnly:h,isDev:!0,activityId:r.activityId??u}}s&&o(h=>h&&h.isView===w.isView&&h.activityId===w.activityId?h:w),r!=null&&r.showDebugLogs&&n&&console.log("[FluigRuntime] Modo Edição Detectado via Modal/URL")};c();const a=setInterval(c,800);return()=>{s=!1,clearInterval(a)}},[r,i]),t?g.jsx(q.Provider,{value:t,children:e}):null}function F(){const e=l.useContext(q);if(!e)throw new Error("useFluigRuntime deve ser usado dentro de <FluigRuntimeProvider>");return e}function ee(e){const{activityId:r,previousActivityId:t}=F();l.useEffect(()=>{r!=null&&e.setValue("CURRENTACTIVITYID",r),t!=null&&e.setValue("LASTACTIVITYID",t)},[r,t,e]),l.useEffect(()=>{var i,s;if(typeof window>"u")return;const o=((i=window.top)==null?void 0:i.document.getElementById("send-process-button"))||((s=window.parent)==null?void 0:s.document.getElementById("workflow-actions-send"))||document.getElementById("send-process-button");if(!o){console.warn("[FluigWorkflowReact] Botão de envio do Fluig não encontrado. A validação automática pode falhar.");return}return window.beforeSendValidate=()=>(e.handleSubmit(()=>{window.top.__FLUIG_REACT_FORM_DATA__=e.getValues(),window.beforeSendValidate=()=>!0;try{o.dispatchEvent(new MouseEvent("click",{view:window,bubbles:!0,cancelable:!0}))}catch{o.click()}},c=>{var a,u,n;console.error("[Validation Errors]",c),(u=(a=window.top)==null?void 0:a.FLUIGC)!=null&&u.toast?(n=window.top)==null||n.FLUIGC.toast({title:"Atenção",message:"Verifique os campos obrigatórios destacados em vermelho.",type:"danger"}):alert("Verifique os campos obrigatórios.")})(),!1),()=>{}},[e])}const te=({workflowStructure:e,sectionsRegistry:r,className:t})=>{const{control:o,handleSubmit:i,register:s,setValue:c,getValues:a}=E.useFormContext(),u=F(),n=l.useRef(!1);l.useEffect(()=>{n.current||(u.activityId!=null&&c("CURRENTACTIVITYID",u.activityId),u.previousActivityId!=null&&c("LASTACTIVITYID",u.previousActivityId),n.current=!0)},[u,c]);const{sections:y,fields:d}=J({control:o,activityId:u.activityId,previousActivityId:u.previousActivityId,workflowStructure:e,sectionsRegistry:r}),b=f=>{console.log("✅ [FluigWorkflowForm] Dados válidos:",f)};return g.jsxs("form",{id:"fluig-react-form",className:t,onSubmit:i(b),noValidate:!0,children:[g.jsxs("div",{className:"hidden",style:{display:"none"},children:[g.jsx("input",{type:"hidden",...s("CURRENTACTIVITYID")}),g.jsx("input",{type:"hidden",...s("LASTACTIVITYID")})]}),y.length===0&&u.activityId!==null&&g.jsxs("p",{children:["Não foi possível encontrar a atividade: ",String(u.activityId)]}),y.map(({name:f,mode:v})=>{if(v==="hidden")return null;const m=r[f];if(!m)return null;const w=m.Component;return g.jsx(w,{id:f,fields:d,readOnly:v==="readonly"},f)}),u.isDev&&u.showDebugSubmit&&g.jsx("div",{style:{width:"100%",padding:"16px",paddingTop:"0"},children:g.jsx("button",{type:"submit",className:"btn btn-primary btn-lg btn-block",children:"Submit (Debug)"})})]})},re=e=>{const{mergedSchema:r}=P(),t=F(),o=E.useForm({resolver:r?B(r):void 0,defaultValues:t.prefilledValues,mode:"onBlur"});return ee(o),l.useEffect(()=>{t.showDebugLogs&&(A.DebugLogger.header("Form Initialized"),A.DebugLogger.table("Prefilled Values (DOM Capture)",t.prefilledValues),A.DebugLogger.info("RHF Current Values",o.getValues()),A.DebugLogger.end())},[t.showDebugLogs,t.prefilledValues,o]),g.jsx(E.FormProvider,{...o,children:g.jsx(te,{...e})})};async function ne(e){if(!e)return console.warn("[setFluigNextActivity] Nenhuma atividade informada."),!1;const{wfView:r,wkfView:t}=await oe();if(!r||!t)return!1;ie(r);const o=Number(e),i=se(r,o);return i?(ae(r,t,i,o),console.log(`[setFluigNextActivity] Próxima atividade definida: ${o}`),!0):(console.warn(`[setFluigNextActivity] Estado ${o} não encontrado nas próximas atividades disponíveis.`),console.log("Estados disponíveis:",r.availableStatesCOPY||r.availableStates),!1)}async function oe(){const e=await A.parentProxy.top.ECM.workflowView,r=await A.parentProxy.top.ECM_WKFView;return!e||!r?(console.warn("[setFluigNextActivity] Contexto Fluig inválido ou não carregado."),{}):{wfView:e,wkfView:r}}function ie(e){e.availableStatesCOPY||(e.availableStatesCOPY=e.availableStates)}function se(e,r){const t=e.availableStatesCOPY||e.availableStates;return t==null?void 0:t.find(o=>o.sequence===r)}function ae(e,r,t,o){e.availableUsers.users=[],e.availableStates=[t],r.selectActivity=()=>o}const O=l.createContext(null);function ce(){const e=l.useContext(O);if(!e)throw new Error("useSection deve ser usado dentro de um componente <Section>");return e}function ue({id:e,className:r,schema:t,children:o,form:i,prefilledValues:s,readOnly:c,fields:a={},hidden:u=!1}){const{isView:n,activityId:y}=F(),d=y,b=E.useFormContext(),f=i||b;if(!f)return console.error(`[FluigWorkflowReact] Section "${e}" não encontrou o 'form'.`),null;if(u)return null;const v=l.useCallback(p=>{var I,V,C;return(I=a.hidden)!=null&&I.includes(p)?"hidden":(V=a.readonly)!=null&&V.includes(p)?"readonly":(C=a.active)!=null&&C.includes(p)?"active":c?"readonly":"active"},[a,c]),m=l.useCallback(p=>v(p)==="hidden",[v]),w=l.useCallback(p=>v(p)==="readonly",[v]),h=l.useCallback(p=>{if(!p)return;const I=f.getValues("CURRENTACTIVITYID");f.setValue("LASTACTIVITYID",I),f.setValue("CURRENTACTIVITYID",d),ne(Number(p))},[f,d]);l.useEffect(()=>{if(!n||!s||!t||!f)return;const p=f.getValues(),I=t.shape;I&&Object.entries(I).forEach(([V,C])=>{var D,k;let S=C;for(;(D=S==null?void 0:S._def)!=null&&D.schema;)S=S._def.schema;const x=(k=S==null?void 0:S._def)==null?void 0:k.fluig;if((x==null?void 0:x.type)!=="options"||!Array.isArray(x.values)||p[V]!==void 0)return;const T={};x.values.forEach(R=>{T[R]=s[R]==="on"}),f.setValue(V,T,{shouldDirty:!1,shouldValidate:!1})})},[n,s,t,f]);const Y=l.useMemo(()=>({id:e,form:f,isHidden:m,isReadOnly:w,isView:n,next:h,wf_activity_id:d}),[e,f,m,w,n,h,d]);return g.jsx(O.Provider,{value:Y,children:g.jsx("section",{id:e,className:[e,r].filter(Boolean).join(" "),children:o})})}const U=l.createContext(null);function le({children:e,config:r}){const t=l.useMemo(()=>A.createFluigClient(r),[r]);return g.jsx(U.Provider,{value:t,children:e})}function de(){const e=l.useContext(U);if(!e)throw new Error("useFluigApi deve ser usado dentro de um <FluigApiProvider>");return e}Object.defineProperty(exports,"DebugLogger",{enumerable:!0,get:()=>A.DebugLogger});Object.defineProperty(exports,"createFluigClient",{enumerable:!0,get:()=>A.createFluigClient});Object.defineProperty(exports,"parentProxy",{enumerable:!0,get:()=>A.parentProxy});exports.FluigApiProvider=le;exports.FluigRuntimeProvider=Z;exports.FluigWorkflowForm=re;exports.SchemaRegistryProvider=G;exports.Section=ue;exports.useFluigApi=de;exports.useFluigRuntime=F;exports.useSchemaRegistry=P;exports.useSection=ce;
|
package/dist/index.js
CHANGED