This repository has been archived by the owner on Feb 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
traverse.ts
377 lines (334 loc) · 11.5 KB
/
traverse.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
import {
Collection,
Manifest,
Required,
Canvas,
AnnotationPage,
Annotation,
ContentResource,
Range,
RangeItems,
Service,
AnnotationCollection,
LinkingProperties,
DescriptiveProperties,
IIIFExternalWebResource,
TraversalMap,
Traversal,
TraversableEntityTypes,
} from '@hyperion-framework/types';
export const types: TraversableEntityTypes[] = [
'Collection',
'Manifest',
'Canvas',
'AnnotationPage',
'AnnotationCollection',
'Annotation',
'ContentResource',
'Range',
'Service',
'Selector',
];
export type TraverseOptions = {
allowUndefinedReturn: boolean;
};
export function identifyResource(resource: any): TraversableEntityTypes {
if (typeof resource === 'undefined' || resource === null) {
throw new Error('Null or undefined is not a valid entity.');
}
if (Array.isArray(resource)) {
throw new Error('Array is not a valid entity');
}
if (typeof resource !== 'object') {
throw new Error(`${typeof resource} is not a valid entity`);
}
if (typeof resource!.type === 'string') {
const hasType = types.indexOf(resource.type);
if (hasType !== -1) {
return types[hasType];
}
}
if (resource!.profile) {
return 'Service';
}
throw new Error('Resource type is not known');
}
export class Traverse {
private traversals: Required<TraversalMap>;
private options: TraverseOptions;
constructor(traversals: TraversalMap, options: Partial<TraverseOptions> = {}) {
this.traversals = {
collection: [],
manifest: [],
canvas: [],
annotationCollection: [],
annotationPage: [],
annotation: [],
contentResource: [],
choice: [],
range: [],
service: [],
...traversals,
};
this.options = {
allowUndefinedReturn: false,
...options,
};
}
static all(traversal: (resource: any) => any) {
return new Traverse({
collection: [traversal],
manifest: [traversal],
canvas: [traversal],
annotationCollection: [traversal],
annotationPage: [traversal],
annotation: [traversal],
contentResource: [traversal],
choice: [traversal],
range: [traversal],
service: [traversal],
});
}
traverseDescriptive<T extends Partial<DescriptiveProperties>>(resource: T) {
if (resource.thumbnail) {
resource.thumbnail = resource.thumbnail.map(thumbnail =>
this.traverseType(thumbnail, this.traversals.contentResource)
);
}
return resource;
}
traverseLinking<T extends Partial<LinkingProperties>>(resource: T) {
if (resource.seeAlso) {
resource.seeAlso = resource.seeAlso.map(content => this.traverseType(content, this.traversals.contentResource));
}
if (resource.service) {
resource.service = resource.service.map(service => this.traverseType(service, this.traversals.service));
}
if (resource.services) {
resource.services = resource.services.map(service => this.traverseType(service, this.traversals.service));
}
if (resource.logo) {
resource.logo = resource.logo.map(content => this.traverseType(content, this.traversals.contentResource));
}
if (resource.homepage) {
resource.homepage = this.traverseType(resource.homepage, this.traversals.contentResource);
}
if (resource.partOf) {
// Array<ContentResource | Canvas | AnnotationCollection>
resource.partOf = resource.partOf.map(partOf => {
if (typeof partOf === 'string' || !partOf.type) {
return this.traverseType(partOf as ContentResource, this.traversals.contentResource);
}
if (partOf.type === 'Canvas') {
return this.traverseType(partOf as Canvas, this.traversals.canvas);
}
if (partOf.type === 'AnnotationCollection') {
return this.traverseType(partOf as AnnotationCollection, this.traversals.annotationCollection);
}
return this.traverseType(partOf as ContentResource, this.traversals.contentResource);
});
}
if (resource.start) {
resource.start = resource.start.map(start => this.traverseType(start, this.traversals.canvas));
}
if (resource.rendering) {
resource.rendering = resource.rendering.map(content =>
this.traverseType(content, this.traversals.contentResource)
);
}
if (resource.supplementary) {
resource.supplementary = resource.supplementary.map(content =>
this.traverseType(content, this.traversals.contentResource)
);
}
return resource;
}
traverseCollectionItems(collection: Collection): Collection {
collection.items.map((collectionOrManifest: Manifest | Collection) => {
if (collectionOrManifest.type === 'Collection') {
return this.traverseCollection(collectionOrManifest as Collection);
}
return this.traverseManifest(collectionOrManifest as Manifest);
});
return collection;
}
traverseCollection(collection: Collection): Collection {
return this.traverseType<Collection>(
this.traverseDescriptive(
this.traverseLinking(this.traversePosterCanvas(this.traverseCollectionItems(collection)))
),
this.traversals.collection
);
}
traverseManifestItems(manifest: Manifest): Manifest {
if (manifest.items) {
manifest.items = manifest.items.map(canvas => this.traverseCanvas(canvas));
}
return manifest;
}
traverseManifestStructures(manifest: Manifest): Manifest {
if (manifest.structures) {
manifest.structures = manifest.structures.map(range => this.traverseRange(range));
}
return manifest;
}
traverseManifest(manifest: Manifest): Manifest {
return this.traverseType<Manifest>(
this.traverseManifestStructures(
this.traversePosterCanvas(this.traverseDescriptive(this.traverseLinking(this.traverseManifestItems(manifest))))
),
this.traversals.manifest
);
}
traverseCanvasItems(canvas: Canvas): Canvas {
canvas.items = (canvas.items || []).map(
(annotationPage: AnnotationPage): AnnotationPage => {
return this.traverseAnnotationPage(annotationPage);
}
);
return canvas;
}
traverseCanvas(canvas: Canvas): Canvas {
return this.traverseType<Canvas>(
this.traversePosterCanvas(this.traverseDescriptive(this.traverseLinking(this.traverseCanvasItems(canvas)))),
this.traversals.canvas
);
}
traverseAnnotationPageItems(annotationPage: AnnotationPage): AnnotationPage {
if (annotationPage.items) {
annotationPage.items = annotationPage.items.map(
(annotation: Annotation): Annotation => {
return this.traverseAnnotation(annotation);
}
);
}
return annotationPage;
}
traverseAnnotationPage(annotationPageJson: AnnotationPage): AnnotationPage {
return this.traverseType<AnnotationPage>(
this.traverseDescriptive(this.traverseLinking(this.traverseAnnotationPageItems(annotationPageJson) as any)),
this.traversals.annotationPage
);
}
// Disabling these.
traverseAnnotationBody(annotation: Annotation): Annotation {
if (Array.isArray(annotation.body)) {
annotation.body = annotation.body.map(
(annotationBody: any): ContentResource => {
return this.traverseContentResource(annotationBody);
}
);
} else if (annotation.body) {
annotation.body = this.traverseContentResource(annotation.body as ContentResource);
}
return annotation;
}
/*
traverseAnnotationTarget(annotation: Annotation): Annotation {
if (Array.isArray(annotation.target)) {
annotation.target = annotation.target.map(
(annotationBody: ContentResource): ContentResource => {
return this.traverseContentResource(annotationBody);
}
);
} else if (annotation.target) {
annotation.target = this.traverseContentResource(annotation.target);
}
return annotation;
}
*/
traversePosterCanvas<T extends Collection | Manifest | Canvas | Range>(json: T): T {
// @deprecated
if (json.posterCanvas) {
json.posterCanvas = this.traverseType(json.posterCanvas, this.traversals.canvas);
}
if (json.placeholderCanvas) {
json.placeholderCanvas = this.traverseType(json.placeholderCanvas, this.traversals.canvas);
}
if (json.accompanyingCanvas) {
json.accompanyingCanvas = this.traverseType(json.accompanyingCanvas, this.traversals.canvas);
}
return json;
}
// @todo traverseAnnotationSelector
traverseAnnotation(annotationJson: Annotation): Annotation {
return this.traverseType<Annotation>(
// Disabled these for now.
// this.traverseAnnotationTarget(this.traverseLinking(this.traverseAnnotationBody(annotationJson))),
this.traverseLinking(this.traverseAnnotationBody(annotationJson)),
this.traversals.annotation
);
}
traverseContentResourceLinking(contentResourceJson: ContentResource): ContentResource {
if (typeof contentResourceJson === 'string' || !contentResourceJson) {
return contentResourceJson;
}
if (contentResourceJson && (contentResourceJson as IIIFExternalWebResource)!.service) {
(contentResourceJson as IIIFExternalWebResource).service = (
(contentResourceJson as IIIFExternalWebResource).service || []
).map(service => this.traverseType(service, this.traversals.service));
}
return contentResourceJson;
}
traverseContentResource(contentResourceJson: ContentResource): ContentResource {
return this.traverseType<ContentResource>(
this.traverseContentResourceLinking(contentResourceJson),
this.traversals.contentResource
);
}
traverseRangeRanges(range: Range): Range {
if (range.items) {
range.items = range.items.map((rangeOrManifest: RangeItems) => {
if (typeof rangeOrManifest === 'string') {
return this.traverseCanvas({ id: rangeOrManifest, type: 'Canvas' });
}
if (rangeOrManifest.type === 'Manifest') {
return this.traverseManifest(rangeOrManifest as Manifest);
}
return this.traverseRange(rangeOrManifest as Range);
});
}
return range;
}
traverseRange(range: Range): Range {
return this.traverseType<Range>(
this.traversePosterCanvas(this.traverseDescriptive(this.traverseLinking(this.traverseRangeRanges(range)))),
this.traversals.range
);
}
traverseType<T>(object: T, traversals: Array<Traversal<T>>): T {
return traversals.reduce((acc: T, traversal: Traversal<T>): T => {
const returnValue = traversal(acc);
if (typeof returnValue === 'undefined' && !this.options.allowUndefinedReturn) {
return acc;
}
return returnValue;
}, object);
}
traverseService(service: Service): Service {
return this.traverseType<Service>(service, this.traversals.service);
}
traverseUnknown(resource: any) {
const type = identifyResource(resource);
switch (type) {
case 'Collection':
return this.traverseCollection(resource as Collection);
case 'Manifest':
return this.traverseManifest(resource as Manifest);
case 'Canvas':
return this.traverseCanvas(resource as Canvas);
case 'AnnotationPage':
return this.traverseAnnotationPage(resource as AnnotationPage);
case 'Annotation':
return this.traverseAnnotation(resource as Annotation);
case 'ContentResource':
return this.traverseContentResource(resource as ContentResource);
case 'Range':
return this.traverseRange(resource as Range);
case 'Service':
return this.traverseService(resource as Service);
default:
throw new Error(`Unknown or unsupported resource type of ${type}`);
}
}
}