-
Notifications
You must be signed in to change notification settings - Fork 3
/
imageViewer.js
256 lines (217 loc) · 7.67 KB
/
imageViewer.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
'use strict';
var $ = require('jquery');
require('jquery-ui');
var OpenSeaDragon = require('openseadragon');
function ImageViewer(config) {
var w = config.writer;
var id = config.parentId+'_imageViewer';
var tagName = config.tag || 'pb'; // page break element name
var attrName = config.attribute || 'facs'; // attribute that stores the image URL
var osd; // openseadragon instance
$('#'+config.parentId).append(''+
'<div id="'+id+'" class="imageViewer">'+
'<div class="toolbar">'+
'<div class="navigation">'+
'<span id="'+id+'_prev" class="button prev" />'+
'<span id="'+id+'_next" class="button next" />'+
'<span class="pageInfo"><input type="text" class="currPage" /> / <span class="totalPages" /></span>'+
'</div>'+
'<div class="zoom">'+
'<span id="'+id+'_zoomIn" class="button zoomIn" />'+
'<span id="'+id+'_zoomOut" class="button zoomOut" />'+
'<span id="'+id+'_home" class="button home" />'+
'</div>'+
'</div>'+
'<div id="'+id+'_osd" class="image"></div>'+
'</div>');
$('#'+config.parentId).css('overflow', 'hidden');
var $parent = $('#'+id);
var $pageBreaks;
var currentIndex = -1;
var ignoreScroll = false;
w.event('loadingDocument').subscribe(function() {
iv.reset();
});
// ensure page break tags are display block
var cssHack = function() {
var rules = $(w.editor.getDoc()).find('#schemaRules')[0];
if (rules == undefined) {
setTimeout(cssHack, 50);
} else {
rules.sheet.insertRule('*[_tag="'+tagName+'"] { display: block; }', 0);
}
}
w.event('documentLoaded').subscribe(function(success, body) {
if (success) {
processDocument(body, true);
setTimeout(cssHack, 50);
}
});
w.event('contentChanged').subscribe(function() {
processDocument(w.editor.getDoc(), false);
});
w.event('writerInitialized').subscribe(function() {
$(w.editor.getDoc()).scroll(handleScroll);
});
/**
* @lends ImageViewer.prototype
*/
var iv = {};
iv.reset = function() {
$pageBreaks = null;
currentIndex = -1;
osdReset();
};
iv.destroy = function() {
osd.destroy();
osd = null;
};
function osdReset() {
osd.drawer.clear();
osd.close();
osd.tileSources = []; // hack to remove any previously added images
}
function processDocument(doc, docLoaded) {
setMessage('');
$pageBreaks = $(doc).find('*[_tag='+tagName+']');
if ($pageBreaks.length === 0) {
hideViewer();
} else {
var tileSources = [];
$pageBreaks.each(function(index, el) {
var url = $(el).attr(attrName);
if (url === undefined || url === '') {
} else {
tileSources.push({
type: 'image',
url: url
});
}
});
var needUpdate = docLoaded || tileSources.length !== osd.tileSources.length;
if (!needUpdate) {
for (var i = 0; i < tileSources.length; i++) {
if (tileSources[i].url !== osd.tileSources[i].url) {
needUpdate = true;
break;
}
}
}
if (needUpdate) {
osd.open(tileSources);
if (tileSources.length === 0) {
w.layoutManager.hideModule('imageViewer');
} else {
w.layoutManager.showModule('imageViewer');
}
$parent.find('.totalPages').html($pageBreaks.length);
currentIndex = -1;
handleScroll();
}
}
}
function loadPage(index, doScroll) {
if (index >= 0 && index < $pageBreaks.length) {
currentIndex = index;
$parent.find('.currPage').val(currentIndex+1);
if (!ignoreScroll && doScroll) {
ignoreScroll = true;
var pb = $pageBreaks.get(currentIndex);
if (currentIndex === 0) $(pb).show();
pb.scrollIntoView();
if (currentIndex === 0) $(pb).hide();
}
}
}
function handleScroll() {
if (!ignoreScroll) {
var ifr = $('iframe', w.editor.getContainer());
var scrollHeight = ifr.height();
var el = w.editor.getDoc().scrollingElement;
var scrollTop = el.scrollTop;
var scrollBottom = scrollTop+scrollHeight;
var index = -1;
$pageBreaks.each(function(i, el) {
var y = $(el).offset().top;
if (y >= scrollTop && y < scrollBottom) {
index = i;
return false;
}
});
ignoreScroll = true;
osd.goToPage(index);
}
ignoreScroll = false;
}
function hideViewer() {
var msg = 'Provide page breaks ('+tagName+') with '+attrName+' attributes \n pointing to image URLs in order to \n display the corresponding images/scans \n for pages in this doument.';
setMessage(msg);
w.layoutManager.hideModule('imageViewer');
}
function resizeImage() {
var container = $parent.parent();
var toolbarHeight = 30;
var cw = container.width();
var ch = container.height()-toolbarHeight;
var img = $parent.find('.image img');
var iw = img.width();
var ih = img.height();
var cratio = ch/cw;
var iratio = ih/iw;
var nh, nw;
if (iratio >= 1) { // portrait
if (iratio > cratio) {
nh = ch;
nw = nh / iratio;
} else {
nw = cw;
nh = nw * iratio;
}
} else { // landscape
}
img.css('height', nh).css('width', nw).css('display', 'block');
}
function setMessage(msg) {
osd.drawer.clear();
osd._showMessage(msg);
}
$parent.find('.image img').on('load', function() {
resizeImage();
});
$parent.find('.currPage').keyup(function(e) {
if (e.keyCode == 13) { // enter key
var val = parseInt($(this).val());
if (!isNaN(val)) {
loadPage(val-1, true);
}
}
});
osd = OpenSeaDragon({
id: id+'_osd',
sequenceMode: true,
autoHideControls: false,
showFullPageControl: false,
previousButton: id+'_prev',
nextButton: id+'_next',
zoomInButton: id+'_zoomIn',
zoomOutButton: id+'_zoomOut',
homeButton: id+'_home'
});
osd.addHandler('open-failed', function(event) {
var msg = event.message;
if (event.source.url === true) {
msg = 'No URI found for @'+attrName+'.';
}
setMessage(msg);
});
osd.addHandler('reset-size', function(event) {
if (event.contentFactor == 0) {
setMessage('No URI found for @'+attrName+'.');
}
});
osd.addHandler('page', function(event) {
loadPage(event.page, true);
});
return iv;
}
module.exports = ImageViewer;