-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentstate.js
336 lines (290 loc) · 10.6 KB
/
contentstate.js
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
const vault = new IIIFVault.Vault();
function getParam() {
var qs = /iiif-content=(.*)/g.exec(window.location.search);
if (qs == null) {
return null;
} else {
return qs[1];
}
}
function drag(ev) {
var json = getAnnotation(osd);
ev.dataTransfer.setData("annotation", json);
}
let loadedManifest = null;
let loadedCanvas = null;
let loadedImageUri = null;
let loadedAnnotation = null;
function share() {
showShare(loadedAnnotation);
}
function showShare(annotation) {
loadedAnnotation = annotation;
let annoRegion = "";
if (annotation.target.selector.value.indexOf(":") != -1) {
annoRegion = annotation.target.selector.value.split(":")[1].split(",");
} else {
annoRegion = annotation.target.selector.value.split("=")[1].split(",");
}
let imageRegion = parseInt(annoRegion[0]) + "," + parseInt(annoRegion[1]) + "," + parseInt(annoRegion[2]) + "," + parseInt(annoRegion[3]);
let size = "1024,"
let previewURL = loadedImageUri + "/" + imageRegion + "/" + size + "/0/default.jpg";
// Setup image download
let preview = document.getElementById('region_image');
preview.src = previewURL;
let download_image = document.getElementById('download_image');
download_image.href = previewURL;
let url = document.getElementById('url_text');
url.value = previewURL;
let contentState = window.location.href.split('?')[0] + "?iiif-content=" + getContentState('xywh=' + imageRegion, annotation.body);
let copyLink = document.getElementById('share_link');
copyLink.href = contentState;
let twitter = document.getElementById('twitter_link');
linkBack = " see this image in context with IIIF Share it!";
twitter.href = "https://twitter.com/intent/tweet?url=" + encodeURI(contentState) + "&hashtags=IIIF,IIIFShareIt&related=IIIF&text=" + encodeURI(annotation.body[0].value + linkBack);
toggleDialog("share_dialog");
}
function closeDialog(event) {
toggleDialog();
}
function dialogClick(event) {
console.log('Dialog event');
}
function addClose(dialog) {
dialog.addEventListener('click', function (event) {
var rect = dialog.getBoundingClientRect();
var isInDialog=(rect.top <= event.clientY && event.clientY <= rect.top + rect.height
&& rect.left <= event.clientX && event.clientX <= rect.left + rect.width);
if (!isInDialog) {
dialog.close();
}
});
}
function toggleDialog(dialogId) {
let dialog = document.getElementById(dialogId);
if (dialog.open) {
dialog.close();
} else {
dialog.showModal();
dialog.style.display = 'flex';
addClose(dialog);
}
}
let anno = null;
// Should really pass the canvas identifier here
function initalise(osd) {
anno = OpenSeadragon.Annotorious(osd, {
widgets: [
'COMMENT',
],
messages: {
"Ok": "Share",
}
});
anno.on('createAnnotation', function(annotation, overrideId) {
console.log('Annotation created');
setAnnoViewMode(anno, annotation);
showShare(annotation);
});
iiifContent = getParam();
if (iiifContent == null) {
let dialog = document.getElementById("OpenManifest");
let openButton = document.getElementById("openButton");
openButton.addEventListener('click', function() {
let manifestURI = document.getElementById('url_text').value;
dialog.close(manifestURI);
dialog.style.display = 'none';
window.location = window.location.href + "?iiif-content=" + manifestURI;
});
dialog.showModal();
dialog.style.display = 'flex';
}
targetCanvas = getCanvas(iiifContent);
region = getRegion(iiifContent);
getManifest(iiifContent)
.then(manifest => {
let canvas = null;
if (targetCanvas == null) {
canvas = vault.get({id: manifest.items[0].id, type: 'Canvas'});
} else {
canvas = vault.get({id: targetCanvas, type: 'Canvas'});
}
loadedCanvas = canvas.id;
let infoJson = getInfoJsonURL(canvas);
loadedImageUri = infoJson;
if (region != null) {
let annotation = {
id: 'annotation_id',
type: 'Annotation',
target: {
source: infoJson,
selector: {
conformsTo: "http://www.w3.org/TR/media-frags/",
type: "FragmentSelector",
value: "xywh=" + region.x + "," + region.y + "," + region.width + "," + region.height
}
}
};
contentState = decode(iiifContent);
if ('body' in contentState) {
annotation.body = contentState.body;
}
anno.addAnnotation(annotation);
loadedAnnotation = anno.getAnnotationById(annotation.id);
setAnnoViewMode(anno, annotation);
anno.selectAnnotation(annotation);
osd.addHandler('open', function () {
var bounds = osd.viewport.imageToViewportRectangle(region.x, region.y, region.width, region.height);
osd.viewport.fitBounds(bounds, true);
});
}
osd.addTiledImage({tileSource: infoJson});
})
.catch((error) => {
console.error('Failed to load manifest:', error);
});
}
function setAnnoViewMode(anno, annotation) {
anno.readOnly = true;
let controls = document.getElementById('buttons');
controls.style.display = "flex";
let newBtn = document.getElementById("new");
regionSplit = annotation.target.selector.value.replace('xywh=pixel:','').split(',')
newBtn.href = "index.html?iiif-content=" + getContentState(null, null);
}
function getInfoJsonURL(canvas) {
let AnnoPageUri = canvas.items[0];
let annotation = vault.get(AnnoPageUri).items[0];
let body = vault.get(annotation).body[0];
let imageResource = vault.get(body);
return imageResource.service[0].id;
}
function getManifest(iiifContent) {
if (isEncoded(iiifContent)) {
// Its an encoded annotation
contentState = decode(iiifContent);
if (contentState.target.type === 'Manifest') {
loadedManifest = contentState.target.id;
return fetchManifest(contentState.target.id);
} else if (contentState.target.type === 'Canvas') {
loadedManifest = contentState.target.partOf[0].id;
return fetchManifest(loadedManifest);
} else {
console.log("Couldn't parse content state " + contentState);
}
} else {
loadedManifest = iiifContent;
return fetchManifest(iiifContent);
}
}
function getCanvas(iiifContent) {
if (isEncoded(iiifContent)) {
contentState = decode(iiifContent);
if (contentState.target.type === 'Canvas') {
if (contentState.target.id.indexOf("#") != -1) {
loadedCanvas = contentState.target.id.split("#")[0];
} else {
loadedCanvas = contentState.target.id;
}
return loadedCanvas;
} else {
// Not sure what this is...
return null;
}
} else {
// This is just a plain manifest URL
return null;
}
}
function getContentState(region=null, body = null) {
if (loadedCanvas == null) {
return loadedManifest;
} else {
let canvasURI = loadedCanvas;
if (region != null) {
if (loadedCanvas.indexOf('#') != -1) {
loadedCanvas = loadedCanvas.split("#")[0];
}
loadedCanvas += "#" + region;
}
contentState = {
"@context": "http://iiif.io/api/presentation/3/context.json",
"type": "Annotation",
"motivation": ["contentState"],
"target": {
"id": loadedCanvas,
"type": "Canvas",
"partOf": [{
"id": loadedManifest,
"type": "Manifest"
}]
}
}
if (body != null) {
contentState.body = body;
}
return encode(contentState);
}
}
/**
* Will return the region in the form:
* { x: 1000, y: 2000, width: 1000, height: 2000 } if its been specified
* otherwise returns null
*/
function getRegion(iiifContent) {
if (isEncoded(iiifContent)) {
contentState = decode(iiifContent);
if (contentState.target.type === 'Canvas') {
if (contentState.target.id.indexOf("#") != -1) {
let regionStrSplit = contentState.target.id.split("#xywh=")[1].split(",");
return {
x: parseInt(regionStrSplit[0]),
y: parseInt(regionStrSplit[1]),
width: parseInt(regionStrSplit[2]),
height: parseInt(regionStrSplit[3]),
}
} else {
// No region specified
return null;
}
} else {
// Not sure what this is...
return null;
}
} else {
// This is just a plain manifest URL
return null;
}
}
function fetchManifest(manifestUri) {
return vault.load(manifestUri);
}
function isEncoded(iiifContent) {
return !iiifContent.startsWith('http');
}
function encode(contentStateJson) {
let uriEncoded = encodeURIComponent(JSON.stringify(contentStateJson)); // using built in function
let base64 = btoa(uriEncoded); // using built in function
let base64url = base64.replace(/\+/g, "-").replace(/\//g, "_");
let base64urlNoPadding = base64url.replace(/=/g, "");
return base64urlNoPadding;
}
function decode(encodedContentState) {
let base64url = restorePadding(encodedContentState);
let base64 = base64url.replace(/-/g, '+').replace(/_/g, '/');
let base64Decoded = atob(base64); // using built in function
let uriDecoded = decodeURIComponent(base64Decoded); // using built in function
return JSON.parse(uriDecoded);
}
function restorePadding(s) {
// The length of the restored string must be a multiple of 4
let pad = s.length % 4;
let padding = "";
if (pad) {
if (pad === 1) {
throw new Error('InvalidLengthError: Input base64url string is the wrong length to determine padding');
}
s += '===='.slice(0, 4 - pad);
}
return s + padding;
}