-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathIIIFBookReader.js
197 lines (157 loc) · 6.72 KB
/
IIIFBookReader.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
// Bind to the BookReader object providing facilities to set the necessary
// BookReader functions from a IIIF endpoint URL.
(function(BR){
BR.prototype.IIIF = function (config) {
// config should have the url of a sequence
// within a passed-in manifest.
var brInstance = this;
brInstance.IIIFsequence = {
title: null,
imagesList: [],
numPages: null,
bookUrl: null
};
oldInit = brInstance.init;
oldMode = brInstance.mode;
brInstance.init = function() {
load(config);
};
brInstance.mode = 2;
function bindBRMethods(){
brInstance.getPageNum = function(index) {
return index+1;
};
brInstance.getSpreadIndices = function(pindex) {
var spreadIndices = [null, null];
if ('rl' == brInstance.pageProgression) {
// Right to Left
if (brInstance.getPageSide(pindex) == 'R') {
spreadIndices[1] = pindex;
spreadIndices[0] = pindex + 1;
} else {
// Given index was LHS
spreadIndices[0] = pindex;
spreadIndices[1] = pindex - 1;
}
} else {
// Left to right
if (brInstance.getPageSide(pindex) == 'L') {
spreadIndices[0] = pindex;
spreadIndices[1] = pindex + 1;
} else {
// Given index was RHS
spreadIndices[1] = pindex;
spreadIndices[0] = pindex - 1;
}
}
return spreadIndices;
};
brInstance.getPageSide = function(index) {
if (0 == (index & 0x1)) {
return 'R';
} else {
return 'L';
}
};
brInstance.getPageHeight = function(index) {
console.log(index);
var fullWidth = brInstance.IIIFsequence.imagesList[index].width,
fullHeight = brInstance.IIIFsequence.imagesList[index].height,
scaleRatio = config.maxWidth/fullWidth;
return fullHeight*scaleRatio;
};
brInstance.getPageWidth = function(index) {
var fullWidth = brInstance.IIIFsequence.imagesList[index].width,
scaleRatio = config.maxWidth/fullWidth;
return fullWidth*scaleRatio;
};
brInstance.getPageURI = function(index) {
// Finds the image info.json url
// from the loaded sequence and returns the
// IIIF-formatted url for the page image
// based on the provided configuration object
// (adjusting for width, etc.).
var infoJsonUrl = brInstance.IIIFsequence.imagesList[index].imageUrl;
var url = infoJsonUrl + "/full/" + config.maxWidth + ",/0/native.jpg";
return url;
};
}
function load(config) {
endpointUrl = config.url,
sequenceId = config.sequenceId;
jQuery.ajax({
url: endpointUrl.replace(/^\s+|\s+$/g, ''),
dataType: 'json',
async: true,
success: function(jsonLd) {
brInstance.jsonLd = jsonLd;
brInstance.bookTitle = jsonLd.label;
brInstance.bookUrl = '#';
parseSequence(sequenceId);
bindBRMethods();
// Call the old initialisation after
// the urls are finished. A better implementation
// would be to employ promises (Likely by including
// the Q Promises/A+ implementation. See issue #1 at
// github.
oldInit.call(brInstance);
// // The following is an attrocious hack and must not
// // be allowed to persist. See issue #2 at github.com
// setTimeout(function() { jQuery(window).trigger('resize'); console.log("resize event fired")}, 2500);
},
error: function() {
console.log('Failed loading ' + brInstance.uri);
}
});
}
function parseSequence(sequenceId) {
jQuery.each(brInstance.jsonLd.sequences, function(index, sequence) {
if (sequence['@id'] === sequenceId) {
brInstance.IIIFsequence.title = "here's a sequence";
brInstance.numLeafs = 515;
brInstance.IIIFsequence.bookUrl = "http://iiif.io";
brInstance.IIIFsequence.imagesList = getImagesList(sequence);
}
});
delete brInstance.jsonLd;
}
function getImagesList(sequence) {
var imagesList = [];
jQuery.each(sequence.canvases, function(index, canvas) {
var imageObj;
if (canvas['@type'] === 'sc:Canvas') {
var images = canvas.resources || canvas.images;
jQuery.each(images, function(index, image) {
if (image['@type'] === 'oa:Annotation') {
imageObj = getImageObject(image);
imageObj.canvasWidth = canvas.width;
imageObj.canvasHeight = canvas.height;
if (!(/#xywh/).test(image.on)) {
imagesList.push(imageObj);
}
}
});
}
});
return imagesList;
}
function getImageObject (image) {
var resource = image.resource;
if (resource.hasOwnProperty('@type') && resource['@type'] === 'oa:Choice') {
var imageObj = getImageProperties(resource.default);
} else {
imageObj = getImageProperties(resource);
}
return(imageObj);
}
function getImageProperties(image) {
var imageObj = {
height: image.height || 0,
width: image.width || 0,
imageUrl: image.service['@id'].replace(/\/$/, ''),
};
imageObj.aspectRatio = (imageObj.width / imageObj.height) || 1;
return imageObj;
}
};
})(BookReader);