]> _ Git - fluidbook-html5.git/commitdiff
done #1668 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 19 Sep 2017 13:04:33 +0000 (15:04 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 19 Sep 2017 13:04:33 +0000 (15:04 +0200)
js/libs/fluidbook/fluidbook.search.js

index a4a36a6d842d4fcb9b0c680d19b28a7be066a377..e333377436f11e61f1b3cd836a8600a48f715e49 100644 (file)
@@ -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 = $('<div class="highlight" data-color="' + color + '"></div>');
-        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;