From 44dc3b0493eedbe7b3f4e826bbb208419fff1b63 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 2 Sep 2020 16:36:30 +0200 Subject: [PATCH] wip #3828 @2 --- .../fluidbook/cart/fluidbook.cart.mopec.js | 273 ++++++++++++++++++ js/libs/fluidbook/fluidbook.cart.js | 4 +- 2 files changed, 276 insertions(+), 1 deletion(-) create mode 100644 js/libs/fluidbook/cart/fluidbook.cart.mopec.js diff --git a/js/libs/fluidbook/cart/fluidbook.cart.mopec.js b/js/libs/fluidbook/cart/fluidbook.cart.mopec.js new file mode 100644 index 00000000..b9225628 --- /dev/null +++ b/js/libs/fluidbook/cart/fluidbook.cart.mopec.js @@ -0,0 +1,273 @@ +function FluidbookCartMopec(cart) { + var $this = this; + this.cart = cart; + this.fluidbook = this.cart.fluidbook; + this.items = this.cart.getSavedData(); + this.data; + this.handleTooltips = true; + this.handleTaxes = false; + this.init(); +} + +FluidbookCartMopec.prototype = { + updateCartData: function (callback) { + var $this = this; + this.sendCookie(); + + $.ajax({ + url: this.baseURL + "customer/section/load/?sections=cart&update_section_id=false", + dataType: 'json', + cache: false, + xhrFields: {withCredentials: true}, + success: function (data) { + $this.fluidbook.hideLoader(); + $this.updateCartFromData(data.cart, callback); + }, + }); + }, + + updateCartFromData: function (data, callback) { + this.data = data; + this.updateCart(); + if (callback !== undefined) { + callback(); + } + }, + + init: function () { + var $this = this; + $(document).on('change', '[data-menu="cart"] .input-number', function () { + $this.updateCartFromQuantities(); + return true; + }); + + this.updateCartData(function () { + + }); + + setTimeout(function () { + $this.updateCartData(function () { + + }); + }, 60000); + + $(window).on('focus', function () { + $this.updateCartData(function () { + + }); + }); + + this.baseURL = 'http://www.mopec.es/' + this.fluidbook.l10n.currentLang + '/'; + }, + addToCart: function (ref, quantity) { + var $this = this; + this.fluidbook.displayLoader(); + if (quantity == undefined) { + quantity = 1; + } + + $.ajax({ + url: 'https://www.remarkable.fr/boutique/com_act.cfm?code_lg=lg_fr&idsite=1', + cache: false, + data: {ref: ref, qte: quantity, ref_fille: '', radio_bloc: 'on'}, + method: 'post', + xhrFields: {withCredentials: true}, + success: function (data) { + var alert = $(data).find('#texte_message_alert').text(); + if (alert !== '') { + $this.fluidbook.tooltip.displayTooltip($(data).find('#texte_message_alert').text(), 'error'); + } else { + $this.fluidbook.tooltip.displayTooltip($this.fluidbook.l10n.__("the item has been added to your cart"), 'invert'); + } + $this.fluidbook.hideLoader(); + $this.updateCartData(function () { + + }); + + } + }) + return false; + + }, + + sendCookie: function () { + // $.cookie("fsremarkablefr=4lnqzxer2votqppqbw6", 1, { + // expires: 10, + // path: '/', + // domain: 'remarkable.fr', + // secure: true + // }); + }, + + removeFromCart: function (ref, callback) { + var $this = this; + this.fluidbook.displayLoader(); + this.data.find('item reference').each(function () { + if ($(this).text() === ref) { + $.ajax({ + url: 'https://www.remarkable.fr/boutique/com_act.cfm?ModeEdit=delete&line=' + $(this).closest('item').attr('id'), + xhrFields: {withCredentials: true}, + success: function () { + $this.fluidbook.hideLoader(); + $this.updateCartData(callback); + } + }) + return true; + } + }); + }, + getItemsNumbers: function () { + return this.data.summary_count; + }, + getAllQuantities: function () { + var res = 0; + this.data.find('item quantite').each(function () { + res += parseInt($(this).text()); + }); + return res; + }, + hasItem: function (itemReference) { + var res = false; + this.data.find('item reference').each(function () { + if ($(this).text() == itemReference) { + res = true; + return true; + } + }); + return res; + }, + updateIcon: function () { + //$(this.fluidbook).trigger('fluidbook.cart.updateIcon', {number: this.getAllQuantities()}); + $(this.fluidbook).trigger('fluidbook.cart.updateIcon', {number: this.getItemsNumbers()}); + }, + + openMenu: function (p1, p2, callback) { + this.fluidbook.menu.quickCloseView(); + return this.openCart(p2, callback); + }, + + openCart: function (p2, callback) { + var $this = this; + this._endMenu(this.fluidbook.l10n.__('your cart'), this.getCartContent(), function () { + $('input[type="number"]').inputNumber(); + + callback(); + }); + }, + + getCartContent: function () { + if (this.getItemsNumbers() == 0) { + return '
' + this.fluidbook.l10n.__('your cart is empty') + '
'; + } + var $this = this; + var content = ''; + var totalht = 0; + $.each(this.data.items, function (index, item) { + var ref = item.product_sku; + var qty = $this.parseInt(item.qty); + var unit = $this.parseFloat(item.product_price_value); + var ht = unit * qty; + + content += ''; + content += ''; + content += ''; + content += ''; + content += ''; + content += ''; + content += ''; + + totalht += ht; + }); + + content += '
' + item.product_name + '
Prix unitaire: ' + $this.formatPrice(unit); + content += '
Prix : ' + $this.formatPrice(ht, 'HT') + '
' + $this.formatPrice(unit) + '' + $this.formatPrice(ht) + '' + getSpriteIcon('interface-close') + '
'; + content += ''; + content += ''; + content += ''; + content += '
Total' + $this.formatPrice(this.data.subtotalAmount) + '
'; + + content += ''; + + return content; + }, + + updateCartFromQuantities: function () { + var $this = this; + var data = {ModeEdit: 'update'}; + var i = 1; + + var del = ''; + $(".cartqty").each(function () { + var v = parseInt($(this).val()); + if (v === 0) { + del = $(this).attr('name'); + return true; + } + data['Item_' + i] = v; + i++; + }); + + if (del != '') { + this.removeFromCart(del, function () { + + }); + return; + } + + this.fluidbook.displayLoader(); + $.ajax({ + url: 'https://www.remarkable.fr/boutique/com_act.cfm?pay=35&code_lg=lg_fr', + method: "post", + data: data, + xhrFields: {withCredentials: true}, + success: function () { + $this.updateCartData(function () { + + }); + } + }) + }, + + updateCart: function () { + + var $this = this; + + $('[data-menu="cart"] .content').html(this.getCartContent()); + $('input[type="number"]').inputNumber(); + this.updateIcon(); + resize(); + }, + + formatPrice: function (price, suffix) { + if (typeof price == 'number') { + price = parseFloat(price); + } + + if (suffix == undefined) { + suffix = ''; + } + + return price.toLocaleString("fr-FR", {style: "currency", currency: "EUR"}) + ' ' + suffix; + }, + + _endMenu: function (title, content, callback) { + var view = this.fluidbook.menu.getCaption(title); + view += '
'; + view += "" + content; + view += '
'; + this.fluidbook.menu.viewWrap(view, 'cart'); + callback(); + }, + + parseFloat: function (s) { + s = s.replace(/\s/g, ''); + return parseFloat(s); + }, + + parseInt: function (s) { + s = s.replace(/\s/g, ''); + return parseInt(s); + }, +}; \ No newline at end of file diff --git a/js/libs/fluidbook/fluidbook.cart.js b/js/libs/fluidbook/fluidbook.cart.js index 6c1036bf..8f0c72c9 100644 --- a/js/libs/fluidbook/fluidbook.cart.js +++ b/js/libs/fluidbook/fluidbook.cart.js @@ -26,7 +26,7 @@ FluidbookCart.prototype = { qty = parseInt($(this).attr('data-cart-qty')); } - if($this.instance.addToCart($(this).data('cart-ref'), qty)){ + if ($this.instance.addToCart($(this).data('cart-ref'), qty)) { $this.fluidbook.tooltip.displayTooltip($this.fluidbook.l10n.__("the item has been added to your cart"), 'invert'); } return false; @@ -42,6 +42,8 @@ FluidbookCart.prototype = { switch (this.fluidbook.settings.basketManager) { case "Remarkable": return new FluidbookCartRemarkable(this); + case 'Mopec': + return new FluidbookCartMopec(this); default: return null; } -- 2.39.5