-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathSequence.ts
More file actions
319 lines (248 loc) · 7.81 KB
/
Copy pathSequence.ts
File metadata and controls
319 lines (248 loc) · 7.81 KB
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
import { ViewingDirection, ViewingHint } from "@iiif/vocabulary/dist-commonjs";
import {
Canvas,
IManifestoOptions,
Manifest,
ManifestResource,
Thumb,
Thumbnail,
Utils
} from "./internal";
export class Sequence extends ManifestResource {
public items: Canvas[] = [];
private _thumbnails: Thumbnail[] | null = null;
constructor(jsonld?: any, options?: IManifestoOptions) {
super(jsonld, options);
}
getCanvases(): Canvas[] {
if (this.items.length) {
return this.items;
}
let items = this.__jsonld.canvases || this.__jsonld.elements;
if (items) {
for (let i = 0; i < items.length; i++) {
const c = items[i];
const canvas: Canvas = new Canvas(c, this.options);
canvas.index = i;
this.items.push(canvas);
}
} else if (this.__jsonld) {
for (let i = 0; i < this.__jsonld.length; i++) {
const c = this.__jsonld[i];
const canvas: Canvas = new Canvas(c, this.options);
canvas.index = i;
this.items.push(canvas);
}
}
return this.items;
}
getCanvasById(id: string): Canvas | null {
for (let i = 0; i < this.getTotalCanvases(); i++) {
const canvas = this.getCanvasByIndex(i);
// normalise canvas id
const canvasId: string = Utils.normaliseUrl(canvas.id);
if (Utils.normaliseUrl(id) === canvasId) {
return canvas;
}
}
return null;
}
getCanvasByIndex(canvasIndex: number): any {
return this.getCanvases()[canvasIndex];
}
getCanvasIndexById(id: string): number | null {
for (let i = 0; i < this.getTotalCanvases(); i++) {
const canvas = this.getCanvasByIndex(i);
if (canvas.id === id) {
return i;
}
}
return null;
}
getCanvasIndexByLabel(label: string, foliated?: boolean): number {
label = label.trim();
if (!isNaN(<any>label)) {
// if the label is numeric
label = parseInt(label, 10).toString(); // trim any preceding zeros.
if (foliated) label += "r"; // default to recto
}
var doublePageRegExp = /(\d*)\D+(\d*)/;
var match, regExp, regStr, labelPart1, labelPart2;
for (let i = 0; i < this.getTotalCanvases(); i++) {
const canvas: Canvas = this.getCanvasByIndex(i);
// check if there's a literal match
if (canvas.getLabel().getValue(this.options.locale) === label) {
return i;
}
// check if there's a match for double-page spreads e.g. 100-101, 100_101, 100 101
match = doublePageRegExp.exec(label);
if (!match) continue;
labelPart1 = match[1];
labelPart2 = match[2];
if (!labelPart2) continue;
regStr = "^" + labelPart1 + "\\D+" + labelPart2 + "$";
regExp = new RegExp(regStr);
if (regExp.test(canvas.getLabel().toString())) {
return i;
}
}
return -1;
}
getLastCanvasLabel(alphanumeric?: boolean): string {
for (let i = this.getTotalCanvases() - 1; i >= 0; i--) {
const canvas: Canvas = this.getCanvasByIndex(i);
const label: string = <string>(
canvas.getLabel().getValue(this.options.locale)
);
if (alphanumeric) {
var regExp = /^[a-zA-Z0-9]*$/;
if (regExp.test(label)) {
return label;
}
} else if (label) {
return label;
}
}
return this.options.defaultLabel;
}
getLastPageIndex(): number {
return this.getTotalCanvases() - 1;
}
getNextPageIndex(canvasIndex: number, pagingEnabled?: boolean): number {
let index: number;
if (pagingEnabled) {
const indices: number[] = this.getPagedIndices(canvasIndex);
const viewingDirection: ViewingDirection | null = this.getViewingDirection();
if (
viewingDirection &&
viewingDirection === ViewingDirection.RIGHT_TO_LEFT
) {
index = indices[0] + 1;
} else {
index = indices[indices.length - 1] + 1;
}
} else {
index = canvasIndex + 1;
}
if (index > this.getLastPageIndex()) {
return -1;
}
return index;
}
getPagedIndices(canvasIndex: number, pagingEnabled?: boolean): number[] {
let indices: number[] = [];
if (!pagingEnabled) {
indices.push(canvasIndex);
} else {
if (this.isFirstCanvas(canvasIndex) || this.isLastCanvas(canvasIndex)) {
indices = [canvasIndex];
} else if (canvasIndex % 2) {
indices = [canvasIndex, canvasIndex + 1];
} else {
indices = [canvasIndex - 1, canvasIndex];
}
const viewingDirection: ViewingDirection | null = this.getViewingDirection();
if (
viewingDirection &&
viewingDirection === ViewingDirection.RIGHT_TO_LEFT
) {
indices = indices.reverse();
}
}
return indices;
}
getPrevPageIndex(canvasIndex: number, pagingEnabled?: boolean): number {
let index: number;
if (pagingEnabled) {
const indices = this.getPagedIndices(canvasIndex);
const viewingDirection: ViewingDirection | null = this.getViewingDirection();
if (
viewingDirection &&
viewingDirection === ViewingDirection.RIGHT_TO_LEFT
) {
index = indices[indices.length - 1] - 1;
} else {
index = indices[0] - 1;
}
} else {
index = canvasIndex - 1;
}
return index;
}
getStartCanvasIndex(): number {
const startCanvas: string = this.getStartCanvas();
if (startCanvas) {
// if there's a startCanvas attribute, loop through the canvases and return the matching index.
for (let i = 0; i < this.getTotalCanvases(); i++) {
const canvas = this.getCanvasByIndex(i);
if (canvas.id === startCanvas) return i;
}
}
// default to first canvas.
return 0;
}
// todo: deprecate
getThumbs(width: number, height?: number): Thumb[] {
//console.warn('getThumbs will be deprecated, use getThumbnails instead');
const thumbs: Thumb[] = [];
const totalCanvases: number = this.getTotalCanvases();
for (let i = 0; i < totalCanvases; i++) {
const canvas: Canvas = this.getCanvasByIndex(i);
const thumb: Thumb = new Thumb(width, canvas);
thumbs.push(thumb);
}
return thumbs;
}
getThumbnails(): Thumbnail[] {
if (this._thumbnails != null) return this._thumbnails;
this._thumbnails = [];
const canvases: Canvas[] = this.getCanvases();
for (let i = 0; i < canvases.length; i++) {
const thumbnail: Thumbnail | null = canvases[i].getThumbnail();
if (thumbnail) {
this._thumbnails.push(thumbnail);
}
}
return this._thumbnails;
}
getStartCanvas(): string {
return this.getProperty("startCanvas");
}
getTotalCanvases(): number {
return this.getCanvases().length;
}
getViewingDirection(): ViewingDirection | null {
if (this.getProperty("viewingDirection")) {
return this.getProperty("viewingDirection");
} else if ((<Manifest>this.options.resource).getViewingDirection) {
return (<Manifest>this.options.resource).getViewingDirection();
}
return null;
}
getViewingHint(): ViewingHint | null {
return this.getProperty("viewingHint");
}
isCanvasIndexOutOfRange(canvasIndex: number): boolean {
return canvasIndex > this.getTotalCanvases() - 1;
}
isFirstCanvas(canvasIndex: number): boolean {
return canvasIndex === 0;
}
isLastCanvas(canvasIndex: number): boolean {
return canvasIndex === this.getTotalCanvases() - 1;
}
isMultiCanvas(): boolean {
return this.getTotalCanvases() > 1;
}
isPagingEnabled(): boolean {
const viewingHint: ViewingHint | null = this.getViewingHint();
if (viewingHint) {
return viewingHint === ViewingHint.PAGED;
}
return false;
}
// checks if the number of canvases is even - therefore has a front and back cover
isTotalCanvasesEven(): boolean {
return this.getTotalCanvases() % 2 === 0;
}
}