2018-05-28 11:50:42 +02:00
|
|
|
|
import { VisibilityRulesService } from '../../utilities/visibility-rules/visibility-rules.service';
|
2017-12-12 12:33:14 +01:00
|
|
|
|
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({
|
2018-05-28 11:50:42 +02:00
|
|
|
|
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 {
|
2018-05-28 11:50:42 +02:00
|
|
|
|
@Input() formGroup: FormGroup
|
|
|
|
|
@Input("progressValueAccuracy") public accuracy: number = 1;
|
|
|
|
|
determinateProgressValue: number;
|
2017-12-05 17:56:21 +01:00
|
|
|
|
|
2018-05-28 11:50:42 +02:00
|
|
|
|
constructor(private visibilityRulesService: VisibilityRulesService) { }
|
2017-12-07 10:26:06 +01:00
|
|
|
|
|
2018-07-11 15:47:36 +02:00
|
|
|
|
public value: number = 0;
|
2018-05-28 11:50:42 +02:00
|
|
|
|
ngOnInit() {
|
|
|
|
|
this.calculateValueForProgressbar()
|
|
|
|
|
this.formGroup
|
|
|
|
|
.valueChanges
|
|
|
|
|
.subscribe(control => {
|
|
|
|
|
this.calculateValueForProgressbar();
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-01-11 12:15:15 +01:00
|
|
|
|
|
2018-05-28 11:50:42 +02: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-12 13:12:13 +01:00
|
|
|
|
|
2018-05-28 11:50:42 +02:00
|
|
|
|
countFormControlsWithValue(form: FormGroup): number {
|
|
|
|
|
let value = 0;
|
|
|
|
|
Object.keys(form.controls).forEach(key => {
|
|
|
|
|
let control = form.controls[key]
|
|
|
|
|
if (control instanceof FormGroup) value += this.countFormControlsWithValue(control);
|
|
|
|
|
else if (control instanceof FormArray) {
|
|
|
|
|
let 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-05-28 11:50:42 +02:00
|
|
|
|
getFormControlDepthLength(form: FormGroup): number {
|
|
|
|
|
let value = 0;
|
|
|
|
|
Object.keys(form.controls).forEach(key => {
|
|
|
|
|
let control = form.controls[key];
|
|
|
|
|
if (control instanceof FormGroup) { value += this.getFormControlDepthLength(control); }
|
|
|
|
|
else if (control instanceof FormArray) {
|
|
|
|
|
let formArray = (<FormArray>control);
|
|
|
|
|
for (let i = 0; i < formArray.length; i++) {
|
2018-09-06 14:50:38 +02:00
|
|
|
|
//if (<FormGroup>formArray.get("" + i).value && this.visibilityRulesService.isElementVisible(null, formArray.get("" + i).value.id))
|
|
|
|
|
//value += this.getFormControlDepthLength(<FormGroup>formArray.get("" + i))
|
2018-05-28 11:50:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-06 14:50:38 +02:00
|
|
|
|
//else if (key === "value" && this.visibilityRulesService.isElementVisible(null, form.controls["id"].value))
|
|
|
|
|
//value++;
|
2018-05-28 11:50:42 +02:00
|
|
|
|
});
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
}
|