From 7251942692bfef7a3373965f6f577ed7d4d0297b Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 19 Oct 2017 17:42:56 +0200 Subject: [PATCH] wait #813 @5 --- js/libs/fluidbook/fluidbook.js | 15 ++-- js/libs/fluidbook/fluidbook.nav.js | 12 +++- js/libs/fluidbook/fluidbook.sound.js | 103 +++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 js/libs/fluidbook/fluidbook.sound.js diff --git a/js/libs/fluidbook/fluidbook.js b/js/libs/fluidbook/fluidbook.js index cb09085c..5093584e 100644 --- a/js/libs/fluidbook/fluidbook.js +++ b/js/libs/fluidbook/fluidbook.js @@ -49,6 +49,7 @@ Fluidbook.prototype = { this.bookmarks = new FluidbookBookmarks(this); this.tooltip = new FluidbookTooltip(this); this.audiodescription = new FluidbookAudioDescription(this); + this.sound = new FluidbookSound(this); if (this.datas.form == 'bulle') { this.form = new FluidbookBulleForm(this); @@ -82,7 +83,6 @@ Fluidbook.prototype = { this.stats = new FluidbookStats(this); this.stats.track(10); - this.initTheme(); this.initKeyboardShortcuts(); }, @@ -162,6 +162,7 @@ Fluidbook.prototype = { this.help.displayAtStartup(); resize(); $(this).trigger('fluidbookready'); + $(this).trigger('fluidbook.ready'); }, loadPlugins: function () { $.each(this.datas.plugins, function (k, plugin) { @@ -394,7 +395,7 @@ Fluidbook.prototype = { $("#pages").addClass('_3dtransition'); $this.loader.setContentsInDoublePage(currentDoublePage, turning.flat, true, function () { $this.loader.setContentsInDoublePage(doublePage, turning.flip, true, function () { - $this.beforeTransition(pageNr, 3); + $this.beforeTransition(pageNr, 3, turning.flip); $(doublePage).addClass(turning.nextFromClass + 'end').one($this.support.getTransitionEndEvent(), function () { if ($this.transitionning == false) { return; @@ -569,7 +570,7 @@ Fluidbook.prototype = { this.displayLoader(); this.loader.preloadPagesBeforeTransition(turning.end, function () { $this.loader.setContentsInDoublePage(doublePage, turning.end, true, function () { - $this.beforeTransition(pageNr, 2); + $this.beforeTransition(pageNr, 2, turning); $("#currentDoublePage").addClass('_2d').addClass('axis_' + $this.transitionAxis).addClass(turning.currentToClass); $(doublePage).removeClass(turning.nextFromClass).one($this.support.getTransitionEndEvent(), function (event) { if ($this.transitionning == false) { @@ -595,7 +596,7 @@ Fluidbook.prototype = { this.displayLoader(); this.loader.preloadPagesBeforeTransition(turning.end, function () { $this.loader.setContentsInDoublePage(doublePage, turning.end, true, function () { - $this.beforeTransition(pageNr, 2); + $this.beforeTransition(pageNr, 2, turning); $("#currentDoublePage").addClass('axis_' + $this.transitionAxis).addClass('_2d').addClass(turning.currentToClass); $(doublePage).removeClass(turning.nextFromClass).one($this.support.getTransitionEndEvent(), function () { if ($this.transitionning == false) { @@ -616,16 +617,16 @@ Fluidbook.prototype = { this.hidePage('right'); } var turning = this.getTurningPages(pageNr); - this.beforeTransition(pageNr); + this.beforeTransition(pageNr,1, turning); this.loader.setContentsInDoublePage(doublePage, turning.end, true, function () { $this.afterTransition(page); }); }, - beforeTransition: function (page, d) { + beforeTransition: function (page, d, turning) { if (d == undefined) { d = 1; } - $(this).trigger('fluidbook.page.change.start', [page]); + $(this).trigger('fluidbook.page.change.start', [page, {transition: d, page: page, turningPages: turning}]); this.tooltip.hideTooltip(); $(".axis_y").removeClass('axis_y'); $(".axis_x").removeClass('axis_x'); diff --git a/js/libs/fluidbook/fluidbook.nav.js b/js/libs/fluidbook/fluidbook.nav.js index 193cefc4..23967d51 100644 --- a/js/libs/fluidbook/fluidbook.nav.js +++ b/js/libs/fluidbook/fluidbook.nav.js @@ -221,6 +221,9 @@ FluidbookNav.prototype = { * __('share on twitter') * __('home') * __('full screen') + * __('switch on / switch off the sound') + * __('switch off the sound') + * __('switch on the sound') */ var $this = this, navOrder = this.fluidbook.datas.navOrder; // Default desktop order, maybe be overridden later @@ -317,6 +320,7 @@ FluidbookNav.prototype = { var icon = navOrder[i]; //var visible = hide.indexOf(icon) == -1; var link = null; + var link2 = null; if (icon == 'home' && !skipHome) { @@ -419,6 +423,9 @@ FluidbookNav.prototype = { } else if (icon == 'fullscreen' && Modernizr.fullscreen && !DATAS.phonegap) { link = this.addLink(navType, 'nav-' + icon, '#', icon, 'full screen', 'switch between fullscreen and normal'); + } else if (icon == 'sound' && this.fluidbook.sound.enabled) { + link = this.addLink(navType, 'nav-sound-on', '#', 'sound-on', 'switch off the sound', 'switch on / switch off the sound'); + link2 = this.addLink(navType, 'nav-sound-off', '#', 'sound-off', 'switch on the sound', 'switch on / switch off the sound'); } else if (icon == 'search') { // Only the horizontal icon menu has the search icon, which opens the main menu @@ -451,6 +458,9 @@ FluidbookNav.prototype = { } $nav.find('ul').append('
  • ' + link + '
  • '); + if (link2 !== null) { + $nav.find('ul').append('
  • ' + link2 + '
  • '); + } } @@ -653,7 +663,7 @@ FluidbookNav.prototype = { // Search form handler $(document).on('submit', '#searchForm', function () { - $this.fluidbook.search.submitForm(); + $this.fluidbook.search.submitForm(); return false; }); diff --git a/js/libs/fluidbook/fluidbook.sound.js b/js/libs/fluidbook/fluidbook.sound.js new file mode 100644 index 00000000..4ff37f9e --- /dev/null +++ b/js/libs/fluidbook/fluidbook.sound.js @@ -0,0 +1,103 @@ +function FluidbookSound(fluidbook) { + this.fluidbook = fluidbook; + + if (this.fluidbook.datas.soundTheme == '' || !Modernizr.audio || this.fluidbook.support.iOS || this.fluidbook.support.android) { + this.enabled = false; + this.on = false; + return; + } + + this.enabled = true; + this.on = !!this.fluidbook.datas.soundOn; + this.audios = []; + this.initEvents(); +} + +FluidbookSound.prototype = { + initEvents: function () { + var $this = this; + $(this.fluidbook).on('fluidbook.ready', function () { + $this.init(); + }); + $(document).on('click touchend', '.icon-sound-off,.icon-sound-on', function () { + $this.toggle(); + return false; + }); + + $(this.fluidbook).on('fluidbook.page.change.start', function (e, page, data) { + $this.playSoundForPage(data); + }); + }, + + init: function () { + if (!this.on) { + this.disable(); + } else { + this.enable(); + } + }, + + toggle: function () { + if (this.on) { + this.disable(); + } else { + this.enable(); + } + }, + + enable: function () { + this.on = true; + $(".icon-sound-off").hide(); + $(".icon-sound-on").show(); + }, + + disable: function () { + this.on = false; + $(".icon-sound-on").hide(); + $(".icon-sound-off").show(); + this.stopAllSounds(); + }, + + playSoundForPage: function (data) { + if (!this.enabled || !this.on || data.transition < 3) { + return; + } + + this.stopAllSounds(); + + var page = data.page; + if (page % 2 == 1) { + page--; + } + + var last = this.fluidbook.datas.pages; + if (last % 2 == 1) { + last++; + } + + var sound = ''; + if (data.turningPages.indexOf(1) >= 0 || data.turningPages.indexOf(last) >= 0) { + sound = 'cover-flip'; + } else { + sound = 'page-flip-' + (Math.round(Math.random() + 1)); + } + + var audio = new Audio('data/sounds/' + sound + '.mp3'); + audio.volume = 1; + audio.play(); + + this.audios.push(audio); + }, + + stopAllSounds: function () { + $.each(this.audios, function (k, audio) { + try { + audio.pause(); + } catch (err) { + + } + audio = null; + }); + this.audios = []; + }, +}; \ No newline at end of file -- 2.39.5