/
list-value.component.ts
168 lines (140 loc) · 5.61 KB
/
list-value.component.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
import { Component, Inject, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild } from '@angular/core';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
import { MatMenuTrigger } from '@angular/material/menu';
import {
ApiResponseError,
CreateListValue,
KnoraApiConnection,
ListNodeV2,
ReadListValue,
ResourcePropertyDefinition,
UpdateListValue
} from '@dasch-swiss/dsp-js';
import { Subscription } from 'rxjs';
import { DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens';
import { BaseValueDirective } from 'src/app/main/directive/base-value.directive';
import { ErrorHandlerService } from 'src/app/main/error/error-handler.service';
import { NotificationService } from 'src/app/main/services/notification.service';
// https://stackoverflow.com/questions/45661010/dynamic-nested-reactive-form-expressionchangedafterithasbeencheckederror
const resolvedPromise = Promise.resolve(null);
@Component({
selector: 'app-list-value',
templateUrl: './list-value.component.html',
styleUrls: ['./list-value.component.scss']
})
export class ListValueComponent extends BaseValueDirective implements OnInit, OnChanges, OnDestroy {
@Input() displayValue?: ReadListValue;
@Input() propertyDef: ResourcePropertyDefinition;
@ViewChild(MatMenuTrigger) menuTrigger: MatMenuTrigger;
valueFormControl: FormControl;
commentFormControl: FormControl;
listRootNode: ListNodeV2;
// active node
selectedNode: ListNodeV2;
form: FormGroup;
valueChangesSubscription: Subscription;
customValidators = [];
constructor(
@Inject(FormBuilder) private _fb: FormBuilder,
@Inject(DspApiConnectionToken) private _dspApiConnection: KnoraApiConnection,
private _errorHandler: ErrorHandlerService
) {
super();
}
getInitValue(): string | null {
if (this.displayValue !== undefined) {
return this.displayValue.listNode;
} else {
return null;
}
}
// override the resetFormControl() from the base component to deal with appending root nodes.
resetFormControl(): void {
super.resetFormControl();
if (this.mode === 'update') {
this.selectedNode = new ListNodeV2();
this.selectedNode.label = this.displayValue.listNodeLabel;
} else {
this.selectedNode = null;
}
if (this.valueFormControl !== undefined) {
if (this.mode !== 'read') {
this.listRootNode = new ListNodeV2();
const rootNodeIris = this.propertyDef.guiAttributes;
for (const rootNodeIri of rootNodeIris) {
// get rid of the "hlist"
const trimmedRootNodeIRI = rootNodeIri.substr(7, rootNodeIri.length - (1 + 7));
this._dspApiConnection.v2.list.getList(trimmedRootNodeIRI).subscribe(
(response2: ListNodeV2) => {
this.listRootNode.children.push(response2);
}, (error: ApiResponseError) => {
this._errorHandler.showMessage(error);
});
}
} else {
this.valueFormControl.setValue(this.displayValue.listNodeLabel);
}
}
}
ngOnInit() {
this.valueFormControl = new FormControl(null);
this.commentFormControl = new FormControl(null);
this.valueChangesSubscription = this.commentFormControl.valueChanges.subscribe(
data => {
this.valueFormControl.updateValueAndValidity();
}
);
this.form = this._fb.group({
value: this.valueFormControl,
comment: this.commentFormControl
});
this.resetFormControl();
resolvedPromise.then(() => {
// add form to the parent form group
this.addToParentFormGroup(this.formName, this.form);
});
}
ngOnChanges(changes: SimpleChanges): void {
this.resetFormControl();
}
ngOnDestroy(): void {
this.unsubscribeFromValueChanges();
resolvedPromise.then(() => {
// remove form from the parent form group
this.removeFromParentFormGroup(this.formName);
});
}
getNewValue(): CreateListValue | false {
if (this.mode !== 'create' || !this.form.valid || this.isEmptyVal()) {
return false;
}
const newListValue = new CreateListValue();
newListValue.listNode = this.valueFormControl.value;
if (this.commentFormControl.value !== null && this.commentFormControl.value !== '') {
newListValue.valueHasComment = this.commentFormControl.value;
}
return newListValue;
}
getUpdatedValue(): UpdateListValue | false {
if (this.mode !== 'update' || !this.form.valid) {
return false;
}
const updatedListValue = new UpdateListValue();
updatedListValue.id = this.displayValue.id;
if (this.selectedNode) {
updatedListValue.listNode = this.selectedNode.id;
} else {
updatedListValue.listNode = this.displayValue.listNode;
}
if (this.commentFormControl.value !== null && this.commentFormControl.value !== '') {
updatedListValue.valueHasComment = this.commentFormControl.value;
}
return updatedListValue;
}
getSelectedNode(item: ListNodeV2) {
this.menuTrigger.closeMenu();
this.valueFormControl.markAsDirty();
this.selectedNode = item;
this.valueFormControl.setValue(item.id);
}
}