1+ import { FlowForm , FormField } from "../../types" ;
2+
3+ export class WorkflowFormManager {
4+
5+ private readonly form : FlowForm ;
6+ private readonly formList : FlowForm [ ] ;
7+
8+ constructor ( form : FlowForm ) {
9+ this . form = form ;
10+ this . formList = [ ] ;
11+ this . loadFormList ( ) ;
12+ }
13+
14+ private loadFormList ( ) : void {
15+ this . formList . push ( this . form ) ;
16+ if ( this . form . subForms && this . form . subForms . length > 0 ) {
17+ for ( const subForm of this . form . subForms ) {
18+ this . formList . push ( subForm ) ;
19+ }
20+ }
21+ }
22+
23+ public getFormFields ( code :string ) {
24+ for ( const subForm of this . formList ) {
25+ if ( subForm . code == code ) {
26+ return subForm . fields || [ ] ;
27+ }
28+ }
29+ return [ ] ;
30+ }
31+
32+ public removeSubForm ( code :string ) {
33+ const currentSubForms = this . form . subForms || [ ] ;
34+ const subForms = currentSubForms . filter ( subForm => subForm . code !== code ) ;
35+ return {
36+ ...this . form ,
37+ subForms :subForms
38+ }
39+ }
40+
41+ public addSubForm ( values :any ) {
42+ const subForms = [ ...this . form . subForms || [ ] ] ;
43+ subForms . push ( values ) ;
44+ return {
45+ ...this . form ,
46+ subForms :subForms
47+ }
48+ }
49+
50+ public mergeValue ( code : string , values : any ) :FlowForm {
51+ const currentFields = this . getFormFields ( code ) ;
52+ const currentCode = values [ 'code' ] ;
53+ const list :FormField [ ] = [ ] ;
54+ let exist = false ;
55+ for ( const field of currentFields ) {
56+ if ( field . code == currentCode ) {
57+ list . push ( values ) ;
58+ exist = true ;
59+ } else {
60+ list . push ( field ) ;
61+ }
62+ }
63+ if ( ! exist ) {
64+ list . push ( values ) ;
65+ }
66+ const mainCode = this . form . code ;
67+ if ( code == mainCode ) {
68+ return {
69+ ...this . form ,
70+ fields :list ,
71+ }
72+ } else {
73+ return {
74+ ...this . form ,
75+ fields :list ,
76+ subForms :this . form . subForms . map ( item => {
77+ if ( item . code === code ) {
78+ return {
79+ ...item ,
80+ fields :list
81+ }
82+ } else {
83+ return item ;
84+ }
85+ } ) ,
86+ }
87+ }
88+ }
89+
90+ public removeField ( formCode : string , fieldCode : string ) {
91+ const currentFields = this . getFormFields ( formCode ) || [ ] ;
92+ const fields = [ ...currentFields ] ;
93+ const list = fields . filter ( file => file . code !== fieldCode ) ;
94+ const mainCode = this . form . code ;
95+ if ( formCode == mainCode ) {
96+ return {
97+ ...this . form ,
98+ fields :list ,
99+ }
100+ } else {
101+ return {
102+ ...this . form ,
103+ fields :list ,
104+ subForms :this . form . subForms . map ( item => {
105+ if ( item . code === formCode ) {
106+ return {
107+ ...item ,
108+ fields :list
109+ }
110+ } else {
111+ return item ;
112+ }
113+ } ) ,
114+ }
115+ }
116+ }
117+
118+
119+ }
0 commit comments