]> _ Git - fluidbook-html5.git/commitdiff
wip #6188 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 12 Sep 2023 11:32:51 +0000 (13:32 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 12 Sep 2023 11:32:51 +0000 (13:32 +0200)
js/libs/fluidbook/fluidbook.js
js/libs/fluidbook/fluidbook.search.js

index f170c07cb73216a3c75f1d247a023d9e399f170a..e25daabbfe4dd870b9436c0e3f296e7cbfe70021 100644 (file)
@@ -226,13 +226,13 @@ Fluidbook.prototype = {
             $('html').addClass('sharp');
         }
 
-        if (this.nav.isInverted() ) {
+        if (this.nav.isInverted()) {
             $('html').addClass('menu-inverted');
         } else {
             $('html').addClass('menu-default');
         }
 
-        if(this.nav.isLogoCentered()){
+        if (this.nav.isLogoCentered()) {
             $('#logo').addClass('center');
         }
 
@@ -312,32 +312,44 @@ Fluidbook.prototype = {
             } catch (err) {
             }
         });
-    }, initPage: function (pageNr, doublePage, position) {
+    },
+
+    initPage: function (pageNr, doublePage, position) {
         if ($("#page_" + pageNr).length > 0) {
             return;
         }
         $(doublePage).find('.' + position).remove();
         var page = '<div class="page ' + position + '" id="page_' + pageNr + '" data-page="' + pageNr + '"><div class="background" page="' + pageNr + '"></div><div class="ctlinks" data-blendmode="normal"></div><div class="texts" highlight=""></div><div class="clinks" data-blendmode="normal"></div><div class="shade"></div></div>';
         $(doublePage).append(page);
-    }, hidePage: function (position) {
+    },
+
+    hidePage: function (position) {
         $("#pages ." + position).hide();
-    }, canChangePage: function () {
+    },
+
+    canChangePage: function () {
         try {
             return this.pagetransitions.canChangePage();
         } catch (e) {
 
         }
         return false;
-    }, initVideos: function () {
+    },
+
+    initVideos: function () {
         this.video.initVideos();
-    }, getNextOffset: function () {
+    },
+
+    getNextOffset: function () {
         var offset = 2;
         if (this.displayOnePage) {
             offset = 1;
         }
         this.transitionAxis = 'x';
         return offset;
-    }, goNextPage: function () {
+    },
+
+    goNextPage: function () {
         if (!this.canChangePage()) {
             return;
         }
@@ -348,13 +360,17 @@ Fluidbook.prototype = {
             this.transitionAxis = 'x';
             this.setCurrentPage(this.normalizePage(this.currentPage) + this.getNextOffset());
         }
-    }, goFirstPage: function () {
+    },
+
+    goFirstPage: function () {
         if (!this.canChangePage()) {
             return;
         }
         this.transitionAxis = 'x';
         this.setCurrentPage(1);
-    }, goPreviousPage: function () {
+    },
+
+    goPreviousPage: function () {
         if (!this.canChangePage()) {
             return;
         }
@@ -365,13 +381,17 @@ Fluidbook.prototype = {
             this.transitionAxis = 'x';
             this.setCurrentPage(this.normalizePage(this.currentPage) - this.getNextOffset());
         }
-    }, goLastPage: function () {
+    },
+
+    goLastPage: function () {
         if (!this.canChangePage()) {
             return;
         }
         this.transitionAxis = 'x';
         this.setCurrentPage(this.contentlock.getMaxPage());
-    }, goNextChapter: function () {
+    },
+
+    goNextChapter: function () {
         if (!this.canChangePage()) {
             return;
         }
@@ -381,7 +401,9 @@ Fluidbook.prototype = {
         }
         this.transitionAxis = 'x';
         this.setCurrentPage(this.normalizePage(next));
-    }, goPreviousChapter: function () {
+    },
+
+    goPreviousChapter: function () {
         if (!this.canChangePage()) {
             return;
         }
@@ -391,7 +413,9 @@ Fluidbook.prototype = {
         }
         this.transitionAxis = 'x';
         this.setCurrentPage(this.normalizePage(prev));
-    }, goNextChapterPage: function () {
+    },
+
+    goNextChapterPage: function () {
         if (!this.canChangePage()) {
             return;
         }
@@ -402,7 +426,9 @@ Fluidbook.prototype = {
         }
         this.transitionAxis = 'y';
         this.setCurrentPage(this.normalizePage(next));
-    }, goPreviousChapterPage: function () {
+    },
+
+    goPreviousChapterPage: function () {
         if (!this.canChangePage()) {
             return;
         }
@@ -413,15 +439,21 @@ Fluidbook.prototype = {
         }
         this.transitionAxis = 'y';
         this.setCurrentPage(this.normalizePage(prev));
-    }, normalizePage: function (page) {
+    },
+
+    normalizePage: function (page) {
         page = Math.max(1, Math.min(page, this.contentlock.getMaxPage()));
         if (!this.displayOnePage && page % 2 === 1) {
             page--;
         }
         return page;
-    }, setCurrentPage: function (page) {
+    },
+
+    setCurrentPage: function (page) {
         window.location.hash = "#/page/" + this.normalizePage(page);
-    }, changeAddress: function () {
+    },
+
+    changeAddress: function () {
         var hash = window.location.hash;
         if ($('.mview[data-hash="' + hash + '"]').length > 0) {
             return;
@@ -588,7 +620,9 @@ Fluidbook.prototype = {
                 window.location.hash = "/page/" + page;
             }
         }
-    }, getButtonsVisibility: function (page) {
+    },
+
+    getButtonsVisibility: function (page) {
         if (page === undefined) {
             page = this.currentPage;
         }
@@ -601,7 +635,9 @@ Fluidbook.prototype = {
         var next = page < max;
         var previous = (page > 1);
         return {previous: previous, next: next};
-    }, hideUnnecessaryButtons: function (page) {
+    },
+
+    hideUnnecessaryButtons: function (page) {
         var speed = 500;
         if (page === undefined) {
             page = this.currentPage;
@@ -679,7 +715,9 @@ Fluidbook.prototype = {
         if (s_out.length > 0) {
             $(s).children(s_out.join(',')).addClass('hidden');
         }
-    }, showAllButtons: function () {
+    },
+
+    showAllButtons: function () {
         $("#next-arrows,#prev-arrows").addClass('help').show();
     },
 
@@ -687,7 +725,9 @@ Fluidbook.prototype = {
         $("#pagesnumbers .left").html(this.getPageNumberOfSide('left', true));
         $("#pagesnumbers .right").html(this.getPageNumberOfSide('right', true));
         $("#pagesnumbers").removeClass('hidden');
-    }, getPhysicalPageNumberOfSide: function (side, strict) { // Side should be 'left' or 'right'
+    },
+
+    getPhysicalPageNumberOfSide: function (side, strict) { // Side should be 'left' or 'right'
         if (undefined === strict) {
             strict = false;
         }
@@ -836,7 +876,7 @@ Fluidbook.prototype = {
         var w = this.wopen(url, '_blank', '');
         if (print) {
             setTimeout(function () {
-               // w.print();
+                // w.print();
             }, 2000);
         }
     },
index eb04dc9dc581343f0a6f9c6e0826ad2398cec557..093b9df08f718015ad5f3d6c64d76b2ea831cc6f 100644 (file)
@@ -11,6 +11,8 @@ function FluidbookSearch(fluidbook) {
     this.resultsNavID = 'searchResultsNav';
     this.hideableElements = $('header,footer,#interface'); // Which elements to show/hide when in search results nav mode
     this.resultsActiveClass = 'searchResultsNavActive';
+    this.robust = fluidbook.settings.search_mode === 'robust';
+    this.hintsEnabled = !this.robust;
 
     this.init();
 }
@@ -144,7 +146,9 @@ FluidbookSearch.prototype = {
 
         this.resultsNav.find('.searchResultsNavQuery').text($('#q').val());
         this.resultsNav.find('.searchResultsNavCounter').text(counterText);
-    }, openResultsNav: function (resultPage) {
+    },
+
+    openResultsNav: function (resultPage) {
         this.updateResultsNav(resultPage);
         this.hideableElements.addClass('hidden');
         this.resultsNav.removeClass('hidden');
@@ -218,18 +222,21 @@ FluidbookSearch.prototype = {
             $this._getHints(q, callback);
         });
     },
+
     find: function (q, callback) {
         var $this = this;
         this._loadLib(function () {
             $this._find(q, callback);
         });
     },
+
     _loadLib: function (callback) {
         if (this.indexLoaded) {
             return callback();
         }
         var $this = this;
-        loadJSLibrary('data/search.index.js?j=' + this.fluidbook.settings.cacheDate, function () {
+
+        let __loadTexts = function () {
             $this._loadTexts(function () {
                 if ($this.highlightEnabled) {
                     loadJSLibrary('data/search.highlight.js?j=' + this.fluidbook.settings.cacheDate, function () {
@@ -241,11 +248,19 @@ FluidbookSearch.prototype = {
                     callback();
                 }
             });
-        });
+        };
+
+        if (!this.robust) {
+            loadJSLibrary('data/search.index.js?j=' + this.fluidbook.settings.cacheDate, function () {
+                __loadTexts();
+            });
+        } else {
+            __loadTexts();
+        }
     },
 
     _loadTexts: function (callback) {
-        if (this.fluidbook.settings.searchWordSelectionAlgorithm === 'expression') {
+        if (this.robust || this.fluidbook.settings.searchWordSelectionAlgorithm === 'expression') {
             loadJSLibrary('data/search.texts.js?j=' + this.fluidbook.settings.cacheDate, function () {
                 callback();
             });
@@ -253,8 +268,9 @@ FluidbookSearch.prototype = {
             callback();
         }
     },
+
     _getHints: function (q, callback) {
-        var words = this.normalizeQuery(q, true);
+        var words = this.normalizeQuery(q, true, false);
         q = words.pop();
         var res = [];
         if (q.length < 3) {
@@ -288,7 +304,60 @@ FluidbookSearch.prototype = {
         return a;
     },
 
+    _findRobust(q, callback) {
+        q = this.normalizeQuery(q, false, true);
+        var res = {};
+        var terms = [];
+        var total = 0;
+        var doublesPages = [];
+        var maxPage = this.fluidbook.contentlock.getMaxPage();
+
+        for (var p in TEXTS) {
+            var t = TEXTS[p];
+            var regexp = new RegExp(q, 'g');
+            var r = t.match(regexp);
+            var nb = 0;
+            try {
+                nb = r.length;
+            } catch (e) {
+
+            }
+            if (nb == 0) {
+                continue;
+            }
+            page = parseInt(p);
+            if (page > maxPage) {
+                continue;
+            }
+
+            if (!this.singleMode && (page % 2) === 1) {
+                page--;
+            }
+
+            doublesPages[page] = [];
+            doublesPages[page][q] = nb;
+
+            res[page] = nb;
+            total += nb;
+        }
+        if (total > 0) {
+            terms = [q];
+        }
+        var returnVal = {
+            total: total, results: res, terms: terms
+        };
+
+        console.log(returnVal);
+
+        callback(returnVal);
+    },
+
     _find: function (q, callback) {
+        if (this.robust) {
+            return this._findRobust(q, callback);
+        }
+
+
         var algo = this.getSearchWordSelectionAlgorithm(q);
         if (algo === 'expression') {
             return this._findExpression(q, callback);
@@ -418,22 +487,29 @@ FluidbookSearch.prototype = {
     sortHints: function (a, b) {
         return b[1] - a[1];
     },
+
     kill: function () {
 
     },
-    normalizeQuery: function (q, split) {
+
+    normalizeQuery: function (q, split, removeSpaces) {
         q = this.noAccents(q);
         q = q.trim();
         q = q.replace(/\s+/g, ' ');
+        if (removeSpaces) {
+            q = q.replace(/\s+/g, '');
+        }
         q = q.toLowerCase();
         if (split) {
             return q.split(' ');
         }
         return q;
     },
+
     noAccents: function (source) {
         return removeDiacritics(source);
     },
+
     setHighlightTerms: function (terms) {
         if (!this.highlightEnabled || window.HIGHLIGHTS === undefined) {
             return;
@@ -472,6 +548,7 @@ FluidbookSearch.prototype = {
             }
         }
     },
+
     clearHighlights: function () {
         $("#searchHighlights").html('');
         for (var p in this.plugins) {
@@ -479,6 +556,7 @@ FluidbookSearch.prototype = {
             plugin.clearHighlights();
         }
     },
+
     highlightSearchTerms: function (pageNr) {
         if (!this.highlightEnabled) {
             console.warn('hightlight disabled');
@@ -586,6 +664,9 @@ FluidbookSearch.prototype = {
     },
 
     initSearchHints: function () {
+        if (!this.hintsEnabled) {
+            return;
+        }
         this.menuSearchHints = $('#menuSearchHints');
         this.fluidbook.hideMenuItems(); // Hide menu items to give space for hints div
 
@@ -594,14 +675,27 @@ FluidbookSearch.prototype = {
         // if (this.menuSearchHints.hasClass('mm-hidden')) {
         //     this.nav.menuAPI.openPanel(this.menuSearchHints, false);
         // }
-    }, getSearchHints: function (q) {
+    },
+
+    getSearchHints: function (q) {
+        if (!this.hintsEnabled) {
+            this.fluidbook.hideMenuItems();
+            this._loadLib(function () {
 
+            });
+            return;
+        }
         var $this = this;
         this.killLastSearchHint();
         this.getHints(q, function (r) {
             $this.displaySearchHints(r);
         });
-    }, displaySearchHints: function (hints) {
+    },
+
+    displaySearchHints: function (hints) {
+        if (!this.hintsEnabled) {
+            return;
+        }
         var $this = this;
 
         if (hints.length == 0) {
@@ -632,10 +726,16 @@ FluidbookSearch.prototype = {
     },
 
     killLastSearchHint: function () {
+        if (!this.hintsEnabled) {
+            return;
+        }
         this.kill();
     },
 
     hideSearchHints: function () {
+        if (!this.hintsEnabled) {
+            return;
+        }
         if (this.menuSearchHints !== undefined) {
             this.menuSearchHints.html('').hide(); // Clear and hide all hints
         }
@@ -816,9 +916,14 @@ FluidbookSearch.prototype = {
         this.menuSearchResults.perfectScrollbar({
             suppressScrollX: true, minScrollbarLength: 40, //maxScrollbarLength: 60
         });
-    }, registerPlugin: function (p) {
+    },
+
+    registerPlugin: function (p) {
         this.plugins.push(p);
-    }, closeKeyboardonTouchDevices: function () {
+    },
+
+
+    closeKeyboardonTouchDevices: function () {
         if (this.fluidbook.support.iOS || this.fluidbook.support.android) {
             $("#q").blur();
         }