103 lines
2.7 KiB
TypeScript
103 lines
2.7 KiB
TypeScript
import { Component, OnInit, Input } from '@angular/core';
|
|
import { FormControl, FormGroup, NgForm, FormArray, AbstractControl } from '@angular/forms';
|
|
import { BaseCriteriaErrorModel } from '../../../../models/criteria/BaseCriteriaErrorModel';
|
|
|
|
@Component({
|
|
selector: 'base-criteria-component',
|
|
template: '',
|
|
providers: [
|
|
|
|
]
|
|
})
|
|
|
|
export class BaseCriteriaComponent implements OnInit {
|
|
|
|
public refreshCallback: Function = null;
|
|
public formGroup: FormGroup = null;
|
|
|
|
constructor(
|
|
public baseErrorModel?: BaseCriteriaErrorModel,
|
|
) {
|
|
}
|
|
|
|
ngOnInit() {
|
|
if (this.baseErrorModel == null) { this.baseErrorModel = new BaseCriteriaErrorModel(); }
|
|
}
|
|
|
|
controlModified(): void {
|
|
this.clearErrorModel();
|
|
if (!this.isFormValid()) { return; }
|
|
if (this.refreshCallback != null) { this.refreshCallback(); }
|
|
}
|
|
|
|
public isFormValid(): boolean {
|
|
this.touchAllFormFields(this.formGroup);
|
|
this.validateAllFormFields(this.formGroup);
|
|
return this.formGroup.valid;
|
|
}
|
|
|
|
public getFormData(): any {
|
|
return this.formGroup.value;
|
|
}
|
|
|
|
public getFormControl(controlName: string): AbstractControl {
|
|
return this.formGroup.get(controlName);
|
|
}
|
|
|
|
public disableFormFields(formControl: AbstractControl) {
|
|
formControl.disable();
|
|
}
|
|
|
|
public validateAllFormFields(formControl: AbstractControl) {
|
|
if (formControl instanceof FormControl) {
|
|
formControl.updateValueAndValidity({ emitEvent: false })
|
|
} else if (formControl instanceof FormGroup) {
|
|
Object.keys(formControl.controls).forEach(item => {
|
|
const control = formControl.get(item);
|
|
this.validateAllFormFields(control);
|
|
})
|
|
} else if (formControl instanceof FormArray) {
|
|
formControl.controls.forEach(item => {
|
|
this.validateAllFormFields(item);
|
|
})
|
|
}
|
|
}
|
|
|
|
public touchAllFormFields(formControl: AbstractControl) {
|
|
if (formControl instanceof FormControl) {
|
|
formControl.markAsTouched();
|
|
} else if (formControl instanceof FormGroup) {
|
|
Object.keys(formControl.controls).forEach(item => {
|
|
const control = formControl.get(item);
|
|
this.touchAllFormFields(control);
|
|
})
|
|
} else if (formControl instanceof FormArray) {
|
|
formControl.controls.forEach(item => {
|
|
this.touchAllFormFields(item);
|
|
})
|
|
}
|
|
}
|
|
|
|
setRefreshCallback(callback: Function): void {
|
|
this.refreshCallback = callback;
|
|
}
|
|
|
|
onCallbackError(error: any) {
|
|
this.setErrorModel(error.error);
|
|
this.validateAllFormFields(this.formGroup);
|
|
}
|
|
|
|
public setErrorModel(errorModel: BaseCriteriaErrorModel) {
|
|
Object.keys(errorModel).forEach(item => {
|
|
(<any>this.baseErrorModel)[item] = (<any>errorModel)[item];
|
|
})
|
|
}
|
|
|
|
public clearErrorModel() {
|
|
Object.keys(this.baseErrorModel).forEach(item => {
|
|
(<any>this.baseErrorModel)[item] = '';
|
|
})
|
|
}
|
|
|
|
}
|