From: Vincent Vanwaelscappel Date: Fri, 14 Mar 2014 18:09:09 +0000 (+0000) Subject: (no commit message) X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=ffcb31e00353db932bde296a2bc8e7f9b96ef8b9;p=fluidbook-html5.git --- diff --git a/js/libs/fluidbook/fluidbook.js b/js/libs/fluidbook/fluidbook.js index b739e5a9..37e97d27 100644 --- a/js/libs/fluidbook/fluidbook.js +++ b/js/libs/fluidbook/fluidbook.js @@ -1,1463 +1,1397 @@ -function Fluidbook(datas) { - this.init(datas); -} - -// Constantes -// Background dispoition -Fluidbook.NONE = 3; -Fluidbook.STRETCH = 0; -Fluidbook.RATIO = 2; -Fluidbook.REPEAT = 1; -// Horizontal alignments -Fluidbook.CENTER = 4; -Fluidbook.LEFT = 5; -Fluidbook.RIGHT = 6; -// Vertical alignments -Fluidbook.MIDDLE = 7; -Fluidbook.TOP = 8; -Fluidbook.BOTTOM = 9; - -Fluidbook.prototype = { - init: function(datas) { - this.datas = datas; - this.junk = datas.cacheDate; - this.cache = new FluidbookCache(datas); - this.service = new FluidbookService(this, datas.id); - this.support = new FluidbookSupport(this); - this.loader = new FluidbookLoader(this); - this.search = new FluidbookSearch(this); - this.pad = new FluidbookPad(this); - this.waiters = []; - if (Modernizr.ftouch && this.support.transitions2d && this.datas.mobileTransitions != 'none') { - this.touch = new FluidbookTouch(this); - } - this.background = new FluidbookBackground(this); - this.viewport = new FluidbookViewport(this.support) - this.l10n = new FluidbookL10N(this, $_GET['lang']); - - this.video = new FluidbookVideo(this); - this.bookmarks = new FluidbookBookmarks(this); - this.index = new FluidbookIndex(this); - this.tooltip = new FluidbookTooltip(this); - if (this.datas.form == 'bulle') { - this.form = new FluidbookBulleForm(this); - } else { - this.form == false; - } - this.refw = 0; - this.refh = 0; - this.zoom = 1; - this.searchHintXHR = null; - this.searchString = ''; - this.termsToHighlight = ''; - this.imagesVersion = (this.datas.mobileVersion == 'html5-images' || !this.support.SVG); - this.vectorTexts = !this.imagesVersion; - this.displayOnePage = false; - this.indexHTML = ''; - this.transitionning = false; - this.gal = null; - this.isReady = false; - - this.transitionAxis = 'x'; - - if (this.support.isMobile) { - $("body").addClass('mobile'); - } - else { - $("body").addClass('desktop'); - } - - if (this.pad.enabled) { - $("body").addClass('pad'); - } - $('html').addClass(this.datas.mobileLVersion); - - this.currentPage = -1; - - this.resize = new FluidbookResize(this); - this.coquillette = new FluidbookCoquillette(this); - - this.stats = new FluidbookStats(this); - this.stats.track(10); - if (!this.support.isMobile) { - this.desktop = new FluidbookDesktop(this); - } - - this.initLoading(); - - }, - initLoading: function() { - this.displayLoader(); - - }, - reloadErrorImages: function() { - $('img').each(function() { - - }); - }, - hideSplash: function() { - if ($("#splash").length == 0) { - return; - } - try { - navigator.splashscreen.hide(); - } catch (err) { - } - - this.loader.canPreload(); - var $this = this; - setTimeout(function() { - $this._hideSplash(); - }, 3000); - }, - _hideSplash: function() { - this.ready(); - $("#main").css('visibility', 'visible'); - this.hideLoader(0, true); - if (this.support.transitions2d) { - $("#splash").css('opacity', 0).one(this.support.getTransitionEndEvent(true), function() { - $(this).remove(); - }); - } else { - $("#splash").fadeOut(500, function() { - $(this.remove()); - }) - } - - }, - ready: function() { - if (this.isReady) { - return; - } - this.isReady = true; - $("#main").css('display', 'block'); - this.nav = new FluidbookNav(this); - this.help = new FluidbookHelp(this); - this.help.displayAtStartup(); - resize(); - $(this).trigger('fluidbookready'); - }, - loadPlugins: function() { - $.each(this.datas.plugins, function(k, plugin) { - try { - var functionName = plugin.replace(/\./g, '_'); - eval(functionName + '();'); - } catch (err) { - } - }); - $.each(this.datas.htmlmultimedia, function(k, code) { - try { - eval(code); - } catch (err) { - } - }); - }, - resetZoom: function() { - var $this = this; - if (this.support.iOS) { - } else if (!this.support.isMobile) { - this.desktop.updateDesktopScale(1); - } - - }, - setZoom: function(zoom) { - if (zoom) { - $("#pages").addClass('zoom'); - } else { - $("#pages").removeClass('zoom'); - } - }, - initPage: function(pageNr, doublePage, position) { - if ($("#page_" + pageNr).length > 0) { - return; - } - $(doublePage).find('.' + position).remove(); - - var page = '
'; - $(doublePage).append(page); - }, - hidePage: function(position) { - $("#pages ." + position).hide(); - }, - initLinks: function(pageNr) { - if (pageNr == undefined) { - pageNr = this.currentPage; - } - - var lClass = 'left'; - if (this.displayOnePage && pageNr % 2 == 1) { - lClass = 'right'; - } - - if (pageNr % 2 == 1) { - pageNr--; - } - - $("#links").removeClass('left').removeClass('right').addClass(lClass).html(this.datas.links[pageNr]).show(); - if (this.datas.bookmark) { - $("#links").append(this.bookmarks.getBookmarkForPage(pageNr)); - } - $("#links").prepend(''); - - var $this = this; - if (this.datas.linkBlinkTime > 0) { - ///this.animateLinks(); - } - setTimeout(function() { - $this.initVideos(); - }, 1000); - }, - animateLinks: function() { - var links = $(".link a.displayArea"); - var bookmarks = $("#links .bookmark:not([data-enabled])"); - if (Modernizr.csstransitions) { - $(links).addClass('animating'); - $(bookmarks).css('opacity', 1).addClass('animating'); - setTimeout(function() { - $(links).css('opacity', 0); - $(bookmarks).css('opacity', 0); - setTimeout(function() { - $(links).removeClass('animating').css('opacity', 1); - $(bookmarks).removeClass('animating').css('opacity', ""); - }, 1100); - }, 50); - } else { - $(links).addClass('animating').fadeOut(1000, function() { - $(links).removeClass('animating').show(); - }).mouseover(function() { - $(links).stop().removeClass('animating').css('opacity', 1).show(); - return true; - }); - } - }, - initVideos: function() { - var $this = this; - $(".videoContainer").each(function() { - $this.video.initVideo(this); - }); - }, - getNextOffset: function() { - var offset = 2; - if (this.displayOnePage) { - offset = 1; - } - this.transitionAxis = 'x'; - return offset; - }, - goNextPage: function() { - if (this.transitionning) { - return; - } - this.transitionAxis = 'x'; - this.setCurrentPage(this.normalizePage(this.currentPage) + this.getNextOffset()); - }, - goFirstPage: function() { - if (this.transitionning) { - return; - } - this.transitionAxis = 'x'; - this.setCurrentPage(1); - }, - goPreviousPage: function() { - if (this.transitionning) { - return; - } - this.transitionAxis = 'x'; - this.setCurrentPage(this.normalizePage(this.currentPage) - this.getNextOffset()); - }, - goLastPage: function() { - if (this.transitionning) { - return; - } - this.transitionAxis = 'x'; - this.setCurrentPage(this.datas.pages); - }, - goNextChapter: function() { - if (this.transitionning) { - return; - } - var next = this.bookmarks.getNextGroupCover(this.currentPage); - if (next === false) { - return; - } - this.transitionAxis = 'x'; - this.setCurrentPage(this.normalizePage(next)); - - }, - goPreviousChapter: function() { - if (this.transitionning) { - return; - } - var prev = this.bookmarks.getPreviousGroupCover(this.currentPage); - if (prev === false) { - return; - } - this.transitionAxis = 'x'; - this.setCurrentPage(this.normalizePage(prev)); - }, - goNextChapterPage: function() { - if (this.transitionning) { - return; - } - - var next = this.bookmarks.getNextPageInGroupOfPage(this.currentPage); - if (next === false) { - return; - } - this.transitionAxis = 'y'; - this.setCurrentPage(this.normalizePage(next)); - }, - goPreviousChapterPage: function() { - if (this.transitionning) { - return; - } - - var prev = this.bookmarks.getPreviousPageInGroupOfPage(this.currentPage); - if (prev === false) { - return; - } - this.transitionAxis = 'y'; - this.setCurrentPage(this.normalizePage(prev)); - }, - normalizePage: function(page) { - page = Math.max(1, Math.min(page, this.datas.pages)); - if (!this.displayOnePage && page % 2 == 1) { - page--; - } - return page; - }, - setCurrentPage: function(page) { - window.location.hash = "#/page/" + this.normalizePage(page); - }, - changeAddress: function() { - var $this = this; - var page; - var args = window.location.hash.split('/'); - if (args.length <= 1 || args[1] == '' || args[1] == undefined) { - return this.setCurrentPage('1'); - } else if (args[1] == 'page') { - page = parseInt(args[2]); - if (isNaN(page) || page == undefined) { - return this.setCurrentPage('1'); - } - if (this.pad.enabled) { - this.transitionAxis = this.pad.getTransitionAxis(this.currentPage, page); - } else { - this.transitionAxis = 'x'; - } - - $($this).trigger('changePage', [page]); - - this.closeView(function() { - $this.pageTransition(page); - $this.resetZoom(); - $this.stats.track(0, page); - $this.hideSplash(); - }, true); - } - else { - this.openView(args[1], args[2], args[3], function() { - $this.hideSplash(); - }); - this.resetZoom(); - } - - return; - }, - pageTransition: function(pageNr) { - this.tooltip.hideTooltip(); - if (pageNr == undefined) { - pageNr = this.currentPage; - if (pageNr == -1) { - pageNr = 1; - } - } - pageNr = this.normalizePage(pageNr); - - - if (pageNr == this.normalizePage(this.currentPage) || this.currentPage == -1 || !this.support.transitions2d || this.datas.mobileTransitions == 'none') { - return this.pageTransition1D(pageNr); - } - - if (this.displayOnePage) { - return this.pageTransition2DPortrait(pageNr); - } - - if (!this.support.transitions3d || this.datas.mobileTransitions == 'slide') { - return this.pageTransition2D(pageNr); - } - - return this.pageTransition3D(pageNr); - }, - pageTransition3D: function(pageNr) { - - var $this = this; - if ($("#pages").hasClass('_3dtransition')) { - return; - } - this.transitionning = true; - var $this = this; - - var turning = this.getTurningPages(pageNr); - - $("#pages").prepend('
'); - var doublePage = $("#nextDoublePage"); - var currentDoublePage = $("#currentDoublePage"); - - this.displayLoader(); - - this.loader.preloadPagesBeforeTransition([turning.currentLeft, turning.currentRight], function() { - $("#pages").addClass('_3dtransition'); - $this.loader.setContentsInDoublePage(currentDoublePage, turning.flat, true, function() { - $this.loader.setContentsInDoublePage(doublePage, turning.flip, true, function() { - $this.beforeTransition(pageNr, 3); - $(doublePage).addClass(turning.nextFromClass + 'end').one($this.support.getTransitionEndEvent(), function() { - if ($this.transitionning == false) { - return; - } - if ($("#nextDoublePage").length == 0) { - $("#pages").removeClass('_3dtransition'); - $this.transitionning = false; - return; - } - - // Set the flat contents with the new page - $this.loader.setContentsInDoublePage(doublePage, turning.end, false, function() { - $(doublePage).removeClass('_3d').removeClass(turning.nextFromClass + 'start').removeClass(turning.nextFromClass + 'end'); - // Remove former part - $("#currentDoublePage").remove(); - $(doublePage).attr('id', 'currentDoublePage'); - - $("#pages").removeClass('_3dtransition'); - $this.afterTransition(pageNr); - - }); - - }); - }); - }); - }); - }, - getTurningPages: function(newPage) { - var res = {}; - - res.dir = 1; - if (newPage < this.currentPage) { - res.dir = -1; - } - - if (this.l10n.dir == 'ltr') { - res.currentLeft = this.currentPage - this.currentPage % 2; - res.currentRight = res.currentLeft + 1; - if (res.dir == 1) { - res.currentToClass = 'prev'; - res.nextFromClass = 'next'; - res.flat = [res.currentLeft, newPage + 1]; - res.flip = [newPage, res.currentRight]; - } else { - res.currentToClass = 'next'; - res.nextFromClass = 'prev'; - res.flat = [newPage, res.currentRight]; - res.flip = [res.currentLeft, newPage + 1]; - } - res['end'] = [newPage, newPage + 1]; - } else { - res.currentRight = this.currentPage - this.currentPage % 2; - res.currentLeft = res.currentRight + 1; - - if (res.dir == 1) { - res.currentToClass = 'next'; - res.nextFromClass = 'prev'; - res.flat = [newPage + 1, res.currentRight]; - res.flip = [res.currentLeft, newPage]; - } else { - res.currentToClass = 'prev'; - res.nextFromClass = 'next'; - res.flat = [res.currentLeft, newPage]; - res.flip = [newPage + 1, res.currentRight]; - } - - if (this.displayOnePage) { - res.end = [newPage, newPage + 1]; - } else { - res.end = [newPage + 1, newPage]; - } - } - res.loading = [res.currentLeft, res.currentRight]; - return json_parse(JSON.stringify(res)); - }, - reloadCurrentPage: function() { - this.pageTransition(this.currentPage); - }, - readingPage: function(side) { - if (!this.displayOnePage) { - var page = this.currentPage; - var change = false; - if (side == 'left' && page % 2 == 1) { - page--; - change = true; - } else if (side == 'right' && page % 2 == 0) { - page++; - change = true; - } - if (change) { - window.location.hash = "/page/" + page; - } - } - }, - hideUnnecessaryButtons: function(page) { - var speed = 500; - if (page == undefined) { - page = this.currentPage; - speed = 0; - } - - if (Modernizr.csstransitions) { - if (page <= 1) { - $("#previous").addClass('hidden'); - } else { - $("#previous").removeClass('hidden'); - } - - if (page >= this.datas.pages) { - $("#next").addClass('hidden'); - } else { - $("#next").removeClass('hidden'); - } - } else { - $("#previous,#next").removeClass('hidden'); - - if (page <= 1) { - $("#previous:visible").fadeOut(speed); - } else { - $("#previous:hidden").fadeIn(speed); - } - - if (page >= this.datas.pages) { - $("#next:visible").fadeOut(speed); - } else { - $("#next:hidden").fadeIn(speed); - } - } - }, - updateShadows: function(page, animationDuration) { - if (animationDuration == undefined) { - animationDuration = 0; - } - animationDuration *= 1000; - - var delay, duration; - if (animationDuration == 0) { - delay = 0; - duration = 0; - } else { - delay = animationDuration * 0.7; - duration = animationDuration * 0.3; - } - - var left = true, right = true; - if (this.displayOnePage) { - right = false; - } else { - if ((page <= 1 && this.l10n.dir == 'ltr') || (page >= this.datas.pages && this.l10n.dir == 'rtl')) { - left = false; - } else if ((page <= 1 && this.l10n.dir == 'rtl') || (page >= this.datas.pages && this.l10n.dir == 'ltr')) { - right = false; - } - } - - - if (left) { - $("#shadow>.left:hidden").delay(delay).fadeIn(duration); - } else { - $("#shadow>.left:visible").fadeOut(duration); - } - - if (right) { - $("#shadow>.right:hidden").delay(delay).fadeIn(duration); - } else { - $("#shadow>.right:visible").fadeOut(duration); - } - }, - showAllButtons: function() { - $("#next,#previous").addClass('help').show(); - }, - pageTransition2D: function(pageNr) { - this.transitionning = true; - var $this = this; - - var turning = this.getTurningPages(pageNr); - - $("#pages").append('
'); - var doublePage = $("#nextDoublePage"); - this.displayLoader(); - this.loader.preloadPagesBeforeTransition(turning.end, function() { - $this.loader.setContentsInDoublePage(doublePage, turning.end, true, function() { - $this.beforeTransition(pageNr, 2); - $("#currentDoublePage").addClass('_2d').addClass('axis_' + $this.transitionAxis).addClass(turning.currentToClass); - $(doublePage).removeClass(turning.nextFromClass).one($this.support.getTransitionEndEvent(), function(event) { - if ($this.transitionning == false) { - return; - } - $("#currentDoublePage").remove(); - $("#nextDoublePage").attr('id', 'currentDoublePage'); - $this.afterTransition(pageNr); - }); - }); - }); - }, - pageTransition2DPortrait: function(pageNr) { - this.transitionning = true; - var $this = this; - - var turning = this.getTurningPages(pageNr); - - $("#pages").append('
'); - var doublePage = $("#nextDoublePage"); - - if (this.displayOnePage) { - this.hidePage('right'); - } - - this.displayLoader(); - this.loader.preloadPagesBeforeTransition(turning.end, function() { - $this.loader.setContentsInDoublePage(doublePage, turning.end, true, function() { - $this.beforeTransition(pageNr, 2); - $("#currentDoublePage").addClass('axis_' + $this.transitionAxis).addClass('_2d').addClass(turning.currentToClass); - $(doublePage).removeClass(turning.nextFromClass).one($this.support.getTransitionEndEvent(), function() { - fb('received event'); - if ($this.transitionning == false) { - return; - } - $("#currentDoublePage").remove(); - $("#nextDoublePage").attr('id', 'currentDoublePage'); - $this.afterTransition(pageNr); - }); - }); - }); - }, - pageTransition1D: function(pageNr) { - var page = pageNr; - var doublePage = $("#currentDoublePage"); - var $this = this; - - if (this.displayOnePage) { - this.hidePage('right'); - } - var turning = this.getTurningPages(pageNr); - this.beforeTransition(pageNr); - this.loader.setContentsInDoublePage(doublePage, turning.end, true, function() { - $this.afterTransition(page); - }); - - }, - beforeTransition: function(page, d) { - if (d == undefined) { - d = 1; - } - $(".axis_y").removeClass('axis_y'); - $(".axis_x").removeClass('axis_x'); - $("#links").hide(); - this.hideLoader(); - this.hideUnnecessaryButtons(page); - var animationDuration = d <= 1 ? 0 : parseFloat(this.datas.mobileTransitionDuration); - this.updateShadows(page, animationDuration); - - fb('beforeTransition ' + page); - }, - afterTransition: function(page) { - if (this.transitionning === false) { - //return; - } - fb('afterTransition ' + page); - - var $this = this; - this.currentPage = page; - - this.setPageNumbers(); - setTimeout(function() { - $this.loader.preloadAround(page); - }, 1000); - this.initLinks(); - this.hideLoader(); - this.resetWaiters(); - - // Clean messy stuffs - $("#pages").removeClass('_3dtransition'); - if ($("#nextDoublePage").length > 0) { - $("#currentDoublePage").remove(); - $("#nextDoublePage").attr('id', 'currentDoublePage'); - } - if ($("#currentDoublePage").length > 1) { - $("#currentDoublePage:gt(0)").remove(); - } - this.transitionning = false; - - if (this.pad.enabled) { - if (this.currentPage == this.datas.pages) { - $("#down").css('opacity', 0); - } else { - $("#down").css('opacity', 1); - if (!this.bookmarks.hasNextPageInGroup(this.currentPage)) { - $("#down").addClass('right'); - } else { - $("#down").removeClass('right'); - } - } - } else { - $("#down").css('opacity', 0); - } - - }, - setPageNumbers: function() { - $("#pagesnumbers .left").html(this.getPageNumberOfSide('left')); - $("#pagesnumbers .right").html(this.getPageNumberOfSide('right')); - - }, - getPageNumberOfSide: function(side) { - var p = $("#currentDoublePage").find('.' + side); - if (p.length == 0) { - return ''; - } - return this.physicalToVirtual($(p).data('page')); - }, - clickLogo: function() { - if (this.datas.url_link == '' || this.datas.url_link == 'http://') { - return; - } - this.wopen(this.datas.url_link, '_blank'); - }, - viewMode: function() { - return $("#view .mview").length > 0; - }, - openView: function(view, param1, param2, callback) { - var $this = this; - - this.displayLoader(); - this.resetWaiters(); - - setTimeout(function() { - $this._openView(view, param1, param2, callback); - }, 20); - }, - addWaiter: function(reset) { - if (reset == undefined) { - reset = false; - } - if (reset) { - this.resetWaiters(); - } - var rand = Math.round(Math.random() * 100000); - this.waiters.push(rand); - return rand; - }, - waiterActive: function(id) { - return this.waiters.indexOf(id) > -1; - }, - resetWaiters: function() { - this.waiters = []; - }, - _openView: function(view, param1, param2, callback) { - var $this = this; - var preload = {index: 'thumbnails', - search: 'thumbnails', - bookmark: 'thumbnails', - video: 'extras'}; - - if (!OFFLINEAPP && this.gal != null && preload[view] != undefined) { - var w = this.addWaiter(true); - this.gal.downloadAndCall(preload[view], function() { - if ($this.waiterActive(w)) { - $this.__openView(view, param1, param2, callback); - } - }); - } else { - this.__openView(view, param1, param2, callback); - } - }, - __openView: function(view, param1, param2, callback) { - var $this = this; - var camelView = view.charAt(0).toUpperCase() + view.substr(1); - - var cb = function() { - $this.openingView(callback); - }; - - if (view == 'index') { - this.openIndex(this.l10n.__('overview'), undefined, true, cb); - } else if (view == 'search') { - this.searchString = param1; - var group = param2; - $("#q").val(param1); - this.displayResults(this.search.find(param1), group, cb); - if (group == undefined) { - this.stats.track(1, 0, param1); - } - } else if (view == 'video') { - this.openVideo(param1, cb); - } else if (view == 'webvideo') { - this.openWebVideo(param1, param2, cb); - } else if (view == 'multimedia') { - this.openMultimedia(param1, cb); - } else if (view == 'chapters') { - this.openChapters(cb); - } else if (view == 'archives') { - this.openArchives(this.l10n.__($("#nav #archives").attr('help')), cb); - } else { - this['open' + camelView](param1, param2, cb); - } - }, - openingView: function(callback) { - var $this = this; - this.resize.resizeView(); - this.tooltip.hideTooltip(); - - var mview = $('#view .mview:last'); - - if (this.support.transitions2d) { - var vertFrom = { - translateY: $(window).height() + 'px' - }; - var vertTo = { - translateY: '0px' - }; - var horiFrom = { - translateX: $(window).width() + 'px' - }; - var horiTo = { - translateX: '0px' - }; - var from = vertFrom; - var to = vertTo; - if ($(mview).hasClass('hori')) { - from = horiFrom; - to = horiTo; - } - - $(mview).show().removeClass('animate').transform(from); - - setTimeout(function() { - $(mview).one($this.support.getTransitionEndEvent(), function() { - $("#main").hide(); - $('body').addClass('view'); - $(mview).removeClass('animate'); - callback(); - $this.hideLoader(); - resize(); - }).addClass('animate').transform(to); - }, 50); - - } else { - $("#main").hide(); - $('body').addClass('view'); - $(mview).show(); - callback(); - this.hideLoader(); - resize(); - } - }, - displayResults: function(data, group, callback) { - var $this = this; - var results = data.results; - var hideNoResults = !this.datas.searchShowNoResultsPages; - if (data.total <= 0) { - alert(this.l10n.__('no result found')); - window.location = "#/page/" + this.currentPage; - return; - } - - this.openIndex(this.l10n.__('search results for') + ' « ' + this.searchString + " »", group, false, function() { - var hits = []; - for (var i = 0; i <= $this.datas.pages; i++) { - hits[i] = 0; - } - - $.each(results, function(k, v) { - hits[k] += v; - }); - - var e = encodeURIComponent($this.searchString); - - $(".doubleThumb").each(function() { - var pages = $(this).data('pages').toString().split(','); - 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.l10n.__('no result found') + '
'); - } else { - $(this).append('
' + hitsp + ' ' + $this.l10n.__('hit(s)') + '
'); - if ($this.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.termsToHighlight = data.terms; - - if (callback != undefined) { - callback(); - } - }); - }, - openMultimedia: function(multimedia, callback) { - var a = $('a[href="#/multimedia/' + multimedia + '"]'); - var markup = decodeURIComponent($(a).attr('data-multimedia')); - - var view = '
' + this.l10n.__('back') + '
'; - view += '
'; - view += markup; - view += '
'; - - $("#view").append('
' + view + '
'); - - if (callback != undefined) { - callback(); - } - }, - openVideo: function(video, callback) { - var a = $('a[href="#/video/' + video + '"]'); - var markup = decodeURIComponent($(a).attr('data-video')); - - var view = '
' + this.l10n.__('back') + '
'; - view += '
'; - view += markup; - view += '
'; - - $("#view").append('
' + view + '
'); - - this.stats.track(11); - - this.initVideos(); - var $this = this; - var times = [250, 500, 750, 1000, 1250]; - $.each(times, function(k, v) { - setTimeout(function() { - $this.resize.resizePopupVideos(); - }, v); - }); - if (callback != undefined) { - callback(); - } - }, - openWebVideo: function(service, video, callback) { - - var view = '
' + this.l10n.__('back') + '
'; - view += '
'; - if (service == 'youtube') { - view += ''; - } else if (service == 'dailymotion') { - view += '' - } - view += '
'; - $("#view").append('
' + view + '
'); - - $("#view .mview:last iframe").each(function() { - $(this).attr('height', ($(this).width() * 9) / 16); - }); - this.stats.track(11); - if (callback != undefined) { - callback(); - } - }, - openLocales: function(p1, p2, callback) { - var view = '
' + this.l10n.__('back') + '

