forked from kitodo/kitodo-presentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchInDocument.js
More file actions
294 lines (263 loc) · 9.6 KB
/
SearchInDocument.js
File metadata and controls
294 lines (263 loc) · 9.6 KB
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
/**
* (c) Kitodo. Key to digital objects e.V. <contact@kitodo.org>
*
* 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 function increases the start parameter of the search form and submits
* the form.
*
* @returns void
*/
function nextResultPage() {
var currentStart = $("#tx-dlf-search-in-document-form input[id='tx-dlf-search-in-document-start']").val();
var newStart = parseInt(currentStart) + 20;
$("#tx-dlf-search-in-document-form input[id='tx-dlf-search-in-document-start']").val(newStart);
$('#tx-dlf-search-in-document-form').submit();
}
/**
* This function decreases the start parameter of the search form and submits
* the form.
*
* @returns void
*/
function previousResultPage() {
var currentStart = $("#tx-dlf-search-in-document-form input[id='tx-dlf-search-in-document-start']").val();
var newStart = (parseInt(currentStart) > 20) ? (parseInt(currentStart) - 20) : 0;
$("#tx-dlf-search-in-document-form input[id='tx-dlf-search-in-document-start']").val(newStart);
$('#tx-dlf-search-in-document-form').submit();
}
/**
* This function resets the start parameter on new queries.
*
* @returns void
*/
function resetStart() {
$("#tx-dlf-search-in-document-form input[id='tx-dlf-search-in-document-start']").val(0);
}
/**
* Add highlight effect for found search phrase.
* @param {array} highlightIds
*
* @returns void
*/
function addHighlightEffect(highlightIds) {
if (highlightIds.length > 0) {
highlightIds.forEach(function (highlightId) {
var targetElement = $('#' + highlightId);
if (targetElement.length > 0 && !targetElement.hasClass('highlight')) {
targetElement.addClass('highlight');
}
});
}
}
/**
* Get base URL for snippet links.
*
* @param {string} id
*
* @returns {string}
*/
function getBaseUrl(id) {
// Take the workview baseUrl from the form action.
// The URL may be in the following form
// - http://example.com/index.php?id=14
// - http://example.com/workview (using slug on page with uid=14)
var baseUrl = $("form#tx-dlf-search-in-document-form").attr('action');
// check if action URL contains id, if not, get URL from window
if(baseUrl === undefined || baseUrl.split('?')[0].indexOf(id) === -1) {
baseUrl = $(location).attr('href');
}
return baseUrl;
}
/**
* Get highlight coordinates as string separated by ';'.
*
* @param {string} highlight
*
* @returns {string}
*/
function getHighlights(highlight) {
var highlights = "";
for(var i = 0; i < highlight.length; i++) {
if (highlights === "") {
highlights += highlight[i];
} else {
if(highlights.indexOf(highlight[i]) === -1) {
highlights += ';' + highlight[i];
}
}
}
return highlights;
}
/**
* Get current URL query parameters.
* It returns array of params in form 'param=value' if there are any params supplied in the given url. If there are none it returns empty array
*
* @param {string} baseUrl
*
* @returns {array} array with params or empty
*/
function getCurrentQueryParams(baseUrl) {
if(baseUrl.indexOf('?') > 0) {
return baseUrl.slice(baseUrl.indexOf('?') + 1).split('&');
}
return [];
}
/**
* Get navigation buttons.
*
* @param {int} start
* @param {int} numFound
*
* @returns {string}
*/
function getNavigationButtons(start, numFound) {
var buttons = "";
if(start > 0) {
buttons += '<input type="button" id="tx-dlf-search-in-document-button-previous" class="button-previous" onclick="previousResultPage();" />';
}
if(numFound > (start + 20)) {
buttons += '<input type="button" id="tx-dlf-search-in-document-button-next" class="button-next" onclick="nextResultPage();" />';
}
return buttons;
}
/**
* Get current page.
*
* @returns {int}
*/
function getCurrentPage() {
var page = 1;
var baseUrl = getBaseUrl(" ");
var queryParams = getCurrentQueryParams(baseUrl);
var pageFound = false;
for(var i = 0; i < queryParams.length; i++) {
var queryParam = queryParams[i].split('=');
if(decodeURIComponent(queryParam[0]) === $("input[id='tx-dlf-search-in-document-page']").attr('name')) {
page = parseInt(queryParam[1], 10);
pageFound = true;
}
}
if (!pageFound) {
var url = baseUrl.split('/');
page = parseInt(url.pop(), 10);
}
return page;
}
/**
* Add highlight to image.
*
* @param {array} data
* @param {string} word
*
* @returns void
*/
function addImageHighlight(data, word) {
var page = getCurrentPage();
if (typeof tx_dlf_viewer !== 'undefined' && tx_dlf_viewer.map != null) { // eslint-disable-line camelcase
var highlights = [];
data['documents'].forEach(function (element, i) {
if(page <= element['page'] && element['page'] < page + tx_dlf_viewer.countPages()) { // eslint-disable-line camelcase
if (element['highlight'].length > 0) {
highlights.push(getHighlights(element['highlight']));
}
addHighlightEffect(element['highlight']);
}
});
tx_dlf_viewer.displayHighlightWord(word); // eslint-disable-line camelcase
} else {
setTimeout(addImageHighlight, 500, data, word);
}
}
/**
* Trigger search for document loaded from hit list.
*
* @returns void
*/
function triggerSearchAfterHitLoad() {
var queryParams = getCurrentQueryParams(getBaseUrl(" "));
var searchedQueryParam = $("input[id='tx-dlf-search-in-document-highlight-word']").attr('name');
for(var i = 0; i < queryParams.length; i++) {
var queryParam = queryParams[i].split('=');
if(searchedQueryParam && decodeURIComponent(queryParam[0]).indexOf(searchedQueryParam) !== -1) {
$("input[id='tx-dlf-search-in-document-query']").val(decodeURIComponent(queryParam[1]));
$("#tx-dlf-search-in-document-form").submit();
break;
}
}
}
$(document).ready(function() {
$("#tx-dlf-search-in-document-form").submit(function(event) {
// Stop form from submitting normally
event.preventDefault();
$('#tx-dlf-search-in-document-loading').show();
$('#tx-dlf-search-in-document-clearing').hide();
$('#tx-dlf-search-in-document-button-next').hide();
$('#tx-dlf-search-in-document-button-previous').hide();
// Send the data using post
$.post(
"/",
{
middleware: "dlf/search-in-document",
q: $( "input[id='tx-dlf-search-in-document-query']" ).val(),
uid: $( "input[id='tx-dlf-search-in-document-id']" ).val(),
pid: $( "input[id='tx-dlf-search-in-document-pid']" ).val(),
start: $( "input[id='tx-dlf-search-in-document-start']" ).val(),
encrypted: $( "input[id='tx-dlf-search-in-document-encrypted']" ).val(),
},
function(data) {
var resultItems = [];
var resultList = '<div class="results-active-indicator"></div><ul>';
var start = $( "input[id='tx-dlf-search-in-document-start']" ).val();
if (data['numFound'] > 0) {
data['documents'].forEach(function (element, i) {
if (start < 0) {
start = i;
}
if (element['snippet'].length > 0) {
resultItems[element['page']] = '<span class="structure">'
+ $('#tx-dlf-search-in-document-label-page').text() + ' ' + element['page']
+ '</span><br />'
+ '<span class="textsnippet">'
+ '<a href=\"' + element['url'] + '\">' + element['snippet'] + '</a>'
+ '</span>';
}
});
// Sort result by page.
resultItems.sort(function (a, b) {
return a - b;
});
resultItems.forEach(function (item, index) {
resultList += '<li>' + item + '</li>';
});
addImageHighlight(data, $( "input[id='tx-dlf-search-in-document-query']" ).val());
} else {
resultList += '<li class="noresult"></li>';
}
resultList += '</ul>';
resultList += getNavigationButtons(start, data['numFound']);
$('#tx-dlf-search-in-document-results').html(resultList);
$('.noresult').text($('#tx-dlf-search-in-document-label-noresult').text());
$('.button-previous').attr('value', $('#tx-dlf-search-in-document-label-previous').text());
$('.button-next').attr('value', $('#tx-dlf-search-in-document-label-next').text());
},
"json"
)
.done(function (data) {
$('#tx-dlf-search-in-document-loading').hide();
$('#tx-dlf-search-in-document-clearing').show();
});
});
// clearing button
$('#tx-dlf-search-in-document-clearing').click(function() {
$('#tx-dlf-search-in-document-results ul').remove();
$('.results-active-indicator').remove();
$('#tx-dlf-search-in-document-query').val('');
});
triggerSearchAfterHitLoad();
});