From f170bb4675be6d3312d9c85a7e2739c87f22aa56 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 23 Oct 2018 18:07:26 +0200 Subject: [PATCH] wip #2277 @4 --- js/libs/fluidbook/fluidbook.bookmarks.js | 6 +- js/libs/fluidbook/fluidbook.contentlock.js | 130 ++++++++++++++++++ js/libs/fluidbook/fluidbook.js | 70 ++-------- js/libs/fluidbook/fluidbook.links.js | 5 + js/libs/fluidbook/fluidbook.loader.js | 10 +- js/libs/fluidbook/fluidbook.search.js | 8 +- js/libs/fluidbook/fluidbook.slider.js | 12 +- js/libs/fluidbook/fluidbook.sound.js | 2 +- js/libs/fluidbook/fluidbook.video.js | 23 ++-- .../fluidbook/forms/fluidbook.form.bourbon.js | 2 +- js/libs/fluidbook/menu/fluidbook.chapters.js | 2 +- js/libs/fluidbook/menu/fluidbook.index.js | 4 +- js/libs/scorm/scorm.js | 4 +- 13 files changed, 188 insertions(+), 90 deletions(-) create mode 100644 js/libs/fluidbook/fluidbook.contentlock.js diff --git a/js/libs/fluidbook/fluidbook.bookmarks.js b/js/libs/fluidbook/fluidbook.bookmarks.js index 7d65ddd2..1a5bdeac 100644 --- a/js/libs/fluidbook/fluidbook.bookmarks.js +++ b/js/libs/fluidbook/fluidbook.bookmarks.js @@ -93,7 +93,7 @@ FluidbookBookmarks.prototype = { var re = 0; for (var i = 0; i < this.bookmarks.length; i++) { var b = this.bookmarks[i]; - if (b > this.fluidbook.getMaxPage()) { + if (b > this.fluidbook.contentlock.getMaxPage()) { continue; } if (rs == 0) { @@ -195,7 +195,7 @@ FluidbookBookmarks.prototype = { var res = []; var nb; var groupId; - for (var i = 1; i <= this.fluidbook.getMaxPage();) { + for (var i = 1; i <= this.fluidbook.contentlock.getMaxPage();) { if (this.isBookmarked(i) || !onlyBookmarked) { groupId = this.getGroupOfPage(i); nb = this.getPagesNumberInGroup(groupId); @@ -360,7 +360,7 @@ FluidbookBookmarks.prototype = { var bookmarks = ""; for (var i = pageNr; i <= to; i++) { - if (i > 0 && i <= this.fluidbook.getMaxPage()) { + if (i > 0 && i <= this.fluidbook.contentlock.getMaxPage()) { var side; if (allwaysAtRight) { side = 'right'; diff --git a/js/libs/fluidbook/fluidbook.contentlock.js b/js/libs/fluidbook/fluidbook.contentlock.js new file mode 100644 index 00000000..489a4cee --- /dev/null +++ b/js/libs/fluidbook/fluidbook.contentlock.js @@ -0,0 +1,130 @@ +function FluidbookContentLock(fluidbook) { + this.fluidbook = fluidbook; + this.maxPage; + this.linksActions = {}; + this.locks = this.fluidbook.datas.content_lock; +} + +FluidbookContentLock.prototype = { + init: function () { + var $this = this; + + key('⌘+alt+u, ctrl+alt+u', function () { + $this.setMaxPage(); + }); + + this.maxPage = this.getNextLockPage(); + if (this.maxPage <= 0) { + this.maxPage = this.fluidbook.datas.pages; + } + }, + + getNextLockPage: function () { + var res = 0; + + $.each(this.locks, function (k, v) { + if (v.unlocked === 1) { + return; + } + res = k; + return true; + }); + return res; + }, + + setMaxPage: function (p, allowbackwards) { + var currentMaxPage = this.maxPage; + if (allowbackwards === undefined) { + allowbackwards = false; + } + if (p === undefined || p <= 0) { + p = this.fluidbook.datas.pages; + } + + if (!allowbackwards && p < this.maxPage) { + return; + } + + this.maxPage = Math.min(p, this.fluidbook.datas.pages); + if (currentMaxPage === this.maxPage) { + return; + } + $(this.fluidbook).trigger('fluidbook.maxpage.set', [this.maxPage]); + this.updateMaxPage(); + }, + + getMaxPage: function () { + return this.maxPage; + }, + + updateMaxPage: function () { + if (this.fluidbook.currentPage > this.maxPage) { + this.fluidbook.setCurrentPage(this.maxPage); + } + + var $this = this; + $.each(this.locks, function (k, v) { + if ($this.maxPage > k) { + $this.locks.unlocked = 1; + } + } + ); + + this.fluidbook.hideUnnecessaryButtons(); + resize(); + }, + + addAction: function (linkId, action) { + + if (this.linksActions[linkId] === undefined) { + this.linksActions[linkId] = []; + } + if (this.linksActions[linkId].indexOf(action) === -1) { + this.linksActions[linkId].push(action); + } + + console.log(this.linksActions); + + this.testConditions(); + }, + + testConditions: function () { + var $this=this; + var change = false; + $.each(this.locks, function (k, v) { + if (v.unlocked === 1) { + return; + } + if (v.conditions.length === 0) { + return; + } + + var conditionsToObserve = v.conditions.length; + $.each(v.conditions, function (i, c) { + if ($this.testCondition(c)) { + conditionsToObserve--; + } + }); + + if (conditionsToObserve === 0) { + $this.locks[k].unlocked = 1; + change = true; + } + }); + + if (change) { + this.setMaxPage(this.getNextLockPage(), false) + } + }, + + testCondition: function (condition) { + var linkId = condition[0]; + var action = condition[1]; + + if (this.linksActions[linkId] === undefined) { + return false; + } + + return this.linksActions[linkId].indexOf(action) >= 0; + }, +} \ No newline at end of file diff --git a/js/libs/fluidbook/fluidbook.js b/js/libs/fluidbook/fluidbook.js index 1dbac033..71154645 100644 --- a/js/libs/fluidbook/fluidbook.js +++ b/js/libs/fluidbook/fluidbook.js @@ -24,6 +24,7 @@ Fluidbook.prototype = { if (this.datas.landingPage != undefined && this.datas.landingPage != '') { this.landingpage = new FluidbookLandingPage(this); } + this.contentlock = new FluidbookContentLock(this); this.menu = new FluidbookMenu(this); this.zoom = new FluidbookZoom(this); this.zoom.resetZoom(); @@ -88,7 +89,7 @@ Fluidbook.prototype = { $('html').addClass(this.datas.mobileLVersion); this.currentPage = -1; - this.initMaxPage(); + this.contentlock.init(); this.resize = new FluidbookResize(this); this.stats = new FluidbookStats(this); @@ -104,51 +105,8 @@ Fluidbook.prototype = { }, 10); }, - initMaxPage: function () { - var $this = this; - key('⌘+alt+r, ctrl+alt+u', function () { - $this.setMaxPage(0); - }); - - this.maxPage = this.datas.pages; - if (this.datas.maxPages > 0) { - this.maxPage = Math.min(this.datas.maxPages, this.datas.pages); - } - }, - setMaxPage: function (p, allowbackwards) { - var currentMaxPage = this.maxPage; - if (allowbackwards === undefined) { - allowbackwards = false; - } - if (p === undefined || p <= 0) { - p = this.datas.pages; - } - - if (!allowbackwards && p < this.maxPage) { - return; - } - - this.maxPage = Math.min(p, this.datas.pages); - if (currentMaxPage === this.maxPage) { - return; - } - $(this).trigger('fluidbook.maxpage.set', [this.maxPage]); - this.updateMaxPage(); - }, - - getMaxPage: function () { - return this.maxPage; - }, - - updateMaxPage: function () { - if (this.currentPage > this.maxPage) { - this.setCurrentPage(this.maxPage); - } else { - //this.reloadCurrentPage(); - } - this.hideUnnecessaryButtons(); - resize(); + return this.contentlock.setMaxPage(p, allowbackwards); }, initTheme: function () { @@ -174,7 +132,6 @@ Fluidbook.prototype = { }); }, initKeyboardShortcuts: function () { - // General keyboard shortcuts key('home', this.goFirstPage.bind(this)); key('end', this.goLastPage.bind(this)); @@ -311,7 +268,7 @@ Fluidbook.prototype = { return; } this.transitionAxis = 'x'; - this.setCurrentPage(this.getMaxPage()); + this.setCurrentPage(this.contentlock.getMaxPage()); }, goNextChapter: function () { if (this.transitionning) { @@ -360,7 +317,7 @@ Fluidbook.prototype = { this.setCurrentPage(this.normalizePage(prev)); }, normalizePage: function (page) { - page = Math.max(1, Math.min(page, this.getMaxPage())); + page = Math.max(1, Math.min(page, this.contentlock.getMaxPage())); if (!this.displayOnePage && page % 2 == 1) { page--; } @@ -602,7 +559,7 @@ Fluidbook.prototype = { if (center) { if (newPage <= 1) { res.center = -1; - } else if (this.getMaxPage() % 2 == 0 && newPage == this.getMaxPage()) { + } else if (this.contentlock.getMaxPage() % 2 == 0 && newPage == this.contentlock.getMaxPage()) { res.center = 1; } } @@ -642,7 +599,7 @@ Fluidbook.prototype = { speed = 0; } - var max = this.getMaxPage() % 2 == 1 ? this.getMaxPage() - 1 : this.getMaxPage(); + var max = this.contentlock.getMaxPage() % 2 == 1 ? this.contentlock.getMaxPage() - 1 : this.contentlock.getMaxPage(); var next = page < max; var previous = (page > 1); @@ -692,9 +649,9 @@ Fluidbook.prototype = { if (this.displayOnePage) { right = true; } else { - if ((page <= 1 && this.l10n.dir == 'ltr') || (page >= this.getMaxPage() && this.l10n.dir == 'rtl')) { + if ((page <= 1 && this.l10n.dir == 'ltr') || (page >= this.contentlock.getMaxPage() && this.l10n.dir == 'rtl')) { left = false; - } else if ((page <= 1 && this.l10n.dir == 'rtl') || (page >= this.getMaxPage() && this.l10n.dir == 'ltr')) { + } else if ((page <= 1 && this.l10n.dir == 'rtl') || (page >= this.contentlock.getMaxPage() && this.l10n.dir == 'ltr')) { right = false; } } @@ -790,6 +747,7 @@ Fluidbook.prototype = { $(".axis_y").removeClass('axis_y'); $(".axis_x").removeClass('axis_x'); $("#links").hide(); + this.scorm.hideScormLinks(); this.hideLoader(); this.hideUnnecessaryButtons(page); var animationDuration = d <= 1 ? 0 : parseFloat(this.datas.mobileTransitionDuration); @@ -827,7 +785,7 @@ Fluidbook.prototype = { } this.transitionning = false; if (this.pad.enabled) { - if (this.currentPage == this.getMaxPage()) { + if (this.currentPage == this.contentlock.getMaxPage()) { $("#down").css('opacity', 0); } else { $("#down").css('opacity', 1); @@ -925,10 +883,10 @@ Fluidbook.prototype = { var pdf; var pdfName; - console.log('open PDF : ' + this.datas.pages + " || " + this.getMaxPage()); + console.log('open PDF : ' + this.datas.pages + " || " + this.contentlock.getMaxPage()); - if (this.datas.pages != this.getMaxPage()) { - pdf = 'https://workshop.fluidbook.com/s/e/' + this.datas.cid + '/1-' + this.getMaxPage(); + if (this.datas.pages != this.contentlock.getMaxPage()) { + pdf = 'https://workshop.fluidbook.com/s/e/' + this.datas.cid + '/1-' + this.contentlock.getMaxPage(); } else if (this.datas.pdfName.substr(0, 4) == 'http') { pdf = this.datas.pdfName; } else { diff --git a/js/libs/fluidbook/fluidbook.links.js b/js/libs/fluidbook/fluidbook.links.js index 081f30a7..1088b162 100644 --- a/js/libs/fluidbook/fluidbook.links.js +++ b/js/libs/fluidbook/fluidbook.links.js @@ -17,6 +17,11 @@ FluidbookLinks.prototype = { this.lowdef = this.fluidbook.support.android || this.fluidbook.support.iOS; + $(document).on('click', '[data-id] a', function () { + $this.fluidbook.contentlock.addAction($(this).closest('[data-id]').attr('data-id'), 'click'); + return true; + }); + $(document).on('click', '[href^="#"]:not([href="#"])', function () { location.hash = $(this).attr('href'); return false; diff --git a/js/libs/fluidbook/fluidbook.loader.js b/js/libs/fluidbook/fluidbook.loader.js index 724df37d..6fa2d920 100644 --- a/js/libs/fluidbook/fluidbook.loader.js +++ b/js/libs/fluidbook/fluidbook.loader.js @@ -30,7 +30,7 @@ FluidbookLoader.prototype = { var $callback = callback; var $page = $pages.shift(); - if ($page > this.fluidbook.getMaxPage() || $page < 1) { + if ($page > this.fluidbook.contentlock.getMaxPage() || $page < 1) { $this.preloadPagesBeforeTransition($pages, $callback); return; } @@ -91,7 +91,7 @@ FluidbookLoader.prototype = { numPreloadAfter = 3; numPreloadBefore = 1; } - var max = Math.min(page + numPreloadAfter, this.fluidbook.getMaxPage()); + var max = Math.min(page + numPreloadAfter, this.fluidbook.contentlock.getMaxPage()); var min = Math.max(1, page - numPreloadBefore); this.toPreload = []; for (var i = min; i <= max; i++) { @@ -102,7 +102,7 @@ FluidbookLoader.prototype = { this.preloadPages(); }, cleanPreloaded: function () { - for (var i = 1; i <= this.fluidbook.getMaxPage(); i++) { + for (var i = 1; i <= this.fluidbook.contentlock.getMaxPage(); i++) { if (this.backgrounds[i] != undefined && this.toPreload.indexOf(i) == -1) { this.deletePage(i); } @@ -188,7 +188,7 @@ FluidbookLoader.prototype = { return this.loadImage('images/shadows/pages/' + position + '.png', this.fluidbook.datas.width / 4, this.fluidbook.datas.height); }, loadLeftPage: function (page, doublePage, callback) { - if (page > 0 && page <= this.fluidbook.getMaxPage()) { + if (page > 0 && page <= this.fluidbook.contentlock.getMaxPage()) { this.loadPage(page, doublePage, 'left', callback); } else { $(doublePage).find('.left').remove(); @@ -196,7 +196,7 @@ FluidbookLoader.prototype = { } }, loadRightPage: function (page, doublePage, callback) { - if (!this.fluidbook.displayOnePage && page <= this.fluidbook.getMaxPage() && page > 0) { + if (!this.fluidbook.displayOnePage && page <= this.fluidbook.contentlock.getMaxPage() && page > 0) { this.loadPage(page, doublePage, 'right', callback); } else { $(doublePage).find('.right').remove(); diff --git a/js/libs/fluidbook/fluidbook.search.js b/js/libs/fluidbook/fluidbook.search.js index f76cb1e8..2653f206 100644 --- a/js/libs/fluidbook/fluidbook.search.js +++ b/js/libs/fluidbook/fluidbook.search.js @@ -119,7 +119,7 @@ FluidbookSearch.prototype = { this.resultPages = []; var q, v, k, kk, word, wordata, page, occurences, p; - var maxPage = this.fluidbook.getMaxPage(); + var maxPage = this.fluidbook.contentlock.getMaxPage(); for (kk in words) { q = words[kk]; @@ -192,7 +192,7 @@ FluidbookSearch.prototype = { var terms = []; var total = 0; var doublesPages = []; - var maxPage = this.fluidbook.getMaxPage(); + var maxPage = this.fluidbook.contentlock.getMaxPage(); for (var p in TEXTS) { var t = TEXTS[p]; @@ -356,7 +356,7 @@ FluidbookSearch.prototype = { } pageNrs.push(pageNr); pageNr++; - if (pageNr < this.fluidbook.getMaxPage()) { + if (pageNr < this.fluidbook.contentlock.getMaxPage()) { pageNrs.push(pageNr); } } @@ -556,7 +556,7 @@ FluidbookSearch.prototype = { var hits = []; // Create a list of all pages so we can record which ones have a hit on the search term - for (var i = 0; i <= this.fluidbook.getMaxPage(); i++) { + for (var i = 0; i <= this.fluidbook.contentlock.getMaxPage(); i++) { hits[i] = 0; } // Map result hits to pages diff --git a/js/libs/fluidbook/fluidbook.slider.js b/js/libs/fluidbook/fluidbook.slider.js index 80a40603..852f1f99 100644 --- a/js/libs/fluidbook/fluidbook.slider.js +++ b/js/libs/fluidbook/fluidbook.slider.js @@ -108,7 +108,7 @@ FluidbookSlider.prototype = { pageMin = 0; } - return Math.min(this.fluidbook.getMaxPage(), Math.max(pageMin, page)); + return Math.min(this.fluidbook.contentlock.getMaxPage(), Math.max(pageMin, page)); }, resize: function (ww, hh, single) { @@ -137,9 +137,9 @@ FluidbookSlider.prototype = { updateSnaps: function (single) { if (single) { - this.snapsCount = this.fluidbook.getMaxPage(); + this.snapsCount = this.fluidbook.contentlock.getMaxPage(); } else { - this.snapsCount = Math.floor(this.fluidbook.getMaxPage() / 2) + 1; + this.snapsCount = Math.floor(this.fluidbook.contentlock.getMaxPage() / 2) + 1; } this.cursorWidth = Math.max(30, this.sliderWidth / this.snapsCount); this.snapsWidth = (this.sliderWidth - this.cursorWidth) / (this.snapsCount - 1); @@ -164,7 +164,7 @@ FluidbookSlider.prototype = { getCursorXByPage: function (page) { var left; if (this.fluidbook.l10n.rtl) { - page = this.fluidbook.getMaxPage() - page; + page = this.fluidbook.contentlock.getMaxPage() - page; } if (this.fluidbook.resize.orientation == 'portrait') { left = this.snapsWidth * (page - 1); @@ -189,7 +189,7 @@ FluidbookSlider.prototype = { if (page > 0) { left = page; } - if (page <= this.fluidbook.getMaxPage()) { + if (page <= this.fluidbook.contentlock.getMaxPage()) { right = page + 1; } } @@ -229,7 +229,7 @@ FluidbookSlider.prototype = { setThumb: function (thumb, page, shade) { thumb.find('.bookmark').attr('data-page', page); - if (page > 0 && page <= this.fluidbook.getMaxPage()) { + if (page > 0 && page <= this.fluidbook.contentlock.getMaxPage()) { thumb.css('visibility', 'visible'); this.fluidbook.loader.getThumbImage(page, thumb.find('.img'), shade); thumb.find('a').attr('href', '#/page/' + page); diff --git a/js/libs/fluidbook/fluidbook.sound.js b/js/libs/fluidbook/fluidbook.sound.js index 63a4d50a..011e542b 100644 --- a/js/libs/fluidbook/fluidbook.sound.js +++ b/js/libs/fluidbook/fluidbook.sound.js @@ -80,7 +80,7 @@ FluidbookSound.prototype = { page--; } - var last = this.fluidbook.getMaxPage(); + var last = this.fluidbook.contentlock.getMaxPage(); if (last % 2 == 1) { last++; } diff --git a/js/libs/fluidbook/fluidbook.video.js b/js/libs/fluidbook/fluidbook.video.js index 9cd918e3..806df50e 100644 --- a/js/libs/fluidbook/fluidbook.video.js +++ b/js/libs/fluidbook/fluidbook.video.js @@ -16,27 +16,26 @@ function FluidbookVideo(fluidbook) { $this.resizeControls(); }); - $(window).on('videoFullscreenEntered', function() { + $(window).on('videoFullscreenEntered', function () { //console.log('>>> Video player entered full screen mode...'); $this.fullscreenActive = true; }); - $(window).on('videoFullscreenExited', function() { + $(window).on('videoFullscreenExited', function () { //console.log('<<< Video player exited full screen mode.'); // Try resizing after a short delay. Depending on the system and the speed of the - setTimeout(function() { + setTimeout(function () { $this.fluidbook.resize.resize(); }, 250); - setTimeout(function() { + setTimeout(function () { $this.fullscreenActive = false; // Stop blocking orientation change / page updates $this.fluidbook.resize.resize(); }, 1000); }); - this.fluidbook = fluidbook; this.video = (Modernizr.video && (Modernizr.video.h264 || Modernizr.video.webm || Modernizr.video.ogg)) != false; @@ -99,6 +98,7 @@ FluidbookVideo.prototype = { sound = $(e).data('sound'), autoplay = $(e).data('autoplay'), setup = $(e).data('setup'), + linkid = $(e).data('link-id'), path, poster, html, @@ -218,15 +218,17 @@ FluidbookVideo.prototype = { player.pause(); }, 100); } - }); - player.play(); // Start player to go to current position - necessary even if it will be paused immediately if (settings.paused) { player.pause(); } + setTimeout(function () { + $this.fluidbook.contentlock.addAction(linkid, 'complete'); + }, (player.duration() - 5) * 1000); + // var playPromise = player.play(); // if (settings.paused) { // if (playPromise && (typeof Promise !== 'undefined') && (playPromise instanceof Promise)) { @@ -260,7 +262,7 @@ FluidbookVideo.prototype = { // $('#' + player.id()).attr('style', ''); // Reset inline styles // }); - player.on('fullscreenchange', function() { + player.on('fullscreenchange', function () { if (player.isFullscreen()) { $(window).trigger('videoFullscreenEntered'); } else { @@ -268,6 +270,9 @@ FluidbookVideo.prototype = { } }); + player.on('ended', function () { + $this.fluidbook.contentlock.addAction(linkid, 'complete'); + }); }, openVideo: function (link) { if (link === undefined) return false; @@ -370,7 +375,7 @@ FluidbookVideo.prototype = { if (skipPopupVideos) { // Filter out any videos that exist inside the #view element playersToBeRemoved = playersToBeRemoved.filter(function (player) { - return ! $.contains(document.getElementById('view'), document.getElementById(player.id())); + return !$.contains(document.getElementById('view'), document.getElementById(player.id())); }); } diff --git a/js/libs/fluidbook/forms/fluidbook.form.bourbon.js b/js/libs/fluidbook/forms/fluidbook.form.bourbon.js index b2292a48..ad81b720 100644 --- a/js/libs/fluidbook/forms/fluidbook.form.bourbon.js +++ b/js/libs/fluidbook/forms/fluidbook.form.bourbon.js @@ -26,7 +26,7 @@ FluidbookBourbonForm.prototype = { view += '
'; view += '
'; view += ''; view += '