From f91b6cf4134f63621cebb39f1f0ae35b0b7e686c Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 11 Apr 2018 19:17:13 +0200 Subject: [PATCH] wip #1926 @6 --- js/libs/cube/util.js | 9 ++++ .../cart/fluidbook.cart.remarkable.js | 49 +++++++++++++++++++ js/libs/fluidbook/fluidbook.cart.js | 30 ++++++++++++ js/libs/fluidbook/fluidbook.js | 2 + js/libs/fluidbook/fluidbook.nav.js | 17 ++++++- style/fluidbook.less | 18 +++++++ 6 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 js/libs/fluidbook/cart/fluidbook.cart.remarkable.js create mode 100644 js/libs/fluidbook/fluidbook.cart.js diff --git a/js/libs/cube/util.js b/js/libs/cube/util.js index 93f3c122..b99dabed 100644 --- a/js/libs/cube/util.js +++ b/js/libs/cube/util.js @@ -145,3 +145,12 @@ function getSVGDocument(element){ return subdoc; } } + +Object.size = function(obj) { + var size = 0, key; + for (key in obj) { + if (obj.hasOwnProperty(key)) size++; + } + return size; +}; + diff --git a/js/libs/fluidbook/cart/fluidbook.cart.remarkable.js b/js/libs/fluidbook/cart/fluidbook.cart.remarkable.js new file mode 100644 index 00000000..8142a785 --- /dev/null +++ b/js/libs/fluidbook/cart/fluidbook.cart.remarkable.js @@ -0,0 +1,49 @@ +function FluidbookCartRemarkable(cart) { + this.cart = cart; + this.fluidbook = this.cart.fluidbook; + this.items = {}; + this.init(); +} + +FluidbookCartRemarkable.prototype = { + init: function () { + + }, + addToCart: function (ref, quantity) { + this.setQuantity(ref, quantity, 'add'); + }, + removeFromCart: function (ref) { + this.setQuantity(ref, 0, 'set'); + }, + setQuantity: function (ref, quantity, operator) { + if (operator === undefined) { + operator = 'set'; + } + if (quantity === undefined) { + quantity = 1; + } + if (this.items[ref] == null) { + this.items[ref] = 0; + } + var q = this.items[ref]; + if (operator == 'set') { + q = quantity; + } else if (operator == 'add') { + q += quantity; + } + + if (q <= 0) { + delete this.items[ref]; + } else { + this.items[ref] = q; + } + this.updateIcon(); + }, + getItemsNumbers: function () { + return Object.size(this.items); + }, + updateIcon: function () { + $(this.fluidbook).trigger('fluidbook.cart.updateIcon', {number: this.getItemsNumbers()}); + }, + +}; \ No newline at end of file diff --git a/js/libs/fluidbook/fluidbook.cart.js b/js/libs/fluidbook/fluidbook.cart.js new file mode 100644 index 00000000..22b43185 --- /dev/null +++ b/js/libs/fluidbook/fluidbook.cart.js @@ -0,0 +1,30 @@ +function FluidbookCart(fluidbook) { + this.fluidbook = fluidbook; + this.enabled = false; + this.instance = null; + this.init(); +} + +FluidbookCart.prototype = { + init: function () { + if (!this.fluidbook.datas.basket) { + return false; + } + var $this = this; + this.enabled = true; + this.instance = this.createInstance(); + + $(document).on('click', '[data-cart-ref]', function () { + $this.instance.addToCart($(this).data('cart-ref')); + return false; + }); + }, + createInstance: function () { + switch (this.fluidbook.datas.basketManager) { + case "Remarkable": + return new FluidbookCartRemarkable(this); + default: + return null; + } + }, +} \ No newline at end of file diff --git a/js/libs/fluidbook/fluidbook.js b/js/libs/fluidbook/fluidbook.js index 1fad66fa..47031ea0 100644 --- a/js/libs/fluidbook/fluidbook.js +++ b/js/libs/fluidbook/fluidbook.js @@ -53,6 +53,7 @@ Fluidbook.prototype = { this.tooltip = new FluidbookTooltip(this); this.audiodescription = new FluidbookAudioDescription(this); this.sound = new FluidbookSound(this); + this.cart =new FluidbookCart(this); if (this.datas.form == 'bulle') { this.form = new FluidbookBulleForm(this); @@ -88,6 +89,7 @@ Fluidbook.prototype = { this.initTheme(); this.initKeyboardShortcuts(); }, + initTheme: function () { if (this.datas.arrowsTheme) { $('html').addClass('sharp'); diff --git a/js/libs/fluidbook/fluidbook.nav.js b/js/libs/fluidbook/fluidbook.nav.js index 02f245e5..68a873fd 100644 --- a/js/libs/fluidbook/fluidbook.nav.js +++ b/js/libs/fluidbook/fluidbook.nav.js @@ -322,7 +322,6 @@ FluidbookNav.prototype = { var link2 = null; if (icon == 'home' && !skipHome) { - var homeURL = this.fluidbook.datas.home; if (this.fluidbook.datas.home == '%apphome%' || forceHome && DATAS.phonegap) { homeURL = decodeURIComponent(window.localStorage.getItem('apphome')); @@ -393,6 +392,22 @@ FluidbookNav.prototype = { link = this.addLink(navType, 'nav-download', '#', 'download', 'download', 'download'); } else if (icon == 'print' && this.fluidbook.datas.print) { link = this.addLink(navType, 'nav-print', '#', 'print', 'print', 'print'); + } else if (icon == 'basket' && this.fluidbook.cart.enabled) { + link = this.addLink(navType, 'nav-basket', '#', 'cart', 'basket', 'basket'); + $(this.fluidbook).on('fluidbook.cart.updateIcon', {link: link.attr('id')}, function (e, data) { + var n = data.number; + var l = $("#" + e.data.link); + + if (n === 0) { + $(l).find('span.number').remove(); + } else { + if ($(l).find('span.number').length === 0) { + $(l).append(''); + } + console.log($(l).find('span.number')); + $(l).find('span.number').text(n); + } + }); } else if (icon == 'lang' && this.fluidbook.l10n.multilangEnabled) { // Note: the "!" at the beginning of the title/help parameters means that we don't want these strings translated link = this.addLink(navType, 'nav-locales', '#/locales', 'locales', '!' + this.fluidbook.l10n.getCurrentLanguageName(), '!Select Language'); diff --git a/style/fluidbook.less b/style/fluidbook.less index 942e04d8..c4d2a9ba 100644 --- a/style/fluidbook.less +++ b/style/fluidbook.less @@ -707,6 +707,10 @@ header { #nav { position: relative; white-space: nowrap; + + #locales { + background-color: @icon-color; + } } input[type="search"]::-webkit-search-decoration, @@ -841,6 +845,20 @@ a.bookmark { } } +/* Cart */ +.icon-cart { + position: relative; + span.number { + position: absolute; + top: 0.7em; + left: 0; + color: @icon-color; + text-align: center; + width: 100%; + font-size: 0.7em; + } +} + // Hack for #1433 html.ios body.portrait #interface { -moz-transition: none; -- 2.39.5