-
Notifications
You must be signed in to change notification settings - Fork 6
/
search-list-value.component.ts
108 lines (90 loc) · 3.06 KB
/
search-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
import {
Component,
Inject,
Input,
OnDestroy,
OnInit,
ViewChild,
} from '@angular/core';
import {
UntypedFormBuilder,
UntypedFormGroup,
Validators,
} from '@angular/forms';
import { MatLegacyMenuTrigger as MatMenuTrigger } from '@angular/material/legacy-menu';
import {
ApiResponseError,
Constants,
KnoraApiConnection,
ListNodeV2,
ResourcePropertyDefinition,
} from '@dasch-swiss/dsp-js';
import { DspApiConnectionToken } from '@dasch-swiss/vre/shared/app-config';
import { ErrorHandlerService } from '@dsp-app/src/app/main/services/error-handler.service';
import { IRI, PropertyValue, Value } from '../operator';
// https://stackoverflow.com/questions/45661010/dynamic-nested-reactive-form-expressionchangedafterithasbeencheckederror
const resolvedPromise = Promise.resolve(null);
@Component({
selector: 'app-search-list-value',
templateUrl: './search-list-value.component.html',
styleUrls: ['./search-list-value.component.scss'],
})
export class SearchListValueComponent
implements OnInit, OnDestroy, PropertyValue
{
@Input() property: ResourcePropertyDefinition;
// parent FormGroup
@Input() formGroup: UntypedFormGroup;
@ViewChild(MatMenuTrigger) menuTrigger: MatMenuTrigger;
type = Constants.ListValue;
form: UntypedFormGroup;
listRootNode: ListNodeV2;
selectedNode: ListNodeV2;
constructor(
@Inject(DspApiConnectionToken)
private _dspApiConnection: KnoraApiConnection,
@Inject(UntypedFormBuilder) private _fb: UntypedFormBuilder,
private _errorHandler: ErrorHandlerService
) {}
ngOnInit() {
this.form = this._fb.group({
listValue: [null, Validators.required],
});
resolvedPromise.then(() => {
// add form to the parent form group
this.formGroup.addControl('propValue', this.form);
});
const rootNodeIri = this._getRootNodeIri();
this._dspApiConnection.v2.list.getList(rootNodeIri).subscribe(
(response: ListNodeV2) => {
this.listRootNode = response;
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
}
);
}
ngOnDestroy() {
// remove form from the parent form group
resolvedPromise.then(() => {
this.formGroup.removeControl('propValue');
});
}
getValue(): Value {
return new IRI(this.form.value.listValue);
}
getSelectedNode(item: ListNodeV2) {
this.menuTrigger.closeMenu();
this.selectedNode = item;
this.form.controls['listValue'].setValue(item.id);
}
private _getRootNodeIri(): string {
const guiAttr = this.property.guiAttributes;
if (guiAttr.length === 1 && guiAttr[0].startsWith('hlist=')) {
const listNodeIri = guiAttr[0].substring(7, guiAttr[0].length - 1); // hlist=<>, get also rid of <>
return listNodeIri;
} else {
console.error('No root node Iri given for property', guiAttr);
}
}
}