function FluidbookSearch(fluidbook) {
this.fluidbook = fluidbook;
this.indexLoaded = false;
+ this.indexLoading = false;
this.termsToHighlight = [];
this.highlights = [];
this.highlightEnabled = fluidbook.settings.highlightResults;
},
_loadLib: function (callback) {
- if (this.indexLoaded) {
+ if (this.indexLoading || this.indexLoaded) {
return callback();
}
+ this.indexLoaded = true;
var $this = this;
+ var __callback = function () {
+ if (this.fluidbook.settings.searchWords) {
+ $this._addLinkWords();
+ }
+ $this.indexLoaded = true;
+ $this.indexLoading = false;
+ callback();
+ };
+
let __loadTexts = function () {
$this._loadTexts(function () {
if ($this.highlightEnabled) {
loadJSLibrary($this.fluidbook.loader.getURL('data/search.highlight.js?j=' + this.fluidbook.settings.cacheDate), function () {
- $this.indexLoaded = true;
- callback();
+ __callback();
});
} else {
- $this.indexLoaded = true;
- callback();
+ __callback();
}
});
};
}
},
+ _addLinkWords: function () {
+ let $this = this;
+ let z = 1;
+ // Add words added by links to the INDEX
+ $.each(this.fluidbook.settings.searchWords, function (word, data) {
+ if (INDEX[word] === undefined) {
+ INDEX[word] = {t: 0, p: {}};
+ }
+
+ $.each(data, function (k, d) {
+ INDEX[word].t++;
+ if (INDEX[word].p[d.page] === undefined) {
+ INDEX[word].p[d.page] = 0;
+ }
+ INDEX[word].p[d.page]++;
+ });
+ });
+
+ // Add words to the HIGHLIGHT data
+ $.each(this.fluidbook.settings.searchWords, function (word, data) {
+ if (HIGHLIGHTS[word] === undefined) {
+ HIGHLIGHTS[word] = [];
+ }
+
+ $.each(data, function (k, d) {
+ let lw = [];
+ let letters = word.split('');
+ $.each(letters, function (kk, letter) {
+ lw.push(((d.coords.width * z) / letters.length));
+ });
+ HIGHLIGHTS[word].push({
+ page: d.page,
+ y: (d.coords.y + d.coords.height) * z,
+ x: (d.coords.x) * z,
+ width: d.coords.width * z,
+ height: d.coords.height * z,
+ scaleY: 1,
+ lw: lw,
+ padding: 0,
+ });
+ });
+ });
+ },
+
_loadTexts: function (callback) {
if (this.robust || this.fluidbook.settings.searchWordSelectionAlgorithm === 'expression') {
loadJSLibrary(this.fluidbook.loader.getURL('data/search.texts.js?j=' + this.fluidbook.settings.cacheDate), function () {
}
}
- var padding = position.height * 0.2;
+
+ var padding = position.padding == undefined ? position.height * 0.2 : position.padding;
+
var z = this.fluidbook.settings.cssScale;
- var h = $('<div class="highlight' + (this.robust ? ' robust' : '') + '" data-color="' + color + '"></div>');
+
var coords = {
top: (position.y - position.height - padding * 2) * z,
left: (position.x + x - padding) * z + offset,
allCoords.push(coords);
}
+ console.log(allCoords);
+
$.each(allCoords, function (i, coords) {
var hash = coords.top + '|' + coords.left + '|' + coords.width + '|' + coords.height;
if ($('[data-hash="' + hash + '"]').length > 0) {
return;
}
+ var h = $('<div class="highlight' + ($this.robust ? ' robust' : '') + '" data-color="' + color + '"></div>');
$(h).attr('data-hash', hash);
$(h).css(coords).transform(transform);
if ($this.fluidbook.elasticslide.isActive()) {