From 7bf64709abcf09832a537a2d438245e572085fe8 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 11 Oct 2017 19:20:51 +0200 Subject: [PATCH] fix #1641 @3 --- js/libs/fluidbook/fluidbook.js | 39 ------- js/libs/fluidbook/fluidbook.menu.js | 80 ------------- js/libs/fluidbook/fluidbook.nav.js | 119 +------------------- js/libs/fluidbook/fluidbook.resize.js | 33 +++++- js/libs/fluidbook/fluidbook.search.js | 130 +++++++++++++++++++++- js/libs/fluidbook/menu/fluidbook.index.js | 4 + 6 files changed, 163 insertions(+), 242 deletions(-) diff --git a/js/libs/fluidbook/fluidbook.js b/js/libs/fluidbook/fluidbook.js index b7f5ca73..d73b637f 100644 --- a/js/libs/fluidbook/fluidbook.js +++ b/js/libs/fluidbook/fluidbook.js @@ -740,45 +740,6 @@ Fluidbook.prototype = { $('#menuList > ul > li, #shareLinks').fadeIn(300); }, - resizeMenu: function () { - - //console.warn('calling resizeSR'); - //console.log('menu open?', this.nav.menuIsOpen); - - if (!fluidbook.nav.menuIsOpen) return false; - - var wh = $(window).height(), - formHeight = $('#searchForm').height(); - marginTop = marginBottom = Math.min(Math.round(wh * 0.075), 50); // Relative margins with a max of 50 - searchResultsMaxHeight = wh - formHeight - marginTop - marginBottom, - mainMenuMaxHeight = wh - $('#menuSearch').height() - $('#shareLinks').outerHeight(); - - // Search results element may not exist when resize is called - if (fluidbook.search.menuSearchResults == undefined) { - fluidbook.search.initSearchResults(); - return false; // initSearchResults will call this resize function anyway - } - - // Max-height for the top level of the main menu so it can scroll between the search box and sharing bar - $('#menuList > ul').css('maxHeight', mainMenuMaxHeight); - - // Set max-height for search results section (using CSS vh units was unreliable) - if (fluidbook.search.menuSearchResults !== undefined) { - fluidbook.search.menuSearchResults.css('maxHeight', searchResultsMaxHeight); - fluidbook.search.menuSearchResults.css('marginTop', marginTop); - fluidbook.search.menuSearchResults.perfectScrollbar('update'); - } - - // Also set same max-height for search hints - if (fluidbook.search.menuSearchHints !== undefined) { - fluidbook.search.menuSearchHints.css('maxHeight', wh - formHeight); - fluidbook.search.menuSearchHints.perfectScrollbar('update'); - } - }, - - - - print: function () { var $this = this; var pdf; diff --git a/js/libs/fluidbook/fluidbook.menu.js b/js/libs/fluidbook/fluidbook.menu.js index a1db912a..a86b891e 100644 --- a/js/libs/fluidbook/fluidbook.menu.js +++ b/js/libs/fluidbook/fluidbook.menu.js @@ -76,16 +76,6 @@ FluidbookMenu.prototype = { }; if (view == 'index') { this.openIndex(this.fluidbook.l10n.__('overview'), undefined, true, cb); - } else if (view == 'search') { - this.searchString = param1; - var group = param2; - $("#q").val(param1); - this.fluidbook.search.find(param1, function (r) { - $this.displayResults(r, group, cb); - }); - if (group == undefined) { - this.fluidbook.stats.track(1, 0, param1); - } } else if (view == 'video') { this.openVideo(param1, cb); } else if (view == 'audio') { @@ -137,76 +127,6 @@ FluidbookMenu.prototype = { resize(); }, 420); }, - - displayResults: function (data, group, callback) { - - var $this = this; - var results = data.results; - var hideNoResults = !this.fluidbook.datas.searchShowNoResultsPages; - if (data.total <= 0) { - alert(this.fluidbook.l10n.__('no result found')); - window.location = "#/page/" + this.fluidbook.currentPage; - return; - } - - this.openIndex(this.fluidbook.l10n.__('search results for') + ' « ' + this.searchString + " »", group, true, function () { - var hits = []; - for (var i = 0; i <= $this.fluidbook.datas.pages; i++) { - hits[i] = 0; - } - - $.each(results, function (k, v) { - hits[k] += v; - }); - var e = encodeURIComponent($this.searchString); - $(".mview .doubleThumb").each(function () { - var pages = $(this).data('pages').toString().split(','); - if (pages.indexOf('1') != -1) { - pages.push('0'); - } - var hitsp = 0; - var pagesWithHits = []; - for (var i in pages) { - var n = parseInt(pages[i]); - if (hits[n] > 0) { - hitsp += hits[n]; - pagesWithHits.push(pages[i]); - } - } - - if ($(this)) - if ($(this).find('.hits').length > 0) { - return; - } - - if (hitsp == 0) { - if (hideNoResults) { - $(this).remove(); - return; - } - $(this).append('
'); - $(this).append('
' + $this.fluidbook.l10n.__('no result found') + '
'); - } else { - $(this).append('
' + hitsp + ' ' + $this.fluidbook.l10n.__('hit(s)') + '
'); - if ($this.fluidbook.pad.enabled) { - if (pagesWithHits.length == 1) { - $(this).find('a').attr('href', '#/page/' + pagesWithHits[0]); - } else { - $(this).find('a').attr('href', '#/search/' + e + '/' + $(this).attr('page')); - } - } else { - $(this).find('a').attr('href', '#/page/' + $(this).attr('page')); - } - } - }); - $this.fluidbook.search.setHighlightTerms(data.terms); - $this.fluidbook.search.highlightSearchTerms($this.fluidbook.currentPage); - if (callback != undefined) { - callback(); - } - }); - } - , openMultimedia: function (multimedia, callback) { var a = $('a[href="#/multimedia/' + multimedia + '"]'); var markup = decodeURIComponent($(a).attr('data-multimedia')); diff --git a/js/libs/fluidbook/fluidbook.nav.js b/js/libs/fluidbook/fluidbook.nav.js index 7bdf1428..db4e3153 100644 --- a/js/libs/fluidbook/fluidbook.nav.js +++ b/js/libs/fluidbook/fluidbook.nav.js @@ -52,7 +52,7 @@ FluidbookNav.prototype = { $('body').addClass('menu-open'); $this.menuIsOpen = true; - setInterval($this.fluidbook.resizeMenu, 500); + setInterval($this.fluidbook.resize.resizeMenu, 500); //$('#q').focus(); // Disabled for now because it triggers the keyboard to open on some mobile devices }); @@ -74,7 +74,7 @@ FluidbookNav.prototype = { }); // Recalculate available size for menu search results - $(window).on('fluidbookresize', fluidbook.resizeMenu); + $(window).on('fluidbookresize', this.fluidbook.resize.resizeMenu); // Handle swipe to close (it's only really practical to have this because swipe to open would interfere with main Fluidbook swipes) // We are not using MMenu's "Drag" add-on because it doesn't close the main menu, only submenus on swipe. @@ -653,120 +653,7 @@ FluidbookNav.prototype = { // Search form handler $(document).on('submit', '#searchForm', function () { - var q = $("#q").val(); - - if (q == '') { - return false; - } - - $this.fluidbook.search.find(q, function (results) { - - // Handle empty result set - if (results.total <= 0) { - fluidbook.search.menuSearchResults.html('
' + this.fluidbook.l10n.__('no result found') + '
'); - fluidbook.search.hideSearchHints(); - fluidbook.search.menuSearchResults.fadeIn(300); - return false; - } - - var hits = []; - - // Create a list of all pages so we can record which ones have a hit on the search term - for (var i = 0; i <= $this.fluidbook.datas.pages; i++) { - hits[i] = 0; - } - // Map result hits to pages - $.each(results.results, function (k, v) { - hits[k] += v; - }); - - // ToDo: check the purpose of the group variable? Also check tracking - //$this.displayResults(r, group, cb); - //console.log('Search results...'); - //console.log(results); - - // Display results - //res += $this.fluidbook.l10n.__('search results for') + ' « ' + q + " »" - - // Use the index of all pages as a starting point to filter results thumbnails - fluidbook.search.menuSearchResults.html($this.fluidbook.menu.index.getView()).hide(); - - // Process each spread of pages to collect the ones with results - fluidbook.search.menuSearchResults.find('.doubleThumb').each(function () { - - var currentPages = $(this).data('pages').toString().split(','), - totalHits = 0, - pagesWithHits = []; - - // Get total number of hits for pages in this spread - for (var i in currentPages) { - var pageNum = parseInt(currentPages[i]); - if (hits[pageNum] > 0) { - totalHits += hits[pageNum]; - pagesWithHits.push(currentPages[i]); - } - } - - - // If there's already a .hits element, this spread has already been processed - if ($(this).find('.hits').length > 0) { - return; - } - - if (totalHits == 0) { - // Should pages with no results still be shown? - if (fluidbook.datas.searchShowNoResultsPages) { - $(this).append('
'); - $(this).append('
' + $this.fluidbook.l10n.__('no result found') + '
'); - } else { - $(this).remove(); - return; - } - } else { - // Add the link to the main result page. Link around the hits div is for better usability (previously wasn't clickable) - $(this).append('
' + totalHits + ' ' + fluidbook.l10n.__('hit(s)') + '
'); - if (fluidbook.pad.enabled) { - if (pagesWithHits.length == 1) { - $(this).find('a').attr('href', '#/page/' + pagesWithHits[0]); - } else { - $(this).find('a').attr('href', '#/search/' + e + '/' + $(this).attr('page')); - } - } else { - $(this).find('a').attr('href', '#/page/' + $(this).attr('page')); - } - } - - }); - - // Set Highlighting of results keywords - $this.fluidbook.search.setHighlightTerms(results.terms); - $this.fluidbook.search.highlightSearchTerms($this.fluidbook.currentPage); - - - fluidbook.search.hideSearchHints(); - fluidbook.search.menuSearchResults.fadeIn(300).scrollTop(0); - - // On some phones, the height of the search results is miscalculated due to the - // keyboard being open and reducing the available window height. We need to wait until - // the keyboard closes before trying to resize again... - setTimeout(fluidbook.resizeMenu, 500); - - - // Initialise scrollbar after populating so bars appear immediately - fluidbook.search.menuSearchResults.perfectScrollbar({ - suppressScrollX: true, - minScrollbarLength: 40, - //maxScrollbarLength: 60 - }); - - }); - - // window.location.hash = '/search/' + q; - // - // if ($this.menuIsOpen) { - // $this.menuAPI.close(); - // } - + $this.fluidbook.search.submitForm(); return false; }); diff --git a/js/libs/fluidbook/fluidbook.resize.js b/js/libs/fluidbook/fluidbook.resize.js index 8bb55da0..c11fd29e 100644 --- a/js/libs/fluidbook/fluidbook.resize.js +++ b/js/libs/fluidbook/fluidbook.resize.js @@ -170,7 +170,7 @@ FluidbookResize.prototype = { // Scale horizontal size and positioning. Transform origin is handled in CSS for LTR/RTL variations $('#horizontalNav').css({ - 'transform': 'translateY(-'+ 50 * navScale +'%) scale('+ navScale +')' + 'transform': 'translateY(-' + 50 * navScale + '%) scale(' + navScale + ')' }) var headerHeight = this.fluidbook.datas.menuHeight * navScale; @@ -312,5 +312,36 @@ FluidbookResize.prototype = { }, getScreenFluidbookWidth: function () { return $("#fluidbook").outerWidth() * fluidbook.resize.bookScale; + }, + resizeMenu: function () { + if (!this.fluidbook.nav.menuIsOpen) return false; + + var wh = $(window).height(), + formHeight = $('#searchForm').height(); + marginTop = marginBottom = Math.min(Math.round(wh * 0.075), 50); // Relative margins with a max of 50 + searchResultsMaxHeight = wh - formHeight - marginTop - marginBottom, + mainMenuMaxHeight = wh - $('#menuSearch').height() - $('#shareLinks').outerHeight(); + + // Search results element may not exist when resize is called + if (this.fluidbook.search.menuSearchResults == undefined) { + this.fluidbook.search.initSearchResults(); + return false; // initSearchResults will call this resize function anyway + } + + // Max-height for the top level of the main menu so it can scroll between the search box and sharing bar + $('#menuList > ul').css('maxHeight', mainMenuMaxHeight); + + // Set max-height for search results section (using CSS vh units was unreliable) + if (this.fluidbook.search.menuSearchResults !== undefined) { + this.fluidbook.search.menuSearchResults.css('maxHeight', searchResultsMaxHeight); + this.fluidbook.search.menuSearchResults.css('marginTop', marginTop); + this.fluidbook.search.menuSearchResults.perfectScrollbar('update'); + } + + // Also set same max-height for search hints + if (this.fluidbook.search.menuSearchHints !== undefined) { + this.fluidbook.search.menuSearchHints.css('maxHeight', wh - formHeight); + this.fluidbook.search.menuSearchHints.perfectScrollbar('update'); + } } }; \ No newline at end of file diff --git a/js/libs/fluidbook/fluidbook.search.js b/js/libs/fluidbook/fluidbook.search.js index d17276d3..4e34127e 100644 --- a/js/libs/fluidbook/fluidbook.search.js +++ b/js/libs/fluidbook/fluidbook.search.js @@ -76,7 +76,7 @@ FluidbookSearch.prototype = { var res = {}; var terms = []; var total = 0; - var doublesPages = {}; + var doublesPages = []; this.resultPages = []; var q, v, k, kk, word, wordata, page, occurences; @@ -99,14 +99,16 @@ FluidbookSearch.prototype = { 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--; } - if (doublesPages[page] == undefined) { - doublesPages[page] = {}; + + if (doublesPages[page] == null || doublesPages[page] == undefined) { + doublesPages[page] = []; } - if (doublesPages[page][kk] == undefined) { + if (doublesPages[page][kk] == null || doublesPages[page][kk] == undefined) { doublesPages[page][kk] = 0; } doublesPages[page][kk] += occurences; @@ -245,7 +247,7 @@ FluidbookSearch.prototype = { } } - if(this.fluidbook.l10n.dir=='rtl'){ + if (this.fluidbook.l10n.dir == 'rtl') { pageNrs.reverse(); } @@ -298,7 +300,7 @@ FluidbookSearch.prototype = { initSearchResults: function () { this.menuSearchResults = $('#menuSearchResults'); - this.fluidbook.resizeMenu(); + this.fluidbook.resize.resizeMenu(); this.menuSearchResults.hide(); }, @@ -379,5 +381,121 @@ FluidbookSearch.prototype = { this.clearHighlights() this.fluidbook.showMenuItems(); // Show menu items that were hidden previously }, + + submitForm: function () { + var q = $("#q").val(); + + if (q == '') { + return false; + } + + var $this = this; + this.find(q, function (results) { + $this.openResults(results); + }); + + + }, + + openResults: function (results) { + var $this = this; + + // Handle empty result set + if (results.total <= 0) { + this.menuSearchResults.html('
' + this.fluidbook.l10n.__('no result found') + '
'); + this.hideSearchHints(); + this.menuSearchResults.fadeIn(300); + return false; + } + + var hits = []; + + // Create a list of all pages so we can record which ones have a hit on the search term + for (var i = 0; i <= this.fluidbook.datas.pages; i++) { + hits[i] = 0; + } + // Map result hits to pages + $.each(results.results, function (k, v) { + hits[k] += v; + }); + + // ToDo: check the purpose of the group variable? Also check tracking + //$this.displayResults(r, group, cb); + //console.log('Search results...'); + //console.log(results); + + // Display results + + // Use the index of all pages as a starting point to filter results thumbnails + this.menuSearchResults.html(this.fluidbook.menu.index.getView()).hide(); + + // Process each spread of pages to collect the ones with results + this.menuSearchResults.find('.doubleThumb').each(function () { + var currentPages = $(this).data('pages').toString().split(','), + totalHits = 0, + pagesWithHits = []; + + // Get total number of hits for pages in this spread + for (var i in currentPages) { + var pageNum = parseInt(currentPages[i]); + if (hits[pageNum] > 0) { + totalHits += hits[pageNum]; + pagesWithHits.push(currentPages[i]); + } + } + + + // If there's already a .hits element, this spread has already been processed + if ($(this).find('.hits').length > 0) { + return; + } + + if (totalHits == 0) { + // Should pages with no results still be shown? + if ($this.fluidbook.datas.searchShowNoResultsPages) { + $(this).append('
'); + $(this).append('
' + $this.fluidbook.l10n.__('no result found') + '
'); + } else { + $(this).remove(); + return; + } + } else { + // Add the link to the main result page. Link around the hits div is for better usability (previously wasn't clickable) + $(this).append('
' + totalHits + ' ' + $this.fluidbook.l10n.__('hit(s)') + '
'); + if ($this.fluidbook.pad.enabled) { + if (pagesWithHits.length == 1) { + $(this).find('a').attr('href', '#/page/' + pagesWithHits[0]); + } else { + $(this).find('a').attr('href', '#/search/' + e + '/' + $(this).attr('page')); + } + } else { + $(this).find('a').attr('href', '#/page/' + $(this).attr('page')); + } + } + + }); + + // Set Highlighting of results keywords + this.setHighlightTerms(results.terms); + this.highlightSearchTerms(this.fluidbook.currentPage); + + + this.hideSearchHints(); + this.menuSearchResults.fadeIn(300).scrollTop(0); + + // On some phones, the height of the search results is miscalculated due to the + // keyboard being open and reducing the available window height. We need to wait until + // the keyboard closes before trying to resize again... + setTimeout(function () { + this.fluidbook.resize.resizeMenu(); + }, 500); + + // Initialise scrollbar after populating so bars appear immediately + this.menuSearchResults.perfectScrollbar({ + suppressScrollX: true, + minScrollbarLength: 40, + //maxScrollbarLength: 60 + }); + } }; diff --git a/js/libs/fluidbook/menu/fluidbook.index.js b/js/libs/fluidbook/menu/fluidbook.index.js index 60de6154..8a2232d6 100644 --- a/js/libs/fluidbook/menu/fluidbook.index.js +++ b/js/libs/fluidbook/menu/fluidbook.index.js @@ -73,6 +73,10 @@ FluidbookIndex.prototype = { c = ' simple ' + s2; } + if (j == 1) { + pages.unshift(0); + } + ix = ix1 + ix2; this.normalHTML += '
' + ix; this.normalHTML += '
'; -- 2.39.5