Skip to content

Commit 08948f2

Browse files
committed
[DSC-1379] feature: init security level for inline group
1 parent b3cae70 commit 08948f2

2 files changed

Lines changed: 99 additions & 31 deletions

File tree

src/app/shared/form/builder/ds-dynamic-form-ui/models/relation-group/modal/dynamic-relation-group-modal.components.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -312,20 +312,20 @@ export class DsDynamicRelationGroupModalComponent extends DynamicFormControlComp
312312
}
313313

314314
private initSecurityLevelConfig(chipModel: DynamicInputModel, modelGroup: DynamicFormGroupModel) {
315-
if (this.model.name === chipModel.name) {
316-
if (modelGroup.group.some((item: any) => item.securityConfigLevel.length > 0)) {
317-
(chipModel as any).securityConfigLevel = [0, 1, 2];
318-
(chipModel as any).toggleSecurityVisibility = true;
319-
}
315+
if (this.model.name === chipModel.name && this.model.securityConfigLevel.length > 1) {
316+
(chipModel as any).securityConfigLevel = this.model.securityConfigLevel;
317+
(chipModel as any).toggleSecurityVisibility = true;
320318

321319
const mainRow = modelGroup.group.find(itemModel => itemModel.name === this.model.name);
322320

323321
(chipModel as any).securityLevel = (mainRow as any).securityLevel || 0;
324-
this.securityLevelParent = (mainRow as any).securityLevel || 0;
322+
this.securityLevelParent = (mainRow as any).securityLevel;
323+
325324
modelGroup.group.forEach((item: any) => {
326325
if (item.name !== this.model.name) {
326+
item.securityConfigLevel = this.model.securityConfigLevel;
327327
item.toggleSecurityVisibility = false;
328-
item.securityLevel = this.securityLevelParent || 0;
328+
item.securityLevel = this.securityLevelParent;
329329
}
330330
});
331331
}

src/app/shared/form/builder/ds-dynamic-form-ui/models/relation-inline-group/dynamic-relation-inline-group.components.ts

Lines changed: 92 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
DynamicFormControlModel,
1111
DynamicFormGroupModel,
1212
DynamicFormLayoutService,
13-
DynamicFormValidationService
13+
DynamicFormValidationService,
14+
DynamicInputModel
1415
} from '@ng-dynamic-forms/core';
1516

1617
import { 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

Comments
 (0)