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;
});
});
$(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) {
this.setBox();
this.initSortable();
+ this.updateBoxesSelector();
if (cb !== undefined) {
cb();
console.log(e);
}
});
+ },
+ initRightSortable() {
+ var $this = this;
new Sortable(document.getElementById('grandvision-selection-slots'), {
group: {
name: 'shared',
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>';
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;
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];
res += '</div>';
$(".boxholder").html(res);
+
+ this.initRightSortable();
},
getProductSlideshow: function (ref, data) {
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);