From 88ba79c406e9d2ea5f4d3d6a9eded7f2a5c7b80b Mon Sep 17 00:00:00 2001 From: Sofia Papacharalampous Date: Mon, 29 Apr 2024 13:41:01 +0300 Subject: [PATCH] clean up description editor progress bar --- .../form-progress-indication.component.ts | 59 ++++--------------- 1 file changed, 13 insertions(+), 46 deletions(-) diff --git a/dmp-frontend/src/app/ui/description/editor/form-progress-indication/form-progress-indication.component.ts b/dmp-frontend/src/app/ui/description/editor/form-progress-indication/form-progress-indication.component.ts index 2661630b1..0c0453c40 100644 --- a/dmp-frontend/src/app/ui/description/editor/form-progress-indication/form-progress-indication.component.ts +++ b/dmp-frontend/src/app/ui/description/editor/form-progress-indication/form-progress-indication.component.ts @@ -16,7 +16,6 @@ export class FormProgressIndicationComponent extends BaseComponent implements On determinateProgressValue: number; progressSoFar: number; total: number; - totalIds: string[] = []; percent: number; fieldTypes: string[] = ['dateValue', 'externalIdentifier.identifier', 'externalIdentifier.type', 'reference', 'references', 'textListValue', 'textValue']; @@ -44,80 +43,48 @@ export class FormProgressIndicationComponent extends BaseComponent implements On } calculateValueForProgressbar() { - this.progressSoFar = this.countCompletedFields(this.formGroup.get('properties'), this.checkVisibility); - this.total = this.countTotalRequiredFields(this.formGroup.get('properties'), this.checkVisibility); + this.progressSoFar = this.countRequiredFields(this.formGroup.get('properties'), this.checkVisibility, true); + this.total = this.countRequiredFields(this.formGroup.get('properties'), this.checkVisibility); this.percent = (this.progressSoFar / this.total) * 100; this.value = Number.parseFloat(this.percent.toPrecision(this.progressValueAccuracy)); } - countTotalRequiredFields(formControl: AbstractControl, checkVisibility = false): number { + countRequiredFields(formControl: AbstractControl, checkVisibility = false, countCompletedFields = false): number { let valueCurrent = 0; if (formControl instanceof UntypedFormGroup) { if(!checkVisibility || (!formControl.get('id')?.value || (this.visibilityRulesService.isVisibleMap[formControl.get('id').value] ?? true))) { Object.keys(formControl.controls).forEach(item => { const control = formControl.get(item); - valueCurrent = valueCurrent + this.countTotalRequiredFields(control, checkVisibility); + valueCurrent = valueCurrent + this.countRequiredFields(control, checkVisibility, countCompletedFields); }); } } else if (formControl instanceof UntypedFormArray) { formControl.controls.forEach(item => { - valueCurrent = valueCurrent + this.countTotalRequiredFieldsByFieldset(item.get('ordinal').value, item.get('fields') as UntypedFormGroup); + valueCurrent = valueCurrent + this.countRequiredFieldsByFieldset(item.get('ordinal').value, item.get('fields') as UntypedFormGroup, countCompletedFields); }); } return valueCurrent; } - countTotalRequiredFieldsByFieldset(ordinal: number, fieldsFormGroup: UntypedFormGroup): number { - let requiredFieldsCount: number = 0; + countRequiredFieldsByFieldset(ordinal: number, fieldsFormGroup: UntypedFormGroup, filterValid: boolean = false): number { + let fieldsCount: number = 0; const fieldNames = Object.keys(fieldsFormGroup.controls); for(let item of fieldNames) { if (!this.checkVisibility || this.visibilityRulesService.isVisible(item, ordinal)) { const fieldControl = fieldsFormGroup.get(item); for (let fieldType of this.fieldTypes) { const typedControl = fieldControl.get(fieldType); - if (this.controlRequired(typedControl) && this.controlEnabled(typedControl)) { - requiredFieldsCount ++; + let controlFilter: boolean = this.controlRequired(typedControl) && this.controlEnabled(typedControl); + if (filterValid) controlFilter = controlFilter && typedControl.valid; + + if (controlFilter) { + fieldsCount ++; break; } } } } - return requiredFieldsCount; - } - - countCompletedFields(formControl: AbstractControl, checkVisibility=false): number { - let completedFieldsCount: number = 0; - if (formControl instanceof UntypedFormGroup) { - if(!checkVisibility || (!formControl.get('id')?.value || (this.visibilityRulesService.isVisibleMap[formControl.get('id').value] ?? true))) { - Object.keys(formControl.controls).forEach(item => { - const control = formControl.get(item); - completedFieldsCount = completedFieldsCount + this.countCompletedFields(control, checkVisibility); - }); - } - } else if (formControl instanceof UntypedFormArray) { - formControl.controls.forEach(item => { - completedFieldsCount = completedFieldsCount + this.countCompletedRequiredFieldsByFieldset(item.get('ordinal').value, item.get('fields') as UntypedFormGroup); - }); - } - return completedFieldsCount; - } - - countCompletedRequiredFieldsByFieldset(ordinal: number, fieldsFormGroup: UntypedFormGroup): number { - let completedFieldsCount: number = 0; - const fieldNames = Object.keys(fieldsFormGroup.controls); - for(let item of fieldNames) { - if (!this.checkVisibility || this.visibilityRulesService.isVisible(item, ordinal)) { - const fieldControl = fieldsFormGroup.get(item); - for (let fieldType of this.fieldTypes) { - const typedControl = fieldControl.get(fieldType); - if (this.controlRequired(typedControl) && this.controlEnabled(typedControl) && typedControl.valid) { - completedFieldsCount ++; - break; - } - } - } - } - return completedFieldsCount; + return fieldsCount; } //////////////////////////////////////////////////////////////////////