areya-energy/assets/js/script.js

127 lines
7.6 KiB
JavaScript

$(window).on('load', function(){
if(localStorage.getItem("dismiss-message") == 1) $('.alert-dismissible').remove();
else $('.alert-dismissible').show();
$('#btn-close-message').click(function(){ localStorage.setItem("dismiss-message", 1); });
if(localStorage.getItem("products") != null) objProducts = JSON.parse(localStorage.getItem("products"));
updateProductsInLocalStorage();
if(hasCartCode()){
if(objProducts.length > 0) $('#modal-confirm-product-replacement').modal('show');
else $('#modal-confirm-addition-of-products').modal('show');
$('.btn-confirm-product-link').click(function(){
objProducts = JSON.parse(productLink);
updateProductsInLocalStorage();
window.location.replace($('meta[name="checkout"]').attr('content'));
});
}
$(document).on('click', '.btn-remove-product', function(){
let productId = $(this).attr('data-id');
let objProductInfo = objProducts.find(obj => obj.id == productId);
let index = objProducts.indexOf(objProductInfo);
if(index != -1) objProducts.splice(index, 1);
localStorage.setItem("products", JSON.stringify(objProducts));
$(this).parent().parent().remove();
$('#count-products-in-bag').text(getQuantityOfProducts());
if(objProducts.length > 0){
$('.cart-link').show();
$(".btn-share").show();
$('#count-products-in-bag').show();
$('#list-products-in-bag').parent().parent().parent().parent().parent().find('.modal-footer').show();
$('#total-price').parent().parent().parent().show();
$('#no-products').hide();
if(typeof updateProducts === "function"){
$('#form-section').show();
$('#list-products').parent().parent().parent().parent().parent().find('.modal-footer').show();
$('#total-checkout-price').parent().parent().parent().show();
$('#no-products-in-checkout').hide();
}
}else{
$('.cart-link').hide();
$(".btn-share").hide();
$('#count-products-in-bag').hide();
$('#list-products-in-bag').parent().parent().parent().parent().parent().find('.modal-footer').hide();
$('#total-price').parent().parent().parent().hide();
$('#no-products').show();
if(typeof updateProducts === "function"){
$('#form-section').hide();
$('#list-products').parent().parent().parent().parent().parent().find('.modal-footer').hide();
$('#total-checkout-price').parent().parent().parent().hide();
$('#no-products-in-checkout').show();
}
}
getInfoFromSelectedProduct().done(function(response, status){
totalPrice = 0;
if(response.contents != undefined) response.contents.forEach(function(product){
let objProduct = getProductInfo(product);
totalPrice += objProduct.preis*objProduct.quantity;
});
$('#total-price').text(numberToEuroFormat(totalPrice));
if(typeof updateProducts === "function") $('#total-checkout-price').text(numberToEuroFormat(totalPrice));
});
productId = $('meta[name="product-id"]').attr('content');
if(productId != undefined){
let objProduct = objProducts.find(obj => obj.id == productId);
if(objProduct == undefined) {
$('#btn-add-to-bag').show();
$('#btn-show-bag').hide();
if(objProducts.length > 0) $('#btn-check-out').parent().hide();
else $('#btn-check-out').parent().show();
}
else {
$('#btn-add-to-bag').hide();
$('#btn-show-bag').show();
$('#btn-check-out').parent().hide();
$('#count-items-in-bag').html(objProduct.quantity+`<svg style="fill: white; width: 12px; margin-left: 3px; margin-bottom: 2px;" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M352 128C352 57.42 294.579 0 224 0 153.42 0 96 57.42 96 128H0v304c0 44.183 35.817 80 80 80h288c44.183 0 80-35.817 80-80V128h-96zM224 48c44.112 0 80 35.888 80 80H144c0-44.112 35.888-80 80-80zm176 384c0 17.645-14.355 32-32 32H80c-17.645 0-32-14.355-32-32V176h48v40c0 13.255 10.745 24 24 24s24-10.745 24-24v-40h160v40c0 13.255 10.745 24 24 24s24-10.745 24-24v-40h48v256z"/></svg>`);
}
}
});
$(document).on('change', '.select-quantity', function(){
let quantity = $(this).val();
let productId = $(this).attr('data-id');
let objProductInfo = objProducts.find(obj => obj.id == productId);
let index = objProducts.indexOf(objProductInfo);
objProducts[index].quantity = quantity;
localStorage.setItem("products", JSON.stringify(objProducts));
$('#count-products-in-bag').text(getQuantityOfProducts());
getInfoFromSelectedProduct().done(function(response, status){
totalPrice = 0;
response.contents.forEach(function(product){
let objProduct = getProductInfo(product);
totalPrice += objProduct.preis*objProduct.quantity;
});
$('#total-price').text(numberToEuroFormat(totalPrice));
if(typeof updateProducts === "function") $('#total-checkout-price').text(numberToEuroFormat(totalPrice));
});
productId = $('meta[name="product-id"]').attr('content');
if(productId != undefined){
let objProduct = objProducts.find(obj => obj.id == productId);
if(objProduct == undefined) {
$('#btn-add-to-bag').show();
$('#btn-show-bag').hide();
if(objProducts.length > 0) $('#btn-check-out').parent().hide();
else $('#btn-check-out').parent().show();
}
else {
$('#btn-add-to-bag').hide();
$('#btn-show-bag').show();
$('#btn-check-out').parent().hide();
$('#count-items-in-bag').html(objProduct.quantity+`<svg style="fill: white; width: 12px; margin-left: 3px; margin-bottom: 2px;" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M352 128C352 57.42 294.579 0 224 0 153.42 0 96 57.42 96 128H0v304c0 44.183 35.817 80 80 80h288c44.183 0 80-35.817 80-80V128h-96zM224 48c44.112 0 80 35.888 80 80H144c0-44.112 35.888-80 80-80zm176 384c0 17.645-14.355 32-32 32H80c-17.645 0-32-14.355-32-32V176h48v40c0 13.255 10.745 24 24 24s24-10.745 24-24v-40h160v40c0 13.255 10.745 24 24 24s24-10.745 24-24v-40h48v256z"/></svg>`);
}
}
});
$('#btn-copy-cart-link').click(function(){
$('.cart-link').select();
document.execCommand("copy");
$(this).html(`<svg style="fill: green; width: 18px;"xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M400 32H48C21.49 32 0 53.49 0 80v352c0 26.51 21.49 48 48 48h352c26.51 0 48-21.49 48-48V80c0-26.51-21.49-48-48-48zm0 32c8.823 0 16 7.178 16 16v352c0 8.822-7.177 16-16 16H48c-8.822 0-16-7.178-16-16V80c0-8.822 7.178-16 16-16h352m-34.301 98.293l-8.451-8.52c-4.667-4.705-12.265-4.736-16.97-.068l-163.441 162.13-68.976-69.533c-4.667-4.705-12.265-4.736-16.97-.068l-8.52 8.451c-4.705 4.667-4.736 12.265-.068 16.97l85.878 86.572c4.667 4.705 12.265 4.736 16.97.068l180.48-179.032c4.704-4.667 4.735-12.265.068-16.97z"/></svg>`);
});
$(".btn-share").click(async function() {
if(navigator.share) { try { await navigator.share(shareData); } catch(err) { console.warn(err); } }
else { console.warn('Native Web Sharing not supported'); }
});
});