/
base-value.directive.ts
201 lines (168 loc) · 6.15 KB
/
base-value.directive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { Directive, Input } from '@angular/core';
import { AbstractControl, FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms';
import { CreateValue, ReadValue, UpdateValue } from '@dasch-swiss/dsp-js';
import { Subscription } from 'rxjs';
@Directive()
export abstract class BaseValueDirective {
/**
* value to be displayed, if any.
*/
@Input() abstract displayValue?: ReadValue;
/**
* sets the mode of the component.
*/
@Input() mode: 'read' | 'update' | 'create' | 'search';
/**
* parent FormGroup that contains all child FormGroups
*/
@Input() parentForm?: FormGroup;
/**
* name of the FormGroup, used to add to the parentForm because the name needs to be unique
*/
@Input() formName = 'Untitled FormGroup';
/**
* controls if the value should be required.
*/
@Input() valueRequiredValidator = true;
shouldShowComment = false;
/**
* formControl element for the value.
*/
abstract valueFormControl: FormControl;
/**
* formControl element for the comment on the value.
*/
abstract commentFormControl: FormControl;
/**
* formGroup that contains FormControl elements.
*/
abstract form: FormGroup;
/**
* subscription used for when the value changes.
*/
abstract valueChangesSubscription: Subscription;
/**
* custom validators for a specific value type.
* Can be initialized to an empty array if not needed.
*/
abstract customValidators: ValidatorFn[];
/**
* standard implementation for comparison of primitive values.
* Returns true if two values are equal.
*
* @param initValue Initially given value.
* @param curValue Current value.
*/
standardValueComparisonFunc(initValue: any, curValue: any): boolean {
return initValue === curValue;
}
/**
* standard implementation to determine if a value or comment have been changed.
*
* @param initValue Initially given value.
* @param initComment Initially given comment.
* @param commentFormControl FormControl of the current comment.
*/
standardValidatorFunc: (val: any, comment: string, commentCtrl: FormControl)
=> ValidatorFn = (initValue: any, initComment: string, commentFormControl: FormControl): ValidatorFn => (control: AbstractControl): { [key: string]: any } | null => {
const invalid = this.standardValueComparisonFunc(initValue, control.value)
&& (initComment === commentFormControl.value || (initComment === null && commentFormControl.value === ''));
return invalid ? { valueNotChanged: { value: control.value } } : null;
};
/**
* returns the initially given value comment set via displayValue.
* Returns null if no value comment was given.
*/
getInitComment(): string | null {
if (this.displayValue !== undefined && this.displayValue.valueHasComment !== undefined) {
return this.displayValue.valueHasComment;
} else {
return null;
}
}
/**
* resets the form control elements
* with displayValue's value and value comment.
* Depending on the mode, validators are reset.
*/
resetFormControl(): void {
if (this.valueFormControl !== undefined && this.commentFormControl !== undefined) {
const initialValue = this.getInitValue();
const initialComment = this.getInitComment();
this.valueFormControl.reset();
this.valueFormControl.setValue(initialValue);
this.commentFormControl.setValue(initialComment);
this.valueFormControl.clearValidators();
// set validators depending on mode
if (this.mode === 'update') {
// console.log('reset update validators');
this.valueFormControl.setValidators([Validators.required, this.standardValidatorFunc(initialValue, initialComment, this.commentFormControl)].concat(this.customValidators));
} else {
// console.log('reset read/create validators');
if (this.valueRequiredValidator) {
this.valueFormControl.setValidators([Validators.required].concat(this.customValidators));
} else {
this.valueFormControl.setValidators(this.customValidators);
}
}
this.valueFormControl.updateValueAndValidity();
}
}
/**
* unsubscribes from the valueChangesSubscription
*/
unsubscribeFromValueChanges(): void {
if (this.valueChangesSubscription !== undefined) {
this.valueChangesSubscription.unsubscribe();
}
}
/**
* hide comment field by default if in READ mode
*/
updateCommentVisibility(): void {
this.shouldShowComment = this.mode === 'read' ? true : false;
}
/**
* toggles visibility of the comment field regardless of the mode
*/
toggleCommentVisibility(): void {
this.shouldShowComment = !this.shouldShowComment;
}
/**
* add the value components FormGroup to a parent FormGroup if one is defined
*/
addToParentFormGroup(name: string, form: FormGroup) {
if (this.parentForm) {
this.parentForm.addControl(name, form);
}
}
/**
* remove the value components FormGroup from a parent FormGroup if one is defined
*/
removeFromParentFormGroup(name: string) {
if (this.parentForm) {
this.parentForm.removeControl(name);
}
}
/**
* checks if the value is empty.
*/
isEmptyVal(): boolean {
return this.valueFormControl.value === null || this.valueFormControl.value === '';
}
/**
* returns the initially given value set via displayValue.
* Returns null if no value was given.
*/
abstract getInitValue(): any;
/**
* returns a value that is to be created.
* Returns false if invalid.
*/
abstract getNewValue(): CreateValue | false;
/**
* returns a value that is to be updated.
* Returns false if invalid.
*/
abstract getUpdatedValue(): UpdateValue | false;
}