]> _ Git - fluidbook-html5.git/commitdiff
wip #3828 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 2 Sep 2020 14:36:30 +0000 (16:36 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 2 Sep 2020 14:36:30 +0000 (16:36 +0200)
js/libs/fluidbook/cart/fluidbook.cart.mopec.js [new file with mode: 0644]
js/libs/fluidbook/fluidbook.cart.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 (file)
index 0000000..b922562
--- /dev/null
@@ -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 '<div class="cart-empty">' + this.fluidbook.l10n.__('your cart is empty') + '</div>';
+        }
+        var $this = this;
+        var content = '<table class="cart-items">';
+        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 += '<tr>';
+            content += '<td class="name">' + item.product_name + '<div class="m">Prix unitaire: ' + $this.formatPrice(unit);
+            content += '<br />Prix : ' + $this.formatPrice(ht, 'HT') + '</div></td>';
+            content += '<td class="quantity"><input name="' + ref + '" class="cartqty" type="number" min="0" max="10000" value="' + qty + '" step="1" /></td>';
+            content += '<td class="price_unit">' + $this.formatPrice(unit) + '</td>';
+            content += '<td class="price_excluding_taxes">' + $this.formatPrice(ht) + '</td>';
+            content += '<td class="delete"><a href="#" data-cart-delete="' + ref + '">' + getSpriteIcon('interface-close') + '</a></td>';
+            content += '</tr>';
+
+            totalht += ht;
+        });
+
+        content += '</table>';
+        content += '<table class="cart-totals">';
+        content += '<tr><td colspan="2" class="hr"></td></tr>';
+        content += '<tr class="total"><td>Total</td><td>' + $this.formatPrice(this.data.subtotalAmount) + '</td></tr>';
+        content += '</table>';
+
+        content += '<div class="cart-footer">';
+        content += '<div class="fonctions"><a href="#/closeview" class="back">Continuer mes achats</a><a href="https://www.remarkable.fr/boutique/panierb.cfm' + aff + '" target="_blank">Valider</a></div>';
+        content += '</div>';
+
+        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 += '<div class="content">';
+        view += "" + content;
+        view += '</div>';
+        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
index 6c1036bfa8aa90804a46df786ead9f13edebc107..8f0c72c91a680850b5531797bada52d8d3d2b33d 100644 (file)
@@ -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;
         }