From: Vincent Vanwaelscappel Date: Thu, 28 Mar 2019 12:23:53 +0000 (+0100) Subject: wip #2655 @6 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=9bf8d2e85cec5781750f5c2fcf14e7abd6536948;p=fluidbook-html5.git wip #2655 @6 --- diff --git a/js/libs/fluidbook/fluidbook.js b/js/libs/fluidbook/fluidbook.js index c0455908..80626b7b 100644 --- a/js/libs/fluidbook/fluidbook.js +++ b/js/libs/fluidbook/fluidbook.js @@ -21,16 +21,13 @@ Fluidbook.prototype = { init: function (datas) { this.flags = {}; - this.networkPauseDate = null; - this.networkPauseQueue = []; - this.networkPausePriorityQueue = []; - this.initNetworkPause(); this.canNavigate = false; this.initSettings(datas); this.singleMode = (this.datas.mobileNavigationType === 'portrait'); this.junk = datas.cacheDate; + this.networkControl = new FluidbookNetworkControl(this); this.input = new FluidbookInput(this); if (this.datas.landingPage != undefined && this.datas.landingPage != '') { this.landingpage = new FluidbookLandingPage(this); @@ -195,10 +192,10 @@ Fluidbook.prototype = { if (this.support.transitions3dacc && this.datas.mobileTransitions === 'flip3d') { $("#main,#viewOverlay,#view").css('visibility', 'visible'); resize(); - this.pauseNetwork(4000); + this.networkControl.pause(4000); setTimeout(function () { this.fluidbook.pagetransitions.flip3d.performancesTest(function () { - $this.pauseNetwork(-1); + $this.networkControl.resume(); $this._hideSplash(); }) }, 500); @@ -207,6 +204,7 @@ Fluidbook.prototype = { } }, + _hideSplash: function () { var $this = this; var timeout = 1200; @@ -611,14 +609,12 @@ Fluidbook.prototype = { , showAllButtons: function () { $("#next,#previous").addClass('help').show(); - } - , + }, setPageNumbers: function () { $("#pagesnumbers .left").html(this.getPageNumberOfSide('left')); $("#pagesnumbers .right").html(this.getPageNumberOfSide('right')); $("#pagesnumbers").removeClass('hidden'); - } - , + }, getPhysicalPageNumberOfSide: function (side) { // Side should be 'left' or 'right' var pageSide = $("#currentDoublePage").find('.' + side); if (pageSide.length == 0) { @@ -626,16 +622,14 @@ Fluidbook.prototype = { } return $(pageSide).data('page'); - } - , + }, getPageNumberOfSide: function (side) { var physical = this.getPhysicalPageNumberOfSide(side); if (!physical || physical === 1) { return ''; } return this.physicalToVirtual(physical); - } - , + }, clickLogo: function () { if (this.datas.url_link == '' || this.datas.url_link == 'http://') { return; @@ -646,8 +640,7 @@ Fluidbook.prototype = { } else { this.wopen(this.datas.url_link, '_blank'); } - } - , + }, addWaiter: function (reset) { if (reset == undefined) { @@ -659,20 +652,16 @@ Fluidbook.prototype = { 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 = []; - } - , + }, physicalToVirtual: function (page) { return this.datas.numerotation[page - 1]; - } - , + }, virtualToPhysical: function (page) { if (page === undefined) { return false; @@ -682,30 +671,25 @@ Fluidbook.prototype = { return false; } return i + 1; - } - , + }, hideMenuItems: function () { $('#menuList > ul > li, #shareLinks').fadeOut(100); - } - , + }, showMenuItems: function () { $('#menuList > ul > li, #shareLinks').fadeIn(300); - } - , + }, print: function (button) { this.stats.track(3); return this.openPDF(button, true); - } - , + }, downloadPDF: function (button) { this.stats.track(7); return this.openPDF(button, false) - } - , + }, openPDF: function (button, print) { var $this = this; @@ -723,8 +707,7 @@ Fluidbook.prototype = { pdfName = e.pop(); this._openFile(pdf, button, 'pdf', pdfName, print); - } - , + }, _openFile: function (url, e, type, localname, print) { if (print === undefined) { @@ -807,8 +790,7 @@ Fluidbook.prototype = { }, function () { }); - } - , + }, _openFilePhonegap: function (url, e, type) { var $this = this; @@ -899,22 +881,19 @@ Fluidbook.prototype = { return true; } return false; - } - , + }, relativeToAbsolute: function (relative) { var a = document.createElement('a'); a.href = relative; return a.cloneNode(false).href; - } - , + }, alertInternetRequired: function () { navigator.notification.alert('', function () { }, this.l10n.__('an internet connection is required for this action')); - } - , + }, touchOffset: function (offset) { offset *= $("#currentDoublePage").width(); @@ -922,15 +901,13 @@ Fluidbook.prototype = { $("#currentDoublePage").css({ translateX: offset }); - } - , + }, displayLoader: function () { $("#loader").addClass('show'); if (!this.support.isMobile) { $('body').addClass('loading'); } - } - , + }, hideLoader: function (delay, force) { if (force == undefined) { @@ -946,8 +923,7 @@ Fluidbook.prototype = { setTimeout(function () { $this._hideLoader(force); }, delay * 1000); - } - , + }, _hideLoader: function (force) { if (force == undefined) { @@ -960,8 +936,7 @@ Fluidbook.prototype = { if (!this.support.isMobile) { $('body').removeClass('loading'); } - } - , + }, wopen: function (url, target, options, print) { var win; @@ -1042,21 +1017,18 @@ Fluidbook.prototype = { } return w; - } - , + }, setFlag: function (name, value) { if (value === undefined) { value = true; } this.flags[name] = value; - } - , + }, hasFlag: function (name) { return this.flags[name] === true; - } - , + }, hasFlags: function (names) { var res = true; @@ -1069,77 +1041,12 @@ Fluidbook.prototype = { return true; }); return res; - } - , - - initNetworkPause: function () { - var $this = this; - setInterval(function () { - - if ($this.networkPauseDate != null && $this.networkPauseDate < Date.now()) { - $this.networkPauseDate = null; - $this.flushNetworkQueue(); - } - }, 1000); - }, - - pauseNetwork: function (during, reset) { - if (reset === undefined) { - reset = false; - } - if (during === -1) { - this.networkPauseDate = null; - this.flushNetworkQueue(); - return; - } - - var date = Date.now() + during + 500; - if (this.networkPauseDate == null || reset) { - this.networkPauseDate = date; - } else { - this.networkPauseDate = Math.max(this.networkPauseDate, date); - } - }, - - executeWhenNetwork: function (f,highPriority) { - if (highPriority === undefined) { - highPriority = false; - } - if (this.networkPauseDate != null) { - if (highPriority) { - this.networkPausePriorityQueue.push([f]); - } else { - this.networkPauseQueue.push([f]); - } - return; - } - f(); }, unlockCurrentPage: function () { this.contentlock.unlockCurrentPage(); }, - flushNetworkQueue: function () { - if ((this.networkPausePriorityQueue.length === 0 && this.networkPauseQueue.length === 0) || this.networkPauseDate != null) { - return; - } - - var f; - if (this.networkPausePriorityQueue.length > 0) { - f = this.networkPausePriorityQueue.shift(); - } else { - f = this.networkPauseQueue.shift(); - } - f(); - - var $this = this; - setTimeout(function () { - $this.flushNetworkQueue(); - }, 100); - } - , - bugreport: function (e) { if (window.confirm('An error occured: ' + e.name + '. Do you want to send a report ?')) { var subject = '[Fluidbook error report] ' + e.message; diff --git a/js/libs/fluidbook/fluidbook.links.js b/js/libs/fluidbook/fluidbook.links.js index 07a28a44..3b72e493 100644 --- a/js/libs/fluidbook/fluidbook.links.js +++ b/js/libs/fluidbook/fluidbook.links.js @@ -193,7 +193,7 @@ FluidbookLinks.prototype = { links.find('.rightContainer').append(links.children('.link.odd')); links.find('.leftContainer').append(links.children('.link')); if (links.find('.link.multimedia iframe').length > 0) { - this.fluidbook.pauseNetwork(30000); + this.fluidbook.networkControl.pause(30000); } if (this.lowdef) { links.find('iframe[data-ld]').each(function () { @@ -398,7 +398,7 @@ FluidbookLinks.prototype = { }); if (pauseNetworkDelay > 0) { - this.fluidbook.pauseNetwork((pauseNetworkDelay + 0.5) * 1000); + this.fluidbook.networkControl.pause((pauseNetworkDelay + 0.5) * 1000); } }, diff --git a/js/libs/fluidbook/fluidbook.loader.js b/js/libs/fluidbook/fluidbook.loader.js index 72b94c58..f643ff97 100644 --- a/js/libs/fluidbook/fluidbook.loader.js +++ b/js/libs/fluidbook/fluidbook.loader.js @@ -113,7 +113,7 @@ FluidbookLoader.prototype = { return; } var $this = this; - this.fluidbook.executeWhenNetwork(function () { + this.fluidbook.networkControl.executeWhenNetwork(function (_cb) { var preloadingPage = $this.toPreload.shift(); var callback = function () { if ($this.toPreload.length === 0) { @@ -121,6 +121,9 @@ FluidbookLoader.prototype = { } else { $this.preloadPages(cb); } + if (_cb !== undefined) { + _cb(); + } }; $this._preloadPage(preloadingPage, callback); diff --git a/js/libs/fluidbook/fluidbook.networkcontrol.js b/js/libs/fluidbook/fluidbook.networkcontrol.js new file mode 100644 index 00000000..0837b4fb --- /dev/null +++ b/js/libs/fluidbook/fluidbook.networkcontrol.js @@ -0,0 +1,96 @@ +function FluidbookNetworkControl(fluidbook) { + this.fluidbook = fluidbook; + this.pauseDate = null; + this.pauseQueue = []; + this.pausePriorityQueue = []; + this.afterProcessingQueue = []; + this.processing = true; + this.init(); +} + +FluidbookNetworkControl.prototype = { + init: function () { + var $this = this; + setInterval(function () { + if ($this.pauseDate != null && $this.pauseDate < Date.now()) { + $this.resume(); + } + }, 1000); + }, + + pause: function (during, reset) { + if (reset === undefined) { + reset = false; + } + + var date = Date.now() + during + 500; + if (this.pauseDate == null || reset) { + this.pauseDate = date; + } else { + this.pauseDate = Math.max(this.pauseDate, date); + } + }, + + resume: function () { + this.pauseDate = null; + this.flushQueue(); + return; + }, + + executeWhenNetwork: function (f, highPriority) { + if (highPriority === undefined) { + highPriority = false; + } + + if (highPriority) { + this.pausePriorityQueue.push(f); + } else { + this.pauseQueue.push(f); + } + + if (this.pauseDate == null) { + this.flushQueue(); + } + }, + + executeAfterProcessing: function (f, loader) { + this.afterProcessingQueue = [f]; + if (!this.processing) { + this.flushAfterProcessingQueue(); + } else { + if (loader !== undefined && loader === true) { + this.fluidbook.displayLoader(); + } + } + }, + + flushAfterProcessingQueue: function () { + $.each(this.afterProcessingQueue, function (k, f) { + f(); + }); + this.afterProcessingQueue = []; + }, + + flushQueue: function () { + if ((this.pausePriorityQueue.length === 0 && this.pauseQueue.length === 0) || this.pauseDate != null) { + this.processing = false; + return; + } + + this.processing = true; + var f; + if (this.pausePriorityQueue.length > 0) { + f = this.pausePriorityQueue.shift(); + } else { + f = this.pauseQueue.shift(); + } + var $this = this; + f(function () { + setTimeout(function () { + $this.flushAfterProcessingQueue(); + $this.processing = false; + $this.flushQueue(); + }, 100); + }); + }, +}; \ No newline at end of file diff --git a/js/libs/fluidbook/fluidbook.pagetransitions.js b/js/libs/fluidbook/fluidbook.pagetransitions.js index 4a41bcd3..997a733f 100644 --- a/js/libs/fluidbook/fluidbook.pagetransitions.js +++ b/js/libs/fluidbook/fluidbook.pagetransitions.js @@ -113,11 +113,11 @@ FluidbookPageTransition.prototype = { }, pauseNetworkDuringTransition: function (pageNr) { - this.fluidbook.pauseNetwork((this.getTransitionDuration(pageNr)) * 1000, true); + this.fluidbook.networkControl.pause((this.getTransitionDuration(pageNr) + 0.5) * 1000, true); }, pauseNetworkDuringPagesPreload: function () { - this.fluidbook.pauseNetwork(30000); + this.fluidbook.networkControl.pause(30000); }, pageTransition3D: function (pageNr) { @@ -193,16 +193,19 @@ FluidbookPageTransition.prototype = { setTimeout(function () { $this.fluidbook.loader.setContentsInDoublePage(currentDoublePage, turning.flat, true, function () { $this.fluidbook.loader.setContentsInDoublePage(doublePage, turning.end, true, function () { - $this.pauseNetworkDuringTransition(pageNr); - $this.flip3d.playTurn(turning.dir, function () { - if ($this.transitionning === false) { - return; - } - // Remove former part - $("#currentDoublePage").remove(); - $(doublePage).attr('id', 'currentDoublePage').removeClass('_3dflip').removeClass('fwd').removeClass('bwd'); - $this.afterTransition(pageNr); - }); + $this.fluidbook.networkControl.executeAfterProcessing(function () { + $this.pauseNetworkDuringTransition(pageNr); + $this.fluidbook.hideLoader(); + $this.flip3d.playTurn(turning.dir, function () { + if ($this.transitionning === false) { + return; + } + // Remove former part + $("#currentDoublePage").remove(); + $(doublePage).attr('id', 'currentDoublePage').removeClass('_3dflip').removeClass('fwd').removeClass('bwd'); + $this.afterTransition(pageNr); + }); + }, true); }); }); }, 10); diff --git a/js/libs/fluidbook/fluidbook.stats.js b/js/libs/fluidbook/fluidbook.stats.js index 2a5f550a..5d32986d 100644 --- a/js/libs/fluidbook/fluidbook.stats.js +++ b/js/libs/fluidbook/fluidbook.stats.js @@ -154,10 +154,11 @@ FluidbookStats.prototype = { }; setTimeout(function () { - $this.fluidbook.executeWhenNetwork(function () { + $this.fluidbook.networkControl.executeWhenNetwork(function (cb) { $this.worker.postMessage(data); + setTimeout(cb, 100); }, false); - }, 500); + }, 2500); } }; diff --git a/js/libs/scorm/scorm.js b/js/libs/scorm/scorm.js index e28ba0a7..00bdf17c 100644 --- a/js/libs/scorm/scorm.js +++ b/js/libs/scorm/scorm.js @@ -103,8 +103,11 @@ function initScormEvents() { $(fluidbook).on('fluidbook.page.navigation', function (e, page) { - fluidbook.executeWhenNetwork(function () { + fluidbook.networkControl.executeWhenNetwork(function (cb) { scormSaveCurrentPosition(page); + if (cb !== undefined) { + setTimeout(cb, 100); + } }, true); }); @@ -115,8 +118,11 @@ function initScormEvents() { // Commit data once a minute setInterval(function () { - fluidbook.executeWhenNetwork(function () { + fluidbook.networkControl.executeWhenNetwork(function (cb) { pipwerks.SCORM.save(); + if (cb !== undefined) { + setTimeout(cb, 100); + } }, true); }, 5000); }