Skip to content

Commit d0e842d

Browse files
committed
WIP: Update range getCanvases to include ..
cases where t and xywh fragments are present in the range
1 parent ed74d4b commit d0e842d

File tree

3 files changed

+94
-16
lines changed

3 files changed

+94
-16
lines changed

src/Range.ts

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,62 @@ export class Range extends ManifestResource {
5656
let manifestCanvases = this.getTopRange(this)
5757
.options.resource.getSequences()[0]
5858
.getCanvases();
59+
let rangeItems;
5960

60-
if (this.canvases) {
61-
let canvasItems: Canvas[] = [];
61+
if (this.__jsonld.items || this.__jsonld.elements) {
62+
rangeItems = this.__jsonld.items || this.__jsonld.elements;
63+
} else if (this.__jsonld) {
64+
rangeItems = this.__jsonld;
65+
}
6266

63-
for (let i = 0; i < this.canvases.length; i++) {
64-
let cId = this.canvases[i];
67+
let canvasItems: Canvas[] = [];
68+
69+
for (let i = 0; i < rangeItems.length; i++) {
70+
if (rangeItems[i]["type"] === "Canvas") {
71+
let cId = rangeItems[i]["id"];
72+
const fullCanvas = manifestCanvases.find(c => c.id === cId);
73+
canvasItems.push(fullCanvas);
74+
} else if (
75+
rangeItems[i]["type"] === "SpecificResource" &&
76+
rangeItems[i]["selector"]["type"] === "FragmentSelector"
77+
) {
78+
let cId = rangeItems[i]["source"];
6579
const fullCanvas = manifestCanvases.find(c => c.id === cId);
80+
fullCanvas[
81+
"id"
82+
] = `${fullCanvas["id"]}#${rangeItems[i]["selector"]["value"]}`;
83+
let canvasJson = fullCanvas.__jsonld;
84+
fullCanvas["items"] = this._updateCanvasIds(
85+
canvasJson,
86+
fullCanvas["id"]
87+
);
6688
canvasItems.push(fullCanvas);
6789
}
68-
return (this._canvases = canvasItems);
6990
}
70-
return [];
91+
return canvasItems ? (this._canvases = canvasItems) : [];
92+
// end of xwyh and t
93+
}
94+
95+
// update __jsonld canvas id's because that is used by other functions in
96+
// the library when working with canvases
97+
_updateCanvasIds(canvasJson: any, newCanvasId: string): any {
98+
// update ids in annotations
99+
const items = canvasJson.items || canvasJson.content;
100+
const annotations = items.length && items[0].items ? items[0].items : [];
101+
if (annotations && canvasJson.items) {
102+
for (let i = 0; i < annotations.length; i++) {
103+
canvasJson["id"] = newCanvasId;
104+
// update target canvas Id in all canvas annotations
105+
canvasJson.items[0].items[i]["target"] = newCanvasId;
106+
}
107+
} else if (annotations) {
108+
for (let i = 0; i < annotations.length; i++) {
109+
canvasJson["id"] = newCanvasId;
110+
// update target canvas Id in all canvas annotations
111+
canvasJson.content[0].items[i]["target"] = newCanvasId;
112+
}
113+
}
114+
return canvasJson;
71115
}
72116

73117
getCanvasByIndex(canvasIndex: number): any {

test/fixtures/pres3.json

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,42 @@
142142
]
143143
}
144144
]
145+
},
146+
{
147+
"id": "http://example.org/iiif/book1/canvas/1",
148+
"type": "Canvas",
149+
"label": {
150+
"@none": [
151+
"p. 1"
152+
]
153+
},
154+
"height": 1000,
155+
"width": 750,
156+
"duration": 180.0,
157+
"items": [
158+
{
159+
"id": "http://example.org/iiif/book1/canvas/1/annotationpage/0",
160+
"type": "AnnotationPage",
161+
"items": [
162+
{
163+
"id": "http://example.org/iiif/book1/canvas/1/annotation/0",
164+
"type": "Annotation",
165+
"motivation": "painting",
166+
"body": {
167+
"id": "http://example.org/iiif/book1/canvas/1/images/page1.jpg",
168+
"type": "Image",
169+
"format": "image/jpeg",
170+
"label": {
171+
"@none": [
172+
"Page 1"
173+
]
174+
}
175+
},
176+
"target": "http://example.org/iiif/book1/canvas/1"
177+
}
178+
]
179+
}
180+
]
145181
}
146182
],
147183
"structures": [
@@ -151,12 +187,11 @@
151187
"behavior": "sequence",
152188
"items": [
153189
{
154-
"id": "http://example.org/iiif/book1/canvas/0",
155-
"type": "Canvas",
156-
"label": {
157-
"@none": [
158-
"p. 1"
159-
]
190+
"type": "SpecificResource",
191+
"source": "http://example.org/iiif/book1/canvas/0",
192+
"selector": {
193+
"type": "FragmentSelector",
194+
"value": "xywh=0,0,750,300"
160195
}
161196
}
162197
]

test/tests/pres3.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,10 @@ describe('presentation 3', function() {
4444

4545
it('range has a canvas', function() {
4646
canvas = range.getCanvases()[0];
47+
console.log('1. Canvas Id Updated with fragment appended', canvas)
48+
console.log('2. Annotations Target (Canvas Id) Updated with fragment appended', canvas.__jsonld.items[0])
4749
expect(canvas).to.exist;
48-
var canvasByIndex = range.getCanvasByIndex(0);
49-
expect(canvasByIndex).to.exist;
50-
var canvasById = range.getCanvasById('http://example.org/iiif/book1/canvas/0');
51-
expect(canvasById).to.exist;
50+
/* B */
5251
});
5352

5453
it('has an annotation body', function() {

0 commit comments

Comments
 (0)