clean up description editor progress bar

This commit is contained in:
Sofia Papacharalampous 2024-04-29 13:41:01 +03:00
parent 2180bebc7f
commit 88ba79c406
1 changed files with 13 additions and 46 deletions

View File

@ -16,7 +16,6 @@ export class FormProgressIndicationComponent extends BaseComponent implements On
determinateProgressValue: number; determinateProgressValue: number;
progressSoFar: number; progressSoFar: number;
total: number; total: number;
totalIds: string[] = [];
percent: number; percent: number;
fieldTypes: string[] = ['dateValue', 'externalIdentifier.identifier', 'externalIdentifier.type', 'reference', 'references', 'textListValue', 'textValue']; fieldTypes: string[] = ['dateValue', 'externalIdentifier.identifier', 'externalIdentifier.type', 'reference', 'references', 'textListValue', 'textValue'];
@ -44,80 +43,48 @@ export class FormProgressIndicationComponent extends BaseComponent implements On
} }
calculateValueForProgressbar() { calculateValueForProgressbar() {
this.progressSoFar = this.countCompletedFields(this.formGroup.get('properties'), this.checkVisibility); this.progressSoFar = this.countRequiredFields(this.formGroup.get('properties'), this.checkVisibility, true);
this.total = this.countTotalRequiredFields(this.formGroup.get('properties'), this.checkVisibility); this.total = this.countRequiredFields(this.formGroup.get('properties'), this.checkVisibility);
this.percent = (this.progressSoFar / this.total) * 100; this.percent = (this.progressSoFar / this.total) * 100;
this.value = Number.parseFloat(this.percent.toPrecision(this.progressValueAccuracy)); 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; let valueCurrent = 0;
if (formControl instanceof UntypedFormGroup) { if (formControl instanceof UntypedFormGroup) {
if(!checkVisibility || (!formControl.get('id')?.value || (this.visibilityRulesService.isVisibleMap[formControl.get('id').value] ?? true))) { if(!checkVisibility || (!formControl.get('id')?.value || (this.visibilityRulesService.isVisibleMap[formControl.get('id').value] ?? true))) {
Object.keys(formControl.controls).forEach(item => { Object.keys(formControl.controls).forEach(item => {
const control = formControl.get(item); const control = formControl.get(item);
valueCurrent = valueCurrent + this.countTotalRequiredFields(control, checkVisibility); valueCurrent = valueCurrent + this.countRequiredFields(control, checkVisibility, countCompletedFields);
}); });
} }
} else if (formControl instanceof UntypedFormArray) { } else if (formControl instanceof UntypedFormArray) {
formControl.controls.forEach(item => { 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; return valueCurrent;
} }
countTotalRequiredFieldsByFieldset(ordinal: number, fieldsFormGroup: UntypedFormGroup): number { countRequiredFieldsByFieldset(ordinal: number, fieldsFormGroup: UntypedFormGroup, filterValid: boolean = false): number {
let requiredFieldsCount: number = 0; let fieldsCount: number = 0;
const fieldNames = Object.keys(fieldsFormGroup.controls); const fieldNames = Object.keys(fieldsFormGroup.controls);
for(let item of fieldNames) { for(let item of fieldNames) {
if (!this.checkVisibility || this.visibilityRulesService.isVisible(item, ordinal)) { if (!this.checkVisibility || this.visibilityRulesService.isVisible(item, ordinal)) {
const fieldControl = fieldsFormGroup.get(item); const fieldControl = fieldsFormGroup.get(item);
for (let fieldType of this.fieldTypes) { for (let fieldType of this.fieldTypes) {
const typedControl = fieldControl.get(fieldType); const typedControl = fieldControl.get(fieldType);
if (this.controlRequired(typedControl) && this.controlEnabled(typedControl)) { let controlFilter: boolean = this.controlRequired(typedControl) && this.controlEnabled(typedControl);
requiredFieldsCount ++; if (filterValid) controlFilter = controlFilter && typedControl.valid;
if (controlFilter) {
fieldsCount ++;
break; break;
} }
} }
} }
} }
return requiredFieldsCount; return fieldsCount;
}
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;
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////