-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathopenseadragon_viewer.js
125 lines (117 loc) · 5.28 KB
/
openseadragon_viewer.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
/*jslint browser: true*/
/*global OpenSeadragon, Drupal*/
/**
* @file
* Displays OpenSeadragon viewer.
*/
(function($) {
'use strict';
/**
* The DOM element that represents the Singleton Instance of this class.
* @type {string}
*/
var base = '#openseadragon-viewer';
/**
* Initialize the OpenSeadragon Viewer.
*/
Drupal.behaviors.openSeadragon = {
attach: function(context, settings) {
Object.keys(settings.openseadragon).forEach(function(osdViewerId) {
// Use custom element #id if set.
base = '#' + osdViewerId;
$(base, context).once('openSeadragonViewer').each(function () {
Drupal.openSeadragonViewer[base] = new Drupal.openSeadragonViewer(base, settings.openseadragon[osdViewerId]);
});
});
},
detach: function() {
$(base).removeClass('openSeadragonViewer-processed');
$(base).removeData();
$(base).off();
delete Drupal.openSeadragonViewer[base];
}
};
/**
* Creates an instance of the OpenSeadragon Viewer widget.
*
* @param {string} base
* The element ID that this class is bound to.
* @param {object} settings
* Drupal.settings for this object widget.
*
* @constructor
*/
Drupal.openSeadragonViewer = function (base, settings) {
var viewer = new OpenSeadragon(settings.options);
var update_clip = function(event) {
var viewer = event.eventSource;
var fitWithinBoundingBox = function(d, max) {
if (d.width / d.height > max.x / max.y) {
return new OpenSeadragon.Point(max.x, parseInt(d.height * max.x / d.width));
} else {
return new OpenSeadragon.Point(parseInt(d.width * max.y / d.height), max.y);
}
};
var getDisplayRegion = function(viewer, source) {
// Determine portion of scaled image that is being displayed.
var box = new OpenSeadragon.Rect(0, 0, source.x, source.y);
var container = viewer.viewport.getContainerSize();
var bounds = viewer.viewport.getBounds();
// If image is offset to the left.
if (bounds.x > 0){
box.x = box.x - viewer.viewport.pixelFromPoint(new OpenSeadragon.Point(0,0)).x;
}
// If full image doesn't fit.
if (box.x + source.x > container.x) {
box.width = container.x - viewer.viewport.pixelFromPoint(new OpenSeadragon.Point(0,0)).x;
if (box.width > container.x) {
box.width = container.x;
}
}
// If image is offset up.
if (bounds.y > 0) {
box.y = box.y - viewer.viewport.pixelFromPoint(new OpenSeadragon.Point(0,0)).y;
}
// If full image doesn't fit.
if (box.y + source.y > container.y) {
box.height = container.y - viewer.viewport.pixelFromPoint(new OpenSeadragon.Point(0,0)).y;
if (box.height > container.y) {
box.height = container.y;
}
}
return box;
};
var source = viewer.source;
var zoom = viewer.viewport.getZoom();
var size = new OpenSeadragon.Rect(0, 0, source.dimensions.x, source.dimensions.y);
var container = viewer.viewport.getContainerSize();
var fit_source = fitWithinBoundingBox(size, container);
var total_zoom = fit_source.x / source.dimensions.x;
var container_zoom = fit_source.x / container.x;
var level = (zoom * total_zoom) / container_zoom;
var box = getDisplayRegion(viewer, new OpenSeadragon.Point(parseInt(source.dimensions.x * level), parseInt(source.dimensions.y * level)));
var scaled_box = new OpenSeadragon.Rect(parseInt(box.x / level), parseInt(box.y / level), parseInt(box.width / level), parseInt(box.height / level));
var params = {
'identifier': source['@id'],
'region': scaled_box.x + ',' + scaled_box.y + ',' + (scaled_box.getBottomRight().x - scaled_box.x) + ',' + (scaled_box.getBottomRight().y - scaled_box.y),
'size': (zoom <= 1) ? source.dimensions.x + ',' + source.dimensions.y : container.x + ',' + container.y
};
// $("#clip").attr('href', Drupal.settings.basePath + 'islandora/object/' + settings.pid + '/print?' + $.param({
// 'clip': $.param(params)
// }));
};
viewer.addHandler("open", update_clip);
viewer.addHandler("animation-finish", update_clip);
if (settings.fitToAspectRatio) {
viewer.addHandler("open", function (event) {
var viewer = event.eventSource;
if (viewer.source.aspectRatio / viewer.viewport.getAspectRatio() <= 1) {
viewer.viewport.fitVertically();
}
else {
viewer.viewport.fitHorizontally();
}
});
}
};
})(jQuery);