Select language

'; - view += '
'; - view += ''; - view += '
'; - - $("#view").append('
' + view + '
'); - if (callback != undefined) { - callback(); - } - }, - openShare: function(p1, p2, callback) { - var view = '
' + this.l10n.__('back') + '

' + this.l10n.__('share') + '

'; - view += '
'; - view += ''; - view += '
'; - - $("#view").append('
' + view + '
'); - if (callback != undefined) { - callback(); - } - }, - openBookmark: function(p1, p2, callback) { - var view = this.bookmarks.getView(); - if (view !== false) { - $("#view").append('
' + view + '
'); - if (callback != undefined) { - callback(); - } - } else { - var message = this.l10n.__("you don't have any bookmarks"); - if (this.datas.phonegap) { - navigator.notification.alert(message, function() { - - }, this.l10n.__('bookmarks')); - } else { - window.alert(message); - window.history.back(); - } - this.hideLoader(); - } - }, - openChapters: function(callback) { - if (this.chapters == undefined) { - this.chapters = new FluidbookChapters(this, this.datas.chapters); - } - - var view = '
' + this.l10n.__('back') + '

' + this.l10n.__('chapters') + '

'; - view += '
'; - view += this.chapters.getView(); - view += '
'; - - $("#view").append('
' + view + '
'); - if (callback != undefined) { - callback(); - } - }, - openIndex: function(title, group, closeAll, callback) { - var c = !closeAll ? ' one' : ''; - var index = '
' + this.l10n.__('back') + '

