/
resource.component.ts
618 lines (485 loc) · 24.1 KB
/
resource.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/* eslint-disable @typescript-eslint/member-ordering */
import { Component, Inject, Input, OnChanges, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { MatTabChangeEvent } from '@angular/material/tabs';
import { Title } from '@angular/platform-browser';
import {
ActivatedRoute,
NavigationEnd,
NavigationError,
NavigationStart,
Router
} from '@angular/router';
import {
ApiResponseError,
Constants,
CountQueryResponse, IHasPropertyWithPropertyDefinition,
KnoraApiConnection,
ReadArchiveFileValue,
ReadAudioFileValue,
ReadDocumentFileValue, ReadResource,
ReadResourceSequence,
ReadStillImageFileValue, SystemPropertyDefinition
} from '@dasch-swiss/dsp-js';
import { Subscription } from 'rxjs';
import { DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens';
import { ErrorHandlerService } from 'src/app/main/error/error-handler.service';
import { NotificationService } from 'src/app/main/services/notification.service';
import { Session, SessionService } from 'src/app/main/services/session.service';
import { DspCompoundPosition, DspResource } from './dsp-resource';
import { PropertyInfoValues } from './properties/properties.component';
import { FileRepresentation, RepresentationConstants } from './representation/file-representation';
import { Region, StillImageComponent } from './representation/still-image/still-image.component';
import { IncomingService } from './services/incoming.service';
import { ResourceService } from './services/resource.service';
import { Events, UpdatedFileEventValue, ValueOperationEventService } from './services/value-operation-event.service';
@Component({
selector: 'app-resource',
templateUrl: './resource.component.html',
styleUrls: ['./resource.component.scss'],
providers: [ValueOperationEventService] // provide service on the component level so that each implementation of this component has its own instance.
})
export class ResourceComponent implements OnInit, OnChanges, OnDestroy {
@ViewChild('stillImage') stillImageComponent: StillImageComponent;
@Input() resourceIri: string;
projectCode: string;
resourceUuid: string;
// used to store the uuid of the value that is parsed from the url params
valueUuid: string;
// this will be the main resource
resource: DspResource;
// in case of incoming representations,
// this will be the currently selected (part-of main) resource
incomingResource: DspResource;
// for the annotations e.g. regions in a still image representation
annotationResources: DspResource[];
selectedRegion: string;
selectedTab = 0;
selectedTabLabel: string;
iiifUrl: string;
// list of representations to be displayed
// --> TODO: will be expanded with | MovingImageRepresentation[] | AudioRepresentation[] etc.
representationsToDisplay: FileRepresentation[] = [];
representationConstants = RepresentationConstants;
// in case of compound object,
// this will store the current page position information
compoundPosition: DspCompoundPosition;
showAllProps = false;
loading = true;
refresh: boolean;
// permissions of logged-in user
session: Session;
adminPermissions = false;
editPermissions = false;
navigationSubscription: Subscription;
valueOperationEventSubscriptions: Subscription[] = [];
constructor(
@Inject(DspApiConnectionToken) private _dspApiConnection: KnoraApiConnection,
private _errorHandler: ErrorHandlerService,
private _incomingService: IncomingService,
private _notification: NotificationService,
private _resourceService: ResourceService,
private _route: ActivatedRoute,
private _router: Router,
private _session: SessionService,
private _titleService: Title,
private _valueOperationEventService: ValueOperationEventService
) {
this._route.params.subscribe(params => {
this.projectCode = params['project'];
this.resourceUuid = params['resource'];
this.valueUuid = params['value'];
if (this.projectCode && this.resourceUuid) {
this.resourceIri = this._resourceService.getResourceIri(this.projectCode, this.resourceUuid);
}
this.getResource(this.resourceIri);
});
this._router.events.subscribe((event) => {
this._titleService.setTitle('Resource view');
if (event instanceof NavigationStart) {
// show loading indicator
// console.log('NavigationStart', this.resourceIri);
}
if (event instanceof NavigationEnd) {
// hide loading indicator
this.refresh = true;
// console.log('NavigationEnd', this.resourceIri);
this.refresh = false;
}
if (event instanceof NavigationError) {
// hide loading indicator
// present error to user
this._errorHandler.showMessage(event.error);
}
});
this.valueOperationEventSubscriptions.push(this._valueOperationEventService.on(
Events.FileValueUpdated, (newFileValue: UpdatedFileEventValue) => {
if (newFileValue) {
this.getResource(this.resourceIri);
}
}));
}
ngOnInit() {
}
ngOnChanges() {
this.loading = true;
// reset all resources
this.resource = undefined;
this.incomingResource = undefined;
this.representationsToDisplay = [];
this.compoundPosition = undefined;
// get resource with all necessary information
// incl. incoming resources and annotations
if (this.resourceIri) {
this.getResource(this.resourceIri);
}
}
ngOnDestroy() {
if (this.navigationSubscription !== undefined) {
this.navigationSubscription.unsubscribe();
}
// unsubscribe from the ValueOperationEventService when component is destroyed
if (this.valueOperationEventSubscriptions !== undefined) {
this.valueOperationEventSubscriptions.forEach(sub => sub.unsubscribe());
}
}
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// general methods
// ------------------------------------------------------------------------
compoundNavigation(page: number) {
this.selectedRegion = undefined;
this.representationsToDisplay = [];
// set current compound object position:
// calculate offset and offset item position from current page and total pages info
const offset = Math.ceil(page / 25) - 1;
const position = Math.floor(page - (offset * 25) - 1);
// get incoming still image representations, if the offset changed
if (offset !== this.compoundPosition.offset) {
this.compoundPosition.offset = offset;
this.getIncomingStillImageRepresentations(offset);
} else {
// get incoming resource, if the offset is the same but page changed
this.getIncomingResource(this.resource.incomingRepresentations[position].id);
}
this.compoundPosition.position = position;
this.compoundPosition.page = page;
this.collectRepresentationsAndAnnotations(this.incomingResource);
}
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// get and display resource
// ------------------------------------------------------------------------
/**
* get a read resource sequence with ontology information and incoming resources.
*
* @param iri resourceIri
*/
getResource(iri: string, incoming: boolean = false): void {
if (!iri) {
return;
}
this._dspApiConnection.v2.res.getResource(iri).subscribe(
(response: ReadResource) => {
const res = new DspResource(response);
this.resource = res;
if (response.isDeleted) {
// deleted resource; no further infos needed
} else {
// if there is no incomingResource and the resource has a still image property, assign the iiiUrl to be passed as an input to the still-image component
if (!this.incomingResource && this.resource.res.properties[Constants.HasStillImageFileValue]) {
this.iiifUrl = (this.resource.res.properties[Constants.HasStillImageFileValue][0] as ReadStillImageFileValue).fileUrl;
}
this.selectedTabLabel = this.resource.res.entityInfo?.classes[this.resource.res.type].label;
// get information about the logged-in user, if one is logged-in
if (this._session.getSession()) {
this.session = this._session.getSession();
// is the logged-in user project member?
// --> TODO: as soon as we know how to handle the permissions, set this value the correct way
this.editPermissions = true;
// is the logged-in user system admin or project admin?
this.adminPermissions = this.session.user.sysAdmin ? this.session.user.sysAdmin : this.session.user.projectAdmin.some(e => e === res.res.attachedToProject);
}
this.collectRepresentationsAndAnnotations(this.resource);
if (!this.representationsToDisplay.length && !this.compoundPosition) {
// the resource could be a compound object
this._incomingService.getStillImageRepresentationsForCompoundResource(this.resource.res.id, 0, true).subscribe(
(countQuery: CountQueryResponse) => {
if (countQuery.numberOfResults > 0) {
this.compoundPosition = new DspCompoundPosition(countQuery.numberOfResults);
this.compoundNavigation(1);
}
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
}
);
} else {
this.requestIncomingResources(this.resource);
}
// gather resource property information
res.resProps = this.initProps(response);
// gather system property information
res.systemProps = this.resource.res.entityInfo.getPropertyDefinitionsByType(SystemPropertyDefinition);
}
this.loading = false;
},
(error: ApiResponseError) => {
this.loading = false;
if (error.status === 404) {
// resource not found
// display message that it couldn't be found
this.resource = undefined;
} else {
this._errorHandler.showMessage(error);
}
}
);
}
getIncomingResource(iri: string) {
this._dspApiConnection.v2.res.getResource(iri).subscribe(
(response: ReadResource) => {
const res = new DspResource(response);
this.incomingResource = res;
// if the resource is a still image, assign the iiiUrl to be passed as an input to the still-image component
if (this.incomingResource.res.properties[Constants.HasStillImageFileValue]) {
this.iiifUrl = (this.incomingResource.res.properties[Constants.HasStillImageFileValue][0] as ReadStillImageFileValue).fileUrl;
}
res.resProps = this.initProps(response);
res.systemProps = this.incomingResource.res.entityInfo.getPropertyDefinitionsByType(SystemPropertyDefinition);
this.collectRepresentationsAndAnnotations(this.incomingResource);
if (this.representationsToDisplay.length && this.representationsToDisplay[0].fileValue && this.compoundPosition) {
this.getIncomingRegions(this.incomingResource, 0);
}
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
}
);
}
tabChanged(e: MatTabChangeEvent) {
if (e.tab.textLabel === 'annotations') {
this.stillImageComponent.renderRegions();
} else {
this.stillImageComponent.removeOverlays();
}
this.selectedTabLabel = e.tab.textLabel;
}
/**
* gather resource property information
*/
protected initProps(resource: ReadResource): PropertyInfoValues[] {
let props = resource.entityInfo.classes[resource.type].getResourcePropertiesList().map(
(prop: IHasPropertyWithPropertyDefinition) => {
let propInfoAndValues: PropertyInfoValues;
switch (prop.propertyDefinition.objectType) {
case Constants.StillImageFileValue:
propInfoAndValues = {
propDef: prop.propertyDefinition,
guiDef: prop,
values: resource.getValuesAs(prop.propertyIndex, ReadStillImageFileValue)
};
const stillImageRepresentations = [new FileRepresentation(
resource.getValuesAs(Constants.HasStillImageFileValue, ReadStillImageFileValue)[0], [])
];
this.representationsToDisplay = stillImageRepresentations;
// --> TODO: get regions here
break;
default:
// the object type is none from above
propInfoAndValues = {
propDef: prop.propertyDefinition,
guiDef: prop,
values: resource.getValues(prop.propertyIndex)
};
}
return propInfoAndValues;
}
);
// sort properties by guiOrder
props = props
.filter(prop => prop.propDef.objectType !== Constants.GeomValue)
.sort((a, b) => (a.guiDef.guiOrder > b.guiDef.guiOrder) ? 1 : -1);
return props;
}
/**
* creates a collection of [[StillImageRepresentation]] belonging to the given resource and assigns it to it.
* each [[StillImageRepresentation]] represents an image including regions.
*
* @param resource The resource to get the images for.
* @returns A collection of images for the given resource.
*/
protected collectRepresentationsAndAnnotations(resource: DspResource): any {
if (!resource) {
return;
}
// --> TODO: should be a general object for all kind of representations
const representations: FileRepresentation[] = [];
// --> TODO: use a switch here to go through the different representation types
if (resource.res.properties[Constants.HasStillImageFileValue]) {
// --> TODO: check if resources is a StillImageRepresentation using the ontology responder (support for subclass relations required)
// resource has StillImageFileValues that are directly attached to it (properties)
const fileValues: ReadStillImageFileValue[] = resource.res.properties[Constants.HasStillImageFileValue] as ReadStillImageFileValue[];
for (const img of fileValues) {
const regions: Region[] = [];
const annotations: DspResource[] = [];
for (const incomingRegion of resource.incomingAnnotations) {
const region = new Region(incomingRegion);
regions.push(region);
const annotation = new DspResource(incomingRegion);
// gather region property information
annotation.resProps = this.initProps(incomingRegion);
// gather system property information
annotation.systemProps = incomingRegion.entityInfo.getPropertyDefinitionsByType(SystemPropertyDefinition);
annotations.push(annotation);
}
const stillImage = new FileRepresentation(img, regions);
representations.push(stillImage);
this.annotationResources = annotations;
// developer feature: this keeps the annotations tab open, if you add "/annotations" to the end of the URL
// e.g. http://0.0.0.0:4200/resource/[project-shortcode]/[resource-iri]/annotations
if (this.valueUuid === 'annotations') {
this.selectedTab = (this.incomingResource ? 2 : 1);
this.selectedTabLabel = 'annotations';
}
}
} else if (resource.res.properties[Constants.HasDocumentFileValue]) {
const fileValues: ReadDocumentFileValue[] = resource.res.properties[Constants.HasDocumentFileValue] as ReadDocumentFileValue[];
for (const doc of fileValues) {
const document = new FileRepresentation(doc);
representations.push(document);
}
} else if (resource.res.properties[Constants.HasAudioFileValue]) {
const fileValue: ReadAudioFileValue = resource.res.properties[Constants.HasAudioFileValue][0] as ReadAudioFileValue;
const audio = new FileRepresentation(fileValue);
representations.push(audio);
} else if (resource.res.properties[Constants.HasArchiveFileValue]) {
const fileValue: ReadArchiveFileValue = resource.res.properties[Constants.HasArchiveFileValue][0] as ReadArchiveFileValue;
const archive = new FileRepresentation(fileValue);
representations.push(archive);
}
this.representationsToDisplay = representations;
}
/**
* get StillImageRepresentations pointing to [[this.resource]].
* This method may have to called several times with an increasing offsetChange in order to get all available StillImageRepresentations.
*
* @param offset the offset to be used (needed for paging). First request uses an offset of 0.
* It takes the number of images returned as an argument.
*/
protected getIncomingStillImageRepresentations(offset: number): void {
// make sure that this.resource has been initialized correctly
if (this.resource === undefined) {
return;
}
if (offset < 0 || offset > this.compoundPosition.maxOffsets) {
this._notification.openSnackBar(`Offset of ${offset} is invalid`);
return;
}
// get all representations for compound resource of this offset sequence
this._incomingService.getStillImageRepresentationsForCompoundResource(this.resource.res.id, offset).subscribe(
(incomingImageRepresentations: ReadResourceSequence) => {
if (incomingImageRepresentations.resources.length > 0) {
// set the incoming representations for the current offset only
this.resource.incomingRepresentations = incomingImageRepresentations.resources;
this.getIncomingResource(this.resource.incomingRepresentations[this.compoundPosition.position].id);
}
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
}
);
}
/**
* requests incoming resources for [[this.resource]].
* Incoming resources are: regions, StillImageRepresentations, and incoming links.
*/
protected requestIncomingResources(resource: DspResource): void {
// make sure that this resource has been initialized correctly
if (resource === undefined) {
return;
}
// request incoming regions --> TODO: add case to get incoming sequences in case of video and audio
if (resource.res.properties[Constants.HasStillImageFileValue] ||
resource.res.properties[Constants.HasDocumentFileValue] ||
resource.res.properties[Constants.HasAudioFileValue] ||
resource.res.properties[Constants.HasArchiveFileValue]) {
// --> TODO: check if resources is a StillImageRepresentation using the ontology responder (support for subclass relations required)
// the resource is a StillImageRepresentation, check if there are regions pointing to it
this.getIncomingRegions(resource, 0);
} else {
// this resource is not a StillImageRepresentation
// check if there are StillImageRepresentations pointing to this resource
// this gets the first page of incoming StillImageRepresentations
// more pages may be requested by [[this.viewer]].
this.getIncomingStillImageRepresentations(this.compoundPosition.offset);
}
// check for incoming links for the current resource
this.getIncomingLinks(0);
}
/**
* gets the incoming regions for [[this.resource]].
*
* @param offset the offset to be used (needed for paging). First request uses an offset of 0.
*/
protected getIncomingRegions(resource: DspResource, offset: number): void {
this._incomingService.getIncomingRegions(resource.res.id, offset).subscribe(
(regions: ReadResourceSequence) => {
// append elements of regions.resources to resource.incoming
Array.prototype.push.apply(resource.incomingAnnotations, regions.resources);
// this.annotationResources.push(regions.resources)
// prepare regions to be displayed
// triggers ngOnChanges of StillImageComponent
this.collectRepresentationsAndAnnotations(resource);
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
}
);
}
/**
* get resources pointing to [[this.resource]] with properties other than knora-api:isPartOf and knora-api:isRegionOf.
*
* @param offset the offset to be used (needed for paging). First request uses an offset of 0.
* It takes the number of images returned as an argument.
*/
protected getIncomingLinks(offset: number): void {
this._incomingService.getIncomingLinksForResource(this.resource.res.id, offset).subscribe(
(incomingResources: ReadResourceSequence) => {
// append elements incomingResources to this.resource.incomingLinks
Array.prototype.push.apply(this.resource.res.incomingReferences, incomingResources.resources);
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
}
);
}
openRegion(iri: string) {
// open annotation tab
this.selectedTab = (this.incomingResource ? 2 : 1);
// activate the selected region
this.selectedRegion = iri;
// and scroll to region with this id
const region = document.getElementById(iri);
if (region) {
region.scrollIntoView({
behavior: 'smooth',
block: 'center'
});
}
}
updateRegions(iri: string) {
console.log('res component: updateRegions', iri);
if (this.incomingResource) {
this.incomingResource.incomingAnnotations = [];
} else {
this.resource.incomingAnnotations = [];
}
this.getIncomingRegions(this.incomingResource ? this.incomingResource : this.resource, 0);
this.openRegion(iri);
}
updateRegion() {
if (this.stillImageComponent !== undefined) {
this.stillImageComponent.updateRegions();
}
}
}