Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight and preserve all searched words - v2 #6

Merged
merged 2 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions Resources/Private/JavaScript/Kitodo/PageView/PageView.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ var dlfViewer = function(settings){
* @type {string|undefined}
* @private
*/
this.highlightKeys = undefined;
this.highlightKeys = 'tx_dlf[highlight_word]';

/**
* @type {string|undefined}
* @private
*/
this.highlightWords = null;

/**
* @type {Object|undefined}
Expand Down Expand Up @@ -289,8 +295,10 @@ dlfViewer.prototype.createControls_ = function(controlNames, layers) {
/**
* Displays highlight words
*/
dlfViewer.prototype.displayHighlightWord = function() {

dlfViewer.prototype.displayHighlightWord = function(highlightWords = null) {
if(highlightWords != null) {
this.highlightWords = highlightWords;
}
if (!dlfUtils.exists(this.highlightLayer)) {

this.highlightLayer = new ol.layer.Vector({
Expand Down Expand Up @@ -345,12 +353,21 @@ dlfViewer.prototype.displayHighlightWord = function() {
}
};

if (hasOwnProperty && this.fulltexts[0] !== undefined && this.fulltexts[0].url !== '' && this.images.length > 0) {
var value = urlParams[param],
values = decodeURIComponent(value).split(' '),
if ((hasOwnProperty || this.highlightWords != null) && this.fulltexts[0] !== undefined && this.fulltexts[0].url !== '' && this.images.length > 0) {
var value = undefined,
fulltextData = dlfFullTextUtils.fetchFullTextDataFromServer(this.fulltexts[0].url, this.images[0]),
fulltextDataImageTwo = undefined;

if(this.highlightWords != null) {
value = this.highlightWords;
} else {
value = urlParams[param];
}

var values = decodeURIComponent(value).split(' ');
values = values.filter(item => item !== '.');
values = values.filter(item => item !== ',');

// check if there is another image / fulltext to look for
if (this.images.length === 2 & this.fulltexts[1] !== undefined && this.fulltexts[1].url !== '') {
var image = $.extend({}, this.images[1]);
Expand Down
83 changes: 77 additions & 6 deletions Resources/Private/JavaScript/Kitodo/PageView/SearchInDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function resetStart() {
* @returns void
*/
function addHighlightEffect(highlightIds) {
// TODO: highlight found phrase in full text - verify page?
if (highlightIds.length > 0) {
highlightIds.forEach(function (highlightId) {
var targetElement = $('#' + highlightId);
Expand Down Expand Up @@ -146,10 +145,28 @@ function getNeededQueryParams(element) {
queryParams[highlightWord] = encodeURIComponent($("input[id='tx-dlf-search-in-document-query']").val());
queryParams.push(page);
queryParams[page] = element['page'];
queryParams.push('hl');
queryParams['hl'] = encodeURIComponent(getHighlightWords(element['words']));

return queryParams;
}

function getHighlightWords(words) {
var highlightWords = $("input[id='tx-dlf-search-in-document-query']").val();

for(var i = 0; i < words.length; i++) {
if (highlightWords === "") {
highlightWords += words[i];
} else {
if(highlightWords.indexOf(words[i]) === -1) {
highlightWords += ' ' + words[i];
}
}
}

return highlightWords;
}

/**
* Get snippet link.
*
Expand Down Expand Up @@ -213,6 +230,7 @@ function search() {
var resultList = '<div class="results-active-indicator"></div><ul>';
var start = -1;
if (data['numFound'] > 0) {
var page = getCurrentPage();
data['documents'].forEach(function (element, i) {
if (start < 0) {
start = i;
Expand All @@ -227,7 +245,9 @@ function search() {
+ '</span>';
}

addHighlightEffect(element['highlight']);
if(element['page'] == page) {
addHighlightEffect(element['highlight']);
}
});
// Sort result by page.
resultItems.sort(function (a, b) {
Expand All @@ -242,13 +262,60 @@ function search() {
resultList += '</ul>';
resultList += getNavigationButtons(start, data['numFound']);
$('#tx-dlf-search-in-document-results').html(resultList);

addImageHighlightAfterFirstLoad(data);
},
"json"
)
.done(function (data) {
$('#tx-dfgviewer-sru-results-loading').hide();
$('#tx-dfgviewer-sru-results-clearing').show();
).done(function (data) {
$('#tx-dfgviewer-sru-results-loading').hide();
$('#tx-dfgviewer-sru-results-clearing').show();
});
}

function getCurrentPage() {
var page = 1;
var queryParams = getCurrentQueryParams(getBaseUrl(" "));

for(var i = 0; i < queryParams.length; i++) {
var queryParam = queryParams[i].split('=');

if(queryParam[0] === $("input[id='tx-dlf-search-in-document-page']").attr('name')) {
page = queryParam[1];
}
}

return page;
}

function addImageHighlightAfterFirstLoad(data) {
var queryParams = getCurrentQueryParams(getBaseUrl(" "));
var hlParameterFound = false;

for(var i = 0; i < queryParams.length; i++) {
var queryParam = queryParams[i].split('=');

if(queryParam[0] ==='hl') {
hlParameterFound = true;
break;
}
}

if(!hlParameterFound && data['numFound'] > 0) {
var page = getCurrentPage();

data['documents'].forEach(function (element, i) {
if(element['page'] == page) {
if (element['words'].length > 0) {
if(tx_dlf_viewer.map != null) {
tx_dlf_viewer.displayHighlightWord(encodeURIComponent(getHighlightWords(element['words'])));
} else {
setTimeout(addImageHighlightAfterFirstLoad, 500, data);
}
}
addHighlightEffect(element['highlight'])
}
});
}
}

function clearSearch() {
Expand All @@ -267,6 +334,10 @@ function triggerSearchAfterHitLoad() {
$("input[id='tx-dlf-search-in-document-query']").val(decodeURIComponent(queryParam[1]));
search();
break;
} else if(queryParam[0].indexOf('query') != -1) {
$("input[id='tx-dlf-search-in-document-query']").val(decodeURIComponent(queryParam[1]).replace('+', ' '));
search();
break;
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion Resources/Private/JavaScript/Kitodo/PageView/Utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,13 +898,15 @@ function trimByChars(string, characters) {
*/
dlfUtils.searchFeatureCollectionForText = function (featureCollection, text) {
var features = [];
//TODO: check what is inside ft
featureCollection.forEach(function (ft) {
if (ft.get('fulltext') !== undefined) {
var trimmedFt = trimByChars(
ft.get('fulltext'),
[',', '.', ';', ':', '-', '\'', '"', '!', '?', '(', ')', '[', ']']
);
if (trimmedFt.toLowerCase() === text.toLowerCase()) {
//it highlights word if it is connected with other one by '-'
if ((trimmedFt.toLowerCase() === text.toLowerCase()) || (ft.get('fulltext').toLowerCase().indexOf(text.concat('-')) != -1)) {
features.push(ft);
}
}
Expand Down
8 changes: 4 additions & 4 deletions Resources/Public/JavaScript/ddbKitodoZeitungsportal.js

Large diffs are not rendered by default.