-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx_dlf_pageview_fulltext_control.js
454 lines (365 loc) · 14.2 KB
/
tx_dlf_pageview_fulltext_control.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
/**
* (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.
*/
/**
* This is necessary to support the scrolling of the element into the viewport
* in case of text hover on the map.
*
* @param elem
* @param speed
* @returns {jQuery}
*/
jQuery.fn.scrollTo = function(elem, speed) {
var manualOffsetTop = $(elem).parent().height() / 2;
$(this).animate({
scrollTop: $(this).scrollTop() - $(this).offset().top + $(elem).offset().top - manualOffsetTop
}, speed == undefined ? 1000 : speed);
return this;
};
/**
* Encapsulates especially the fulltext behavior
* @constructor
* @param {ol.Map} map
* @param {Object} image
* @param {string} fulltextUrl
*/
var dlfViewerFullTextControl = function(map, image, fulltextUrl) {
/**
* @private
* @type {ol.Map}
*/
this.map = map;
/**
* @type {Object}
* @private
*/
this.image = image;
/**
* @type {string}
* @private
*/
this.url = fulltextUrl;
/**
* @type {Object}
* @private
*/
this.dic = $('#tx-dlf-tools-fulltext').length > 0 && $('#tx-dlf-tools-fulltext').attr('data-dic') ?
dlfUtils.parseDataDic($('#tx-dlf-tools-fulltext')) :
{'fulltext-on':'Activate Fulltext','fulltext-off':'Dectivate Fulltext'};
/**
* @type {ol.Feature|undefined}
* @private
*/
this.fulltextData_ = undefined;
/**
* @type {Object}
* @private
*/
this.layers_ = {
textblock: new ol.layer.Vector({
'source': new ol.source.Vector(),
'style': dlfViewerOL3Styles.defaultStyle()
}),
textline: new ol.layer.Vector({
'source': new ol.source.Vector(),
'style': dlfViewerOL3Styles.invisibleStyle()
}),
select: new ol.layer.Vector({
'source': new ol.source.Vector(),
'style': dlfViewerOL3Styles.selectStyle()
}),
hoverTextblock: new ol.layer.Vector({
'source': new ol.source.Vector(),
'style': dlfViewerOL3Styles.hoverStyle()
}),
hoverTextline: new ol.layer.Vector({
'source': new ol.source.Vector(),
'style': dlfViewerOL3Styles.textlineStyle()
})
};
/**
* @type {ol.Feature}
* @private
*/
this.selectedFeature_ = undefined;
/**
* @type {Object}
* @private
*/
this.handlers_ = {
mapClick: $.proxy(function(event) {
// the click handler adds the clicked feature to a
// select layer which could be used to create a highlight
// effect on the map
var feature = this.map.forEachFeatureAtPixel(event['pixel'], function(feature, layer) {
if (feature.get('type') === 'textblock') {
return feature;
}
});
// deselect all
if (feature === undefined) {
this.layers_.select.getSource().clear();
this.selectedFeature_ = undefined;
this.showFulltext(undefined);
return;
};
// highlight features
if (this.selectedFeature_) {
// remove old clicks
this.layers_.select.getSource().removeFeature(this.selectedFeature_);
}
if (feature) {
// remove hover for preventing an adding of styles
this.layers_.hoverTextblock.getSource().clear();
// add feature
this.layers_.select.getSource().addFeature(feature);
}
this.selectedFeature_ = feature;
if (dlfUtils.exists(feature)) {
this.showFulltext([feature]);
}
},
this),
mapHover: $.proxy(function(event) {
// hover in case of dragging
if (event['dragging']) {
return;
};
var hoverSourceTextblock_ = this.layers_.hoverTextblock.getSource(),
hoverSourceTextline_ = this.layers_.hoverTextline.getSource(),
selectSource_ = this.layers_.select.getSource(),
map_ = this.map,
textblockFeature,
textlineFeature;
map_.forEachFeatureAtPixel(event['pixel'], function(feature, layer) {
if (feature.get('type') === 'textblock') {
textblockFeature = feature;
}
if (feature.get('type') === 'textline') {
textlineFeature = feature;
}
});
//
// Handle TextBlock elements
//
var activeSelectTextBlockEl_ = selectSource_.getFeatures().length > 0 ?
selectSource_.getFeatures()[0] : undefined,
activeHoverTextBlockEl_ = hoverSourceTextblock_.getFeatures().length > 0 ?
hoverSourceTextblock_.getFeatures()[0] : undefined,
isFeatureEqualSelectFeature_ = activeSelectTextBlockEl_ !== undefined && textblockFeature !== undefined &&
activeSelectTextBlockEl_.getId() === textblockFeature.getId() ? true : false,
isFeatureEqualToOldHoverFeature_ = activeHoverTextBlockEl_ !== undefined && textblockFeature !== undefined &&
activeHoverTextBlockEl_.getId() === textblockFeature.getId() ? true : false;
if (!isFeatureEqualToOldHoverFeature_ && !isFeatureEqualSelectFeature_) {
// remove old textblock hover features
hoverSourceTextblock_.clear();
if (textblockFeature) {
// add textblock feature to hover
hoverSourceTextblock_.addFeature(textblockFeature);
}
}
//
// Handle TextLine elements
//
var activeHoverTextBlockEl_ = hoverSourceTextline_.getFeatures().length > 0 ?
hoverSourceTextline_.getFeatures()[0] : undefined,
isFeatureEqualToOldHoverFeature_ = activeHoverTextBlockEl_ !== undefined && textlineFeature !== undefined &&
activeHoverTextBlockEl_.getId() === textlineFeature.getId() ? true : false;
if (!isFeatureEqualToOldHoverFeature_) {
if (activeHoverTextBlockEl_) {
// remove highlight effect on fulltext view
var oldTargetElem = $('#' + activeHoverTextBlockEl_.getId());
if (oldTargetElem.hasClass('highlight') ) {
oldTargetElem.removeClass('highlight');
}
// remove old textline hover features
hoverSourceTextline_.clear();
}
if (textlineFeature) {
// add highlight effect to fulltext view
var targetElem = $('#' + textlineFeature.getId());
if (targetElem.length > 0 && !targetElem.hasClass('highlight')) {
targetElem.addClass('highlight');
$('#tx-dlf-fulltextselection').scrollTo(targetElem, 50);
hoverSourceTextline_.addFeature(textlineFeature);
}
}
}
},
this)
};
// add active / deactive behavior in case of click on control
var anchorEl = $('#tx-dlf-tools-fulltext');
if (anchorEl.length > 0){
var toogleFulltext = $.proxy(function(event) {
event.preventDefault();
if ($(event.target).hasClass('active')) {
this.deactivate();
return;
}
this.activate();
}, this);
anchorEl.on('click', toogleFulltext);
anchorEl.on('touchstart', toogleFulltext);
}
// set initial title of fulltext element
$("#tx-dlf-tools-fulltext")
.text(this.dic['fulltext-on'])
.attr('title', this.dic['fulltext-on']);
// if fulltext is activated via cookie than run activation methode
if (dlfUtils.getCookie("tx-dlf-pageview-fulltext-select") == 'enabled') {
// activate the fulltext behavior
this.activate(anchorEl);
}
};
/**
* Activate Fulltext Features
*/
dlfViewerFullTextControl.prototype.activate = function() {
var controlEl = $('#tx-dlf-tools-fulltext');
// if the activate method is called for the first time fetch
// fulltext data from server
if (this.fulltextData_ === undefined) {
this.fulltextData_ = dlfViewerFullTextControl.fetchFulltextDataFromServer(this.url, this.image);
if (this.fulltextData_ !== undefined) {
// add features to fulltext layer
this.layers_.textblock.getSource().addFeatures(this.fulltextData_.getTextblocks());
this.layers_.textline.getSource().addFeatures(this.fulltextData_.getTextlines());
// add first feature of textBlockFeatures to map
if (this.fulltextData_.getTextblocks().length > 0) {
this.layers_.select.getSource().addFeature(this.fulltextData_.getTextblocks()[0]);
this.selectedFeature_ = this.fulltextData_.getTextblocks()[0];
this.showFulltext(this.fulltextData_.getTextblocks());
}
}
}
// now activate the fulltext overlay and map behavior
this.enableFulltextSelect();
dlfUtils.setCookie("tx-dlf-pageview-fulltext-select", 'enabled');
$(controlEl).addClass('active');
// trigger event
$(this).trigger("activate-fulltext", this);
};
/**
* Activate Fulltext Features
*/
dlfViewerFullTextControl.prototype.deactivate = function() {
var controlEl = $('#tx-dlf-tools-fulltext');
// deactivate fulltext
this.disableFulltextSelect();
dlfUtils.setCookie("tx-dlf-pageview-fulltext-select", 'disabled');
$(controlEl).removeClass('active');
// trigger event
$(this).trigger("deactivate-fulltext", this);
};
/**
* Disable Fulltext Features
*
* @return void
*/
dlfViewerFullTextControl.prototype.disableFulltextSelect = function() {
// register event listeners
this.map.un('click', this.handlers_.mapClick);
this.map.un('pointermove', this.handlers_.mapHover);
// remove layers
for (var key in this.layers_) {
if (this.layers_.hasOwnProperty(key)) {
this.map.removeLayer(this.layers_[key]);
}
};
var className = 'fulltext-visible';
$("#tx-dlf-tools-fulltext").removeClass(className)
.text(this.dic['fulltext-on'])
.attr('title', this.dic['fulltext-on']);
$('#tx-dlf-fulltextselection').removeClass(className);
$('#tx-dlf-fulltextselection').hide();
$('body').removeClass(className);
};
/**
* Activate Fulltext Features
* @param {Array.<ol.Feature>} textBlockFeatures
* @þaram {Array.<ol.Feature>} textLineFeatures
*/
dlfViewerFullTextControl.prototype.enableFulltextSelect = function(textBlockFeatures, textLineFeatures) {
// register event listeners
this.map.on('click', this.handlers_.mapClick);
this.map.on('pointermove', this.handlers_.mapHover);
// add layers to map
for (var key in this.layers_) {
if (this.layers_.hasOwnProperty(key)) {
this.map.addLayer(this.layers_[key]);
}
};
// show fulltext container
var className = 'fulltext-visible';
$("#tx-dlf-tools-fulltext").addClass(className)
.text(this.dic['fulltext-off'])
.attr('title', this.dic['fulltext-off']);
$('#tx-dlf-fulltextselection').addClass(className);
$('#tx-dlf-fulltextselection').show();
$('body').addClass(className);
};
/**
* Method fetches the fulltext data from the server
* @param {string} url
* @param {Object} image
* @param {number=} opt_offset
* @return {ol.Feature|undefined}
* @static
*/
dlfViewerFullTextControl.fetchFulltextDataFromServer = function(url, image, opt_offset){
// fetch data from server
var request = $.ajax({
url: url,
async: false
});
// parse alto data
var offset = dlfUtils.exists(opt_offset) ? opt_offset : undefined,
parser = new dlfAltoParser(image, undefined, undefined, offset),
fulltextCoordinates = request.responseXML ? parser.parseFeatures(request.responseXML) :
request.responseText ? parser.parseFeatures(request.responseText) : [];
if (fulltextCoordinates.length > 0) {
return fulltextCoordinates[0];
}
return undefined;
};
/**
* Activate Fulltext Features
*
* @param {Array.<ol.Feature>|undefined} features
*/
dlfViewerFullTextControl.prototype.showFulltext = function(features) {
var popupHTML = '',
/**
* Functions wraps fulltext context of a given textblock to a html string
* @param {ol.Feature} feature
* @return {string}
*/
appendHTML = function(feature) {
var html = '',
textlines = feature.get('textlines');
for (var i = 0; i < textlines.length; i++) {
html = html + '<span class="textline" id="' + textlines[i].getId() + '">';
var content = textlines[i].get('content');
for (var j = 0; j < content.length; j++) {
html = html + '<span class="' + content[j].get('type') + '" id="' + content[j].getId()
+ '">' + content[j].get('fulltext').replace(/\n/g, '<br />') + '</span>';
}
html = html + '</span>';
}
return html;
};
// iterate over given textblocks
if (features !== undefined) {
for (var i = 0; i < features.length; i++) {
popupHTML = popupHTML + appendHTML(features[i]) + '<br /><br />';
}
};
$('#tx-dlf-fulltextselection').html(popupHTML);
};