argos/dmp-frontend/src/app/form/pprogress-bar/progress-bar.component.ts

70 lines
2.7 KiB
TypeScript
Raw Normal View History

2018-11-27 18:33:17 +01:00
import { Component, Input, OnInit, ViewEncapsulation } from '@angular/core';
import { FormArray, FormGroup } from '@angular/forms';
import { takeUntil } from 'rxjs/operators';
import { BaseComponent } from '../../core/common/base/base.component';
import { VisibilityRulesService } from '../../utilities/visibility-rules/visibility-rules.service';
2017-12-05 17:56:21 +01:00
@Component({
2018-10-05 17:00:54 +02:00
selector: 'app-progress-bar',
templateUrl: './progress-bar.component.html',
styles: ['.alwaysVisible .ui-progressbar-label { display:block!important; }'],
encapsulation: ViewEncapsulation.None
2017-12-05 17:56:21 +01:00
})
2018-11-27 18:33:17 +01:00
export class ProgressBarComponent extends BaseComponent implements OnInit {
2018-10-05 17:00:54 +02:00
@Input() formGroup: FormGroup;
@Input() public progressValueAccuracy = 1;
determinateProgressValue: number;
2017-12-05 17:56:21 +01:00
2018-11-27 18:33:17 +01:00
constructor(private visibilityRulesService: VisibilityRulesService) { super(); }
2017-12-07 10:26:06 +01:00
2018-10-05 17:00:54 +02:00
public value = 0;
ngOnInit() {
this.calculateValueForProgressbar();
this.formGroup
.valueChanges
2018-11-27 18:33:17 +01:00
.pipe(takeUntil(this._destroyed))
2018-10-05 17:00:54 +02:00
.subscribe(control => {
this.calculateValueForProgressbar();
});
}
2018-01-11 12:15:15 +01:00
2018-10-05 17:00:54 +02:00
calculateValueForProgressbar() {
const progressSoFar = this.countFormControlsWithValue(this.formGroup);
const total = this.getFormControlDepthLength(this.formGroup);
const perc = (progressSoFar / total) * 100;
this.value = Number.parseFloat(perc.toPrecision(this.progressValueAccuracy));
}
2017-12-12 13:12:13 +01:00
2018-10-05 17:00:54 +02:00
countFormControlsWithValue(form: FormGroup): number {
let value = 0;
Object.keys(form.controls).forEach(key => {
const control = form.controls[key];
if (control instanceof FormGroup) { value += this.countFormControlsWithValue(control); } else if (control instanceof FormArray) {
const formArray = (<FormArray>control);
for (let i = 0; i < formArray.length; i++) {
value += this.countFormControlsWithValue(<FormGroup>formArray.get('' + i));
}
} else if (key === 'value' && control.value != null && control.value !== '') { value++; }
});
return value;
}
2017-12-05 17:56:21 +01:00
2018-10-05 17:00:54 +02:00
getFormControlDepthLength(form: FormGroup): number {
let value = 0;
Object.keys(form.controls).forEach(key => {
const control = form.controls[key];
if (control instanceof FormGroup) { value += this.getFormControlDepthLength(control); } else if (control instanceof FormArray) {
const formArray = (<FormArray>control);
for (let i = 0; i < formArray.length; i++) {
if (<FormGroup>formArray.get('' + i).value && this.visibilityRulesService.checkElementVisibility(formArray.get('' + i).value.id)) {
value += this.getFormControlDepthLength(<FormGroup>formArray.get('' + i));
}
2018-10-05 17:00:54 +02:00
}
} else if (key === 'value' && this.visibilityRulesService.checkElementVisibility(form.controls['id'].value)) {
value++;
2018-10-05 17:00:54 +02:00
}
});
return value;
}
2018-05-28 11:50:42 +02:00
}