From c8662f9034ecc3c06698c97a3dc29cfcf719d7bf Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 23 Feb 2021 13:03:24 +0100 Subject: [PATCH] wip #4286 @1.5 --- .../cart/fluidbook.cart.grandvision.js | 82 +++++++++++++++++-- js/libs/fluidbook/fluidbook.cart.js | 9 +- 2 files changed, 81 insertions(+), 10 deletions(-) diff --git a/js/libs/fluidbook/cart/fluidbook.cart.grandvision.js b/js/libs/fluidbook/cart/fluidbook.cart.grandvision.js index a74880ec..1a3905dc 100644 --- a/js/libs/fluidbook/cart/fluidbook.cart.grandvision.js +++ b/js/libs/fluidbook/cart/fluidbook.cart.grandvision.js @@ -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('
'); $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 = '
'; res += '

My Boxes

'; res += ''; res += '' + getSpriteIcon('grandvision-add') + ' Create a new box'; @@ -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 += ''; + }); + $("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 += '
'; $(".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); diff --git a/js/libs/fluidbook/fluidbook.cart.js b/js/libs/fluidbook/fluidbook.cart.js index e025b993..007ee9f5 100644 --- a/js/libs/fluidbook/fluidbook.cart.js +++ b/js/libs/fluidbook/fluidbook.cart.js @@ -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; }); -- 2.39.5