From 63d22ebe5483d452107a7bf55be2f179f87b78cc Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 12 Apr 2018 19:25:47 +0200 Subject: [PATCH] wip #1927 @3 --- .../cart/fluidbook.cart.remarkable.js | 39 +++++-- js/libs/fluidbook/fluidbook.cache.js | 10 +- js/libs/fluidbook/fluidbook.cart.js | 106 +++++++++++++++++- js/libs/fluidbook/fluidbook.nav.js | 1 + js/libs/fluidbook/fluidbook.tooltip.js | 3 +- style/fluidbook.less | 43 +++++++ 6 files changed, 186 insertions(+), 16 deletions(-) diff --git a/js/libs/fluidbook/cart/fluidbook.cart.remarkable.js b/js/libs/fluidbook/cart/fluidbook.cart.remarkable.js index 2c309ec3..566c5802 100644 --- a/js/libs/fluidbook/cart/fluidbook.cart.remarkable.js +++ b/js/libs/fluidbook/cart/fluidbook.cart.remarkable.js @@ -2,7 +2,7 @@ function FluidbookCartRemarkable(cart) { var $this = this; this.cart = cart; this.fluidbook = this.cart.fluidbook; - this.items = {}; + this.items = this.cart.getSavedData(); this.database; $.each(this.fluidbook.datas.basketReferences, function (k, v) { $this.database = {}; @@ -21,7 +21,11 @@ function FluidbookCartRemarkable(cart) { FluidbookCartRemarkable.prototype = { init: function () { - + var $this = this; + $(document).on('change', '[data-menu="cart"] .input-number', function () { + $this.updateCart(); + return true; + }); }, addToCart: function (ref, quantity) { this.setQuantity(ref, quantity, 'add'); @@ -51,6 +55,7 @@ FluidbookCartRemarkable.prototype = { } else { this.items[ref] = q; } + this.cart.saveData(this.items); this.updateIcon(); }, getItemsNumbers: function () { @@ -71,26 +76,40 @@ FluidbookCartRemarkable.prototype = { }, openCart: function (p2, callback) { + var $this = this; + this._endMenu(this.fluidbook.l10n.__('your cart'), this.getCartContent(), function () { + $('input[type="number"]').inputNumber(); + + callback(); + }); + }, + + getCartContent: function () { var $this = this; var content = ''; $.each(this.items, function (ref, quantity) { var item = $this.getItemData(ref); - content += ''; content += ''; - content += ''; + content += ''; content += ''; content += ''; content += ''; - content += ''; + content += ''; + content += ''; + }); + content += '
' + item.name + '' + quantity + '' + $this.formatPrice(item.price, 'HT') + '' + $this.formatPrice(item.price * quantity, 'HT') + '' + $this.formatPrice((item.price * quantity) * (1 + item.tax), 'TTC') + '
'; + return content; + }, - content + ''; + updateCart: function () { + var $this = this; + $(".cartqty").each(function () { + $this.setQuantity($(this).attr('name'), parseInt($(this).val()), 'set'); }); - content + ''; - this._endMenu(this.fluidbook.l10n.__('your cart'), content, callback); }, - formatPrice: function (price, currency, suffix) { + formatPrice: function (price, suffix) { if (suffix == undefined) { suffix = ''; } @@ -102,7 +121,6 @@ FluidbookCartRemarkable.prototype = { return this.database[ref]; }, - openShipping: function (p2, callback) { var content = ''; this._endMenu(this.fluidbook.l10n.__('your contact details'), content, callback); @@ -113,7 +131,6 @@ FluidbookCartRemarkable.prototype = { this._endMenu(this.fluidbook.l10n.__('confirmation'), content, callback); }, - _endMenu: function (title, content, callback) { var view = '
' + this.fluidbook.menu.closeButton() + '

' + title + '

