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

68 lines
2.7 KiB
TypeScript
Raw Normal View History

import { VisibilityRulesService } from '../../visibility-rules/visibility-rules.service';
import { ViewEncapsulation } from '@angular/core';
2017-12-05 17:56:21 +01:00
import { Component, Input, OnInit } from '@angular/core';
import { FormGroup, FormControl, FormArray } from '@angular/forms'
@Component({
selector: '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
})
export class ProgressBarComponent implements OnInit {
2017-12-07 17:34:48 +01:00
@Input() formGroup: FormGroup
2018-01-10 09:17:22 +01:00
@Input("progressValueAccuracy") public accuracy: number = 1;
determinateProgressValue:number;
2017-12-05 17:56:21 +01:00
2017-12-07 10:26:06 +01:00
constructor(private visibilityRulesService: VisibilityRulesService) { }
private value: number = 0;
2017-12-05 17:56:21 +01:00
ngOnInit() {
2017-12-12 13:12:13 +01:00
this.calculateValueForProgressbar()
2017-12-05 17:56:21 +01:00
this.formGroup
.valueChanges
2017-12-07 17:34:48 +01:00
.subscribe(control => {
2017-12-12 13:12:13 +01:00
this.calculateValueForProgressbar();
2017-12-07 17:34:48 +01:00
});
2017-12-05 17:56:21 +01:00
}
2018-01-10 09:17:22 +01:00
2017-12-12 13:12:13 +01:00
calculateValueForProgressbar(){
var progressSoFar = this.countFormControlsWithValue(this.formGroup);
var total = this.getFormControlDepthLength(this.formGroup);
var perc = (progressSoFar / total) * 100;
this.value = Number.parseFloat(perc.toPrecision(this.accuracy));
}
2017-12-07 10:26:06 +01:00
countFormControlsWithValue(form: FormGroup): number {
2017-12-05 17:56:21 +01:00
let value = 0;
Object.keys(form.controls).forEach(key => {
let control = form.controls[key]
2017-12-07 10:26:06 +01:00
if (control instanceof FormGroup) value += this.countFormControlsWithValue(control);
2017-12-05 17:56:21 +01:00
else if (control instanceof FormArray) {
let formArray = (<FormArray>control);
for (let i = 0; i < formArray.length; i++) {
2017-12-07 10:26:06 +01:00
value += this.countFormControlsWithValue(<FormGroup>formArray.get("" + i))
2017-12-05 17:56:21 +01:00
}
}
2017-12-07 10:26:06 +01:00
else if (key === "value" && control.value != null) value++;
2017-12-05 17:56:21 +01:00
});
return value;
}
getFormControlDepthLength(form: FormGroup): number {
let value = 0;
Object.keys(form.controls).forEach(key => {
let control = form.controls[key]
2018-01-10 09:17:22 +01:00
if (control instanceof FormGroup)
{ value += this.getFormControlDepthLength(control);}
2017-12-05 17:56:21 +01:00
else if (control instanceof FormArray) {
let formArray = (<FormArray>control);
for (let i = 0; i < formArray.length; i++) {
value += this.getFormControlDepthLength(<FormGroup>formArray.get("" + i))
}
}
2017-12-07 10:26:06 +01:00
else if (key === "value" && this.visibilityRulesService.isElementVisible(null, form.controls["id"].value)) value++;
2017-12-05 17:56:21 +01:00
});
return value;
}
}