-
Notifications
You must be signed in to change notification settings - Fork 6
/
display-edit.component.ts
443 lines (354 loc) · 15.7 KB
/
display-edit.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import { animate, state, style, transition, trigger } from '@angular/animations';
import { Component, EventEmitter, Inject, Input, OnInit, Output, ViewChild } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import {
ApiResponseError,
Constants,
DeleteValue,
DeleteValueResponse,
KnoraApiConnection,
PermissionUtil,
ReadLinkValue,
ReadResource,
ReadUser,
ReadValue,
ResourcePropertyDefinition,
UpdateResource,
UpdateValue,
WriteValueResponse
} from '@dasch-swiss/dsp-js';
import { mergeMap } from 'rxjs/operators';
import { DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens';
import { BaseValueDirective } from 'src/app/main/directive/base-value.directive';
import {
ConfirmationDialogComponent,
ConfirmationDialogData,
ConfirmationDialogValueDeletionPayload
} from '../../../../main/action/confirmation-dialog/confirmation-dialog.component';
import { PropertyInfoValues } from '../../properties/properties.component';
import { UserService } from '../../services/user.service';
import {
DeletedEventValue,
EmitEvent,
Events,
UpdatedEventValues,
ValueOperationEventService
} from '../../services/value-operation-event.service';
import { ValueService } from '../../services/value.service';
@Component({
selector: 'app-display-edit',
templateUrl: './display-edit.component.html',
styleUrls: ['./display-edit.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 DisplayEditComponent implements OnInit {
@ViewChild('displayVal') displayValueComponent: BaseValueDirective;
@Input() displayValue: ReadValue;
@Input() propArray: PropertyInfoValues[];
@Input() parentResource: ReadResource;
@Input() configuration?: object;
@Input() canDelete: boolean;
@Input() projectStatus: boolean;
@Input() valueUuidToHighlight: string;
@Output() referredResourceClicked: EventEmitter<ReadLinkValue> = new EventEmitter<ReadLinkValue>();
@Output() referredResourceHovered: EventEmitter<ReadLinkValue> = new EventEmitter<ReadLinkValue>();
constants = Constants;
mode: 'read' | 'update' | 'create' | 'search';
canModify: boolean;
editModeActive = false;
submittingValue = false;
shouldShowCommentToggle: boolean;
// type of given displayValue
// or knora-api-js-lib class representing the value
valueTypeOrClass: string;
// indicates if value can be edited
readOnlyValue: boolean;
// indicates if the action bubble with the CRUD buttons should be shown
showActionBubble = false;
// string used as class name to add add to value-component element on hover
valueHovered: boolean;
dateDisplayOptions: 'era' | 'calendar' | 'all';
showDateLabels = false;
// gui element in case of textValue
textValueGuiEle: 'simpleText' | 'textArea' | 'richText';
dateFormat: string;
user: ReadUser;
ontoIri: string;
constructor(
@Inject(DspApiConnectionToken) private _dspApiConnection: KnoraApiConnection,
private _valueOperationEventService: ValueOperationEventService,
private _dialog: MatDialog,
private _userService: UserService,
private _valueService: ValueService) {
}
ngOnInit() {
this.ontoIri = this.parentResource.type.split('#')[0];
this.mode = 'read';
this.dateDisplayOptions = 'all';
this.showDateLabels = true;
this.dateFormat = 'dd.MM.YYYY';
// determine if user has modify permissions
const allPermissions = PermissionUtil.allUserPermissions(this.displayValue.userHasPermission as 'RV' | 'V' | 'M' | 'D' | 'CR');
this.canModify = allPermissions.indexOf(PermissionUtil.Permissions.M) !== -1;
// check if comment toggle button should be shown
this.checkCommentToggleVisibility();
this.valueTypeOrClass = this._valueService.getValueTypeOrClass(this.displayValue);
// get the resource property definition
const resPropDef = this.parentResource.entityInfo.getPropertyDefinitionsByType(ResourcePropertyDefinition).filter(
(propDef: ResourcePropertyDefinition) => propDef.id === this.displayValue.property
);
// we should also take the gui element into account in case of text value
// since simple text values and rich text values share the same object type 'TextValue',
// we need to use the ValueTypeService in order to assign it the correct object type for the ngSwitch in the template
if (this.valueTypeOrClass === 'ReadTextValueAsString') {
// handle the correct gui element depending on guiEle property
this.textValueGuiEle = this._valueService.getTextValueGuiEle(resPropDef[0].guiElement);
}
if (resPropDef.length !== 1) {
// this should never happen because we always have the property info for the given value
throw new Error('Resource Property Definition could not be found: ' + this.displayValue.property);
}
this.readOnlyValue = this._valueService.isReadOnly(this.valueTypeOrClass, this.displayValue, resPropDef[0]);
// prevent getting info about system user (standoff link values are managed by the system)
if (this.displayValue.attachedToUser !== 'http://www.knora.org/ontology/knora-admin#SystemUser') {
this._userService.getUser(this.displayValue.attachedToUser).subscribe(
user => {
this.user = user.user;
}
);
}
}
getTooltipText(): string {
const creationDate = 'Creation date: ' + this.displayValue.valueCreationDate;
const creatorInfo = this.user ? '\n Value creator: ' + this.user?.givenName + ' ' + this.user?.familyName : '';
return creationDate + creatorInfo;
}
/**
* react when a standoff link in a text has received a click event.
*
* @param resIri the Iri of the resource the standoff link refers to.
*/
standoffLinkClicked(resIri: string): void {
// find the corresponding standoff link value
const referredResStandoffLinkVal: ReadLinkValue[] = this._getStandoffLinkValueForResource(resIri);
// only emit an event if the corresponding standoff link value could be found
if (referredResStandoffLinkVal.length === 1) {
this.referredResourceClicked.emit(referredResStandoffLinkVal[0]);
}
}
/**
* react when a standoff link in a text has received a hover event.
*
* @param resIri the Iri of the resource the standoff link refers to.
*/
standoffLinkHovered(resIri: string): void {
// find the corresponding standoff link value
const referredResStandoffLinkVal: ReadLinkValue[] = this._getStandoffLinkValueForResource(resIri);
// only emit an event if the corresponding standoff link value could be found
if (referredResStandoffLinkVal.length === 1) {
this.referredResourceHovered.emit(referredResStandoffLinkVal[0]);
}
}
/**
* show the form components and CRUD buttons to update an existing value or add a new value.
*/
activateEditMode() {
this.editModeActive = true;
this.valueHovered = false;
this.mode = 'update';
// hide comment toggle button while in edit mode
this.checkCommentToggleVisibility();
// hide read mode comment when switching to edit mode
this.displayValueComponent.shouldShowComment = false;
}
/**
* save a new version of an existing property value.
*/
saveEditValue() {
// hide the CRUD buttons
this.editModeActive = false;
this.showActionBubble = false;
// show the progress indicator
this.submittingValue = true;
const updatedVal = this.displayValueComponent.getUpdatedValue();
if (updatedVal instanceof UpdateValue) {
const updateRes = new UpdateResource();
updateRes.id = this.parentResource.id;
updateRes.type = this.parentResource.type;
updateRes.property = this.displayValue.property;
updateRes.value = updatedVal;
this._dspApiConnection.v2.values.updateValue(updateRes as UpdateResource<UpdateValue>).pipe(
mergeMap((res: WriteValueResponse) => this._dspApiConnection.v2.values.getValue(this.parentResource.id, res.uuid))
).subscribe(
(res2: ReadResource) => {
this._valueOperationEventService.emit(
new EmitEvent(Events.ValueUpdated, new UpdatedEventValues(
this.displayValue, res2.getValues(this.displayValue.property)[0])));
this.displayValue = res2.getValues(this.displayValue.property)[0];
this.mode = 'read';
// hide comment once back in read mode
this.displayValueComponent.updateCommentVisibility();
// check if comment toggle button should be shown
this.checkCommentToggleVisibility();
// hide the progress indicator
this.submittingValue = false;
},
(error: ApiResponseError) => {
// error handling
this.editModeActive = true;
// hide the progress indicator
this.submittingValue = false;
switch (error.status) {
case 400:
this.displayValueComponent.valueFormControl.setErrors({ duplicateValue: true });
break;
default:
console.log('There was an error processing your request. Details: ', error);
break;
}
}
);
} else {
console.error('invalid value');
// hide the progress indicator
this.submittingValue = false;
}
}
/**
* open a confirmation dialog box to ensure the user would like to complete the action.
*/
openDialog() {
const dialogData = new ConfirmationDialogData();
dialogData.value = this.displayValue;
dialogData.buttonTextOk = 'Yes, delete the value';
dialogData.buttonTextCancel = 'No, keep the value';
const dialogRef =
this._dialog.open<ConfirmationDialogComponent, ConfirmationDialogData>(ConfirmationDialogComponent, { data: dialogData });
dialogRef.afterClosed().subscribe((payload: ConfirmationDialogValueDeletionPayload) => {
if (payload && payload.confirmed) {
this.deleteValue(payload.deletionComment);
}
});
}
/**
* delete a value from a property.
* Emits an event that can be listened to.
*/
deleteValue(comment?: string) {
const deleteVal = new DeleteValue();
deleteVal.id = this.displayValue.id;
deleteVal.type = this.displayValue.type;
deleteVal.deleteComment = comment;
const updateRes = new UpdateResource();
updateRes.type = this.parentResource.type;
updateRes.id = this.parentResource.id;
updateRes.property = this.displayValue.property;
updateRes.value = deleteVal;
this._dspApiConnection.v2.values.deleteValue(updateRes as UpdateResource<DeleteValue>).pipe(
mergeMap((res: DeleteValueResponse) => {
// emit a ValueDeleted event to the listeners in resource-view component to trigger an update of the UI
this._valueOperationEventService.emit(new EmitEvent(Events.ValueDeleted, new DeletedEventValue(deleteVal)));
return res.result;
})).subscribe();
}
/**
* hide the form components and CRUD buttons and show the value in read mode.
*/
cancelEditValue() {
this.editModeActive = false;
this.showActionBubble = false;
this.mode = 'read';
// hide comment once back in read mode
this.displayValueComponent.updateCommentVisibility();
// check if comment toggle button should be shown
this.checkCommentToggleVisibility();
}
/**
* show or hide the comment.
*/
toggleComment() {
this.displayValueComponent.toggleCommentVisibility();
}
/**
* check if the comment toggle button should be shown.
* Only show the comment toggle button if user is in READ mode and a comment exists for the value.
*/
checkCommentToggleVisibility() {
this.shouldShowCommentToggle = (
this.mode === 'read' &&
this.displayValue.valueHasComment !== '' &&
this.displayValue.valueHasComment !== undefined
);
}
/**
* show CRUD buttons and add 'hover' class to the element only if editModeActive is false
*/
mouseEnter() {
this.showActionBubble = true;
if (!this.editModeActive) {
this.valueHovered = true;
}
}
/**
* hide CRUD buttons and remove the 'hightlighted' class from the element
*/
mouseLeave() {
this.showActionBubble = false;
this.valueHovered = false;
}
/**
* given a uuid of a value, highlights the corresponding value
* @param uuid uuid of the value
* @returns true if the provided uuid matches the uuid of the displayValue and edit mode is not active, false otherwise
*/
shouldHighlightValue(uuid: string): boolean {
if (uuid !== undefined && uuid === this.displayValue.uuid && !this.editModeActive) {
return true;
}
return false;
}
/**
* given a resource Iri, finds the corresponding standoff link value.
* Returns an empty array if the standoff link cannot be found.
*
* @param resIri the Iri of the resource.
*/
private _getStandoffLinkValueForResource(resIri: string): ReadLinkValue[] {
// find the PropertyInfoValues for the standoff link value
const standoffLinkPropInfoVals: PropertyInfoValues[] = this.propArray.filter(
resPropInfoVal => resPropInfoVal.propDef.id === Constants.HasStandoffLinkToValue
);
if (standoffLinkPropInfoVals.length === 1) {
// find the corresponding standoff link value
const referredResStandoffLinkVal: ReadValue[] = standoffLinkPropInfoVals[0].values.filter(
(standoffLinkVal: ReadValue) => standoffLinkVal instanceof ReadLinkValue
&& (standoffLinkVal as ReadLinkValue).linkedResourceIri === resIri
);
// if no corresponding standoff link value was found,
// this array is empty
return referredResStandoffLinkVal as ReadLinkValue[];
} else {
// this should actually never happen
// because all resource types have a cardinality for a standoff link value
return [];
}
}
}