From bd3cc8fa0036217eae352a4c0d441644058d06ee Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 14 Sep 2023 20:26:42 +0200 Subject: [PATCH] wait #6269 @1 --- js/libs/fluidbook/fluidbook.js | 8 +- js/libs/fluidbook/fluidbook.loader.js | 104 +++++++++++++++++++------- js/libs/fluidbook/fluidbook.sound.js | 3 +- 3 files changed, 83 insertions(+), 32 deletions(-) diff --git a/js/libs/fluidbook/fluidbook.js b/js/libs/fluidbook/fluidbook.js index f170c07c..11e5eedc 100644 --- a/js/libs/fluidbook/fluidbook.js +++ b/js/libs/fluidbook/fluidbook.js @@ -226,13 +226,13 @@ Fluidbook.prototype = { $('html').addClass('sharp'); } - if (this.nav.isInverted() ) { + if (this.nav.isInverted()) { $('html').addClass('menu-inverted'); } else { $('html').addClass('menu-default'); } - if(this.nav.isLogoCentered()){ + if (this.nav.isLogoCentered()) { $('#logo').addClass('center'); } @@ -801,7 +801,7 @@ Fluidbook.prototype = { } else if (this.settings.pdfName.substr(0, 4) === 'http') { pdf = this.settings.pdfName; } else { - pdf = this.relativeToAbsolute('data/' + this.settings.pdfName); + pdf = this.relativeToAbsolute(this.loader.getURL('data/' + this.settings.pdfName)); } var e = pdf.split('/'); pdfName = e.pop(); @@ -836,7 +836,7 @@ Fluidbook.prototype = { var w = this.wopen(url, '_blank', ''); if (print) { setTimeout(function () { - // w.print(); + // w.print(); }, 2000); } }, diff --git a/js/libs/fluidbook/fluidbook.loader.js b/js/libs/fluidbook/fluidbook.loader.js index ba0a14c3..9b9e37f1 100644 --- a/js/libs/fluidbook/fluidbook.loader.js +++ b/js/libs/fluidbook/fluidbook.loader.js @@ -17,14 +17,32 @@ function FluidbookLoader(fluidbook) { this.format = fluidbook.settings.imageFormat; this.imageMimeType = this.format === 'jpg' ? 'image/jpeg' : 'image/png'; this._needSeparateTextures = []; - - this.init(); + this.loadbalancing = false; } FluidbookLoader.prototype = { - init: function () { - this.shadeLeft = this.loadImage('images/shadows/pages/left.png'); - this.shadeRight = this.loadImage('images/shadows/pages/right.png'); + init: function (callback) { + let $this = this; + if (this.fluidbook.settings.hosting_loadbalancer) { + $.ajax("https://toolbox.fluidbook.com/lb.json").done(function (data) { + $this.loadbalancing = data; + $this.loadShades(callback); + }).fail(function () { + $this.loadbalancing = false; + $this.loadShades(callback); + }); + } else { + this.loadShades(callback); + } + }, + + loadShades: function (callback) { + console.log('load shades'); + this.shadeLeft = this.loadImage(this.getURL('images/shadows/pages/left.png')); + this.shadeRight = this.loadImage(this.getURL('images/shadows/pages/right.png')); + if (callback) { + callback(); + } }, getPageDimensions: function (page, width) { @@ -55,13 +73,27 @@ FluidbookLoader.prototype = { }, preloadStart: function (callback) { - if (this.fluidbook.shortLoading) { - this.toPreload = [1]; - } else { - this.toPreload = [1, 2, 3]; + let $this = this; + this.init(function () { + $this.fluidbook.sound.preloadSounds(); + if ($this.fluidbook.shortLoading) { + $this.toPreload = [1]; + } else { + $this.toPreload = [1, 2, 3]; + } + $this.preloaded = $this.toPreload.slice(0); + $this.preloadPages(callback); + }); + }, + + balanceLinkAssets(links) { + if (!this.loadbalancing) { + return links; } - this.preloaded = this.toPreload.slice(0); - this.preloadPages(callback); + let $this = this; + return links.replace(/data\/[^"]*/gm, function (matched) { + return $this.getURL(matched); + }); }, preloadPagesBeforeTransition: function (pages, callback) { @@ -210,7 +242,7 @@ FluidbookLoader.prototype = { this.backgrounds[page] = null; $__callback(); } else { - this.backgrounds[page] = this.loadImage(backgroundURL, null, null, null, page, function () { + this.backgrounds[page] = this.loadImage(this.getURL(backgroundURL), null, null, null, page, function () { $__callback(); }); } @@ -281,7 +313,9 @@ FluidbookLoader.prototype = { } $("#loadedcontents [data-page='" + page + "']").remove(); - }, setContentsInDoublePage: function (doublePage, pages, immediate, callback) { + }, + + setContentsInDoublePage: function (doublePage, pages, immediate, callback) { var $this = this; var leftPage = pages[0]; @@ -387,29 +421,35 @@ FluidbookLoader.prototype = { var ext = this.fluidbook.settings.actualHtmlExtension; if (ext === undefined || ext === null || ext === 'html') { - return content; + return this.balanceLinkAssets(content); } var res = content.replace(/data\/([^"]*)\.html/gm, "data/$1." + ext); - return res; + return this.balanceLinkAssets(res); }, loadPageShade: function (position) { - return this.loadImage('images/shadows/pages/' + position + '.png', this.fluidbook.settings.width / 4, this.fluidbook.settings.height); - }, loadLeftPage: function (page, doublePage, callback) { + return this.loadImage(this.getURL('images/shadows/pages/' + position + '.png'), this.fluidbook.settings.width / 4, this.fluidbook.settings.height); + }, + + loadLeftPage: function (page, doublePage, callback) { if (page > 0 && page <= this.fluidbook.contentlock.getMaxPage()) { this.loadPage(page, doublePage, 'left', callback); } else { $(doublePage).find('.left').remove(); callback(); } - }, loadRightPage: function (page, doublePage, callback) { + }, + + loadRightPage: function (page, doublePage, callback) { if (!this.fluidbook.displayOnePage && page <= this.fluidbook.contentlock.getMaxPage() && page > 0) { this.loadPage(page, doublePage, 'right', callback); } else { $(doublePage).find('.right').remove(); callback(); } - }, getBackgroundURL: function (page) { + }, + + getBackgroundURL: function (page) { var version = this.getVersionToLoad(page); if (version === 'vector') { return false; @@ -470,7 +510,7 @@ FluidbookLoader.prototype = { callback(); return false; } - this.backgrounds[page] = this.loadImage(url, null, null, null, page, callback); + this.backgrounds[page] = this.loadImage(this.getURL(url), null, null, null, page, callback); }, needSeparateTextures: function (page) { @@ -554,7 +594,7 @@ FluidbookLoader.prototype = { nb++; var i = $(this).data('id'); var url = $(this).data('image').replace(/^\.\./, 'data') + '?j=' + (new Date()).getTime(); - textures[i] = loadImage(url, cb, 'i_' + i); + textures[i] = loadImage(this.getURL(url), cb, 'i_' + i); }); if (nb === 0) { @@ -630,7 +670,7 @@ FluidbookLoader.prototype = { } } - if($(this).is('[data-layer="bothsvg"]')){ + if ($(this).is('[data-layer="bothsvg"]')) { ctx.fillStyle = '#ffffff'; ctx.fillRect(left, top, width, height); } @@ -690,6 +730,14 @@ FluidbookLoader.prototype = { }); }, + getURL(url) { + if (!this.loadbalancing) { + return url; + } + let server = this.loadbalancing[Math.floor(Math.random() * this.loadbalancing.length)]; + return 'https://' + server + '/' + this.fluidbook.settings.id + '_' + this.fluidbook.settings.hash + '/' + url; + }, + getTexture: function (page, callback, enter) { if (enter === undefined) { enter = true; @@ -870,16 +918,20 @@ FluidbookLoader.prototype = { callback(); }); } else { - this.texts[pageNr] = this.loadImage(url, null, null, 'image/svg+xml', pageNr, callback); + this.texts[pageNr] = this.loadImage(this.getURL(url), null, null, 'image/svg+xml', pageNr, callback); } - }, loadDatas: function (pageNr, callback) { + }, + + loadDatas: function (pageNr, callback) { var $this = this; this.loadTexts(pageNr, function () { $this.setBackground(pageNr, function () { callback(); }); }); - }, getImage: function (src, width, height, type, callback) { + }, + + getImage: function (src, width, height, type, callback) { var img = this.loadImage(src, width, height, type, null, callback); return $(img).get(0).outerHTML; }, @@ -926,7 +978,7 @@ FluidbookLoader.prototype = { } callback(); }; - var img = this.loadImage('data/thumbnails/p' + pageNr + '.' + this.format, null, null, this.imageMimeType, pageNr, cb); + var img = this.loadImage(this.getURL('data/thumbnails/p' + pageNr + '.' + this.format), null, null, this.imageMimeType, pageNr, cb); this.thumbnails[pageNr] = img; return img; }, diff --git a/js/libs/fluidbook/fluidbook.sound.js b/js/libs/fluidbook/fluidbook.sound.js index eee5c39d..641302b6 100644 --- a/js/libs/fluidbook/fluidbook.sound.js +++ b/js/libs/fluidbook/fluidbook.sound.js @@ -13,7 +13,6 @@ function FluidbookSound(fluidbook) { this.playing = null; this.simpleTheme = this.fluidbook.settings.simpleSoundTheme; - this.preloadSounds(); this.initEvents(); } @@ -57,7 +56,7 @@ FluidbookSound.prototype = { } else { src = 'data/sounds/' + v + '.mp3'; } - var s = new Audio(src); + var s = new Audio($this.fluidbook.loader.getURL(src)); s.volume = 1; s.preload = 'auto'; $this.audios[v] = s; -- 2.39.5