forked from IIIF-Commons/manifesto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRange.ts
228 lines (187 loc) · 6.62 KB
/
Range.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
import {
Duration,
IManifestoOptions,
ManifestResource,
TreeNode,
TreeNodeType,
Utils
} from "./internal";
import {
Behavior,
ViewingDirection,
ViewingHint
} from "@iiif/vocabulary/dist-commonjs";
export class Range extends ManifestResource {
private _ranges: Range[] | null = null;
public canvases: string[] | null = null;
public items: ManifestResource[] = [];
public parentRange: Range | undefined;
public path: string;
public treeNode: TreeNode;
constructor(jsonld?: any, options?: IManifestoOptions) {
super(jsonld, options);
}
getCanvasIds(): string[] {
if (this.__jsonld.canvases) {
return this.__jsonld.canvases;
} else if (this.canvases) {
return this.canvases;
}
return [];
}
getDuration(): Duration | undefined {
// For this implementation, we want to catch SOME of the temporal cases - i.e. when there is a t=1,100
if (this.canvases && this.canvases.length) {
const startTimes: number[] = [];
const endTimes: number[] = [];
// When we loop through all of the canvases we store the recorded start and end times.
// Then we choose the maximum and minimum values from this. This will give us a more accurate duration for the
// Chosen range. However this is still not perfect and does not cover more complex ranges. These cases are out of
// scope for this change.
for (const canvas of this.canvases) {
if (!canvas) continue;
const [, canvasId, start, end] = (canvas.match(
/(.*)#t=([0-9.]+),?([0-9.]+)?/
) || [undefined, canvas]) as string[];
if (canvasId) {
startTimes.push(parseFloat(start));
endTimes.push(parseFloat(end));
}
}
if (startTimes.length && endTimes.length) {
return new Duration(Math.min(...startTimes), Math.max(...endTimes));
}
} else {
// get child ranges and calculate the start and end based on them
const childRanges = this.getRanges();
const startTimes: number[] = [];
const endTimes: number[] = [];
// Once again, we use a max/min to get the ranges.
for (const childRange of childRanges) {
const duration = childRange.getDuration();
if (duration) {
startTimes.push(duration.start);
endTimes.push(duration.end);
}
}
// And return the minimum as the start, and the maximum as the end.
if (startTimes.length && endTimes.length) {
return new Duration(Math.min(...startTimes), Math.max(...endTimes));
}
}
let start: number | undefined;
let end: number | undefined;
// There are 2 paths for this implementation. Either we have a list of canvases, or a list of ranges
// which may have a list of ranges.
// This is one of the limitations of this implementation.
if (this.canvases && this.canvases.length) {
// When we loop through each of the canvases we are expecting to see a fragment or a link to the whole canvas.
// For example - if we have http://example.org/canvas#t=1,100 it will extract 1 and 100 as the start and end.
for (let i = 0; i < this.canvases.length; i++) {
const canvas: string = this.canvases[i];
let temporal: number[] | null = Utils.getTemporalComponent(canvas);
if (temporal && temporal.length > 1) {
if (i === 0) {
// Note: Cannot guarantee ranges are sequential (fixed above)
start = Number(temporal[0]);
}
if (i === this.canvases.length - 1) {
end = Number(temporal[1]); // Note: The end of this duration may be targeting a different canvas.
}
}
}
} else {
// In this second case, where there are nested ranges, we recursively get the duration
// from each of the child ranges (a start and end) and then choose the first and last for the bounds of this range.
const childRanges: Range[] = this.getRanges();
for (let i = 0; i < childRanges.length; i++) {
const childRange: Range = childRanges[i];
const duration: Duration | undefined = childRange.getDuration();
if (duration) {
if (i === 0) {
start = duration.start;
}
if (i === childRanges.length - 1) {
end = duration.end;
}
}
}
}
if (start !== undefined && end !== undefined) {
return new Duration(start, end);
}
return undefined;
}
// getCanvases(): ICanvas[] {
// if (this._canvases) {
// return this._canvases;
// }
// return this._canvases = <ICanvas[]>this.items.en().where(m => m.isCanvas()).toArray();
// }
getRanges(): Range[] {
if (this._ranges) {
return this._ranges;
}
return (this._ranges = <Range[]>this.items.filter(m => m.isRange()));
}
getBehavior(): Behavior | null {
let behavior: any = this.getProperty("behavior");
if (Array.isArray(behavior)) {
behavior = behavior[0];
}
if (behavior) {
return behavior;
}
return null;
}
getViewingDirection(): ViewingDirection | null {
return this.getProperty("viewingDirection");
}
getViewingHint(): ViewingHint | null {
return this.getProperty("viewingHint");
}
getTree(treeRoot: TreeNode): TreeNode {
treeRoot.data = this;
this.treeNode = treeRoot;
const ranges: Range[] = this.getRanges();
if (ranges && ranges.length) {
for (let i = 0; i < ranges.length; i++) {
const range: Range = ranges[i];
const node: TreeNode = new TreeNode();
treeRoot.addNode(node);
this._parseTreeNode(node, range);
}
}
Utils.generateTreeNodeIds(treeRoot);
return treeRoot;
}
public spansTime(time: number): boolean {
const duration: Duration | undefined = this.getDuration();
if (duration) {
if (time >= duration.start && time <= duration.end) {
return true;
}
}
return false;
}
private _parseTreeNode(node: TreeNode, range: Range): void {
node.label = <string>range.getLabel().getValue(this.options.locale);
node.data = range;
node.data.type = Utils.normaliseType(TreeNodeType.RANGE);
range.treeNode = node;
const ranges: Range[] = range.getRanges();
if (ranges && ranges.length) {
for (let i = 0; i < ranges.length; i++) {
const childRange = ranges[i];
const behavior: Behavior | null = childRange.getBehavior();
if (behavior === Behavior.NO_NAV) {
continue;
} else {
const childNode: TreeNode = new TreeNode();
node.addNode(childNode);
this._parseTreeNode(childNode, childRange);
}
}
}
}
}