212 lines
8.3 KiB
TypeScript
212 lines
8.3 KiB
TypeScript
import {ChangeDetectorRef, Component, ElementRef, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
|
|
import { AbstractControl, UntypedFormArray, UntypedFormControl, UntypedFormGroup } from '@angular/forms';
|
|
import { VisibilityRulesService } from '@app/ui/description/editor/description-form/visibility-rules/visibility-rules.service';
|
|
import { BaseComponent } from '@common/base/base.component';
|
|
import { takeUntil } from 'rxjs/operators';
|
|
|
|
@Component({
|
|
selector: 'app-form-progress-indication',
|
|
templateUrl: './form-progress-indication.component.html',
|
|
styleUrls: ['./form-progress-indication.component.scss']
|
|
})
|
|
export class FormProgressIndicationComponent extends BaseComponent implements OnInit, OnChanges {
|
|
@Input() formGroup: UntypedFormGroup;
|
|
@Input() isDmpEditor: boolean;
|
|
@Input() isDatasetEditor: boolean;
|
|
@Input() public progressValueAccuracy = 2;
|
|
determinateProgressValue: number;
|
|
progressSoFar: number;
|
|
total: number;
|
|
percent: number;
|
|
|
|
constructor(private visibilityRulesService: VisibilityRulesService) { super(); }
|
|
|
|
public value = 0;
|
|
ngOnInit() {
|
|
this.init();
|
|
}
|
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
|
if(changes.formGroup) {
|
|
this.init();
|
|
}
|
|
}
|
|
|
|
init() {
|
|
setTimeout(() => {this.calculateValueForProgressbar();});
|
|
this.formGroup
|
|
.valueChanges
|
|
.pipe(takeUntil(this._destroyed))
|
|
.subscribe(control => {
|
|
setTimeout(() => {this.calculateValueForProgressbar();});
|
|
});
|
|
}
|
|
|
|
calculateValueForProgressbar() {
|
|
if (this.isDmpEditor) {
|
|
this.progressSoFar = this.countFormControlsValidForProgress(this.formGroup);
|
|
this.total = this.countFormControlsRequiredFieldsForTotal(this.formGroup);
|
|
} else if (this.isDatasetEditor) {
|
|
this.progressSoFar = this.countFormControlsValidForProgress(this.formGroup) + this.countFormControlsWithValueForProgress(this.formGroup);
|
|
this.total = this.countFormControlsRequiredFieldsForTotal(this.formGroup, true) + this.CountFormControlDepthLengthFotTotal(this.formGroup);
|
|
} else {
|
|
this.progressSoFar = this.countFormControlsWithValueForProgress(this.formGroup);
|
|
this.total = this.CountFormControlDepthLengthFotTotal(this.formGroup);
|
|
}
|
|
this.percent = (this.progressSoFar / this.total) * 100;
|
|
this.value = Number.parseFloat(this.percent.toPrecision(this.progressValueAccuracy));
|
|
}
|
|
|
|
countFormControlsWithValueForProgress(formControl: AbstractControl): number {
|
|
let valueCurent = 0;
|
|
if (formControl instanceof UntypedFormGroup) {
|
|
if (this.checkFormsIfIsFieldsAndVisible(formControl) && this.checkIfIsRequired((formControl as UntypedFormGroup))) {
|
|
if (this.hasValue(formControl))
|
|
valueCurent++;
|
|
}
|
|
if (this.chechFieldIfIsFieldSetAndVisible((formControl as UntypedFormGroup)) && this.checkIfIsRequired((formControl as UntypedFormGroup))) {
|
|
valueCurent = valueCurent + this.compositeFieldsGetChildsForProgress(formControl);
|
|
} else {
|
|
Object.keys(formControl.controls).forEach(item => {
|
|
const control = formControl.get(item);
|
|
valueCurent = valueCurent + this.countFormControlsWithValueForProgress(control);
|
|
});
|
|
}
|
|
} else if (formControl instanceof UntypedFormArray) {
|
|
formControl.controls.forEach(item => {
|
|
valueCurent = valueCurent + this.countFormControlsWithValueForProgress(item);
|
|
});
|
|
}
|
|
return valueCurent;
|
|
}
|
|
private hasValue(formGroup: UntypedFormGroup): boolean {
|
|
return formGroup.get('value').valid && formGroup.get('value').value != null && formGroup.get('value').value !== '' && (this.visibilityRulesService.isVisibleMap[formGroup.get('id').value] ?? true);
|
|
}
|
|
|
|
private compositeFieldsGetChildsForProgress(formGroup: UntypedFormGroup): number {
|
|
let valueCurent = 0;
|
|
if (this.visibilityRulesService.isVisibleMap[formGroup.get('id').value] ?? true) {
|
|
(formGroup.get('fields') as UntypedFormArray).controls.forEach((element: UntypedFormGroup) => {
|
|
valueCurent = valueCurent + this.countFormControlsWithValueForProgress(element);
|
|
});
|
|
|
|
(formGroup.get('multiplicityItems') as UntypedFormArray).controls.forEach((element: UntypedFormGroup) => {
|
|
valueCurent = valueCurent + this.countFormControlsWithValueForProgress(element);
|
|
});
|
|
}
|
|
return valueCurent;
|
|
}
|
|
|
|
private checkIfIsRequired(formControl: UntypedFormGroup): boolean {
|
|
return !!(formControl.get('validationRequired') && formControl.get('validationRequired').value);
|
|
|
|
}
|
|
|
|
private checkFormsIfIsFieldsAndVisible(formControl: UntypedFormGroup): boolean {
|
|
if (formControl.contains('id') && formControl.contains('value')) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
private chechFieldIfIsFieldSetAndVisible(formControl: UntypedFormGroup): boolean {
|
|
if (formControl.contains('id') && formControl.contains('fields')) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
CountFormControlDepthLengthFotTotal(formControl: AbstractControl): number {
|
|
let valueCurent = 0;
|
|
if (formControl instanceof UntypedFormArray) {
|
|
formControl.controls.forEach(item => {
|
|
valueCurent = valueCurent + this.CountFormControlDepthLengthFotTotal(item);
|
|
});
|
|
} else if (formControl instanceof UntypedFormGroup) {
|
|
if ((formControl as UntypedFormGroup).contains('id') && (formControl as UntypedFormGroup).contains('value') && (this.visibilityRulesService.isVisibleMap[(formControl as UntypedFormGroup).get('id').value] ?? true) && this.checkIfIsRequired((formControl as UntypedFormGroup))) {
|
|
valueCurent++;
|
|
} else if ((formControl as UntypedFormGroup).contains('id') && (formControl as UntypedFormGroup).contains('fields')) {
|
|
valueCurent = valueCurent + this.compositeFieldsGetChildsForTotal(formControl);
|
|
} else {
|
|
Object.keys(formControl.controls).forEach(item => {
|
|
const control = formControl.get(item);
|
|
valueCurent = valueCurent + this.CountFormControlDepthLengthFotTotal(control);
|
|
});
|
|
}
|
|
}
|
|
|
|
return valueCurent;
|
|
}
|
|
|
|
private compositeFieldsGetChildsForTotal(formGroup: UntypedFormGroup): number {
|
|
let valueCurent = 0;
|
|
if (this.visibilityRulesService.isVisibleMap[formGroup.get('id').value] ?? true) {
|
|
(formGroup.get('fields') as UntypedFormArray).controls.forEach((element: UntypedFormGroup) => {
|
|
valueCurent = valueCurent + this.CountFormControlDepthLengthFotTotal(element);
|
|
});
|
|
(formGroup.get('multiplicityItems') as UntypedFormArray).controls.forEach((element: UntypedFormGroup) => {
|
|
valueCurent = valueCurent + this.CountFormControlDepthLengthFotTotal(element);
|
|
});
|
|
}
|
|
return valueCurent;
|
|
}
|
|
|
|
countFormControlsValidForProgress(formControl: AbstractControl): number {
|
|
let valueCurrent = 0;
|
|
if (formControl instanceof UntypedFormControl) {
|
|
if (this.controlRequired(formControl) && this.controlEnabled(formControl) && formControl.valid) {
|
|
valueCurrent++;
|
|
}
|
|
} else if (formControl instanceof UntypedFormGroup) {
|
|
Object.keys(formControl.controls).forEach(item => {
|
|
const control = formControl.get(item);
|
|
valueCurrent = valueCurrent + this.countFormControlsValidForProgress(control);
|
|
});
|
|
} else if (formControl instanceof UntypedFormArray) {
|
|
formControl.controls.forEach(item => {
|
|
valueCurrent = valueCurrent + this.countFormControlsValidForProgress(item);
|
|
});
|
|
}
|
|
return valueCurrent;
|
|
}
|
|
|
|
countFormControlsRequiredFieldsForTotal(formControl: AbstractControl, checkVisibility = false): number {
|
|
let valueCurrent = 0;
|
|
if (formControl instanceof UntypedFormControl) {
|
|
if (this.controlRequired(formControl) && this.controlEnabled(formControl)) {
|
|
valueCurrent++;
|
|
}
|
|
} else 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.countFormControlsRequiredFieldsForTotal(control, checkVisibility);
|
|
});
|
|
}
|
|
} else if (formControl instanceof UntypedFormArray) {
|
|
formControl.controls.forEach(item => {
|
|
valueCurrent = valueCurrent + this.countFormControlsRequiredFieldsForTotal(item, checkVisibility);
|
|
});
|
|
}
|
|
return valueCurrent;
|
|
}
|
|
|
|
controlRequired(formControl: AbstractControl) {
|
|
if (formControl.validator) {
|
|
const validator = formControl.validator({} as AbstractControl);
|
|
if (validator && validator.required) {
|
|
return true;
|
|
}
|
|
} else { return false }
|
|
}
|
|
|
|
controlEnabled(formControl: AbstractControl) {
|
|
if (formControl.enabled) {
|
|
return true;
|
|
} else { return false }
|
|
}
|
|
|
|
isEditor(): boolean {
|
|
return this.isDmpEditor || this.isDatasetEditor;
|
|
}
|
|
}
|