-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx_dlf_utils-resolutions-view-source.js
732 lines (633 loc) · 21.7 KB
/
tx_dlf_utils-resolutions-view-source.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
'use strict';
/**
* (c) Kitodo. Key to digital objects e.V. <[email protected]>
*
* This file is part of the Kitodo and TYPO3 projects.
*
* @license GNU General Public License version 3 or later.
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/
/**
* Base namespace for utility functions used by the dlf module.
*
* @const
*/
var dlfUtils = dlfUtils || {};
/**
* @type {{ZOOMIFY: string}}
*/
dlfUtils.CUSTOM_MIMETYPE = {
IIIF: 'application/vnd.kitodo.iiif',
IIP: 'application/vnd.netfpx',
ZOOMIFY: 'application/vnd.kitodo.zoomify'
};
/**
* @type {number}
*/
dlfUtils.RUNNING_INDEX = 99999999;
/**
* @param imageSourceObjs
* @param {string} opt_origin
* @return {Array.<ol.layer.Layer>}
*/
dlfUtils.createOl3Layers = function (imageSourceObjs, opt_origin) {
var origin = opt_origin !== undefined ? opt_origin : null,
widthSum = 0,
offsetWidth = 0,
layers = [];
imageSourceObjs.forEach(function (imageSourceObj) {
var tileSize = void 0;
if (widthSum > 0) {
// set offset width in case of multiple images
offsetWidth = widthSum;
}
//
// Create layer
//
var extent = [offsetWidth, -imageSourceObj.height, imageSourceObj.width + offsetWidth, 0],
layer = void 0;
if (imageSourceObj.mimetype === dlfUtils.CUSTOM_MIMETYPE.ZOOMIFY) {
// create zoomify layer
layer = new ol.layer.Tile({
source: new ol.source.Zoomify({
url: imageSourceObj.src,
size: [imageSourceObj.width, imageSourceObj.height],
crossOrigin: origin,
offset: [offsetWidth, 0]
})
});
} else if (imageSourceObj.mimetype === dlfUtils.CUSTOM_MIMETYPE.IIIF) {
var quality;
tileSize = imageSourceObj.tilesize !== undefined && imageSourceObj.tilesize.length > 0
? imageSourceObj.tilesize[0]
: 256,
quality = imageSourceObj.qualities !== undefined && imageSourceObj.qualities.length > 0
? $.inArray('color', imageSourceObj.qualities) >= 0
? 'color'
: $.inArray('native', imageSourceObj.qualities) >= 0
? 'native'
: 'default'
: 'default';
layer = new ol.layer.Tile({
source: new dlfViewerSource.IIIF({
url: imageSourceObj.src,
size: [imageSourceObj.width, imageSourceObj.height],
crossOrigin: origin,
resolutions: imageSourceObj.resolutions,
tileSize: tileSize,
format: 'jpg',
quality: quality,
offset: [offsetWidth, 0],
projection: new ol.proj.Projection({
code: 'kitodo-image',
units: 'pixels',
extent: extent
})
})
});
} else if (imageSourceObj.mimetype === dlfUtils.CUSTOM_MIMETYPE.IIP) {
tileSize = imageSourceObj.tilesize !== undefined && imageSourceObj.tilesize.length > 0
? imageSourceObj.tilesize[0]
: 256;
layer = new ol.layer.Tile({
source: new dlfViewerSource.IIP({
url: imageSourceObj.src,
size: [imageSourceObj.width, imageSourceObj.height],
crossOrigin: origin,
tileSize: tileSize,
offset: [offsetWidth, 0]
})
});
} else {
// create static image source
layer = new ol.layer.Image({
source: new ol.source.ImageStatic({
url: imageSourceObj.src,
projection: new ol.proj.Projection({
code: 'kitodo-image',
units: 'pixels',
extent: extent
}),
imageExtent: extent,
crossOrigin: origin
})
});
}
layers.push(layer);
// add to cumulative width
widthSum += imageSourceObj.width;
});
return layers;
};
/**
* @param {Array.<{src: *, width: *, height: *}>} images
* @return {ol.View}
*/
dlfUtils.createOl3View = function (images) {
//
// Calculate map extent
//
var maxLonX = images.reduce(function (prev, curr) {
return prev + curr.width;
}, 0),
maxLatY = images.reduce(function (prev, curr) {
return Math.max(prev, curr.height);
}, 0),
extent = [0, -maxLatY, maxLonX, 0];
var resolutions = [8,4,2,1,0.5,0.25,0.125,0.0675];
// globally define max zoom
window.OL3_MAX_ZOOM = 8;
// define map projection
var proj = new ol.proj.Projection({
code: 'kitodo-image',
units: 'pixels',
extent: extent
});
// define view
var viewParams = {
projection: proj,
center: ol.extent.getCenter(extent),
zoom: 1,
maxZoom: window.OL3_MAX_ZOOM,
resolutions: resolutions,
extent: extent
};
return new ol.View(viewParams);
};
/**
* Returns true if the specified value is not undefiend
* @param {?} val
* @return {boolean}
*/
dlfUtils.exists = function (val) {
return val !== undefined;
};
/**
* Fetch image data for given image sources.
*
* @param {Array.<{url: *, mimetype: *}>} imageSourceObjs
* @return {JQueryStatic.Deferred}
*/
dlfUtils.fetchImageData = function (imageSourceObjs) {
// use deferred for async behavior
var deferredResponse = new $.Deferred();
/**
* This holds information about the loading state of the images
* @type {Array.<number>}
*/
var imageSourceData = [],
loadCount = 0,
finishLoading = function finishLoading() {
loadCount += 1;
if (loadCount === imageSourceObjs.length) {
deferredResponse.resolve(imageSourceData);
}
};
imageSourceObjs.forEach(function (imageSourceObj, index) {
if (imageSourceObj.mimetype === dlfUtils.CUSTOM_MIMETYPE.ZOOMIFY) {
dlfUtils.fetchZoomifyData(imageSourceObj)
.done(function (imageSourceDataObj) {
imageSourceData[index] = imageSourceDataObj;
finishLoading();
});
} else if (imageSourceObj.mimetype === dlfUtils.CUSTOM_MIMETYPE.IIIF) {
dlfUtils.getIIIFResource(imageSourceObj)
.done(function (imageSourceDataObj) {
imageSourceData[index] = imageSourceDataObj;
finishLoading();
});
} else if (imageSourceObj.mimetype === dlfUtils.CUSTOM_MIMETYPE.IIP) {
dlfUtils.fetchIIPData(imageSourceObj)
.done(function (imageSourceDataObj) {
imageSourceData[index] = imageSourceDataObj;
finishLoading();
});
} else {
// In the worse case expect static image file
dlfUtils.fetchStaticImageData(imageSourceObj)
.done(function (imageSourceDataObj) {
imageSourceData[index] = imageSourceDataObj;
finishLoading();
});
}
});
return deferredResponse;
};
/**
* Fetches the image data for static images source.
*
* @param {{url: *, mimetype: *}} imageSourceObj
* @return {JQueryStatic.Deferred}
*/
dlfUtils.fetchStaticImageData = function (imageSourceObj) {
// use deferred for async behavior
var deferredResponse = new $.Deferred();
// Create new Image object.
var image = new Image();
// Register onload handler.
image.onload = function () {
var imageDataObj = {
src: this.src,
mimetype: imageSourceObj.mimetype,
width: this.width,
height: this.height
};
deferredResponse.resolve(imageDataObj);
};
// Initialize image loading.
image.src = imageSourceObj.url;
return deferredResponse;
};
/**
* @param imageSourceObj
* @returns {JQueryStatic.Deferred}
*/
dlfUtils.getIIIFResource = function getIIIFResource(imageSourceObj) {
var deferredResponse = new $.Deferred();
var type = 'GET';
$.ajax({
url: dlfViewerSource.IIIF.getMetdadataURL(imageSourceObj.url),
type: type,
dataType: 'json'
}).done(cb).fail(error);
function cb(data) {
var mimetype = imageSourceObj.mimetype;
if (dlfUtils.supportsIIIF(data)) {
if (data.protocol && data.protocol === 'http://iiif.io/api/image') {
var uri = decodeURI(data['@id']);
uri = dlfUtils.removeInfoJson(uri);
var imageResource = dlfUtils.buildImageV2(mimetype, uri, data);
deferredResponse.resolve(imageResource);
} else {
var _uri = imageSourceObj.url;
_uri = dlfUtils.removeInfoJson(_uri);
var _imageResource = dlfUtils.buildImageV1(mimetype, _uri, data);
deferredResponse.resolve(_imageResource);
}
}
}
function error(jqXHR, errorThrown) {
console.log("error", jqXHR.status);
console.log("status: " + errorThrown);
}
return deferredResponse;
};
/**
* @param uri
* @returns {*}
*/
dlfUtils.removeInfoJson = function removeInfoJson(uri) {
if (uri.endsWith('/info.json')) {
uri = uri.substr(0, uri.lastIndexOf('/'));
}
return uri;
};
/**
*
* @param data
* @param data.protocol
* @param data.identifier
* @param data.width
* @param data.height
* @param data.profile
* @param data.documentElement
* @returns {boolean}
*/
dlfUtils.supportsIIIF = function supportsIIIF(data) {
// Version 2.0 and forwards
if (data.protocol && data.protocol === 'http://iiif.io/api/image') {
return true;
// Version 1.1
} else if (data['@context'] && (
data['@context'] === "http://library.stanford.edu/iiif/image-api/1.1/context.json" ||
data['@context'] === "http://iiif.io/api/image/1/context.json")) {
return true;
// Version 1.0
} else if (data.profile &&
data.profile.indexOf("http://library.stanford.edu/iiif/image-api/compliance.html") === 0) {
return true;
} else if (data.identifier && data.width && data.height) {
return true;
} else return data.documentElement && "info" === data.documentElement.tagName &&
"http://library.stanford.edu/iiif/image-api/ns/" === data.documentElement.namespaceURI;
};
dlfUtils.iiifProfiles = {
'http://iiif.io/api/image/2/level1.json': {
formats: ['jpg'],
qualities: ['default']
},
'http://iiif.io/api/image/2/level2.json': {
formats: ['jpg', 'png'],
qualities: ['default', 'bitonal']
},
'http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level1': {
formats: ['jpg'],
qualities: ['native']
},
'http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level2': {
formats: ['jpg', 'png'],
qualities: ['native', 'color', 'grey', 'bitonal']
},
'http://library.stanford.edu/iiif/image-api/compliance.html#level1': {
formats: ['jpg'],
qualities: ['native']
},
'http://library.stanford.edu/iiif/image-api/compliance.html#level2': {
formats: ['jpg', 'png'],
qualities: ['native', 'color', 'grey', 'bitonal']
},
'none' : {
formats: [],
qualities: []
}
};
/**
*
* @param mimetype
* @param uri
* @param jsonld
* @param jsonld.tiles
* @param jsonld.width
* @param jsonld.height
* @param jsonld.profile
* @param jsonld.scaleFactors
* @returns {{src: *, width, height, tilesize: [*,*], qualities: *, formats: *, resolutions: *, mimetype: *}}
*/
dlfUtils.buildImageV2 = function buildImageV2(mimetype, uri, jsonld) {
var levelProfile = jsonld.profile === undefined || dlfUtils.iiifProfiles[jsonld.profile[0]] === undefined ? dlfUtils.iiifProfiles['none'] : dlfUtils.iiifProfiles[jsonld.profile[0]];
return {
src: uri,
width: jsonld.width,
height: jsonld.height,
tilesize: jsonld.tiles !== undefined ? [jsonld.tiles.map(function (a) {
return a.width;
})[0], jsonld.tiles.map(function (a) {
return a.height;
})[0]] : [256,256],
qualities: ['default'].concat(levelProfile.qualities).concat(jsonld.profile[1].qualities === undefined ? [] : jsonld.profile[1].qualities),
formats: ['jpg'].concat(levelProfile.formats).concat(jsonld.profile[1].formats === undefined ? [] : jsonld.profile[1].formats),
resolutions: jsonld.tiles !== undefined ? jsonld.tiles.map(function (a) {
return a.scaleFactors;
})[0] : [],
mimetype: mimetype
};
};
/**
*
* @param mimetype
* @param uri
* @param jsonld
* @param jsonld.width
* @param jsonld.height
* @param jsonld.scale_factors
* @param jsonld.tile_width
* @param jsonld.tile_height
* @param jsonld.qualities
* @param jsonld.formats
* @returns {{src: *, width, height, tilesize: [*,*], qualities: *, formats: *, resolutions: *, mimetype: *}}
*/
dlfUtils.buildImageV1 = function buildImageV1(mimetype, uri, jsonld) {
var levelProfile = jsonld.profile === undefined || dlfUtils.iiifProfiles[jsonld.profile] === undefined ? dlfUtils.iiifProfiles['none'] : dlfUtils.iiifProfiles[jsonld.profile];
return {
src: uri,
width: jsonld.width,
height: jsonld.height,
tilesize: [jsonld.tile_width, jsonld.tile_height],
qualities: ['native'].concat(levelProfile.qualities).concat(jsonld.qualities === undefined ? [] : jsonld.qualities),
formats: ['jpg'].concat(levelProfile.formats).concat(jsonld.formats === undefined ? [] : jsonld.formats),
resolutions: jsonld.scale_factors,
mimetype: mimetype
};
};
/**
* Fetches the image data for iip images source.
*
* @param {{url: *, mimetype: *}} imageSourceObj
* @return {JQueryStatic.Deferred}
*/
dlfUtils.fetchIIPData = function (imageSourceObj) {
// use deferred for async behavior
var deferredResponse = new $.Deferred();
$.ajax({
url: dlfViewerSource.IIP.getMetdadataURL(imageSourceObj.url) //'http://localhost:8000/fcgi-bin/iipsrv.fcgi?FIF=F4713/HD7.tif&obj=IIP,1.0&obj=Max-size&obj=Tile-size&obj=Resolution-number',
}).done(cb);
function cb(response, type) {
if (type !== 'success') throw new Error('Problems while fetching ImageProperties.xml');
var imageDataObj = $.extend({
src: imageSourceObj.url,
mimetype: imageSourceObj.mimetype
}, dlfViewerSource.IIP.parseMetadata(response));
deferredResponse.resolve(imageDataObj);
}
return deferredResponse;
};
/**
* Fetch image data for zoomify source.
*
* @param {{url: *, mimetype: *}} imageSourceObj
* @return {JQueryStatic.Deferred}
*/
dlfUtils.fetchZoomifyData = function (imageSourceObj) {
// use deferred for async behavior
var deferredResponse = new $.Deferred();
$.ajax({
url: imageSourceObj.url
}).done(cb);
function cb(response, type) {
if (type !== 'success') {
throw new Error('Problems while fetching ImageProperties.xml');
}
var properties = $(response).find('IMAGE_PROPERTIES');
var imageDataObj = {
src: imageSourceObj.url.substring(0, imageSourceObj.url.lastIndexOf("/") + 1),
width: parseInt(properties.attr('WIDTH')),
height: parseInt(properties.attr('HEIGHT')),
tilesize: parseInt(properties.attr('TILESIZE')),
mimetype: imageSourceObj.mimetype
};
deferredResponse.resolve(imageDataObj);
}
return deferredResponse;
};
/**
* @param {string} name Name of the cookie
* @return {string|null} Value of the cookie
* @TODO replace unescape function
*/
dlfUtils.getCookie = function (name) {
var results = document.cookie.match("(^|;) ?" + name + "=([^;]*)(;|$)");
if (results) {
return decodeURI(results[2]);
} else {
return null;
}
};
/**
* Returns url parameters
* @returns {Object|undefined}
*/
dlfUtils.getUrlParams = function () {
if (Object.prototype.hasOwnProperty.call(location, 'search')) {
var search = decodeURIComponent(location.search).slice(1).split('&'),
params = {};
search.forEach(function (item) {
var s = item.split('=');
params[s[0]] = s[1];
});
return params;
}
return undefined;
};
/**
* Returns true if the specified value is null.
* @param {?} val
* @return {boolean}
*/
dlfUtils.isNull = function (val) {
return val === null;
};
/**
* Returns true if the specified value is null, empty or undefined.
* @param {?} val
* @return {boolean}
*/
dlfUtils.isNullEmptyUndefinedOrNoNumber = function (val) {
return val === null || val === undefined || val === '' || isNaN(val);
};
/**
* @param {Array.<{url: *, mimetype: *}>} imageObjs
* @return {boolean}
*/
dlfUtils.isCorsEnabled = function (imageObjs) {
// fix for proper working with ie
if (!window.location.origin) {
window.location.origin = window.location.protocol + '//' + window.location.hostname +
(window.location.port ? ':' + window.location.port : '');
}
// fetch data from server
// with access control allowed
var response = true;
imageObjs.forEach(function (imageObj) {
var url = imageObj.mimetype === dlfUtils.CUSTOM_MIMETYPE.ZOOMIFY
? imageObj.url.replace('ImageProperties.xml', 'TileGroup0/0-0-0.jpg')
:
imageObj.mimetype === dlfUtils.CUSTOM_MIMETYPE.IIIF
? dlfViewerSource.IIIF.getMetdadataURL(imageObj.url)
: imageObj.mimetype === dlfUtils.CUSTOM_MIMETYPE.IIP
? dlfViewerSource.IIP.getMetdadataURL(imageObj.url)
: imageObj.url;
url = window.location.origin + window.location.pathname + '?eID=tx_dlf_geturl_eid&url=' + encodeURIComponent(url) + '&header=2';
$.ajax({
url: url,
async: false
}).done(function (data, type) {
response = type === 'success' && data.indexOf('Access-Control-Allow-Origin') !== -1;
}).error(function (data, type) {
response = false;
});
});
return response;
};
/**
* Functions checks if WebGL is enabled in the browser
* @return {boolean}
*/
dlfUtils.isWebGLEnabled = function () {
if (!!window.WebGLRenderingContext) {
var canvas = document.createElement("canvas"),
rendererNames = ["webgl", "experimental-webgl", "moz-webgl", "webkit-3d"],
context = false;
for (var i = 0; i < rendererNames.length; i++) {
try {
context = canvas.getContext(rendererNames[i]);
if (context && typeof context.getParameter === "function") {
// WebGL is enabled;
return true;
}
} catch (e) {}
}
// WebGL not supported
return false;
}
// WebGL not supported
return false;
};
/**
* @param {Element} element
* @return {Object}
*/
dlfUtils.parseDataDic = function (element) {
var dataDicString = $(element).attr('data-dic'),
dataDicRecords = dataDicString.split(';'),
dataDic = {};
for (var i = 0, ii = dataDicRecords.length; i < ii; i++) {
var key = dataDicRecords[i].split(':')[0],
value = dataDicRecords[i].split(':')[1];
dataDic[key] = value;
}
return dataDic;
};
/**
* Set a cookie value
*
* @param {string} name The key of the value
* @param {?} value The value to save
*/
dlfUtils.setCookie = function (name, value) {
document.cookie = name + "=" + decodeURI(value) + "; path=/";
};
/**
* Scales down the given features geometries. as a further improvement this function
* adds a unique id to every feature
* @param {Array.<ol.Feature>} features
* @param {Object} imageObj
* @param {number} width
* @param {number} height
* @param {number=} opt_offset
* @deprecated
* @return {Array.<ol.Feature>}
*/
dlfUtils.scaleToImageSize = function (features, imageObj, width, height, opt_offset) {
// update size / scale settings of imageObj
var image = void 0;
if (width && height) {
image = {
'width': width,
'height': height,
'scale': imageObj.width / width
};
}
if (image === undefined) return [];
var scale = image.scale,
offset = opt_offset !== undefined ? opt_offset : 0;
// do rescaling and set a id
for (var i in features) {
var oldCoordinates = features[i].getGeometry().getCoordinates()[0],
newCoordinates = [];
for (var j = 0; j < oldCoordinates.length; j++) {
newCoordinates.push(
[offset + scale * oldCoordinates[j][0], 0 - scale * oldCoordinates[j][1]]);
}
features[i].setGeometry(new ol.geom.Polygon([newCoordinates]));
// set index
dlfUtils.RUNNING_INDEX += 1;
features[i].setId('' + dlfUtils.RUNNING_INDEX);
}
return features;
};
/**
* Search a feature collcetion for a feature with the given text
* @param {Array.<ol.Feature>} featureCollection
* @param {string} text
* @return {Array.<ol.Feature>|undefined}
*/
dlfUtils.searchFeatureCollectionForText = function (featureCollection, text) {
var features = [];
featureCollection.forEach(function (ft) {
if (ft.get('fulltext') !== undefined) {
if (ft.get('fulltext').toLowerCase().indexOf(text.toLowerCase()) > -1) features.push(ft);
}
});
return features.length > 0 ? features : undefined;
};