'; view += '
'; diff --git a/js/libs/fluidbook/fluidbook.cache.js b/js/libs/fluidbook/fluidbook.cache.js index fd757ce7..22651a41 100644 --- a/js/libs/fluidbook/fluidbook.cache.js +++ b/js/libs/fluidbook/fluidbook.cache.js @@ -35,13 +35,19 @@ FluidbookCache.prototype = { return this._cache[key] != null; } }, - get: function (key) { + get: function (key, defaultValue) { + if (defaultValue === undefined) { + defaultValue = null; + } var res; if (this._support) { res = localStorage.getItem(this._prefix + key); } else { res = this._cache[key]; } + if (!this.isset(key)) { + return defaultValue; + } var f = res.substr(0, 1); if (f == '[' || f == '{') { res = json_parse(res); @@ -60,7 +66,7 @@ FluidbookCache.prototype = { }, checkValidity: function () { if (!this.isset('validity') || this.get('validity') != this._date) { - this.clear(); + //this.clear(); this.set('validity', this._date); } } diff --git a/js/libs/fluidbook/fluidbook.cart.js b/js/libs/fluidbook/fluidbook.cart.js index 22b43185..67df20e8 100644 --- a/js/libs/fluidbook/fluidbook.cart.js +++ b/js/libs/fluidbook/fluidbook.cart.js @@ -16,9 +16,16 @@ FluidbookCart.prototype = { $(document).on('click', '[data-cart-ref]', function () { $this.instance.addToCart($(this).data('cart-ref')); + $this.fluidbook.tooltip.displayTooltip($this.fluidbook.l10n.__("the item has been added to your cart")); return false; }); }, + getSavedData: function () { + return this.fluidbook.cache.get('cart', {}) + }, + saveData: function (data) { + this.fluidbook.cache.set('cart', data); + }, createInstance: function () { switch (this.fluidbook.datas.basketManager) { case "Remarkable": @@ -27,4 +34,101 @@ FluidbookCart.prototype = { return null; } }, -} \ No newline at end of file +}; + +(function ($) { + function JQinputNumber(element) { + this.element = element; + this.positive = false; + this.integer = false; + this.step = 1; + this.inited = false; + + var istep = parseFloat(this.element.attr('step')); + this.min = undefined !== this.element.attr('min') ? parseFloat(this.element.attr('min')) : Number.NEGATIVE_INFINITY; + this.max = undefined !== this.element.attr('max') ? parseFloat(this.element.attr('max')) : Number.POSITIVE_INFINITY; + this.errorMax = this.element.data('max-error'); + + this.integer = (!isNaN(istep) && istep % 1 === 0); + this.positive = (this.min > 0); + + if (!isNaN(istep)) { + this.step = istep; + } + this.init(); + } + + JQinputNumber.prototype = { + init: function () { + var $this = this; + + this.element.attr('novalidate', 'novalidate'); + this.element.prop('type', 'text'); + this.element.wrap('
'); + this.wrapper = this.element.parent('.input-number'); + this.wrapper.prepend('-'); + this.wrapper.append('+'); + + this.wrapper.on('click', 'a', function () { + if ($(this).hasClass('minus')) { + $this.increment(-1 * $this.step); + } else if ($(this).hasClass('plus')) { + $this.increment($this.step); + } + return false; + }); + + this.element.on('change', function () { + $this.element.val($this.normalize()); + $this.wrapper.trigger('change'); + }); + + this.element.val(this.normalize()); + this.inited = true; + }, + normalize: function (v) { + var origV; + if (v === undefined) { + v = this.element.val(); + } + if (typeof v !== "number") { + + if (this.integer) { + v = parseInt(v); + } else { + v = parseFloat(v); + } + + } + if (isNaN(v)) { + v = 0; + } + origV = v; + v = Math.max(v, this.min); + v = Math.min(v, this.max); + v = Math.round(v / this.step) * this.step; + if (v < origV && this.errorMax !== null) { + alert(this.errorMax); + } + return v; + }, + increment: function (i) { + var v = this.normalize(); + var v1 = v; + v += i; + v = this.normalize(v); + if (v === v1) { + return; + } + this.element.val(v); + this.wrapper.trigger('change'); + } + }; + + jQuery.fn.inputNumber = function () { + return this.each(function () { + var $this = $(this); + $(this).data('inputNumber', new JQinputNumber($this)); + }); + }; +})(jQuery); \ No newline at end of file diff --git a/js/libs/fluidbook/fluidbook.nav.js b/js/libs/fluidbook/fluidbook.nav.js index 794e64a7..8aabeeab 100644 --- a/js/libs/fluidbook/fluidbook.nav.js +++ b/js/libs/fluidbook/fluidbook.nav.js @@ -408,6 +408,7 @@ FluidbookNav.prototype = { $(l).find('span.number').text(n); } }); + this.fluidbook.cart.instance.updateIcon(); } 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/js/libs/fluidbook/fluidbook.tooltip.js b/js/libs/fluidbook/fluidbook.tooltip.js index bb86822b..c426ac74 100644 --- a/js/libs/fluidbook/fluidbook.tooltip.js +++ b/js/libs/fluidbook/fluidbook.tooltip.js @@ -104,7 +104,6 @@ FluidbookTooltip.prototype = { } this.displayTooltip(text); - this.updateTooltipPosition(); return false; }, @@ -129,7 +128,7 @@ FluidbookTooltip.prototype = { }); return false; } - + this.updateTooltipPosition(); return true; }, hideTooltip: function () { diff --git a/style/fluidbook.less b/style/fluidbook.less index c4d2a9ba..4fec0b54 100644 --- a/style/fluidbook.less +++ b/style/fluidbook.less @@ -859,6 +859,49 @@ a.bookmark { } } +[data-menu="cart"] { + table { + width: 1004px; + margin-left: 10px; + td { + padding: 15px; + text-align: left; + vertical-align: middle; + white-space: nowrap; + &.name { + white-space: normal; + } + &.price { + font-weight: 700; + } + } + .input-number { + position: relative; + width: 160px; + height: 44px; + input { + height: 44px; + width: 60px; + display: inline-block; + margin: 0 2px; + border: 0; + vertical-align: top; + padding: 20px; + text-align: center; + } + a { + display: inline-block; + height: 44px; + width: 44px; + background-color: @menu-button-background; + text-align: center; + font-weight: 700; + font-size: 30px; + } + } + } +} + // Hack for #1433 html.ios body.portrait #interface { -moz-transition: none; -- 2.39.5