/
list-item-form.component.ts
311 lines (261 loc) · 10.8 KB
/
list-item-form.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import { animate, state, style, transition, trigger } from '@angular/animations';
import { Component, EventEmitter, Inject, Input, OnInit, Output } from '@angular/core';
import { MatDialog, MatDialogConfig } from '@angular/material/dialog';
import {
ApiResponseData,
ApiResponseError,
ChildNodeInfo,
CreateChildNodeRequest,
DeleteListNodeResponse,
KnoraApiConnection,
ListInfoResponse,
ListNode,
ListNodeInfoResponse,
StringLiteral
} from '@dasch-swiss/dsp-js';
import { DspApiConnectionToken } from '@dasch-swiss/dsp-ui';
import { DialogComponent } from 'src/app/main/dialog/dialog.component';
import { ErrorHandlerService } from 'src/app/main/error/error-handler.service';
export class ListNodeOperation {
operation: 'create' | 'insert' | 'update' | 'delete' | 'reposition';
listNode: ListNode;
}
@Component({
selector: 'app-list-item-form',
templateUrl: './list-item-form.component.html',
styleUrls: ['./list-item-form.component.scss'],
animations: [
// the fade-in/fade-out animation.
// https://www.kdechant.com/blog/angular-animations-fade-in-and-fade-out
trigger('simpleFadeAnimation', [
// the "in" style determines the "resting" state of the element when it is visible.
state('in', style({opacity: 1})),
// fade in when created.
transition(':enter', [
// the styles start from this point when the element appears
style({opacity: 0}),
// and animate toward the "in" state above
animate(150)
]),
// fade out when destroyed.
transition(':leave',
// fading out uses a different syntax, with the "style" being passed into animate()
animate(150, style({opacity: 0})))
])
]
})
export class ListItemFormComponent implements OnInit {
/**
* node id, in case of edit item
*/
@Input() iri?: string;
/**
* project shortcode
*/
@Input() projectcode?: string;
/**
* project id
*/
@Input() projectIri?: string;
/**
* parent node id
*/
@Input() parentIri?: string;
@Input() labels?: StringLiteral[];
// set main / pre-defined language
@Input() language?: string;
@Input() position: number;
// is this node in the last position of the list
@Input() lastPosition: boolean = false;
@Input() newNode: boolean = false;
@Output() refreshParent: EventEmitter<ListNodeOperation> = new EventEmitter<ListNodeOperation>();
loading: boolean;
initComponent: boolean;
placeholder: string = 'Append item to ';
showActionBubble: boolean = false;
constructor(
@Inject(DspApiConnectionToken) private _dspApiConnection: KnoraApiConnection,
private _errorHandler: ErrorHandlerService,
private _dialog: MatDialog
) { }
ngOnInit() {
this.initComponent = true;
if (this.labels && this.labels.length > 0) {
this.placeholder = 'Edit item ';
}
// it can be used in the input placeholder
if (this.newNode) {
this._dspApiConnection.admin.listsEndpoint.getListNodeInfo(this.parentIri).subscribe(
(response: ApiResponseData<ListNodeInfoResponse | ListInfoResponse>) => {
if (response.body instanceof ListInfoResponse) { // root node
this.placeholder += response.body.listinfo.labels[0].value;
} else { // child node
this.placeholder += response.body.nodeinfo.labels[0].value;
}
this.initComponent = false;
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
}
);
}
}
/**
* Called from the template when the plus button is clicked.
* Sends the info to make a new child node to DSP-API and refreshes the UI to show the newly added node at the end of the list.
*/
createChildNode() {
if (!this.labels.length) {
return;
}
this.loading = true;
// generate the data payload
const childNode: CreateChildNodeRequest = new CreateChildNodeRequest();
childNode.parentNodeIri = this.parentIri;
childNode.projectIri = this.projectIri;
childNode.name = this.projectcode + '-' + Math.random().toString(36).substr(2) + Math.random().toString(36).substr(2);
// initialize labels
let i = 0;
for (const l of this.labels) {
childNode.labels[i] = new StringLiteral();
childNode.labels[i].language = l.language;
childNode.labels[i].value = l.value;
i++;
}
childNode.comments = []; // TODO: comments are not yet implemented in the template
// init data to emit to parent
const listNodeOperation: ListNodeOperation = new ListNodeOperation();
// send payload to dsp-api's api
this._dspApiConnection.admin.listsEndpoint.createChildNode(childNode).subscribe(
(response: ApiResponseData<ListNodeInfoResponse>) => {
// this needs to return a ListNode as opposed to a ListNodeInfo, so we make one
listNodeOperation.listNode = new ListNode();
listNodeOperation.listNode.hasRootNode = response.body.nodeinfo.hasRootNode;
listNodeOperation.listNode.id = response.body.nodeinfo.id;
listNodeOperation.listNode.labels = response.body.nodeinfo.labels;
listNodeOperation.listNode.name = response.body.nodeinfo.name;
listNodeOperation.listNode.position = response.body.nodeinfo.position;
listNodeOperation.operation = 'create';
this.refreshParent.emit(listNodeOperation);
this.loading = false;
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
}
);
}
/**
* Called from the template any time the label changes.
* Currently only implemented for labels because entering comments is not yet supported.
*
* @param data the data that was changed.
*/
handleData(data: StringLiteral[]) {
// this shouldn't run on the init...
if (!this.initComponent) {
this.labels = data;
}
}
/**
* Show action bubble with various CRUD buttons when hovered over.
*/
mouseEnter() {
this.showActionBubble = true;
}
/**
* Hide action bubble with various CRUD buttons when not hovered over.
*/
mouseLeave() {
this.showActionBubble = false;
}
/**
* Called when the 'edit' or 'delete' button is clicked.
*
* @param mode mode to tell DialogComponent which part of the template to show.
* @param name label of the node; for now this is always the first label in the array.
* @param iri iri of the node.
*/
openDialog(mode: string, name: string, iri?: string): void {
const dialogConfig: MatDialogConfig = {
width: '640px',
position: {
top: '112px'
},
data: {
mode: mode,
title: mode === 'editListNode' ? name : 'Insert new child node',
id: iri,
project: this.projectIri,
projectCode: this.projectcode,
parentIri: this.parentIri,
position: this.position
}
};
// open the dialog box
const dialogRef = this._dialog.open(
DialogComponent,
dialogConfig
);
dialogRef.afterClosed().subscribe((data: ChildNodeInfo | boolean) => {
// init data to emit to parent
const listNodeOperation = new ListNodeOperation();
if (mode === 'insertListNode' && data) {
// the call to DSP-API to insert the new node is done in the child component
listNodeOperation.listNode = (data as ListNode);
listNodeOperation.operation = 'insert';
this.refreshParent.emit(listNodeOperation);
} else if (mode === 'editListNode' && data) { // update
// the call to DSP-API to update the node is done in the child component
listNodeOperation.listNode = (data as ListNode);
listNodeOperation.operation = 'update';
// emit data to parent to update the view
this.refreshParent.emit(listNodeOperation);
this.labels = (data as ChildNodeInfo).labels;
} else if (mode === 'deleteListNode' && typeof(data) === 'boolean' && data === true) { // delete
// delete the node
this._dspApiConnection.admin.listsEndpoint.deleteListNode(iri).subscribe(
(response: ApiResponseData<DeleteListNodeResponse>) => {
listNodeOperation.listNode = response.body.node;
listNodeOperation.operation = 'delete';
// emit data to parent to update the view
this.refreshParent.emit(listNodeOperation);
},
(error: ApiResponseError) => {
// if DSP-API returns a 400, it is likely that the list node is in use so we inform the user of this
if (error.status === 400) {
const errorDialogConfig: MatDialogConfig = {
width: '640px',
position: {
top: '112px'
},
data: { mode: 'deleteListNodeError'}
};
// open the dialog box
this._dialog.open(DialogComponent, errorDialogConfig);
} else {
// use default error behavior
this._errorHandler.showMessage(error);
}
}
);
}
});
}
/**
* Called from the template when either of the two reposition buttons is clicked
* @param direction in which direction the node should move
*/
repositionNode(direction: 'up' | 'down') {
const listNodeOperation = new ListNodeOperation();
listNodeOperation.operation = 'reposition';
listNodeOperation.listNode = new ListNode();
// set desired node position
if (direction === 'up') {
listNodeOperation.listNode.position = this.position - 1;
} else {
listNodeOperation.listNode.position = this.position + 1;
}
listNodeOperation.listNode.id = this.iri;
this.refreshParent.emit(listNodeOperation);
}
}