From fbc8a53949a5751a7ca48d60d532b569bf53eb45 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 29 Mar 2018 17:54:47 +0200 Subject: [PATCH] wip #2013 @1.5 --- js/libs/fluidbook/fluidbook.search.js | 134 +++++++++++++++++++++----- 1 file changed, 111 insertions(+), 23 deletions(-) diff --git a/js/libs/fluidbook/fluidbook.search.js b/js/libs/fluidbook/fluidbook.search.js index 1a244da3..3a607970 100644 --- a/js/libs/fluidbook/fluidbook.search.js +++ b/js/libs/fluidbook/fluidbook.search.js @@ -47,19 +47,31 @@ FluidbookSearch.prototype = { } var $this = this; loadJSLibrary('data/search.index.js', function () { - if ($this.highlightEnabled) { - loadJSLibrary('data/search.highlight.js', function () { + $this._loadTexts(function () { + if ($this.highlightEnabled) { + loadJSLibrary('data/search.highlight.js', function () { + $this.indexLoaded = true; + callback(); + }); + } else { $this.indexLoaded = true; callback(); - }); - } else { - $this.indexLoaded = true; - callback(); - } + } + }); }); }, + + _loadTexts: function (callback) { + if (this.fluidbook.datas.searchWordSelectionAlgorithm == 'expression') { + loadJSLibrary('data/search.texts.js', function () { + callback(); + }); + } else { + callback(); + } + }, _getHints: function (q, callback) { - var words = this.normalizeQuery(q); + var words = this.normalizeQuery(q, true); q = words.pop(); var res = []; if (q.length < 3) { @@ -78,16 +90,29 @@ FluidbookSearch.prototype = { res.sort(this.sortHints); callback(res.slice(0, 12)); }, - _find: function (q, callback) { - var words = this.normalizeQuery(q); + getSearchWordSelectionAlgorithm: function (q) { + var a = this.fluidbook.datas.searchWordSelectionAlgorithm; + if (q.indexOf(' ') === -1 && a == 'expression') { + a = 'begins'; + } + return a; + }, + + _find: function (q, callback) { + var algo = this.getSearchWordSelectionAlgorithm(q); + if (algo == 'expression') { + return this._findExpression(q, callback); + } + var words = this.normalizeQuery(q, true); var res = {}; var terms = []; var total = 0; var doublesPages = []; this.resultPages = []; - var q, v, k, kk, word, wordata, page, occurences; + var q, v, k, kk, word, wordata, page, occurences, p; + for (kk in words) { q = words[kk]; @@ -97,17 +122,16 @@ FluidbookSearch.prototype = { continue; } - if (this.fluidbook.datas.searchWordSelectionAlgorithm == 'begins' && k.indexOf(q) != 0) { + if (algo == 'begins' && k.indexOf(q) != 0) { continue; - } else if (this.fluidbook.datas.searchWordSelectionAlgorithm == 'contains' && k.indexOf(q) == -1) { + } else if (algo == 'contains' && k.indexOf(q) == -1) { continue; - } else if (this.fluidbook.datas.searchWordSelectionAlgorithm == 'exact' && k != q) { + } else if (algo == 'exact' && k != q) { continue; } v = INDEX[k]; for (page in v.p) { var occurences = v.p[page]; - console.log(page + ' -> ' + occurences); page = parseInt(page); if ((page % 2) == 1) { page--; @@ -150,16 +174,67 @@ FluidbookSearch.prototype = { callback(returnVal); }, + + _findExpression: function (q, callback) { + q = this.normalizeQuery(q, false); + var words = this.normalizeQuery(q, true); + var res = {}; + var terms = []; + var total = 0; + var doublesPages = []; + + 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 % 2) == 1) { + page--; + } + + doublesPages[page] = []; + doublesPages[page][q] = nb; + + res[page] = nb; + total += nb; + } + if (total > 0) { + terms = words; + } + var returnVal = { + total: total, + results: res, + terms: terms + }; + + console.log(returnVal); + + callback(returnVal); + }, + sortHints: function (a, b) { return b[1] - a[1]; }, kill: function () { }, - normalizeQuery: function (q) { + normalizeQuery: function (q, split) { q = this.noAccents(q); q = q.toLowerCase(); - return q.split(' '); + if (split) { + return q.split(' '); + } + return q; + }, noAccents: function (source) { source = source.replace(/[àáâãäå]/g, "a"); @@ -197,23 +272,31 @@ FluidbookSearch.prototype = { if (terms.length == 0) { return; } + var algo = this.getSearchWordSelectionAlgorithm(terms.join(' ')); + if (algo == 'expression') { + return; + } for (var t in terms) { var term = terms[t]; for (var w in HIGHLIGHTS) { - if (w.length < this.fluidbook.datas.ignoreWordLimit) { + if (algo != 'expression' && w.length < this.fluidbook.datas.ignoreWordLimit) { continue; } - if (this.fluidbook.datas.searchWordSelectionAlgorithm == 'begins' && w.indexOf(term) != 0) { + if (algo == 'begins' && w.indexOf(term) != 0) { continue; - } else if (this.fluidbook.datas.searchWordSelectionAlgorithm == 'contains' && w.indexOf(term) == -1) { + } else if (algo == 'contains' && w.indexOf(term) == -1) { continue; - } else if (this.fluidbook.datas.searchWordSelectionAlgorithm == 'exact' && w != term) { + } else if (algo == 'exact' && w != term) { continue; } + var color = t; + if (algo == 'expression') { + color = 0; + } - var h = {occurences: HIGHLIGHTS[w], color: t, word: w, term: term}; + var h = {occurences: HIGHLIGHTS[w], color: color, word: w, term: term}; this.highlights.push(h); } } @@ -296,7 +379,12 @@ FluidbookSearch.prototype = { 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 + 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 transform = {}; if (position.rotation) { transform.rotate = -position.rotation + 'deg'; -- 2.39.5