51 lines
1.8 KiB
TypeScript
51 lines
1.8 KiB
TypeScript
|
|
||
|
import { Component, Input, OnInit } from '@angular/core';
|
||
|
import { FormGroup, FormControl, FormArray } from '@angular/forms'
|
||
|
@Component({
|
||
|
selector: 'progress-bar',
|
||
|
templateUrl: './progress-bar.component.html',
|
||
|
})
|
||
|
export class ProgressBarComponent implements OnInit {
|
||
|
@Input() formGroup: FormGroup
|
||
|
|
||
|
private value: number;
|
||
|
ngOnInit() {
|
||
|
this.formGroup
|
||
|
.valueChanges
|
||
|
.subscribe(control => {
|
||
|
this.value = (this.getDirtyValues(this.formGroup) / this.getFormControlDepthLength(this.formGroup)) * 100
|
||
|
})
|
||
|
}
|
||
|
|
||
|
getDirtyValues(form: FormGroup): number {
|
||
|
let value = 0;
|
||
|
Object.keys(form.controls).forEach(key => {
|
||
|
let control = form.controls[key]
|
||
|
if (control instanceof FormGroup) value += this.getDirtyValues(control);
|
||
|
else if (control instanceof FormArray) {
|
||
|
let formArray = (<FormArray>control);
|
||
|
for (let i = 0; i < formArray.length; i++) {
|
||
|
value += this.getDirtyValues(<FormGroup>formArray.get("" + i))
|
||
|
}
|
||
|
}
|
||
|
else if (control.valid) value++;
|
||
|
});
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
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++) {
|
||
|
value += this.getFormControlDepthLength(<FormGroup>formArray.get("" + i))
|
||
|
}
|
||
|
}
|
||
|
else if (control instanceof FormControl) value++;
|
||
|
});
|
||
|
return value;
|
||
|
}
|
||
|
}
|