/
list-item.component.ts
170 lines (148 loc) · 6.56 KB
/
list-item.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
169
170
import { Component, EventEmitter, Inject, Input, OnInit, Output } from '@angular/core';
import {
ApiResponseData,
ApiResponseError,
KnoraApiConnection,
ListChildNodeResponse,
ListNode,
ListResponse,
RepositionChildNodeRequest,
RepositionChildNodeResponse
} from '@dasch-swiss/dsp-js';
import { DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens';
import { ErrorHandlerService } from 'src/app/main/error/error-handler.service';
import { ListNodeOperation } from '../list-item-form/list-item-form.component';
@Component({
selector: 'app-list-item',
templateUrl: './list-item.component.html',
styleUrls: ['./list-item.component.scss']
})
export class ListItemComponent implements OnInit {
@Input() list: ListNode[];
@Input() parentIri?: string;
@Input() projectCode: string;
@Input() projectStatus: boolean;
@Input() projectIri: string;
@Input() childNode: boolean;
@Input() language?: string;
@Output() refreshChildren: EventEmitter<ListNode[]> = new EventEmitter<ListNode[]>();
expandedNode: string;
constructor(
@Inject(DspApiConnectionToken) private _dspApiConnection: KnoraApiConnection,
private _errorHandler: ErrorHandlerService
) { }
ngOnInit() {
// in case of parent node: run the following request to get the entire list
if (!this.childNode) {
this._dspApiConnection.admin.listsEndpoint.getList(this.parentIri).subscribe(
(result: ApiResponseData<ListResponse>) => {
this.list = result.body.list.children;
this.language = result.body.list.listinfo.labels[0].language;
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
}
);
}
}
/**
* checks if parent node should show its children.
* @param id id of parent node.
*/
showChildren(id: string): boolean {
return (id === this.expandedNode);
}
/**
* called from template when the 'expand' button is clicked.
*
* @param id id of parent node for which the 'expand' button was clicked.
*/
toggleChildren(id: string) {
if (this.showChildren(id)) {
this.expandedNode = undefined;
} else {
this.expandedNode = id;
}
}
/**
* called when the 'refreshParent' event from ListItemFormComponent is triggered.
*
* @param data info about the operation that was performed on the node and should be reflected in the UI.
* @param firstNode states whether or not the node is a new child node; defaults to false.
*/
updateView(data: ListNodeOperation, firstNode: boolean = false) {
// update the view by updating the existing list
if (data instanceof ListNodeOperation) {
switch (data.operation) {
case 'create': {
if (firstNode) {
// in case of new child node, we have to use the children from list
const index: number = this.list.findIndex(item => item.id === this.expandedNode);
this.list[index].children.push(data.listNode);
} else {
this.list.push(data.listNode);
}
break;
}
case 'insert': {
// get the corresponding list from the API again and reassign the local list with its response
this._dspApiConnection.admin.listsEndpoint.getList(this.parentIri).subscribe(
(result: ApiResponseData<ListResponse | ListChildNodeResponse>) => {
if (result.body instanceof ListResponse) {
this.list = result.body.list.children; // root node
} else {
this.list = result.body.node.children; // child node
}
// emit the updated list of children to the parent node
this.refreshChildren.emit(this.list);
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
}
);
break;
}
case 'update': {
// use the position from the response from DSP-API to find the correct node to update
this.list[data.listNode.position].labels = data.listNode.labels;
this.list[data.listNode.position].comments = data.listNode.comments;
break;
}
case 'delete': {
// conveniently, the response returned by DSP-API contains the entire list without the deleted node within its 'children'
// we can just reassign the list to this
this.list = data.listNode.children;
// emit the updated list of children to the parent node
this.refreshChildren.emit(this.list);
break;
}
case 'reposition': {
const repositionRequest: RepositionChildNodeRequest = new RepositionChildNodeRequest();
repositionRequest.parentNodeIri = this.parentIri;
repositionRequest.position = data.listNode.position;
// since we don't have any way to know the parent IRI from the ListItemForm component, we need to do the API call here
// --> TODO now we have the parent IRI within the ListItemForm component so we can move this logic there
this._dspApiConnection.admin.listsEndpoint.repositionChildNode(data.listNode.id, repositionRequest).subscribe(
(res: ApiResponseData<RepositionChildNodeResponse>) => {
this.list = res.body.node.children;
this.refreshChildren.emit(this.list);
}
);
break;
}
default: {
break;
}
}
}
}
/**
* updates the children of the parent node
*
* @param children the updated list of children nodes
* @param position the position of the parent node
*/
updateParentNodeChildren(children: ListNode[], position: number) {
this.list[position].children = children;
}
}