From 8c057ab399a7869f13c8a671240eb733e1b60042 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 19 Sep 2017 15:04:33 +0200 Subject: [PATCH] done #1668 @1 --- js/libs/fluidbook/fluidbook.search.js | 34 ++++++++++++++++++++------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/js/libs/fluidbook/fluidbook.search.js b/js/libs/fluidbook/fluidbook.search.js index a4a36a6d..e3333774 100644 --- a/js/libs/fluidbook/fluidbook.search.js +++ b/js/libs/fluidbook/fluidbook.search.js @@ -188,10 +188,16 @@ FluidbookSearch.prototype = { for (var t in terms) { var term = terms[t]; for (var w in HIGHLIGHTS) { - if (w.indexOf(term) == 0) { - var h = {occurences: HIGHLIGHTS[w], color: t, word: w, term: term}; - this.highlights.push(h); + if (this.fluidbook.datas.searchWordSelectionAlgorithm == 'begins' && w.indexOf(term) != 0) { + continue; + } else if (this.fluidbook.datas.searchWordSelectionAlgorithm == 'contains' && w.indexOf(term) == -1) { + continue; + } else if (this.fluidbook.datas.searchWordSelectionAlgorithm == 'exact' && w != term) { + continue; } + + var h = {occurences: HIGHLIGHTS[w], color: t, word: w, term: term}; + this.highlights.push(h); } } }, @@ -235,20 +241,30 @@ FluidbookSearch.prototype = { }, highlightOccurence: function (position, word, term, color, offset) { - if (word != term) { + if (word == term) { + width = position.width; + x = 0; + } else { // When we just highlight a part of the word. For example, searched term is brand and word found is brands var width = 0; - for (var i = 0; i < term.length; i++) { - width += position.lw[i]; + var x = 0; + var startIndex = word.indexOf(term); + for (var i = 0; i < word.length; i++) { + if (i < startIndex) { + x += position.lw[i]; + } else { + width += position.lw[i]; + } + if (i >= term.length) { + break; + } } - } else { - width = position.width; } var padding = position.height * 0.2; var z = this.fluidbook.datas.cssScale; var h = $('
'); - var coords = {top: (position.y - position.height - padding * 2) * z, left: (position.x - padding) * z + offset, width: (width + 2 * padding) * z, height: (position.height + padding * 3) * z}; + var coords = {top: (position.y - position.height - padding * 2) * z, left: (position.x + x - padding) * z + offset, width: (width + 2 * padding) * z, height: (position.height + padding * 3) * z}; var hash = coords.top + '|' + coords.left + '|' + coords.width + '|' + coords.height; if ($('[data-hash="' + hash + '"]').length > 0) { return; -- 2.39.5