@@ -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,76 @@ 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+ if ( this . model . securityConfigLevel ?. length === 1 ) {
203+ modelGroup . group . forEach ( ( item : any ) => {
204+ item . securityConfigLevel = this . model . securityConfigLevel ;
205+ item . toggleSecurityVisibility = false ;
206+ item . securityLevel = this . model . securityLevel ;
207+ } ) ;
208+ }
209+ }
210+
211+ private getRowValue ( formGroup : DynamicFormGroupModel , securityLevel ?: number ) {
212+ let mainSecurityLevel ;
213+ if ( isNotEmpty ( securityLevel ) ) {
214+ mainSecurityLevel = securityLevel ;
215+ } else {
216+ const mainRow = formGroup . group . find ( itemModel => itemModel . name === this . model . name ) ;
217+ mainSecurityLevel = ( mainRow as any ) . securityLevel ;
218+ }
159219 const groupValue = Object . create ( { } ) ;
160220 formGroup . group . forEach ( ( model : any ) => {
161221 if ( model . name !== this . model . mandatoryField ) {
162222 if ( isEmpty ( model . value ) ) {
163223 groupValue [ model . name ] = PLACEHOLDER_PARENT_METADATA ;
164224 } else {
165225 if ( typeof model . value === 'string' ) {
166- groupValue [ model . name ] = new FormFieldMetadataValueObject ( model . value , null , model . securityLevel ) ;
226+ groupValue [ model . name ] = new FormFieldMetadataValueObject ( model . value , null , mainSecurityLevel ) ;
167227 } else {
168- groupValue [ model . name ] = model . value ;
228+ groupValue [ model . name ] = Object . assign ( new FormFieldMetadataValueObject ( ) , model . value , { securityLevel : mainSecurityLevel || null } ) ;
169229 }
170230 }
171231 } else {
172232 if ( typeof model . value === 'string' ) {
173- groupValue [ model . name ] = new FormFieldMetadataValueObject ( model . value , null , model . securityLevel ) ;
233+ groupValue [ model . name ] = new FormFieldMetadataValueObject ( model . value , null , mainSecurityLevel ) ;
174234 } else {
175- groupValue [ model . name ] = model . value ;
235+ groupValue [ model . name ] = Object . assign ( new FormFieldMetadataValueObject ( ) , model . value , { securityLevel : mainSecurityLevel || null } ) ;
176236 }
177237 }
178238 } ) ;
@@ -192,7 +252,7 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
192252 return normValue ;
193253 }
194254
195- private hasPlaceholder ( value : string | FormFieldMetadataValueObject ) : boolean {
255+ private hasPlaceholder ( value : string | FormFieldMetadataValueObject ) : boolean {
196256 return ( value instanceof FormFieldMetadataValueObject ) ? value . hasPlaceholder ( ) : ( isNotEmpty ( value ) && value === PLACEHOLDER_PARENT_METADATA ) ;
197257 }
198258
@@ -216,15 +276,29 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
216276 }
217277
218278 private updateArrayModelValue ( groupValue , index ) {
219- let modelValue = this . model . value ;
279+ let parentSecurityLevel = this . model . securityLevel || this . model . securityConfigLevel ?. length > 0 ? this . model . securityConfigLevel [ 0 ] : null ;
280+ for ( const name of Object . keys ( groupValue ) ) {
281+ if ( name === this . model . name && isNotEmpty ( groupValue [ name ] . securityLevel ) ) {
282+ parentSecurityLevel = groupValue [ name ] . securityLevel ;
283+ break ;
284+ }
285+ }
286+ if ( isNotEmpty ( parentSecurityLevel ) ) {
287+ Object . keys ( groupValue ) . forEach ( model => {
288+ if ( groupValue [ model ] instanceof Object ) {
289+ groupValue [ model ] . securityLevel = parentSecurityLevel ;
290+ }
291+ } ) ;
292+ this . model . securityLevel = parentSecurityLevel ;
293+ }
220294
295+ let modelValue = this . model . value ;
221296 if ( isEmpty ( modelValue ) ) {
222297 modelValue = [ groupValue ] ;
223298 } else {
224299 modelValue [ index ] = groupValue ;
225300 }
226301 this . model . value = modelValue ;
227- this . change . emit ( ) ;
228302 }
229303
230304 onCustomEvent ( event ) {
@@ -246,7 +320,7 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
246320
247321 if ( arrayOfValue [ index ] === undefined || arrayOfValue [ previousIndex ] === undefined ) {
248322 return ;
249- } else if ( arrayOfValue . length > 0 ) {
323+ } else if ( arrayOfValue . length > 0 ) {
250324 arrayOfValue = arrayOfValue . filter ( ( el ) => el !== undefined ) ;
251325 } else {
252326 return ;
@@ -260,9 +334,11 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
260334 }
261335
262336 private copyArrayItem ( event ) {
263- const index = event . model . parent . index ;
264- const groupValue = this . getRowValue ( event . model as DynamicFormGroupModel ) ;
337+ const index = Array . isArray ( this . model . value ) ? this . model . value . length : event . model . parent . index ;
338+ const mainRow = event . model . group . find ( itemModel => itemModel . name === this . model . name ) ;
339+ const groupValue = this . getRowValue ( event . model as DynamicFormGroupModel , mainRow . securityLevel ) ;
265340 this . updateArrayModelValue ( groupValue , index ) ;
341+ this . findModelGroups ( ) ;
266342 this . change . emit ( ) ;
267343 }
268344}
0 commit comments