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

67 lines
1.9 KiB
TypeScript
Raw Normal View History

2017-11-27 14:40:16 +01:00
import { Field } from '../../models/Field';
2018-09-06 14:50:38 +02:00
import { Component, Input, OnInit, ViewEncapsulation, ChangeDetectionStrategy } 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-05-28 11:50:42 +02:00
selector: '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-05-28 11:50:42 +02:00
export class DynamicFormFieldComponent implements OnInit {
@Input() field: Field;
2018-09-06 14:50:38 +02:00
form: FormGroup;
2018-05-28 11:50:42 +02:00
@Input() pathName: string;
@Input() path: string;
2018-09-06 14:50:38 +02:00
change: Subscription;
2018-09-18 14:41:24 +02:00
trackByFn = (index, item) => item ? item["id"] : null
2018-09-06 14:50:38 +02:00
constructor(private route: ActivatedRoute, public visibilityRulesService: VisibilityRulesService) {
2018-05-28 11:50:42 +02:00
2018-09-06 14:50:38 +02:00
}
2018-05-28 11:50:42 +02:00
ngOnInit() {
2018-09-18 14:41:24 +02:00
if (this.field) {
this.form = this.visibilityRulesService.getFormGroup(this.field.id)
if (!this.form) debugger;
if (!this.form.get('value')) debugger;
this.change = this.form.get('value').valueChanges.subscribe(item => {
this.visibilityRulesService.updateValueAndVisibility(this.field.id)
})
}
2018-05-28 11:50:42 +02:00
}
ngOnChanges(changeRecord) {
}
2018-09-06 14:50:38 +02:00
ngOnDestroy(): void {
//Called once, before the instance is destroyed.
//Add 'implements OnDestroy' to the class.
2018-09-18 14:41:24 +02:00
if(this.change) this.change.unsubscribe()
2018-09-06 14:50:38 +02:00
}
2018-05-28 11:50:42 +02:00
clearInput() {
}
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");
}
}