argos/dmp-frontend/src/app/form/dynamic-fields/dynamic-form-field.componen...

65 lines
1.8 KiB
TypeScript
Raw Normal View History

2017-11-27 14:40:16 +01:00
import { Field } from '../../models/Field';
2018-10-05 17:00:54 +02:00
import { Component, Input, OnInit, ViewEncapsulation, ChangeDetectionStrategy, OnChanges, OnDestroy } from '@angular/core';
2017-11-27 14:40:16 +01:00
import { FormGroup, ValidatorFn, AbstractControl, Validators } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
2018-05-28 11:50:42 +02:00
import { VisibilityRulesService } from '../../utilities/visibility-rules/visibility-rules.service';
2018-09-06 14:50:38 +02:00
import { Subscription } from 'rxjs';
2017-11-27 14:40:16 +01:00
@Component({
2018-10-05 17:00:54 +02:00
selector: 'app-df-field',
templateUrl: './dynamic-form-field.component.html',
// styles: ['.checkBoxLabelCustom {font-weight: 700;}']
styleUrls: [
'./dynamic-form-field.component.css'
],
encapsulation: ViewEncapsulation.None
2017-11-27 14:40:16 +01:00
})
2018-10-05 17:00:54 +02:00
export class DynamicFormFieldComponent implements OnInit, OnChanges, OnDestroy {
@Input() field: Field;
form: FormGroup;
@Input() pathName: string;
@Input() path: string;
change: Subscription;
trackByFn = (index, item) => item ? item['id'] : null;
constructor(private route: ActivatedRoute, public visibilityRulesService: VisibilityRulesService) {
2018-05-28 11:50:42 +02:00
2018-10-05 17:00:54 +02:00
}
2018-05-28 11:50:42 +02:00
2018-10-05 17:00:54 +02:00
ngOnInit() {
if (this.field) {
this.form = this.visibilityRulesService.getFormGroup(this.field.id);
this.change = this.form.get('value').valueChanges.subscribe(item => {
this.visibilityRulesService.updateValueAndVisibility(this.field.id);
});
}
}
2018-05-28 11:50:42 +02:00
2018-10-05 17:00:54 +02:00
ngOnChanges(changeRecord) {
2018-05-28 11:50:42 +02:00
2018-10-05 17:00:54 +02:00
}
2018-05-28 11:50:42 +02:00
2018-10-05 17:00:54 +02:00
ngOnDestroy(): void {
//Called once, before the instance is destroyed.
//Add 'implements OnDestroy' to the class.
if (this.change) { this.change.unsubscribe(); }
}
2018-09-06 14:50:38 +02:00
2018-10-05 17:00:54 +02:00
clearInput() {
}
2018-05-28 11:50:42 +02:00
2018-10-05 17:00:54 +02:00
get isValid() {
return this.form.get('value').valid;
}
get isValidRequired() {
return this.form.get('value').hasError('required');
}
get isValidPattern() {
return this.form.get('value').hasError('pattern');
}
get isValidCustom() {
return this.form.get('value').hasError('forbiddenName');
}
2018-05-28 11:50:42 +02:00
}