function FluidbookCartKimplay(cart) {
- var $this = this;
this.cart = cart;
this.fluidbook = this.cart.fluidbook;
this.data = this.fluidbook.settings.basketReferences;
this.showAddToCartTooltips = false;
+ this.items = {};
this.init();
}
FluidbookCartKimplay.prototype = {
init: function () {
var $this = this;
- this.items = this.fluidbook.cache.get('cart', []);
+ this.items = this.fluidbook.cache.get('cart', {});
$(document).on(this.fluidbook.input.clickEvent, '.exportCartPDF', function () {
$this.exportPDF();
return false;
});
+
+ $(document).on(this.fluidbook.input.clickEvent, '#kimplay-additem button', function () {
+ let form = $(this).closest('#kimplay-additem');
+ $this.items['' + $(this).data('ref')] = {
+ quantity: $(form).find('[name=qty]').val(),
+ comment: $(form).find('[name=comment]').val()
+ };
+ $this.fluidbook.tooltip.displayTooltipDuring($this.fluidbook.l10n.__("the item has been added to your cart"), 2500);
+ $this.fluidbook.menu.closeView();
+ $this.save();
+ });
},
- addToCart: function (ref, quantity) {
+ addToCart: function (ref) {
+ let quantity, comment;
+
+ if (this.items[ref] === undefined) {
+ quantity = 1;
+ comment = '';
+ } else {
+ quantity = this.items[ref].quantity;
+ comment = this.items[ref].comment;
+ }
let view = `<div id="kimplay-additem">
${this.fluidbook.menu.getCaption("", 'small')}
Quantité souhaitée :<br>
<input type="text" name="qty" value="${quantity}"><br>
Commentaire (facultatif)
- <textarea name="commentaire"></textarea><br>
- <button>Ajouter à ma sélection</button>
+ <textarea name="comment">${comment}</textarea><br>
+ <button data-ref="${ref}">Ajouter à ma sélection</button>
</div>
</div>
</div>`;
this.fluidbook.menu.openCustomView(view, 'cart-kimplay-qty');
},
- _addToCart: function (ref, quantity) {
- if (this.items.indexOf(ref) === -1) {
- this.items.push(ref);
- this.save();
- }
-
- return this.fluidbook.l10n.__("the item has been added to your selection");
- },
-
removeFromCart: function (key) {
this.items.splice(key, 1);
this.save();
},
getItems: function () {
- var res = [];
- var $this = this;
- $(this.items).each(function (i, ref) {
- if ($this.data[ref] !== undefined && $this.data[ref] !== null) {
- res.push(ref);
- }
- });
- return res;
+ return this.items;
},
+ hasItem: function (ref) {
+ return this.items[ref] !== undefined;
+ },
getItemsReferences: function () {
- return this.getItems();
- }, getItemsNumbers: function () {
+ let res = [];
+ $.each(this.getItems(), function (i, item) {
+ res.push(i);
+ });
+ return res;
+ },
+
+ getItemsNumbers: function () {
return this.getItems().length;
- }, getAllQuantities: function () {
- return this.getItemsNumbers();
+ },
+
+ getAllQuantities: function () {
+ let res = 0;
+ $.each(this.getItems(), function (i, item) {
+ res += parseInt(item.quantity);
+ });
+ return res;
},
updateCart: function () {
}
if (refs.indexOf(r) === -1) {
refs.push(r);
- if ($this.instance.getItems().indexOf(r) === -1) {
+ if ($this.hasItem(r) === -1) {
force = true;
}
}
});
} else {
- force = $this.instance.getItems().indexOf(ref) === -1;
+ force = $this.hasItem(ref) === -1;
refs = [ref];
}
$this.fluidbook.tooltip.displayTooltipDuring(tooltip, 2500, tooltipStyle);
}
} catch (e) {
-
+ console.log(e);
}
return false;
});
},
+ hasItem: function (ref) {
+ if (this.instance.hasItem !== undefined) {
+ return this.instance.hasItem(ref);
+ }
+ return this.instance.getItems().indexOf(ref)
+ },
+
updateLinks: function () {
$('a[data-cart-ref]').removeClass('active');