File: D:/HostingSpaces/Eurotools/euro-tools.nl/resources/assets/js/shop/cart.js
var ShoppingCart = {
// Initialize click event
init: function () {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('[contenteditable]').bind('blur', function () {
///storeProjectBilledAmount
var amount = $(this).html();
var id = $(this).data('id');
if (amount > 0) {
ShoppingCart.setItemAmountInShoppingcart(id, amount);
}
});
$('[contenteditable]').bind('keypress', function (e) {
if (event.keyCode === 10 || event.keyCode === 13) {
$(this).blur();
e.preventDefault();
}
});
$('.order-box .amount, .amount-box .amount').on('change', function () {
var amount = parseInt($(this).val());
var id = $(this).data('id');
if (amount > 0) {
ShoppingCart.setItemAmountInShoppingcart(id, amount);
}
});
},
addProductToShoppingcart: function (id, type) {
var amount = parseInt($('.order-box .amount').val());
if (amount > 0) {
$.post("/addProductToShoppingcart", {productableID: id, itemType: type, amount: amount})
.done(function (redirectUrl) {
window.location = redirectUrl;
});
}
//ShoppingCart.animateButtonToShoppingcartIcon();
},
removeItemFromShoppingcart: function (id, type) {
ShoppingCart.showModal(id, type);
},
setItemAmountInShoppingcart: function (id, amount) {
if (amount > 0) {
$.post("/setItemAmountInShoppingcart", {itemID: id, amount: amount})
.done(function (data) {
$(".shoppingCartContent").html(data);
ShoppingCart.init();
ShoppingCart.updateCartButtonCounter();
});
} else if (amount === 0) {
this.removeItemFromShoppingcart(id);
}
},
updateCartButtonCounter: function () {
$.get("/getCartItemCount", (function (data) {
$("span.shoppingCartButtonCounter").attr('data-counter', data);
}));
},
showModal: function (id, type) {
$('.delete-modal').css("display", "flex");
$('.delete-modal a.close, .delete-modal a.no').bind('click', function (ev) {
$('.delete-modal').hide();
});
$('.delete-modal a.yes').bind('click', function () {
$.post("/removeItemFromShoppingcart", {productableID: id, itemType: type})
.done(function (data) {
$(".shoppingCartContent").html(data);
ShoppingCart.updateCartButtonCounter();
});
});
},
animateButtonToShoppingcartIcon: function () {
var $addToCartButton = $('a.shoppingCart');
var $shoppingCartButton = $('a.shoppingCartButton:visible');
if($addToCartButton && $shoppingCartButton){
var startPosition = $addToCartButton.offset();
var endPosition = $shoppingCartButton.offset();
var $flyButton = $addToCartButton.clone().appendTo( 'body > .content' );
$flyButton.html('');
$flyButton.css({position: 'absolute', left: startPosition.left +'px', top: startPosition.top +'px', width: $addToCartButton.width() + 'px', opacity: '0.5', backgroundColor: '#cccccc'});
$flyButton.animate({
opacity: 0,
left: endPosition.left,
top: 0,
width: 0,
}, 500, 'linear', function() {
$(this).remove();
$shoppingCartButton.addClass('inflate');
setTimeout(function(){ $shoppingCartButton.removeClass('inflate'); }, 200);
});
}
}
};
ShoppingCart.init();