' + title + '

'; - index += this.index.getView(group); - $("#view").append('
' + index + '
'); - - this.bookmarks.updateBookmarks(); - if (callback != undefined) { - callback(); - } - }, - openArchives: function(title, callback) { - var archives = '
' + this.l10n.__('back') + '

' + title + '

'; - archives += '
'; - $("#view").append('
' + archives + '
'); - if (callback != undefined) { - callback(); - } - }, - physicalToVirtual: function(page) { - return this.datas.numerotation[page - 1]; - }, - virtualToPhysical: function(page) { - return this.datas.numerotation.indexOf(page) + 1; - }, - closeView: function(callback, all, animate) { - var $this = this; - if (all == undefined) { - all = false; - } - if (animate == undefined) { - animate = true; - } - if ($('.mview').scrollTop() > 0) { - $('.mview').scrollTo(0, 500, function() { - $this.closeView(callback, all, animate); - }); - return; - } - - all = all || ($("#view .mview").length <= 1); - if (!this.viewMode()) { - callback(); - return; - } - - var mview = $("#view .mview:last"); - if (all) { - $("#view .mview:not(:last)").remove(); - } - - var to = { - translateY: $(window).height() + 'px' - }; - - if ($(mview).hasClass('hori')) { - to = {translateX: $(window).width() + 'px'} - } - - if (animate && this.support.transitions2d) { - setTimeout(function() { - $(mview).one($this.support.getTransitionEndEvent(), function() { - $(this).remove(); - callback(); - }).addClass('animate').transform(to); - }, 50); - - } else { - $(mview).remove() - callback(); - } - if (all) { - $("#main").show(); - $('body').removeClass('view'); - } - resize(); - }, - getSearchHints: function(q) { - this.killLastSearchHint(); - this.displaySearchHints(this.search.getHints(q)); - }, - displaySearchHints: function(hints) { - - this.hideSearchHints(); - if (hints.length == 0) { - return; - } - $.each(hints, function(k, v) { - $("#searchHints").append('' + v[0] + ' (' + v[1] + ')'); - }); - var qoffset = $("#q").offset(); - var top = qoffset.top + $("#q").outerHeight() + 5; - - var left = qoffset.left; - if (this.l10n.dir == 'rtl') { - left = -1 * ($(window).width() - left - $("#q").outerWidth()); - } - $("#searchHints").css({top: top, left: left}).show(); - }, - killLastSearchHint: function() { - this.search.kill(); - }, - hideSearchHints: function() { - this.killLastSearchHint(); - $("#searchHints").html(''); - $("#searchHints").hide(); - }, - getLocationToShare: function() { - if (this.datas.phonegap) { - return this.datas.offlineLink; - } else { - var l = window.location.toString(); - var e = l.split("#"); - return e[0]; - } - }, - sendEmail: function() { - window.location = 'mailto:?subject=' + this.datas.title + '&body=' + this.datas.title + ' : ' + this.getLocationToShare(); - this.stats.track(5); - }, - sendTwitter: function() { - this.service.open('twitterShare', { - url: this.getLocationToShare(), - post: this.datas.twitter_description.replace('%title%', this.datas.title) - }); - this.stats.track(13); - }, - sendFacebook: function() { - this.service.open('facebookShare', { - url: this.getLocationToShare() - }); - this.stats.track(12); - }, - sendGoogleplus: function() { - this.service.open('googleplusShare', { - url: this.getLocationToShare() - }); - this.stats.track(12); - }, - sendLinkedin: function() { - this.service.open('linkedinShare', { - url: this.getLocationToShare() - }); - this.stats.track(12); - }, - sendViadeo: function() { - this.service.open('viadeoShare', { - url: this.getLocationToShare() - }); - this.stats.track(12); - }, - print: function() { - var $this = this; - var pdf; - var pdfName; - if (this.datas.pdfName.substr(0, 4) == 'http') { - pdf = this.datas.pdfName; - } else { - pdf = getBaseURL() + '/data/' + this.datas.pdfName; - } - var e = pdf.split('/'); - pdfName = e.pop(); - if (this.gal && !OFFLINEAPP) { - this.displayLoader(); - this.gal.downloadAndCall('extras', function() { - $this._openFile(pdf, $("#print"), 'pdf', pdfName); - }); - } else { - this._openFile(pdf, $("#print"), 'pdf', pdfName); - } - }, - _openFile: function(url, e, type, localname) { - - var $this = this; - - if (this.datas.phonegap != false) { - if (type == undefined) { - var e = url.split('.'); - type = e.pop(); - } - - if (url.indexOf('http') == 0) { - return this._downloadFilePhonegap(url, localname, LocalFileSystem.TEMPORARY, this._openFilePhonegap, [url, e, type]); - } else { - if (this._openFilePhonegap(url, e, type)) { - return; - } - } - - } - this.wopen(url, '_blank'); - }, - _downloadFilePhonegap: function(url, localname, fs, callback, callbackArgs) { - var $this = this; - if (fs == undefined) { - fs = LocalFileSystem.PERSISTENT; - } - - window.requestFileSystem(fs, 0, function(fileSystem) { - fileSystem.root.getFile("dummy.html", {create: true, exclusive: false}, function(fileEntry) { - var filePath = fileEntry.fullPath.replace("dummy.html", "") + localname; - var fileTransfer = new FileTransfer(); - var uri = encodeURI(url); - fileTransfer.download( - uri, - filePath, - function(entry) { - if (callback != undefined) { - if (callbackArgs == undefined) { - callbackArgs = []; - } - callbackArgs[0] = entry.toURL(); - callback.apply($this, callbackArgs); - } - }, - function(error) { - - } - ); - }); - }, function() { - - }); - }, - _openFilePhonegap: function(url, e, type) { - var $this = this; - var types_ios = {pdf: 'com.adobe.pdf'}; - var types_android = {pdf: 'application/pdf'}; - if (this.datas.phonegap == 'ios') { - this.displayLoader(); - if (types_ios[type] != undefined) { - var offset = $(e).offset(); - offset.left += ($(e).width() / 2); - offset.top += ($(e).height()); - - try { - ExternalFileUtil.openWith(url, types_ios[type], function() { - $this.hideLoader(); - }, function() { - $this.wopen(url, "_blank", 'location=no'); - $this.hideLoader(); - }, offset); - - } catch (err) { - $this.wopen(url, "_blank", 'location=no'); - $this.hideLoader(); - } - return true; - } - } - if (this.datas.phonegap == 'android') { - if (types_android[type] != undefined) { - this.displayLoader(); - setTimeout(function() { - webintent.startActivity({ - action: webintent.ACTION_VIEW, - type: types_android[type], - url: url - }, function(args) { - $this.hideLoader(5); - }, function(args) { - $this.hideLoader(5); - }); - }, 100); - - } - return true; - - } - return false; - }, - highlightSearchTerms: function(pageNr) { - return; - }, - touchOffset: function(offset) { - offset *= $("#currentDoublePage").width(); - $("#currentDoublePage").addClass('sliding'); - $("#currentDoublePage").css({ - translateX: offset - }); - }, - displayLoader: function() { - if (this.support.isMobile) { - this.coquillette.show(); - } else { - this.coquillette.show(); - $('body').addClass('loading'); - } - }, - hideLoader: function(delay, force) { - if (force == undefined) { - force = false; - } - if (delay == undefined) { - delay = 0; - } - var $this = this; - if (delay == 0) { - return this._hideLoader(force); - } - setTimeout(function() { - $this._hideLoader(force); - }, delay * 1000); - }, - _hideLoader: function(force) { - if (force == undefined) { - force = false; - } - if (!force && $('#splash').css('visibility') == 'visible') { - return; - } - if (this.support.isMobile) { - this.coquillette.hide(); - } else { - this.coquillette.hide(); - $('body').removeClass('loading'); - } - }, - pollZoom: function() { - var z = this.support.getZoomLevel(); - //console.log(z); - $('html').attr('data-zoom', z); - if (z <= 1) { - $("html").removeClass('pan'); - } else { - $("html").addClass('pan'); - } - }, - intentShare: function(subject, body) { - if (subject == undefined) { - subject = this.datas.title; - } - if (body == undefined) { - body = this.datas.title + ' : ' + this.getLocationToShare(); - } - - if (this.datas.phonegap == 'android') { - var extras = {}; - extras[webintent.EXTRA_SUBJECT] = subject; - extras[webintent.EXTRA_TEXT] = body; - webintent.startActivity({ - action: webintent.ACTION_SEND, - type: 'text/plain', - extras: extras - }, function(args) { - }, function(args) { - }); - } else { - window.location = 'mailto:?subject=' + subject + '&body=' + body; - } - this.stats.track(5); - }, - wopen: function(url, target, options) { - if (target == undefined) { - target = '_self'; - } - if (options == undefined) { - options = ''; - } - - var locationdefault = 'yes'; - if (this.datas.phonegap) { - locationdefault = 'yes'; - } - - if (options == '') { - options = 'location=' + locationdefault; - } else if (options.indexOf('location=') == -1) { - options += ',location=' + locationdefault; - } - window.open(url, target, options); - } -} - +function Fluidbook(datas) { + this.init(datas); +} + +// Constantes +// Background dispoition +Fluidbook.NONE = 3; +Fluidbook.STRETCH = 0; +Fluidbook.RATIO = 2; +Fluidbook.REPEAT = 1; +// Horizontal alignments +Fluidbook.CENTER = 4; +Fluidbook.LEFT = 5; +Fluidbook.RIGHT = 6; +// Vertical alignments +Fluidbook.MIDDLE = 7; +Fluidbook.TOP = 8; +Fluidbook.BOTTOM = 9; +Fluidbook.prototype = { + init: function(datas) { + this.datas = datas; + this.junk = datas.cacheDate; + this.cache = new FluidbookCache(datas); + this.service = new FluidbookService(this, datas.id); + this.support = new FluidbookSupport(this); + this.loader = new FluidbookLoader(this); + this.search = new FluidbookSearch(this); + this.pad = new FluidbookPad(this); + this.waiters = []; + if (Modernizr.ftouch && this.support.transitions2d && this.datas.mobileTransitions != 'none') { + this.touch = new FluidbookTouch(this); + } + this.background = new FluidbookBackground(this); + this.viewport = new FluidbookViewport(this.support) + this.l10n = new FluidbookL10N(this, $_GET['lang']); + this.video = new FluidbookVideo(this); + this.bookmarks = new FluidbookBookmarks(this); + this.index = new FluidbookIndex(this); + this.tooltip = new FluidbookTooltip(this); + if (this.datas.form == 'bulle') { + this.form = new FluidbookBulleForm(this); + } else { + this.form == false; + } + this.refw = 0; + this.refh = 0; + this.zoom = 1; + this.searchHintXHR = null; + this.searchString = ''; + this.termsToHighlight = ''; + this.imagesVersion = (this.datas.mobileVersion == 'html5-images' || !this.support.SVG); + this.vectorTexts = !this.imagesVersion; + this.displayOnePage = false; + this.indexHTML = ''; + this.transitionning = false; + this.gal = null; + this.isReady = false; + this.transitionAxis = 'x'; + if (this.support.isMobile) { + $("body").addClass('mobile'); + } + else { + $("body").addClass('desktop'); + } + + if (this.pad.enabled) { + $("body").addClass('pad'); + } + $('html').addClass(this.datas.mobileLVersion); + this.currentPage = -1; + this.resize = new FluidbookResize(this); + this.coquillette = new FluidbookCoquillette(this); + this.stats = new FluidbookStats(this); + this.stats.track(10); + if (!this.support.isMobile) { + this.desktop = new FluidbookDesktop(this); + } + + this.initLoading(); + }, + initLoading: function() { + this.displayLoader(); + }, + reloadErrorImages: function() { + $('img').each(function() { + + }); + }, + hideSplash: function() { + if ($("#splash").length == 0) { + return; + } + try { + navigator.splashscreen.hide(); + } catch (err) { + } + + this.loader.canPreload(); + var $this = this; + setTimeout(function() { + $this._hideSplash(); + }, 3000); + }, + _hideSplash: function() { + this.ready(); + $("#main").css('visibility', 'visible'); + this.hideLoader(0, true); + if (this.support.transitions2d) { + $("#splash").css('opacity', 0).one(this.support.getTransitionEndEvent(true), function() { + $(this).remove(); + }); + } else { + $("#splash").fadeOut(500, function() { + $(this.remove()); + }) + } + + }, + ready: function() { + if (this.isReady) { + return; + } + this.isReady = true; + $("#main").css('display', 'block'); + this.nav = new FluidbookNav(this); + this.help = new FluidbookHelp(this); + this.help.displayAtStartup(); + resize(); + $(this).trigger('fluidbookready'); + }, + loadPlugins: function() { + $.each(this.datas.plugins, function(k, plugin) { + try { + var functionName = plugin.replace(/\./g, '_'); + eval(functionName + '();'); + } catch (err) { + } + }); + $.each(this.datas.htmlmultimedia, function(k, code) { + try { + eval(code); + } catch (err) { + } + }); + }, + resetZoom: function() { + var $this = this; + if (this.support.iOS) { + } else if (!this.support.isMobile) { + this.desktop.updateDesktopScale(1); + } + + }, + setZoom: function(zoom) { + if (zoom) { + $("#pages").addClass('zoom'); + } else { + $("#pages").removeClass('zoom'); + } + }, + initPage: function(pageNr, doublePage, position) { + if ($("#page_" + pageNr).length > 0) { + return; + } + $(doublePage).find('.' + position).remove(); + var page = '
'; + $(doublePage).append(page); + }, + hidePage: function(position) { + $("#pages ." + position).hide(); + }, + initLinks: function(pageNr) { + if (pageNr == undefined) { + pageNr = this.currentPage; + } + + var lClass = 'left'; + if (this.displayOnePage && pageNr % 2 == 1) { + lClass = 'right'; + } + + if (pageNr % 2 == 1) { + pageNr--; + } + + $("#links").removeClass('left').removeClass('right').addClass(lClass).html(this.datas.links[pageNr]).show(); + if (this.datas.bookmark) { + $("#links").append(this.bookmarks.getBookmarkForPage(pageNr)); + } + $("#links").prepend(''); + var $this = this; + if (this.datas.linkBlinkTime > 0) { + ///this.animateLinks(); + } + setTimeout(function() { + $this.initVideos(); + }, 1000); + }, + animateLinks: function() { + var links = $(".link a.displayArea"); + var bookmarks = $("#links .bookmark:not([data-enabled])"); + if (Modernizr.csstransitions) { + $(links).addClass('animating'); + $(bookmarks).css('opacity', 1).addClass('animating'); + setTimeout(function() { + $(links).css('opacity', 0); + $(bookmarks).css('opacity', 0); + setTimeout(function() { + $(links).removeClass('animating').css('opacity', 1); + $(bookmarks).removeClass('animating').css('opacity', ""); + }, 1100); + }, 50); + } else { + $(links).addClass('animating').fadeOut(1000, function() { + $(links).removeClass('animating').show(); + }).mouseover(function() { + $(links).stop().removeClass('animating').css('opacity', 1).show(); + return true; + }); + } + }, + initVideos: function() { + var $this = this; + $(".videoContainer").each(function() { + $this.video.initVideo(this); + }); + }, + getNextOffset: function() { + var offset = 2; + if (this.displayOnePage) { + offset = 1; + } + this.transitionAxis = 'x'; + return offset; + }, + goNextPage: function() { + if (this.transitionning) { + return; + } + this.transitionAxis = 'x'; + this.setCurrentPage(this.normalizePage(this.currentPage) + this.getNextOffset()); + }, + goFirstPage: function() { + if (this.transitionning) { + return; + } + this.transitionAxis = 'x'; + this.setCurrentPage(1); + }, + goPreviousPage: function() { + if (this.transitionning) { + return; + } + this.transitionAxis = 'x'; + this.setCurrentPage(this.normalizePage(this.currentPage) - this.getNextOffset()); + }, + goLastPage: function() { + if (this.transitionning) { + return; + } + this.transitionAxis = 'x'; + this.setCurrentPage(this.datas.pages); + }, + goNextChapter: function() { + if (this.transitionning) { + return; + } + var next = this.bookmarks.getNextGroupCover(this.currentPage); + if (next === false) { + return; + } + this.transitionAxis = 'x'; + this.setCurrentPage(this.normalizePage(next)); + }, + goPreviousChapter: function() { + if (this.transitionning) { + return; + } + var prev = this.bookmarks.getPreviousGroupCover(this.currentPage); + if (prev === false) { + return; + } + this.transitionAxis = 'x'; + this.setCurrentPage(this.normalizePage(prev)); + }, + goNextChapterPage: function() { + if (this.transitionning) { + return; + } + + var next = this.bookmarks.getNextPageInGroupOfPage(this.currentPage); + if (next === false) { + return; + } + this.transitionAxis = 'y'; + this.setCurrentPage(this.normalizePage(next)); + }, + goPreviousChapterPage: function() { + if (this.transitionning) { + return; + } + + var prev = this.bookmarks.getPreviousPageInGroupOfPage(this.currentPage); + if (prev === false) { + return; + } + this.transitionAxis = 'y'; + this.setCurrentPage(this.normalizePage(prev)); + }, + normalizePage: function(page) { + page = Math.max(1, Math.min(page, this.datas.pages)); + if (!this.displayOnePage && page % 2 == 1) { + page--; + } + return page; + }, + setCurrentPage: function(page) { + window.location.hash = "#/page/" + this.normalizePage(page); + }, + changeAddress: function() { + var $this = this; + var page; + var args = window.location.hash.split('/'); + if (args.length <= 1 || args[1] == '' || args[1] == undefined) { + return this.setCurrentPage('1'); + } else if (args[1] == 'page') { + page = parseInt(args[2]); + if (isNaN(page) || page == undefined) { + return this.setCurrentPage('1'); + } + if (this.pad.enabled) { + this.transitionAxis = this.pad.getTransitionAxis(this.currentPage, page); + } else { + this.transitionAxis = 'x'; + } + + $($this).trigger('changePage', [page]); + this.closeView(function() { + $this.pageTransition(page); + $this.resetZoom(); + $this.stats.track(0, page); + $this.hideSplash(); + }, true); + } + else { + this.openView(args[1], args[2], args[3], function() { + $this.hideSplash(); + }); + this.resetZoom(); + } + + return; + }, + pageTransition: function(pageNr) { + this.tooltip.hideTooltip(); + if (pageNr == undefined) { + pageNr = this.currentPage; + if (pageNr == -1) { + pageNr = 1; + } + } + pageNr = this.normalizePage(pageNr); + if (pageNr == this.normalizePage(this.currentPage) || this.currentPage == -1 || !this.support.transitions2d || this.datas.mobileTransitions == 'none') { + return this.pageTransition1D(pageNr); + } + + if (this.displayOnePage) { + return this.pageTransition2DPortrait(pageNr); + } + + if (!this.support.transitions3d || this.datas.mobileTransitions == 'slide') { + return this.pageTransition2D(pageNr); + } + + return this.pageTransition3D(pageNr); + }, + pageTransition3D: function(pageNr) { + + var $this = this; + if ($("#pages").hasClass('_3dtransition')) { + return; + } + this.transitionning = true; + var $this = this; + var turning = this.getTurningPages(pageNr); + $("#pages").prepend('
'); + var doublePage = $("#nextDoublePage"); + var currentDoublePage = $("#currentDoublePage"); + this.displayLoader(); + this.loader.preloadPagesBeforeTransition([turning.currentLeft, turning.currentRight], function() { + $("#pages").addClass('_3dtransition'); + $this.loader.setContentsInDoublePage(currentDoublePage, turning.flat, true, function() { + $this.loader.setContentsInDoublePage(doublePage, turning.flip, true, function() { + $this.beforeTransition(pageNr, 3); + $(doublePage).addClass(turning.nextFromClass + 'end').one($this.support.getTransitionEndEvent(), function() { + if ($this.transitionning == false) { + return; + } + if ($("#nextDoublePage").length == 0) { + $("#pages").removeClass('_3dtransition'); + $this.transitionning = false; + return; + } + + // Set the flat contents with the new page + $this.loader.setContentsInDoublePage(doublePage, turning.end, false, function() { + $(doublePage).removeClass('_3d').removeClass(turning.nextFromClass + 'start').removeClass(turning.nextFromClass + 'end'); + // Remove former part + $("#currentDoublePage").remove(); + $(doublePage).attr('id', 'currentDoublePage'); + $("#pages").removeClass('_3dtransition'); + $this.afterTransition(pageNr); + }); + }); + }); + }); + }); + }, + getTurningPages: function(newPage) { + var res = {}; + res.dir = 1; + if (newPage < this.currentPage) { + res.dir = -1; + } + + if (this.l10n.dir == 'ltr') { + res.currentLeft = this.currentPage - this.currentPage % 2; + res.currentRight = res.currentLeft + 1; + if (res.dir == 1) { + res.currentToClass = 'prev'; + res.nextFromClass = 'next'; + res.flat = [res.currentLeft, newPage + 1]; + res.flip = [newPage, res.currentRight]; + } else { + res.currentToClass = 'next'; + res.nextFromClass = 'prev'; + res.flat = [newPage, res.currentRight]; + res.flip = [res.currentLeft, newPage + 1]; + } + res['end'] = [newPage, newPage + 1]; + } else { + res.currentRight = this.currentPage - this.currentPage % 2; + res.currentLeft = res.currentRight + 1; + if (res.dir == 1) { + res.currentToClass = 'next'; + res.nextFromClass = 'prev'; + res.flat = [newPage + 1, res.currentRight]; + res.flip = [res.currentLeft, newPage]; + } else { + res.currentToClass = 'prev'; + res.nextFromClass = 'next'; + res.flat = [res.currentLeft, newPage]; + res.flip = [newPage + 1, res.currentRight]; + } + + if (this.displayOnePage) { + res.end = [newPage, newPage + 1]; + } else { + res.end = [newPage + 1, newPage]; + } + } + res.loading = [res.currentLeft, res.currentRight]; + return json_parse(JSON.stringify(res)); + }, + reloadCurrentPage: function() { + this.pageTransition(this.currentPage); + }, + readingPage: function(side) { + if (!this.displayOnePage) { + var page = this.currentPage; + var change = false; + if (side == 'left' && page % 2 == 1) { + page--; + change = true; + } else if (side == 'right' && page % 2 == 0) { + page++; + change = true; + } + if (change) { + window.location.hash = "/page/" + page; + } + } + }, + hideUnnecessaryButtons: function(page) { + var speed = 500; + if (page == undefined) { + page = this.currentPage; + speed = 0; + } + + if (Modernizr.csstransitions) { + if (page <= 1) { + $("#previous").addClass('hidden'); + } else { + $("#previous").removeClass('hidden'); + } + + if (page >= this.datas.pages) { + $("#next").addClass('hidden'); + } else { + $("#next").removeClass('hidden'); + } + } else { + $("#previous,#next").removeClass('hidden'); + if (page <= 1) { + $("#previous:visible").fadeOut(speed); + } else { + $("#previous:hidden").fadeIn(speed); + } + + if (page >= this.datas.pages) { + $("#next:visible").fadeOut(speed); + } else { + $("#next:hidden").fadeIn(speed); + } + } + }, + updateShadows: function(page, animationDuration) { + if (animationDuration == undefined) { + animationDuration = 0; + } + animationDuration *= 1000; + var delay, duration; + if (animationDuration == 0) { + delay = 0; + duration = 0; + } else { + delay = animationDuration * 0.7; + duration = animationDuration * 0.3; + } + + var left = true, right = true; + if (this.displayOnePage) { + right = false; + } else { + if ((page <= 1 && this.l10n.dir == 'ltr') || (page >= this.datas.pages && this.l10n.dir == 'rtl')) { + left = false; + } else if ((page <= 1 && this.l10n.dir == 'rtl') || (page >= this.datas.pages && this.l10n.dir == 'ltr')) { + right = false; + } + } + + + if (left) { + $("#shadow>.left:hidden").delay(delay).fadeIn(duration); + } else { + $("#shadow>.left:visible").fadeOut(duration); + } + + if (right) { + $("#shadow>.right:hidden").delay(delay).fadeIn(duration); + } else { + $("#shadow>.right:visible").fadeOut(duration); + } + }, + showAllButtons: function() { + $("#next,#previous").addClass('help').show(); + }, + pageTransition2D: function(pageNr) { + this.transitionning = true; + var $this = this; + var turning = this.getTurningPages(pageNr); + $("#pages").append('
'); + var doublePage = $("#nextDoublePage"); + this.displayLoader(); + this.loader.preloadPagesBeforeTransition(turning.end, function() { + $this.loader.setContentsInDoublePage(doublePage, turning.end, true, function() { + $this.beforeTransition(pageNr, 2); + $("#currentDoublePage").addClass('_2d').addClass('axis_' + $this.transitionAxis).addClass(turning.currentToClass); + $(doublePage).removeClass(turning.nextFromClass).one($this.support.getTransitionEndEvent(), function(event) { + if ($this.transitionning == false) { + return; + } + $("#currentDoublePage").remove(); + $("#nextDoublePage").attr('id', 'currentDoublePage'); + $this.afterTransition(pageNr); + }); + }); + }); + }, + pageTransition2DPortrait: function(pageNr) { + this.transitionning = true; + var $this = this; + var turning = this.getTurningPages(pageNr); + $("#pages").append('
'); + var doublePage = $("#nextDoublePage"); + if (this.displayOnePage) { + this.hidePage('right'); + } + + this.displayLoader(); + this.loader.preloadPagesBeforeTransition(turning.end, function() { + $this.loader.setContentsInDoublePage(doublePage, turning.end, true, function() { + $this.beforeTransition(pageNr, 2); + $("#currentDoublePage").addClass('axis_' + $this.transitionAxis).addClass('_2d').addClass(turning.currentToClass); + $(doublePage).removeClass(turning.nextFromClass).one($this.support.getTransitionEndEvent(), function() { + fb('received event'); + if ($this.transitionning == false) { + return; + } + $("#currentDoublePage").remove(); + $("#nextDoublePage").attr('id', 'currentDoublePage'); + $this.afterTransition(pageNr); + }); + }); + }); + }, + pageTransition1D: function(pageNr) { + var page = pageNr; + var doublePage = $("#currentDoublePage"); + var $this = this; + if (this.displayOnePage) { + this.hidePage('right'); + } + var turning = this.getTurningPages(pageNr); + this.beforeTransition(pageNr); + this.loader.setContentsInDoublePage(doublePage, turning.end, true, function() { + $this.afterTransition(page); + }); + }, + beforeTransition: function(page, d) { + if (d == undefined) { + d = 1; + } + $(".axis_y").removeClass('axis_y'); + $(".axis_x").removeClass('axis_x'); + $("#links").hide(); + this.hideLoader(); + this.hideUnnecessaryButtons(page); + var animationDuration = d <= 1 ? 0 : parseFloat(this.datas.mobileTransitionDuration); + this.updateShadows(page, animationDuration); + fb('beforeTransition ' + page); + }, + afterTransition: function(page) { + if (this.transitionning === false) { + //return; + } + fb('afterTransition ' + page); + var $this = this; + this.currentPage = page; + this.setPageNumbers(); + setTimeout(function() { + $this.loader.preloadAround(page); + }, 1000); + this.initLinks(); + this.hideLoader(); + this.resetWaiters(); + // Clean messy stuffs + $("#pages").removeClass('_3dtransition'); + if ($("#nextDoublePage").length > 0) { + $("#currentDoublePage").remove(); + $("#nextDoublePage").attr('id', 'currentDoublePage'); + } + if ($("#currentDoublePage").length > 1) { + $("#currentDoublePage:gt(0)").remove(); + } + this.transitionning = false; + if (this.pad.enabled) { + if (this.currentPage == this.datas.pages) { + $("#down").css('opacity', 0); + } else { + $("#down").css('opacity', 1); + if (!this.bookmarks.hasNextPageInGroup(this.currentPage)) { + $("#down").addClass('right'); + } else { + $("#down").removeClass('right'); + } + } + } else { + $("#down").css('opacity', 0); + } + + }, + setPageNumbers: function() { + $("#pagesnumbers .left").html(this.getPageNumberOfSide('left')); + $("#pagesnumbers .right").html(this.getPageNumberOfSide('right')); + }, + getPageNumberOfSide: function(side) { + var p = $("#currentDoublePage").find('.' + side); + if (p.length == 0) { + return ''; + } + return this.physicalToVirtual($(p).data('page')); + }, + clickLogo: function() { + if (this.datas.url_link == '' || this.datas.url_link == 'http://') { + return; + } + this.wopen(this.datas.url_link, '_blank'); + }, + viewMode: function() { + return $("#view .mview").length > 0; + }, + openView: function(view, param1, param2, callback) { + var $this = this; + this.displayLoader(); + this.resetWaiters(); + setTimeout(function() { + $this._openView(view, param1, param2, callback); + }, 20); + }, + addWaiter: function(reset) { + if (reset == undefined) { + reset = false; + } + if (reset) { + this.resetWaiters(); + } + var rand = Math.round(Math.random() * 100000); + this.waiters.push(rand); + return rand; + }, + waiterActive: function(id) { + return this.waiters.indexOf(id) > -1; + }, + resetWaiters: function() { + this.waiters = []; + }, + _openView: function(view, param1, param2, callback) { + var $this = this; + var preload = {index: 'thumbnails', + search: 'thumbnails', + bookmark: 'thumbnails', + video: 'extras'}; + if (!OFFLINEAPP && this.gal != null && preload[view] != undefined) { + var w = this.addWaiter(true); + this.gal.downloadAndCall(preload[view], function() { + if ($this.waiterActive(w)) { + $this.__openView(view, param1, param2, callback); + } + }); + } else { + this.__openView(view, param1, param2, callback); + } + }, + __openView: function(view, param1, param2, callback) { + var $this = this; + var camelView = view.charAt(0).toUpperCase() + view.substr(1); + var cb = function() { + $this.openingView(callback); + }; + if (view == 'index') { + this.openIndex(this.l10n.__('overview'), undefined, true, cb); + } else if (view == 'search') { + this.searchString = param1; + var group = param2; + $("#q").val(param1); + this.search.find(param1, function(r) { + $this.displayResults(r, group, cb); + }); + if (group == undefined) { + this.stats.track(1, 0, param1); + } + } else if (view == 'video') { + this.openVideo(param1, cb); + } else if (view == 'webvideo') { + this.openWebVideo(param1, param2, cb); + } else if (view == 'multimedia') { + this.openMultimedia(param1, cb); + } else if (view == 'chapters') { + this.openChapters(cb); + } else if (view == 'archives') { + this.openArchives(this.l10n.__($("#nav #archives").attr('help')), cb); + } else { + this['open' + camelView](param1, param2, cb); + } + }, + openingView: function(callback) { + var $this = this; + this.resize.resizeView(); + this.tooltip.hideTooltip(); + var mview = $('#view .mview:last'); + if (this.support.transitions2d) { + var vertFrom = { + translateY: $(window).height() + 'px' + }; + var vertTo = { + translateY: '0px' + }; + var horiFrom = { + translateX: $(window).width() + 'px' + }; + var horiTo = { + translateX: '0px' + }; + var from = vertFrom; + var to = vertTo; + if ($(mview).hasClass('hori')) { + from = horiFrom; + to = horiTo; + } + + $(mview).show().removeClass('animate').transform(from); + setTimeout(function() { + $(mview).one($this.support.getTransitionEndEvent(), function() { + $("#main").hide(); + $('body').addClass('view'); + $(mview).removeClass('animate'); + callback(); + $this.hideLoader(); + resize(); + }).addClass('animate').transform(to); + }, 50); + } else { + $("#main").hide(); + $('body').addClass('view'); + $(mview).show(); + callback(); + this.hideLoader(); + resize(); + } + }, + displayResults: function(data, group, callback) { + var $this = this; + var results = data.results; + var hideNoResults = !this.datas.searchShowNoResultsPages; + if (data.total <= 0) { + alert(this.l10n.__('no result found')); + window.location = "#/page/" + this.currentPage; + return; + } + + this.openIndex(this.l10n.__('search results for') + ' « ' + this.searchString + " »", group, false, function() { + var hits = []; + for (var i = 0; i <= $this.datas.pages; i++) { + hits[i] = 0; + } + + $.each(results, function(k, v) { + hits[k] += v; + }); + var e = encodeURIComponent($this.searchString); + $(".doubleThumb").each(function() { + var pages = $(this).data('pages').toString().split(','); + 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.l10n.__('no result found') + '
'); + } else { + $(this).append('
' + hitsp + ' ' + $this.l10n.__('hit(s)') + '
'); + if ($this.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.termsToHighlight = data.terms; + if (callback != undefined) { + callback(); + } + }); + }, + openMultimedia: function(multimedia, callback) { + var a = $('a[href="#/multimedia/' + multimedia + '"]'); + var markup = decodeURIComponent($(a).attr('data-multimedia')); + var view = '
' + this.l10n.__('back') + '
'; + view += '
'; + view += markup; + view += '
'; + $("#view").append('
' + view + '
'); + if (callback != undefined) { + callback(); + } + }, + openVideo: function(video, callback) { + var a = $('a[href="#/video/' + video + '"]'); + var markup = decodeURIComponent($(a).attr('data-video')); + var view = '
' + this.l10n.__('back') + '
'; + view += '
'; + view += markup; + view += '
'; + $("#view").append('
' + view + '
'); + this.stats.track(11); + this.initVideos(); + var $this = this; + var times = [250, 500, 750, 1000, 1250]; + $.each(times, function(k, v) { + setTimeout(function() { + $this.resize.resizePopupVideos(); + }, v); + }); + if (callback != undefined) { + callback(); + } + }, + openWebVideo: function(service, video, callback) { + + var view = '
' + this.l10n.__('back') + '
'; + view += '
'; + if (service == 'youtube') { + view += ''; + } else if (service == 'dailymotion') { + view += '' + } + view += '
'; + $("#view").append('
' + view + '
'); + $("#view .mview:last iframe").each(function() { + $(this).attr('height', ($(this).width() * 9) / 16); + }); + this.stats.track(11); + if (callback != undefined) { + callback(); + } + }, + openLocales: function(p1, p2, callback) { + var view = '
' + this.l10n.__('back') + '

