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

70 lines
1.9 KiB
TypeScript
Raw Normal View History

2018-11-27 18:33:17 +01:00
import { Component, Input, OnChanges, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core';
import { FormGroup } from '@angular/forms';
2017-11-27 14:40:16 +01:00
import { ActivatedRoute } from '@angular/router';
2018-09-06 14:50:38 +02:00
import { Subscription } from 'rxjs';
2018-11-27 18:33:17 +01:00
import { takeUntil } from 'rxjs/operators';
import { BaseComponent } from '../../core/common/base/base.component';
import { Field } from '../../models/Field';
import { VisibilityRulesService } from '../../utilities/visibility-rules/visibility-rules.service';
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-11-27 18:33:17 +01:00
export class DynamicFormFieldComponent extends BaseComponent implements OnInit, OnChanges, OnDestroy {
2018-10-05 17:00:54 +02:00
@Input() field: Field;
form: FormGroup;
2018-10-05 17:00:54 +02:00
change: Subscription;
trackByFn = (index, item) => item ? item['id'] : null;
2018-11-27 18:33:17 +01:00
constructor(
private route: ActivatedRoute,
public visibilityRulesService: VisibilityRulesService
) {
super();
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);
2018-11-27 18:33:17 +01:00
this.change = this.form.get('value').valueChanges
.pipe(takeUntil(this._destroyed))
.subscribe(item => {
this.visibilityRulesService.updateValueAndVisibility(this.field.id);
});
2018-10-05 17:00:54 +02:00
}
}
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
}