@@ -10,7 +10,8 @@ import {
1010 DynamicFormControlModel ,
1111 DynamicFormGroupModel ,
1212 DynamicFormLayoutService ,
13- DynamicFormValidationService
13+ DynamicFormValidationService ,
14+ DynamicInputModel
1415} from '@ng-dynamic-forms/core' ;
1516
1617import { DynamicRelationGroupModel } from '../relation-group/dynamic-relation-group.model' ;
@@ -49,24 +50,24 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
4950 public formGroup : FormGroup ;
5051 public formModel : DynamicFormControlModel [ ] ;
5152
52- @ViewChild ( 'formRef' , { static : false } ) private formRef : FormComponent ;
53+ @ViewChild ( 'formRef' , { static : false } ) private formRef : FormComponent ;
5354 protected metadataSecurityConfiguration : MetadataSecurityConfiguration ;
5455
5556 constructor ( private formBuilderService : FormBuilderService ,
56- private formService : FormService ,
57- protected layoutService : DynamicFormLayoutService ,
58- protected submissionService : SubmissionService ,
59- protected validationService : DynamicFormValidationService
57+ private formService : FormService ,
58+ protected layoutService : DynamicFormLayoutService ,
59+ protected submissionService : SubmissionService ,
60+ protected validationService : DynamicFormValidationService
6061 ) {
6162 super ( layoutService , validationService ) ;
6263 }
6364
6465 ngOnInit ( ) {
6566 this . submissionService . getSubmissionSecurityConfiguration ( this . model . submissionId ) . pipe (
6667 take ( 1 ) ) . subscribe ( security => {
67- this . metadataSecurityConfiguration = security ;
68- } ) ;
69- const config = { rows : this . model . formConfiguration } as SubmissionFormsModel ;
68+ this . metadataSecurityConfiguration = security ;
69+ } ) ;
70+ const config = { rows : this . model . formConfiguration } as SubmissionFormsModel ;
7071
7172 this . formId = this . formService . getUniqueId ( this . model . id ) ;
7273 this . formModel = this . initArrayModel ( config ) ;
@@ -76,7 +77,6 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
7677
7778 initArrayModel ( formConfig ) : DynamicRowArrayModel [ ] {
7879 let arrayCounter = 0 ;
79-
8080 const config = {
8181 id : this . model . id + '_array' ,
8282 initialCount : isNotEmpty ( this . model . value ) ? ( this . model . value as any [ ] ) . length : 1 ,
@@ -114,8 +114,12 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
114114 this . model . readOnly ,
115115 this . formBuilderService . getTypeBindModel ( ) ,
116116 true ,
117- this . metadataSecurityConfiguration ) ;
118- return formModel [ 0 ] ;
117+ this . metadataSecurityConfiguration ) [ 0 ] ;
118+
119+ ( formModel as any ) . group ?. forEach ( ( modelItem : DynamicInputModel ) => {
120+ this . initSecurityLevelConfig ( modelItem , ( formModel as any ) ) ;
121+ } ) ;
122+ return formModel ;
119123 }
120124
121125 onBlur ( event : DynamicFormControlEvent ) {
@@ -124,7 +128,11 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
124128
125129 onChange ( event : DynamicFormControlEvent ) {
126130 const index = event . model . parent . parent . index ;
127- const groupValue = this . getRowValue ( event . model . parent as DynamicFormGroupModel ) ;
131+ let parentSecurityLevel ;
132+ if ( event . type === 'change' ) {
133+ parentSecurityLevel = this . model . securityLevel ;
134+ }
135+ const groupValue = this . getRowValue ( event . model . parent as DynamicFormGroupModel , parentSecurityLevel ) ;
128136
129137 if ( this . hasEmptyGroupValue ( groupValue ) ) {
130138 this . removeItemFromArray ( event ) ;
@@ -155,24 +163,69 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
155163 }
156164 }
157165
158- private getRowValue ( formGroup : DynamicFormGroupModel ) {
166+ private findModelGroups ( ) {
167+ this . formModel . forEach ( ( row : any ) => {
168+ row . groups . forEach ( ( groupArray ) => {
169+ groupArray . group . forEach ( ( groupRow ) => {
170+ const modelRow = groupRow as DynamicFormGroupModel ;
171+ modelRow . group . forEach ( ( model : DynamicInputModel ) => {
172+ this . initSecurityLevelConfig ( model , modelRow ) ;
173+ } ) ;
174+ } ) ;
175+ } ) ;
176+ } ) ;
177+ }
178+
179+ private initSecurityLevelConfig ( model : DynamicInputModel | any , modelGroup : DynamicFormGroupModel ) {
180+ if ( this . model . name === model . name && this . model . securityConfigLevel . length > 1 ) {
181+ model . securityConfigLevel = this . model . securityConfigLevel ;
182+ model . toggleSecurityVisibility = true ;
183+
184+ let mainSecurityLevel ;
185+ const mainRow = modelGroup . group . find ( itemModel => itemModel . name === this . model . name ) ;
186+ if ( isNotEmpty ( this . model . securityLevel ) ) {
187+ mainSecurityLevel = this . model . securityLevel ;
188+ } else {
189+ mainSecurityLevel = ( mainRow as any ) . securityLevel ;
190+ }
191+
192+ model . securityLevel = mainSecurityLevel ;
193+
194+ modelGroup . group . forEach ( ( item : any ) => {
195+ if ( item . name !== this . model . name ) {
196+ item . securityConfigLevel = this . model . securityConfigLevel ;
197+ item . toggleSecurityVisibility = false ;
198+ item . securityLevel = mainSecurityLevel ;
199+ }
200+ } ) ;
201+ }
202+ }
203+
204+ private getRowValue ( formGroup : DynamicFormGroupModel , securityLevel ?: number ) {
205+ let mainSecurityLevel ;
206+ if ( isNotEmpty ( securityLevel ) ) {
207+ mainSecurityLevel = securityLevel ;
208+ } else {
209+ const mainRow = formGroup . group . find ( itemModel => itemModel . name === this . model . name ) ;
210+ mainSecurityLevel = ( mainRow as any ) . securityLevel ;
211+ }
159212 const groupValue = Object . create ( { } ) ;
160213 formGroup . group . forEach ( ( model : any ) => {
161214 if ( model . name !== this . model . mandatoryField ) {
162215 if ( isEmpty ( model . value ) ) {
163216 groupValue [ model . name ] = PLACEHOLDER_PARENT_METADATA ;
164217 } else {
165218 if ( typeof model . value === 'string' ) {
166- groupValue [ model . name ] = new FormFieldMetadataValueObject ( model . value , null , model . securityLevel ) ;
219+ groupValue [ model . name ] = new FormFieldMetadataValueObject ( model . value , null , mainSecurityLevel ) ;
167220 } else {
168- groupValue [ model . name ] = model . value ;
221+ groupValue [ model . name ] = { ... model . value , securityLevel : mainSecurityLevel } ;
169222 }
170223 }
171224 } else {
172225 if ( typeof model . value === 'string' ) {
173- groupValue [ model . name ] = new FormFieldMetadataValueObject ( model . value , null , model . securityLevel ) ;
226+ groupValue [ model . name ] = new FormFieldMetadataValueObject ( model . value , null , mainSecurityLevel ) ;
174227 } else {
175- groupValue [ model . name ] = model . value ;
228+ groupValue [ model . name ] = { ... model . value , securityLevel : mainSecurityLevel } ;
176229 }
177230 }
178231 } ) ;
@@ -192,7 +245,7 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
192245 return normValue ;
193246 }
194247
195- private hasPlaceholder ( value : string | FormFieldMetadataValueObject ) : boolean {
248+ private hasPlaceholder ( value : string | FormFieldMetadataValueObject ) : boolean {
196249 return ( value instanceof FormFieldMetadataValueObject ) ? value . hasPlaceholder ( ) : ( isNotEmpty ( value ) && value === PLACEHOLDER_PARENT_METADATA ) ;
197250 }
198251
@@ -216,15 +269,29 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
216269 }
217270
218271 private updateArrayModelValue ( groupValue , index ) {
219- let modelValue = this . model . value ;
272+ let parentSecurityLevel = this . model . securityLevel || this . model . securityConfigLevel [ 0 ] ;
273+ for ( const name of Object . keys ( groupValue ) ) {
274+ if ( name === this . model . name && isNotEmpty ( groupValue [ name ] . securityLevel ) ) {
275+ parentSecurityLevel = groupValue [ name ] . securityLevel ;
276+ break ;
277+ }
278+ }
279+ if ( isNotEmpty ( parentSecurityLevel ) ) {
280+ Object . keys ( groupValue ) . forEach ( model => {
281+ if ( groupValue [ model ] instanceof Object ) {
282+ groupValue [ model ] . securityLevel = parentSecurityLevel ;
283+ }
284+ } ) ;
285+ this . model . securityLevel = parentSecurityLevel ;
286+ }
220287
288+ let modelValue = this . model . value ;
221289 if ( isEmpty ( modelValue ) ) {
222290 modelValue = [ groupValue ] ;
223291 } else {
224292 modelValue [ index ] = groupValue ;
225293 }
226294 this . model . value = modelValue ;
227- this . change . emit ( ) ;
228295 }
229296
230297 onCustomEvent ( event ) {
@@ -246,7 +313,7 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
246313
247314 if ( arrayOfValue [ index ] === undefined || arrayOfValue [ previousIndex ] === undefined ) {
248315 return ;
249- } else if ( arrayOfValue . length > 0 ) {
316+ } else if ( arrayOfValue . length > 0 ) {
250317 arrayOfValue = arrayOfValue . filter ( ( el ) => el !== undefined ) ;
251318 } else {
252319 return ;
@@ -260,9 +327,10 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
260327 }
261328
262329 private copyArrayItem ( event ) {
263- const index = event . model . parent . index ;
264- const groupValue = this . getRowValue ( event . model as DynamicFormGroupModel ) ;
330+ const index = Array . isArray ( this . model . value ) ? this . model . value . length : event . model . parent . index ;
331+ const groupValue = this . getRowValue ( event . model as DynamicFormGroupModel , this . model . securityLevel ) ;
265332 this . updateArrayModelValue ( groupValue , index ) ;
333+ this . findModelGroups ( ) ;
266334 this . change . emit ( ) ;
267335 }
268336}
0 commit comments