Select language

'; + view += '
'; + view += ''; + view += '
'; + $("#view").append('
' + view + '
'); + if (callback != undefined) { + callback(); + } + }, + openShare: function(p1, p2, callback) { + var view = '
' + this.l10n.__('back') + '

' + this.l10n.__('share') + '

'; + view += '
'; + view += ''; + view += '
'; + $("#view").append('
' + view + '
'); + if (callback != undefined) { + callback(); + } + }, + openBookmark: function(p1, p2, callback) { + var view = this.bookmarks.getView(); + if (view !== false) { + $("#view").append('
' + view + '
'); + if (callback != undefined) { + callback(); + } + } else { + var message = this.l10n.__("you don't have any bookmarks"); + if (this.datas.phonegap) { + navigator.notification.alert(message, function() { + + }, this.l10n.__('bookmarks')); + } else { + window.alert(message); + window.history.back(); + } + this.hideLoader(); + } + }, + openChapters: function(callback) { + if (this.chapters == undefined) { + this.chapters = new FluidbookChapters(this, this.datas.chapters); + } + + var view = '
' + this.l10n.__('back') + '

' + this.l10n.__('chapters') + '

'; + view += '
'; + view += this.chapters.getView(); + view += '
'; + $("#view").append('
' + view + '
'); + if (callback != undefined) { + callback(); + } + }, + openIndex: function(title, group, closeAll, callback) { + var c = !closeAll ? ' one' : ''; + var index = '
' + this.l10n.__('back') + '

