]> _ Git - fluidbook-html5.git/commitdiff
wip #4286 @1.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 23 Feb 2021 12:03:24 +0000 (13:03 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 23 Feb 2021 12:03:24 +0000 (13:03 +0100)
js/libs/fluidbook/cart/fluidbook.cart.grandvision.js
js/libs/fluidbook/fluidbook.cart.js

index a74880ec320ff5db525b4811ac78a1e107370814..1a3905dc952885d022229af992ef0bda8471941b 100644 (file)
@@ -12,14 +12,17 @@ FluidbookCartGrandVision.prototype = {
         var $this = this;
         this.items = this.fluidbook.cache.get('cart', []);
         this.boxes = this.fluidbook.cache.get('boxes', [{name: 'Box 1', refs: []}]);
-        if (this.boxes.length === 0) {
+        if (this.boxes===undefined || this.boxes===null || this.boxes.length === 0) {
             this.boxes = [{name: 'Box 1', refs: []}];
         }
         this.boxIndex = Math.max(0, Math.min(this.boxes.length - 1, parseInt(this.fluidbook.cache.get('boxindex', '0'))));
 
-        $(document).on('click', '.slots [data-cart-delete]', function () {
+        $(document).on('click', '.slots [data-cart-delete]', function (e) {
             $(this).closest('.item').replaceWith('<div class="slot"></div>');
             $this.save();
+            e.preventDefault();
+            e.stopImmediatePropagation();
+            e.stopPropagation();
             return false;
         });
 
@@ -42,9 +45,56 @@ FluidbookCartGrandVision.prototype = {
         });
 
         $(document).on('change', 'select[name="box"]', function () {
-            $this.boxIndex = parseInt($(this).find('option[value="' + $(this).val() + '"').data('i'));
+            $this.boxIndex = parseInt($(this).find('option[value="' + $(this).val() + '"]').data('i'));
             return true;
         });
+
+        $(document).on('click', '[data-action="delete"]', function () {
+            if (window.confirm('Are you sure you want to delete this box ?')) {
+                $this.removeCurrentBox();
+            }
+            return false;
+        });
+
+        $(document).on('click', '.addbox', function () {
+            $this.addNewBox();
+            return false;
+        });
+
+        $(document).on('click', '[data-action="rename"]', function () {
+            var h3 = $('.box h3');
+            $(h3).text(window.prompt('Please enter the box name', $(h3).text()));
+            $this.save();
+            $this.updateBoxesSelector();
+            return false;
+        });
+    },
+
+    addNewBox:function(){
+        this.boxes.push({name: 'Box ' + (this.boxes.length + 2), refs: []});
+        this.boxIndex = this.boxes.length - 1;
+
+        this.fluidbook.cache.set('boxes', this.boxes);
+        this.boxIndex = Math.max(0, Math.min(this.boxes.length - 1, this.boxIndex));
+        this.fluidbook.cache.set('boxindex', this.boxIndex);
+
+        this.updateBoxesSelector();
+        this.setBox();
+    },
+
+    removeCurrentBox: function () {
+        this.boxes.splice(this.boxIndex);
+
+        if (this.boxes.length === 0) {
+            this.boxes = [{name: 'Box 1', refs: []}];
+        }
+        this.fluidbook.cache.set('boxes', this.boxes);
+        this.boxIndex = Math.max(0, Math.min(this.boxes.length - 1, this.boxIndex));
+        this.fluidbook.cache.set('boxindex', this.boxIndex);
+
+        this.updateBoxesSelector();
+        this.setBox();
+
     },
 
     removeFromCart: function (ref) {
@@ -141,6 +191,7 @@ FluidbookCartGrandVision.prototype = {
 
         this.setBox();
         this.initSortable();
+        this.updateBoxesSelector();
 
         if (cb !== undefined) {
             cb();
@@ -161,7 +212,10 @@ FluidbookCartGrandVision.prototype = {
                 console.log(e);
             }
         });
+    },
 
+    initRightSortable() {
+        var $this = this;
         new Sortable(document.getElementById('grandvision-selection-slots'), {
             group: {
                 name: 'shared',
@@ -216,9 +270,6 @@ FluidbookCartGrandVision.prototype = {
         var res = '<div id="grandvision-boxes">';
         res += '<h2>My Boxes</h2>';
         res += '<select name="box">';
-        $.each(this.boxes, function (k, b) {
-            res += '<option data-index="' + k + '">' + b.name + '</option>';
-        });
         res += '</select>';
         res += '<a class="addbox" href="#">' + getSpriteIcon('grandvision-add') + ' Create a new box</a>';
 
@@ -228,6 +279,16 @@ FluidbookCartGrandVision.prototype = {
         return res;
     },
 
+    updateBoxesSelector: function () {
+        var res = '';
+        var $this = this;
+        $.each(this.boxes, function (k, b) {
+            var selected = (k === $this.boxIndex) ? ' selected ' : '';
+            res += '<option data-index="' + k + '"' + selected + '>' + b.name + '</option>';
+        });
+        $("select[name='box']").html(res);
+    },
+
     getItemsHTML: function () {
         var selection = '';
         var $this = this;
@@ -314,6 +375,11 @@ FluidbookCartGrandVision.prototype = {
         if (index === undefined) {
             index = this.boxIndex;
         }
+        if (isNaN(index)) {
+            index = 0;
+        }
+
+        index = Math.max(0, Math.min(this.boxes.length - 1, index));
 
         var b = this.boxes[index];
 
@@ -337,6 +403,8 @@ FluidbookCartGrandVision.prototype = {
         res += '</div>';
 
         $(".boxholder").html(res);
+
+        this.initRightSortable();
     },
 
     getProductSlideshow: function (ref, data) {
@@ -451,7 +519,7 @@ FluidbookCartGrandVision.prototype = {
                     refs.push($(this).data('ref'));
                 }
             });
-            $this.boxes[this.boxIndex] = {name: $(this).find('h3').eq(0).text(), refs: refs.slice(0, 10)};
+            $this.boxes[$this.boxIndex] = {name: $(this).find('h3').eq(0).text(), refs: refs.slice(0, 10)};
         });
 
         this.fluidbook.cache.set('boxes', this.boxes);
index e025b993461f9d7b390500597de696824e694cb3..007ee9f5a8fe27913f5cbbca7774cbec9f703bf8 100644 (file)
@@ -16,7 +16,10 @@ FluidbookCart.prototype = {
         this.enabled = true;
         this.instance = this.createInstance();
 
-        $(document).on('click', ':not(.override-delete) [data-cart-delete]', function () {
+        $(document).on('click', '[data-cart-delete]', function () {
+            if ($(this).closest('.override-delete').length > 0) {
+                return true;
+            }
             $this.instance.removeFromCart($(this).data('cart-delete'));
             $this.instance.updateCart(false);
             return false;
@@ -43,9 +46,9 @@ FluidbookCart.prototype = {
                 tooltip = $this.fluidbook.l10n.__("the item has been added to your cart");
             }
             $this.fluidbook.tooltip.displayTooltip(tooltip, tooltipStyle);
-            setTimeout(function(){
+            setTimeout(function () {
                 $this.fluidbook.tooltip.hideTooltip();
-            },2500)
+            }, 2500)
 
             return false;
         });