(function($) {
/*var observer = new MutationObserver(function( mutations ) {
mutations.forEach(function( mutation ) {
var newNodes = mutation.addedNodes; // DOM NodeList
if( newNodes !== null ) { // If there are new nodes added
//alert('something has been changed');
console.log('observer acionado');
getCart();
}
});
});
// Configuration of the observer:
var config = {
attributes: true,
childList: true,
characterData: true
};
// Pass in the target node, as well as the observer options
var target = $( ".cart-preview" );
observer.observe(target, config);*/
// Later, you can stop observing
// observer.disconnect(); */
$("body").on('DOMSubtreeModified', ".cart-preview", function() {
// code here
console.log('observer acionado');
getCart();
});
/* $("body").on('DOMSubtreeModified', "span[data-cart=amount]", function() {
// code here
console.log('observer acionado amount');
ajaxCart();
});*/
var cont;
// Chama delete ao clicar no x
$(".cart-details ").bind("DOMSubtreeModified",function(){
$( ".prod-delete" ).click(function() {
if(!(cont == this.id) ){
cartDelete(this.id)
cont = this.id;
}
});
});
function ajaxCart(){
var dataSession = $("html").attr("data-session");
if(dataSession!='undefined'){
$.ajax({
method: "GET",
url: "/web_api/cart/"+dataSession
}).done(function( response, textStatus, jqXHR ) {
//$('.cart-details').html(" ");
$('.cart-details').html('
');
$('.resume').html(" ");
var total = 0.00;
var qtdTotal = 0;
var image;
$( response ).each(function() {
total += parseFloat((this.Cart.price * this.Cart.quantity));
qtdTotal += parseInt(this.Cart.quantity);
});
totalCart = String(total.toFixed(2)).replace('.', ',');
$("span[data-cart=price]").html( totalCart );
$("span[data-cart=amount]").html( qtdTotal );
$( response ).each(function( index ) {
if(this.Cart.product_image.thumbs){
image = this.Cart.product_image.thumbs[90].https;
}else{
image = 'https://images.tcdn.com.br/files/1052366/themes/5/img/no-image.svg';}
totalProduct = String((this.Cart.price*this.Cart.quantity).toFixed(2)).replace('.', ',');
var product = `
`;
$('.mini-products-list').append(product);
});
// Resumo do Carrinho
var resumeCart = `
Subtotal:
R$ ${ totalCart }
`
$('.resume').append(resumeCart);
}).fail(function( jqXHR, status, errorThrown ){
var response = $.parseJSON( jqXHR.responseText );
$('.cart-details').html('
Seu carrinho está vazioVeja nossos produtos para preenchê-lo
');
});
}else{
console.log('data session undefined');
}
}
function getCart() {
$('.cart-details').html(" ");
$('.resume').html(" ");
$("span[data-cart=price]").html('');
$("span[data-cart=amount]").html('');
$("span[class=totalCart]").html('R$ $0,00');
setTimeout(function(){
ajaxCart();
}, 1000);
}
// Chama o get do Carrinho
/*$("span[data-cart=amount]").bind("DOMSubtreeModified",function(){
console.log('carregando carrinho');
getCart();
});*/
/*$('.cart-preview').bind("DOMSubtreeModified",function(){
console.log('carregando carrinho');
getCart();
}); */
// Delete Product
function cartDelete(productId) {
var dataSession = $("html").attr("data-session");
$.ajax({
method: "DELETE",
url: "/web_api/carts/"+dataSession+"/"+productId
}).done(function( response, textStatus, jqXHR ) {
var qtdCart = parseInt($("span[data-cart=amount]").html());
$("span[data-cart=price]").html('0,00');
$("span[class=totalCart]").html('R$ $0,00');
$("span[data-cart=amount]").html(qtdCart - 1);
getCart();
}).fail(function( jqXHR, status, errorThrown ){
window.location.assign("/loja/carrinho.php");
});
}
$( ".download-foto-produto" ).click(function() {
forceDownloadFoto();
});
function forceDownloadFoto(){
url = $("#imgView").prop('src');
console.log(url);
fileName = $("#imgView").attr('title');
var xhr = new XMLHttpRequest();
xhr.open("GET", url+'?download=true&filename='+fileName, true);
xhr.responseType = "blob";
xhr.onload = function(){
var urlCreator = window.URL || window.webkitURL;
var imageUrl = urlCreator.createObjectURL(this.response);
var tag = document.createElement('a');
console.log(imageUrl);
tag.style.display = 'none';
tag.href = imageUrl;
tag.download = fileName;
document.body.appendChild(tag);
tag.click();
document.body.removeChild(tag);
}
xhr.send();
}
$(document).ready( function(){
setTimeout(function(){
ajaxCart();
}, 3000);
});
// Quantidade
$('[data-app="product.qty"]').on('click', function() {
var $productId = $(this).attr('data-product');
var value = parseInt($("#quant").val());
var option = $(this).attr('data-action')== 'minus' ? -1 : 1;
value += option;
if(parseInt(value) > 0){
$("#quant").val(value).change();
/*console.log('value: '+value+' - option: '+option);
console.log($("#quant").val());*/
}
});
function showModal() {
console.log('exibindo modal cart preview');
$('.cart-preview').modal('show');
$('.cart-preview').show();
}
function reloaderPreview(callback) {
//Renderiza novamente o componente de cart-preview
store.render.init();
setTimeout(function() {
callback();
}, 800);
}
function addCarrinho() {
var dataSession = $("html").attr("data-session");
/*var productQtd = parseInt($('#quant').attr('value'));*/
var productQtd = parseInt($('#quant').val());
var dataProductId = parseInt($('#bt_comprar').attr('data-product'));
var variacao = parseInt($('#selectedVariant').attr('value'));
var verificaVariacao = $('#verifica_variacao').attr('value');
var verificaClientesAguardando = $('#verifica_clientes_aguardando').attr('value');
var verificaEstoqueVenda = $('#verifica_estoque_venda').attr('value');
var verificaVariacaoDuplaValor = $('#verifica_variacao_dupla_valor').attr('value');
var layoutVariacao = $('#layout_variacao').attr('value');
var defineRadioSelect = $('#define_radio_select').attr('value');
console.log('add qtd: '+productQtd);
$('body').addClass('is-addcart');
$('#span_mensagem_carrinho').html('');
$('#span_mensagem_carrinho').removeClass('error-msg');
$('#span_mensagem_carrinho').removeClass('success-msg');
$.ajax({
method: "POST",
url: "/web_api/cart/",
contentType: "application/json; charset=utf-8",
data: '{"Cart":{"session_id":"'+dataSession+
'","product_id":"'+ dataProductId +
'","quantity":"'+productQtd+
'","variant_id":"'+variacao+
'","verifica_variacao":"'+verificaVariacao+
'","verifica_clientes_aguardando":"'+verificaClientesAguardando+
'","verifica_estoque_venda":"'+verificaEstoqueVenda+
'","verifica_variacao_dupla_valor":"'+verificaVariacaoDuplaValor+
'","layout_variacao":"'+layoutVariacao+
'","define_radio_select":"'+defineRadioSelect+
'"}}'
}).done(function( response, textStatus, jqXHR ) {
console.log(response);
var qtdCart = parseInt($("span[data-cart=amount]").html());
$("span[data-cart=amount]").html(qtdCart + 1);
$('#span_mensagem_carrinho').removeClass('error-msg');
$('#span_mensagem_carrinho').addClass('success-msg');
$('#span_mensagem_carrinho').html('Produto adicionado ao Carrinho de Compras!');
$('#quant').val(1);
getCart();
reloaderPreview(showModal);
}).fail(function( jqXHR, status, errorThrown ){
var response = $.parseJSON( jqXHR.responseText );
console.log(response);
console.log(response.causes);
var message = '';
if(response.causes){
for(let i = 0; i < response.causes.length; i = i + 1) {
message = message + response.causes[i] + '
';
}
}else{
message = 'Erro ao adicionar produto ao Carrinho de Compras!';
}
$('#span_mensagem_carrinho').removeClass('success-msg');
$('#span_mensagem_carrinho').addClass('error-msg');
$('#span_mensagem_carrinho').html(message);
/*
setTimeout(function(){
$('body').removeClass('is-addcart');
$('#span_mensagem_carrinho').html(message);
}, 3000);*/
});
return false;
}
/*$("#bt_comprar").click(function() {
console.log('adicionando produto ao carrinho');
addCarrinho();
}); */
})(jQuery);
/*---------------CLOUD ZOOM ---------------*/
(function($) {
function format(t) {
for (var o = 1; o < arguments.length; o++) t = t.replace("%" + (o - 1), arguments[o]);
return t
}
function CloudZoom(t, o) {
var e, i, s, n, a, r, d, l, u = $("img", t),
p = null,
c = null,
h = null,
m = null,
f = null,
g = null,
v = 0,
b = 0,
x = 0,
y = 0,
z = 0,
w = 0,
O = this;
setTimeout(function() {
if (null === c) {
t.width()
}
}, 200);
var k = function() {
null !== g && (g.remove(), g = null)
};
this.removeBits = function() {
h && (h.remove(), h = null), m && (m.remove(), m = null), f && (f.remove(), f = null), k(), $(".cloud-zoom-loading", t.parent()).remove()
}, this.destroy = function() {
t.data("zoom", null), c && (c.unbind(), c.remove(), c = null), p && (p.remove(), p = null), this.removeBits()
}, this.fadedOut = function() {
p && (p.remove(), p = null), this.removeBits()
}, this.controlLoop = function() {
if (h) {
var t = r - u.offset().left - .5 * n >> 0,
e = d - u.offset().top - .5 * a >> 0;
0 > t ? t = 0 : t > u.outerWidth() - n && (t = u.outerWidth() - n), 0 > e ? e = 0 : e > u.outerHeight() - a && (e = u.outerHeight() - a), h.css({
left: t,
top: e
}), h.css("background-position", -t + "px " + -e + "px"), b = t / u.outerWidth() * s.width >> 0, x = e / u.outerHeight() * s.height >> 0, z += (b - z) / o.smoothMove, y += (x - y) / o.smoothMove, p.css("background-position", -(z >> 0) + "px " + (-(y >> 0) + "px"))
}
v = setTimeout(function() {
O.controlLoop()
}, 30)
}, this.init2 = function(t, o) {
w++, 1 === o && (s = t), 2 === w && this.init()
}, this.init = function() {
$(".cloud-zoom-loading", t.parent()).remove(), $(".mousetrap").remove(), c = t.parent().append(format("", u.outerWidth(), u.outerHeight(), 0, 0)).find(":last"), c.bind("mousemove", this, function(t) {
r = t.pageX, d = t.pageY
}), c.bind("mouseleave", this, function() {
return clearTimeout(v), h && h.fadeOut(0), m && m.fadeOut(0), f && f.fadeOut(0), p.fadeOut(1, function() {
O.fadedOut()
}), !1
}), c.bind("mouseenter", this, function(e) {
r = e.pageX, d = e.pageY, l = e.data, p && (p.stop(!0, !1), p.remove());
var i = o.adjustX,
v = o.adjustY,
b = u.outerWidth(),
x = u.outerHeight(),
y = o.zoomWidth,
z = o.zoomHeight;
"auto" == o.zoomWidth && (y = b), "auto" == o.zoomHeight && (z = x);
var w = t.parent();
switch (o.position) {
case "top":
v -= z;
break;
case "right":
i += b;
break;
case "bottom":
v += x;
break;
case "left":
i -= y;
break;
case "inside":
y = b, z = x;
break;
default:
w = $("#" + o.position), w.length ? (y = w.innerWidth(), z = w.innerHeight()) : (w = t, i += b, v += x)
}
p = w.append(format('', i, v, y, z, s.src)).find(":last"), u.attr("title") && o.showTitle && p.append(format('%0
', u.attr("title"))).find(":last").css("opacity", o.titleOpacity), $.browser.msie && $.browser.version < 7 && (g = $('').css({
position: "absolute",
left: i,
top: v,
zIndex: 99,
width: y,
height: z
}).insertBefore(p)), p.fadeIn(0), h && (h.remove(), h = null), n = u.outerWidth() / s.width * p.width(), a = u.outerHeight() / s.height * p.height(), h = t.append(format("", n, a)).find(":last"), c.css("cursor", h.css("cursor"));
var O = !1;
o.tint && (h.css("background", 'url("' + u.attr("src") + '")'), m = t.append(format('', u.outerWidth(), u.outerHeight(), o.tint)).find(":last"), m.css("opacity", o.tintOpacity), O = !0, m.fadeIn(0)), o.softFocus && (h.css("background", 'url("' + u.attr("src") + '")'), f = t.append(format('', u.outerWidth() - 2, u.outerHeight() - 2, o.tint)).find(":last"), f.css("background", 'url("' + u.attr("src") + '")'), f.css("opacity", .5), O = !0, f.fadeIn(0)), O || h.css("opacity", o.lensOpacity), "inside" !== o.position && h.fadeIn(0), l.controlLoop()
})
}, e = new Image, $(e).load(function() {
O.init2(this, 0)
}), e.src = u.attr("src"), i = new Image, $(i).load(function() {
O.init2(this, 1)
}), i.src = t.attr("href")
}
$(document).ready(function() {
$(".cloud-zoom, .cloud-zoom-gallery").CloudZoom()
}), $.fn.CloudZoom = function(options) {
try {
document.execCommand("BackgroundImageCache", !0, !0)
} catch (e) {}
return this.each(function() {
var relOpts, opts;
eval("var a = {" + $(this).attr("rel") + "}"), relOpts = a, $(this).is(".cloud-zoom") ? ($(this).css({
position: "relative",
display: "block"
}), $("img", $(this)).css({
display: "block"
}), "wrap" != $(this).parent().attr("id") && $(this).wrap(''), opts = $.extend({}, $.fn.CloudZoom.defaults, options), opts = $.extend({}, opts, relOpts), $(this).data("zoom", new CloudZoom($(this), opts))) : $(this).is(".cloud-zoom-gallery") && (opts = $.extend({}, relOpts, options), $(this).data("relOpts", opts), $(this).bind("click", $(this), function(t) {
var o = t.data.data("relOpts");
return $("#" + o.useZoom).data("zoom").destroy(), $("#" + o.useZoom).attr("href", t.data.attr("href")), $("#" + o.useZoom + " img").attr("src", t.data.data("relOpts").smallImage), $("#" + t.data.data("relOpts").useZoom).CloudZoom(), !1
}), $(this).bind("click", $(this), function() {
return !1
}))
}), this
}, $.fn.CloudZoom.defaults = {
zoomWidth: "auto",
zoomHeight: "auto",
position: "inside",
autoInside: true,
tint: !1,
tintOpacity: .5,
lensOpacity: .5,
softFocus: !1,
smoothMove: 3,
showTitle: !0,
titleOpacity: .5,
adjustX: 0,
adjustY: 0
}
})(jQuery);