' + title + '

'; + index += this.index.getView(group); + $("#view").append('
' + index + '
'); + this.bookmarks.updateBookmarks(); + if (callback != undefined) { + callback(); + } + }, + openArchives: function(title, callback) { + var archives = '
' + this.l10n.__('back') + '

' + title + '

'; + archives += '
'; + $("#view").append('
' + archives + '
'); + if (callback != undefined) { + callback(); + } + }, + physicalToVirtual: function(page) { + return this.datas.numerotation[page - 1]; + }, + virtualToPhysical: function(page) { + return this.datas.numerotation.indexOf(page) + 1; + }, + closeView: function(callback, all, animate) { + var $this = this; + if (all == undefined) { + all = false; + } + if (animate == undefined) { + animate = true; + } + if ($('.mview').scrollTop() > 0) { + $('.mview').scrollTo(0, 500, function() { + $this.closeView(callback, all, animate); + }); + return; + } + + all = all || ($("#view .mview").length <= 1); + if (!this.viewMode()) { + callback(); + return; + } + + var mview = $("#view .mview:last"); + if (all) { + $("#view .mview:not(:last)").remove(); + } + + var to = { + translateY: $(window).height() + 'px' + }; + if ($(mview).hasClass('hori')) { + to = {translateX: $(window).width() + 'px'} + } + + if (animate && this.support.transitions2d) { + setTimeout(function() { + $(mview).one($this.support.getTransitionEndEvent(), function() { + $(this).remove(); + callback(); + }).addClass('animate').transform(to); + }, 50); + } else { + $(mview).remove() + callback(); + } + if (all) { + $("#main").show(); + $('body').removeClass('view'); + } + resize(); + }, + getSearchHints: function(q) { + + var $this = this; + this.killLastSearchHint(); + this.search.getHints(q, function(r) { + $this.displaySearchHints(r); + }); + }, + displaySearchHints: function(hints) { + + this.hideSearchHints(); + if (hints.length == 0) { + return; + } + $.each(hints, function(k, v) { + $("#searchHints").append('' + v[0] + ' (' + v[1] + ')'); + }); + var qoffset = $("#q").offset(); + var top = qoffset.top + $("#q").outerHeight() + 5; + var left = qoffset.left; + if (this.l10n.dir == 'rtl') { + left = -1 * ($(window).width() - left - $("#q").outerWidth()); + } + $("#searchHints").css({top: top, left: left}).show(); + }, + killLastSearchHint: function() { + this.search.kill(); + }, + hideSearchHints: function() { + this.killLastSearchHint(); + $("#searchHints").html(''); + $("#searchHints").hide(); + }, + getLocationToShare: function() { + if (this.datas.phonegap) { + return this.datas.offlineLink; + } else { + var l = window.location.toString(); + var e = l.split("#"); + return e[0]; + } + }, + sendEmail: function() { + window.location = 'mailto:?subject=' + this.datas.title + '&body=' + this.datas.title + ' : ' + this.getLocationToShare(); + this.stats.track(5); + }, + sendTwitter: function() { + this.service.open('twitterShare', { + url: this.getLocationToShare(), + post: this.datas.twitter_description.replace('%title%', this.datas.title) + }); + this.stats.track(13); + }, + sendFacebook: function() { + this.service.open('facebookShare', { + url: this.getLocationToShare() + }); + this.stats.track(12); + }, + sendGoogleplus: function() { + this.service.open('googleplusShare', { + url: this.getLocationToShare() + }); + this.stats.track(12); + }, + sendLinkedin: function() { + this.service.open('linkedinShare', { + url: this.getLocationToShare() + }); + this.stats.track(12); + }, + sendViadeo: function() { + this.service.open('viadeoShare', { + url: this.getLocationToShare() + }); + this.stats.track(12); + }, + print: function() { + var $this = this; + var pdf; + var pdfName; + if (this.datas.pdfName.substr(0, 4) == 'http') { + pdf = this.datas.pdfName; + } else { + pdf = getBaseURL() + '/data/' + this.datas.pdfName; + } + var e = pdf.split('/'); + pdfName = e.pop(); + if (this.gal && !OFFLINEAPP) { + this.displayLoader(); + this.gal.downloadAndCall('extras', function() { + $this._openFile(pdf, $("#print"), 'pdf', pdfName); + }); + } else { + this._openFile(pdf, $("#print"), 'pdf', pdfName); + } + }, + _openFile: function(url, e, type, localname) { + + var $this = this; + if (this.datas.phonegap != false) { + if (type == undefined) { + var e = url.split('.'); + type = e.pop(); + } + + if (url.indexOf('http') == 0) { + return this._downloadFilePhonegap(url, localname, LocalFileSystem.TEMPORARY, this._openFilePhonegap, [url, e, type]); + } else { + if (this._openFilePhonegap(url, e, type)) { + return; + } + } + + } + this.wopen(url, '_blank'); + }, + _downloadFilePhonegap: function(url, localname, fs, callback, callbackArgs) { + var $this = this; + if (fs == undefined) { + fs = LocalFileSystem.PERSISTENT; + } + + window.requestFileSystem(fs, 0, function(fileSystem) { + fileSystem.root.getFile("dummy.html", {create: true, exclusive: false}, function(fileEntry) { + var filePath = fileEntry.fullPath.replace("dummy.html", "") + localname; + var fileTransfer = new FileTransfer(); + var uri = encodeURI(url); + fileTransfer.download( + uri, + filePath, + function(entry) { + if (callback != undefined) { + if (callbackArgs == undefined) { + callbackArgs = []; + } + callbackArgs[0] = entry.toURL(); + callback.apply($this, callbackArgs); + } + }, + function(error) { + + } + ); + }); + }, function() { + + }); + }, + _openFilePhonegap: function(url, e, type) { + var $this = this; + var types_ios = {pdf: 'com.adobe.pdf'}; + var types_android = {pdf: 'application/pdf'}; + if (this.datas.phonegap == 'ios') { + this.displayLoader(); + if (types_ios[type] != undefined) { + var offset = $(e).offset(); + offset.left += ($(e).width() / 2); + offset.top += ($(e).height()); + try { + ExternalFileUtil.openWith(url, types_ios[type], function() { + $this.hideLoader(); + }, function() { + $this.wopen(url, "_blank", 'location=no'); + $this.hideLoader(); + }, offset); + } catch (err) { + $this.wopen(url, "_blank", 'location=no'); + $this.hideLoader(); + } + return true; + } + } + if (this.datas.phonegap == 'android') { + if (types_android[type] != undefined) { + this.displayLoader(); + setTimeout(function() { + webintent.startActivity({ + action: webintent.ACTION_VIEW, + type: types_android[type], + url: url + }, function(args) { + $this.hideLoader(5); + }, function(args) { + $this.hideLoader(5); + }); + }, 100); + } + return true; + } + return false; + }, + highlightSearchTerms: function(pageNr) { + return; + }, + touchOffset: function(offset) { + offset *= $("#currentDoublePage").width(); + $("#currentDoublePage").addClass('sliding'); + $("#currentDoublePage").css({ + translateX: offset + }); + }, + displayLoader: function() { + if (this.support.isMobile) { + this.coquillette.show(); + } else { + this.coquillette.show(); + $('body').addClass('loading'); + } + }, + hideLoader: function(delay, force) { + if (force == undefined) { + force = false; + } + if (delay == undefined) { + delay = 0; + } + var $this = this; + if (delay == 0) { + return this._hideLoader(force); + } + setTimeout(function() { + $this._hideLoader(force); + }, delay * 1000); + }, + _hideLoader: function(force) { + if (force == undefined) { + force = false; + } + if (!force && $('#splash').css('visibility') == 'visible') { + return; + } + if (this.support.isMobile) { + this.coquillette.hide(); + } else { + this.coquillette.hide(); + $('body').removeClass('loading'); + } + }, + pollZoom: function() { + var z = this.support.getZoomLevel(); + //console.log(z); + $('html').attr('data-zoom', z); + if (z <= 1) { + $("html").removeClass('pan'); + } else { + $("html").addClass('pan'); + } + }, + intentShare: function(subject, body) { + if (subject == undefined) { + subject = this.datas.title; + } + if (body == undefined) { + body = this.datas.title + ' : ' + this.getLocationToShare(); + } + + if (this.datas.phonegap == 'android') { + var extras = {}; + extras[webintent.EXTRA_SUBJECT] = subject; + extras[webintent.EXTRA_TEXT] = body; + webintent.startActivity({ + action: webintent.ACTION_SEND, + type: 'text/plain', + extras: extras + }, function(args) { + }, function(args) { + }); + } else { + window.location = 'mailto:?subject=' + subject + '&body=' + body; + } + this.stats.track(5); + }, + wopen: function(url, target, options) { + if (target == undefined) { + target = '_self'; + } + if (options == undefined) { + options = ''; + } + + var locationdefault = 'yes'; + if (this.datas.phonegap) { + locationdefault = 'yes'; + } + + if (options == '') { + options = 'location=' + locationdefault; + } else if (options.indexOf('location=') == -1) { + options += ',location=' + locationdefault; + } + window.open(url, target, options); + } +} + diff --git a/js/libs/fluidbook/fluidbook.search.js b/js/libs/fluidbook/fluidbook.search.js index 1bff48ff..672d284c 100644 --- a/js/libs/fluidbook/fluidbook.search.js +++ b/js/libs/fluidbook/fluidbook.search.js @@ -1,16 +1,37 @@ + function FluidbookSearch() { - loadJSLibrary( - 'data/search.index.js' - , function() { - }); + this.indexLoaded = false; } FluidbookSearch.prototype = { - getHints: function(q) { + getHints: function(q, callback) { + var $this = this; + this._loadLib(function() { + $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', function() { + loadJSLibrary('data/search.texts.js', function() { + $this.indexLoaded = true; + callback(); + }); + }); + }, + _getHints: function(q, callback) { var words = this.normalizeQuery(q); q = words.pop(); var res = []; - if (q.length < 3) { return res; } @@ -25,49 +46,51 @@ FluidbookSearch.prototype = { } res.sort(this.sortHints); - return res.slice(0, 12); + callback(res.slice(0, 12)); }, - find: function(q) { + _find: function(q, callback) { var words = this.normalizeQuery(q); - var res = {}; var terms = []; var total = 0; - var q, v, k, kk, word, wordata, page, occurences; for (kk in words) { q = words[kk]; - for (k in INDEX) { v = INDEX[k]; if (k.indexOf(q) != 0) { continue; } - for (word in v.w) { - wordata = v.w[word]; - terms.push(word); - for (page in wordata.p) { - - occurences = wordata.p[page]; - - page = parseInt(page); - - - if (res[page] == undefined) { - res[page] = 0; - } - res[page] += occurences; - total += occurences; + for (page in v.p) { + var occurences = v.p[page]; + page = parseInt(page); + if (res[page] == undefined) { + res[page] = 0; } + res[page] += occurences; + total += occurences; } + /*for (word in v.w) { + wordata = v.w[word]; + terms.push(word); + for (page in wordata.p) { + occurences = wordata.p[page]; + page = parseInt(page); + if (res[page] == undefined) { + res[page] = 0; + } + res[page] += occurences; + total += occurences; + } + }*/ } } - return { + callback({ total: total, results: res, terms: terms.join(' ') - }; + }); }, sortHints: function(a, b) { return b[1] - a[1]; @@ -106,7 +129,6 @@ FluidbookSearch.prototype = { source = source.replace(/[œ]/g, "oe"); source = source.replace(/[Œ]/g, "OE"); return source; - } };