///
var FMPresent = {
init: function () {
// valida se as classes padrões já iniciaram
var idValid = setInterval(function () {
if (typeof FMC === 'undefined' || typeof FM === 'undefined' || typeof FMPresent.settings === 'undefined' || typeof FM !== 'object' || typeof FMC !== 'object') {
return;
}
clearInterval(idValid);
});
var countInterval = 0;
// valida se as classes já iniciaram e se o app esta ativo
var idS = setInterval(function () {
// valida se o app esta ativo
if (!FMC.isActive(FM.apps, FMPresent.settings.appId)) {
countInterval++;
if (countInterval >= 50) {
clearInterval(idS);
}
//console.log('Present offline :(');
return false;
}
// encerra a rotina
clearInterval(idS);
// se for valido retorna
//if (FM.isStorage(FM.keys.present)) {
// FMC.file(FM.getStorage(FM.keys.present));
// return;
//}
}, FM.timeInterval);
// seta a base de informações dos brindes
var idSs = setInterval(function () {
if (!FM.isStorage(FM.keys.present)) {
return;
}
FMPresent.settings.data = FM.getStorage(FM.keys.present);
if (FMPresent.settings.data !== 'undefined' && FMPresent.settings.data !== null) {
// encerra a rotina
clearInterval(idSs);
}
}, FM.timeInterval);
$(document).on('click', '#fmais-present-prev-click', function (e) {
e.preventDefault();
FMPresent.prevSlider();
});
$(document).on('click', '#fmais-present-next-click', function (e) {
e.preventDefault();
FMPresent.nextSlide();
});
// add detalhes do brinde no campo de texto para ser enviado junto ao pedido
$('#formularioCheckout').submit(function (e) {
FMPresent.readTable();
});
// inserir o present no carrinho
setInterval(function () {
//FMPresent.populate();
FMPresent.insert();
FMPresent.settings.quantity = FMPresent.qtd();
}, FM.timeInterval);
// lista as informações do cep atual
let presentId = setInterval(function () {
let dataJson = document.getElementById('present-json-data');
if (dataJson !== null && typeof dataJson !== 'undefined' && dataJson !== undefined) {
FMPresent.presentData();
clearInterval(presentId);
}
}, FM.timeInterval);
//valida se o app esta valido para apresentar a mensagem
//setInterval(FMPresent.findPresent, FM.timeToLoad);
},
readTable: function () {
if (window.location.pathname.replace('/index', '').replace('/', '').indexOf('checkout') < 0) {
return;
}
if (!FMC.isActive(FM.apps, FMPresent.settings.appId)) {
return;
}
if (typeof $('#formularioCheckout textarea[name=cliente_obs]').val() === 'undefined') {
var html = '';
$('#formularioCheckout').append(html);
}
var text = '';
var obs = $('#formularioCheckout textarea[name=cliente_obs]').val();
if (typeof obs !== 'undefined') {
text = obs;
}
$('table.tabela-carrinho tbody tr.fmais-present-slide-item-car').each(function (e, item) {
var newText = '\n\n 🎁 Brinde: ➖ ' + $(this).data('name') + ' ➖';
if (text.indexOf(newText) < 0) {
text += newText;
}
});
$('#formularioCheckout textarea[name=cliente_obs]').val(text);
return text;
},
insert: function () {
var data = FM.getStorage(FM.keys.present);
if (data === null) {
return;
}
if (document.getElementsByClassName('pagina-carrinho').length <= 0) {
return;
}
if (!FMC.isActive(FM.apps, FMPresent.settings.appId)) {
return;
}
$.each(data, function (index, item) {
var id = 'fmais-present-row-' + item.id;
if (typeof $('table.tabela-carrinho tbody tr#' + id + ' td').html() === 'undefined') {
// valida se pode ou não inserir o brinde
var ok = false;
// se o valor e quantidade for zero o presente já pode ser inserido no carrinho
ok = (item.minimumPrice === 0 && item.minimumQuantity === 0);
// se o valor minimo for maior que zero, valida se já pode ser inserido o prêmio no carrinho
if (item.minimumPrice > 0) {
if (!ok && FMPresent.price() < item.minimumPrice) {
ok = false;
}
if (!ok && FMPresent.price() >= item.minimumPrice) {
ok = true;
}
}
// se a quantidade minima é maior que zero, valida se já pode ser inserido o prêmio no carrinho
if (item.minimumQuantity > 0) {
if (!ok && FMPresent.settings.quantity < item.minimumQuantity) {
ok = false;
}
if (!ok && FMPresent.settings.quantity >= item.minimumQuantity) {
ok = true;
}
}
// caso o brinde ainda não tenha sido atingido retorna
if (!ok) {
return;
}
// insere html
var html = '
'
if (FMPresent.settings.isCar()) {
html += '';
html += ' ';
html += '  ';
html += ' ';
html += ' | ';
html += ' ';
html += ' ' + item.name + ' ';
html += ' Você ganhou';
html += ' | ';
html += ' 1 | ';
html += ' R$ 0,00 | ';
} else {
html += ' ';
html += ' ' + item.name + ' ';
html += ' Você ganhou';
html += ' | ';
html += ' 1 | ';
html += ' R$ 0,00 | ';
}
html += '
';
$('table.tabela-carrinho tbody').append(html)
}
});
},
findPresent: function () {
var data = document.getElementById('present-json-data');
var storeId = (FM.store !== undefined && FM.store !== null && FM.store !== '') ? FM.store.toLowerCase() : FM.store;
if (data === null) {
var newData = FM.getStorage(FM.keys.present);
if (newData !== null) {
var response = {
data: {
json: JSON.stringify(newData),
html: FM.urlCdn('plugin/apps/present/' + storeId + '.html?ver=1')
}
};
FMPresent.presentData(response);
return;
}
}
if (data !== null) {
var json = data.textContent;
var response = {
data: {
json: json,
html: FM.urlCdn('plugin/apps/present/' + storeId + '.html?ver=1')
}
};
FMPresent.presentData(response);
return;
}
else {
// verifica se o cache ainda é válido
FM.get('plugin/html/' + storeId + '/10').onreadystatechange = (function () {
if (FM.completed(this)) {
var json = JSON.parse(this.responseText);
var response = {
data: json.data
};
FMPresent.presentData(response);
} else if (FM.error(this)) {
FM.setStorage(FM.keys.present, data, FM.time);
}
});
}
},
urlHtml: function () {
return FMC.settings.urlCdn('plugin/apps/present/' + FM.store.toLocaleLowerCase() + '.html?ver=1.0');
},
presentData: function () {
if (document.getElementById("fmais-present-2.0") !== null) {
return;
}
var url = FMPresent.urlHtml();
var json = document.getElementById('present-json-data').textContent;
if (json === null) {
return;
}
if (json !== null && json !== undefined && json !== '') {
var jsonData = JSON.parse(json);
if (jsonData.length <= 0) {
return;
}
FM.setStorage(FM.keys.present, jsonData, FM.time);
}
if (document.getElementById("fmais-present-2.0") !== null) {
return;
}
if ($('#corpo') !== undefined && $('#corpo') !== null && $('#corpo') !== '') {
if (document.getElementById("fmais-present-2.0") === null) {
if (document.getElementsByClassName('pagina-pedido-finalizado').length > 0) {
return;
}
if (document.getElementById("fmais-present-2.0") !== null) {
return;
}
if (FMC.isActive(FM.apps, FMPresent.settings.appId)) {
var data = JSON.parse(json);
data = data[0];
if (data.ShowBannerPage === null && data.showBannerPage === null) {
return;
}
if (document.getElementsByClassName('pagina-produto').length > 0) {
var isOk = false;
var list = (typeof data.ShowBannerPage !== 'undefined' && data.ShowBannerPage !== null) ? data.ShowBannerPage.split(',') : ((typeof data.showBannerPage !== 'undefined' && data.ShowBannerPage !== null) ? data.showBannerPage.split(',') : []);
$.each(list, function (index, item) {
if (item === 'pagina-produto') {
isOk = true;
}
});
if (!isOk) { return; }
try {
$.get(url, function (response) {
if (document.getElementById("fmais-present-2.0") !== null) {
return;
}
$('#corpo .conteiner .secao-principal').after(response);
FMPresent.startSlider();
});
//if (FMC.validURL(json.data.html)) {
// $.get(json.data.html, function (response) {
// if (document.getElementById("fmais-present-2.0") !== null) {
// return;
// }
// try {
// $('#corpo .conteiner .secao-principal').after(response);
// FMPresent.startSlider();
// } catch (e) {
// }
// });
//} else {
// try {
// json.data.html = (json.data.html !== null && typeof json.data.html !== 'undefined') ? json.data.html.replace('', '') : json.data.html;
// $('#corpo .conteiner .secao-principal').after(json.data.html);
// FMPresent.startSlider();
// } catch (e) {
// }
//}
} catch (e) {
return;
}
} else if (document.getElementsByClassName('pagina-inicial').length > 0) {
var isOk = false;
var list = (typeof data.ShowBannerPage !== 'undefined' && data.ShowBannerPage !== null) ? data.ShowBannerPage.split(',') : ((typeof data.showBannerPage !== 'undefined' && data.ShowBannerPage !== null) ? data.showBannerPage.split(',') : []);
$.each(list, function (index, item) {
if (item === 'pagina-inicial') {
isOk = true;
}
});
if (!isOk) { return; }
try {
$.get(url, function (response) {
if (document.getElementById("fmais-present-2.0") !== null) {
return;
}
$('#corpo').prepend('' + response + '
');
FMPresent.startSlider();
});
//if (FMC.validURL(json.data.html)) {
// $.get(json.data.html, function (response) {
// if (document.getElementById("fmais-present-2.0") !== null) {
// return;
// }
// try {
// $('#corpo').prepend('' + response + '
');
// FMPresent.startSlider();
// } catch (e) {
// }
// });
//} else {
// try {
// json.data.html = (json.data.html !== null && typeof json.data.html !== 'undefined') ? json.data.html.replace('', '') : json.data.html;
// $('#corpo').prepend('' + json.data.html + '
');
// FMPresent.startSlider();
// } catch (e) {
// }
//}
} catch (e) {
}
} else if (document.getElementsByClassName('pagina-categoria').length > 0) {
var isOk = false;
var list = (typeof data.ShowBannerPage !== 'undefined' && data.ShowBannerPage !== null) ? data.ShowBannerPage.split(',') : ((typeof data.showBannerPage !== 'undefined' && data.ShowBannerPage !== null) ? data.showBannerPage.split(',') : []);
$.each(list, function (index, item) {
if (item === 'pagina-categoria') {
isOk = true;
}
});
if (!isOk) { return; }
try {
$.get(url, function (response) {
if (document.getElementById("fmais-present-2.0") !== null) {
return;
}
$('#corpo div.conteiner .secao-principal').before(response);
FMPresent.startSlider();
});
//if (FMC.validURL(json.data.html)) {
// $.get(json.data.html, function (response) {
// if (document.getElementById("fmais-present-2.0") !== null) {
// return;
// }
// try {
// $('#corpo div.conteiner .secao-principal').before(response);
// FMPresent.startSlider();
// } catch (e) {
// }
// });
//} else {
// try {
// json.data.html = (json.data.html !== null && typeof json.data.html !== 'undefined') ? json.data.html.replace('', '') : json.data.html;
// $('#corpo div.conteiner .secao-principal').before(json.data.html);
// FMPresent.startSlider();
// } catch (e) {
// }
//}
} catch (e) {
return;
}
return;
} else if (document.getElementsByClassName('pagina-carrinho').length > 0) {
var isOk = false;
var list = (typeof data.ShowBannerPage !== 'undefined' && data.ShowBannerPage !== null) ? data.ShowBannerPage.split(',') : ((typeof data.showBannerPage !== 'undefined' && data.ShowBannerPage !== null) ? data.showBannerPage.split(',') : []);
$.each(list, function (index, item) {
if (item === 'pagina-carrinho') {
isOk = true;
}
});
if (!isOk) { return; }
try {
$.get(url, function (response) {
if (document.getElementById("fmais-present-2.0") !== null) {
return;
}
// TODO: caso ocorra erro de duplicidade no checkout, atualizar aqui
$('#corpo .conteiner .secao-principal').prepend(response);
FMPresent.startSlider();
});
//if (FMC.validURL(json.data.html)) {
// $.get(json.data.html, function (response) {
// if (document.getElementById("fmais-present-2.0") !== null) {
// return;
// }
// try {
// $('#corpo .conteiner').prepend(response);
// FMPresent.startSlider();
// } catch (e) {
// }
// });
//} else {
// try {
// json.data.html = (json.data.html !== null && typeof json.data.html !== 'undefined') ? json.data.html.replace('', '') : json.data.html;
// $('#corpo .conteiner').prepend(json.data.html);
// FMPresent.startSlider();
// } catch (e) {
// }
//}
} catch (e) {
return;
}
}
}
}
}
},
startSlider: function () {
//try {
// fidelizarSlider('#fmais-container', {
// autoplay: true,
// autoplaySpeed: 2500
// });
//} catch (e) {
//}
},
populate: function () {
//if (FM.isMobile) {
// return;
//}
if (FMPresent.settings.isPage()) {
return;
}
if (!FM.isStorage(FM.keys.present)) {
return;
}
var data = FM.getStorage(FM.keys.present);
if (data === null) {
return;
}
var count = document.getElementsByClassName('fmais-present-slide-item');
if (count !== null && count.length === data.length) {
return;
}
// insere o html na pagina
//FMPresent.settings.createSlidesHtml();
var count = document.getElementsByClassName('fmais-present-slide-item').length;
if (count === data.length) {
return;
}
// atualiza title slider
$('.fmais-present-slider-title-h4').html(' ' + data[0].title + ' ');
for (var i = 0; i < data.length; i++) {
var id = (i + 1);
var html = '';
html += '
';
html += '
';
html += '
' + id + '/' + data.length + '
';
html += '

';
html += '
';
html += '
';
html += '
' + data[i].name + '
';
html += '
' + data[i].description + '
';
// html += '
' + data[i].description + '
';
html += '
';
html += '
';
html += '
';
$('.fmais-present-slideshow-container').append(html);
// add pagination
var page = '';
$('.fmais-present-slider-page-bottom').append(page);
if (data.length <= 1) {
$('.fmais-present-slider-page-bottom').hide();
$('.fmais-present-prev-next').hide();
}
}
if (data.length > 1) {
var nextHtml = '';
nextHtml += '
❮';
nextHtml += '
❯';
nextHtml += '
';
$('.fmais-present-slideshow-container').append(nextHtml);
}
$('#fmais-present-slider-master').show();
if (document.getElementsByClassName('fmais-present-slide-item').length > 0) {
var first = document.getElementsByClassName('fmais-present-slide-item')[0].id;
$('#' + first).show();
}
},
prevSlider: function () {
var slides = document.getElementsByClassName("fmais-present-slide-item");
var actual = 'fmais-present-slide-item-';
var total = document.getElementsByClassName("fmais-present-slide-item").length;
var firstId = parseInt(document.getElementsByClassName("fmais-present-slide-item")[0].dataset.id);
var lastId = parseInt((document.getElementsByClassName("fmais-present-slide-item"))[document.getElementsByClassName("fmais-present-slide-item").length - 1].dataset.id);
var last = actual;
var idActual = 0;
for (var i = 0; i < slides.length; i++) {
if (slides[i].style.display === "block") {
idActual = (parseInt(slides[i].dataset.id) - 1);
idActual = (idActual === 0) ? lastId : idActual;
actual += idActual;
}
}
for (var i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
var visible = (document.getElementById(actual) !== null) ? actual : last;
document.getElementById(visible).style.display = "block";
// atualiza a paginação do rodapé
var items = document.getElementsByClassName("fmais-present-dot");
for (var i = 0; i < items.length; i++) {
FM.removeAddClass('.fmais-present-dot-' + slides[i].dataset.id, 'fmais-present-active');
}
FM.removeAddClass('.fmais-present-dot-' + idActual, 'fmais-present-active', 'fmais-present-active');
},
nextSlide: function () {
var slides = document.getElementsByClassName("fmais-present-slide-item");
var actual = 'fmais-present-slide-item-';
var total = document.getElementsByClassName("fmais-present-slide-item").length;
var firstId = parseInt(document.getElementsByClassName("fmais-present-slide-item")[0].dataset.id);
var idActual = parseInt(document.getElementsByClassName("fmais-present-slide-item")[0].dataset.id);
var first = actual + idActual;
var id = 0;
for (var i = 0; i < slides.length; i++) {
if (slides[i].style.display === "block") {
id = (parseInt(slides[i].dataset.id) + 1);
actual += id;
idActual = id;
}
}
for (var i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
var visible = (document.getElementById(actual) !== null) ? actual : first;
document.getElementById(visible).style.display = "block";
// atualiza a paginação do rodapé
var items = document.getElementsByClassName("fmais-present-dot");
for (var i = 0; i < items.length; i++) {
FM.removeAddClass('.fmais-present-dot-' + slides[i].dataset.id, 'fmais-present-active');
}
FM.removeAddClass('.fmais-present-dot-' + (idActual > total ? firstId : idActual), 'fmais-present-active', 'fmais-present-active');
setTimeout(FMPresent.nextSlide, 5000);
},
qtd: function () {
var count = 0;
//var list = $('table.tabela-carrinho div.quantidade input[name=quantidade]');
var list = document.querySelectorAll('table.tabela-carrinho div.quantidade input[name=quantidade]');
var listCheckout = document.querySelectorAll('table.tabela-carrinho tbody tr td.conteiner-qtd div');
// var listCheckout = $('table.tabela-carrinho tbody tr td.conteiner-qtd div');
if (list.length > 0) {
for (var i = 0; i < list.length; i++) {
try {
count += parseInt(list[i].value);
} catch (e) {
}
}
} else if (listCheckout.length > 0) {
for (var i = 0; i < listCheckout.length; i++) {
try {
count += parseInt(listCheckout[i].innerHTML);
} catch (e) {
}
}
}
return count;
},
price: function () {
if (typeof $('.carrinho-checkout').html() === 'undefined') {
var total = $('.total').data('total');
return ((total === undefined ? FMPresent.settings.priceTotal : total) - FMPresent.getValueShipping());
} else {
var shipping = $('.frete-preco strong').html();
if (typeof shipping === 'undefined') {
shipping = '0';
}
try {
shipping = $('.frete-preco strong').html().toString().replace(/\s/g, '').replace('R$', '').replace('.', '').replace(',', '.');
shipping = parseFloat(shipping);
} catch (e) {
shipping = 0;
}
var total = $('.total').data('total');
var subtotal = (total === undefined ? $('.subtotal').data('float') : total);
return (subtotal - shipping);
}
},
data: function () {
try {
return JSON.parse($('#present-json-data').html());
} catch (e) {
return 'Error';
}
},
getValueShipping: function () {
var price = document.getElementsByName('formaEnvio');
var result = 0;
if (price !== null && price.length > 0) {
$.each(price, function (index, item) {
if (item.checked) {
result = parseFloat(item.getAttribute('data-valor'));
}
});
}
return result;
},
settings: {
appId: 10,
data: null,
isCar: function () {
var pages = 'carrinho'.split(',');
var ok = false;
var page = window.location.pathname.replace('/index', '').replace('/', '');
if (page === '') {
return ok;
}
for (var i = 0; i < pages.length; i++) {
if (pages[i].indexOf(page) >= 0) {
ok = true;
}
}
return ok;
},
isPage: function () {
var pages = 'carrinho,carrinho/index,/carrinho/index,checkout,checkout/,/checkout/'.split(',');
var ok = false;
var page = window.location.pathname.replace('/index', '');
var firstPage = page.replace('index', '');
var newPage = firstPage.replace('/', '');
var lastPage = newPage.replace('/', '');
if (lastPage === '') {
return ok;
}
for (var i = 0; i < pages.length; i++) {
if (pages[i].indexOf(lastPage) >= 0) {
ok = true;
}
}
return ok;
},
quantity: 0,
priceTotal: parseFloat((document.querySelector('div.total .valor-total') !== null ? document.querySelector('div.total .valor-total').getAttribute('data-total-valor') : "0")),
priceSubtotal: ($("div.subtotal strong.titulo.cor-principal").data('subtotal-valor') === null || typeof $("div.subtotal strong.titulo.cor-principal").data('subtotal-valor') === 'undefined') ? ((typeof $('table.tabela-carrinho tr .padding-preco .subtotal').data('float') === 'undefined') ? 0 : parseFloat($('table.tabela-carrinho tr .padding-preco .subtotal').data('float'))) : parseFloat($("div.subtotal strong.titulo.cor-principal").data('subtotal-valor')),
price: ($("div.subtotal strong.titulo.cor-principal").data('subtotal-valor') === null || typeof $("div.subtotal strong.titulo.cor-principal").data('subtotal-valor') === 'undefined' || typeof $("div.subtotal strong.titulo.cor-principal").data('subtotal-valor') === 'object') ? ((typeof $('table.tabela-carrinho tr .padding-preco .subtotal').data('float') === 'undefined' || typeof $('table.tabela-carrinho tr .padding-preco .subtotal').data('float') === 'object') ? 0 : parseFloat($('table.tabela-carrinho tr .padding-preco .subtotal').data('float'))) : parseFloat($("div.subtotal strong.titulo.cor-principal").data('subtotal-valor')),
discount: parseFloat(((document.getElementById('cupom_desconto') !== null) ? document.getElementById('cupom_desconto').getAttribute('data-desconto-valor') : "0")),
discountTotal: ((document.getElementById('cupom_desconto') !== null && document.getElementById('cupom_desconto').getAttribute('data-desconto-aplicar-no-total') === 'não') ? false : true),
createSlidesHtml: function () {
var html = '';
html += '
';
html += '
';
html += '
';
html += '
';
html += '
';
html += '
';
html += '
';
// inserir lista
html += '
';
html += '
';
// inserir paginacao
html += '
';
html += '
';
html += '
';
html += '
';
html += '
';
if (document.getElementById('fmais-present-slider-master') === null || document.getElementById('fmais-present-slider-master') === undefined) {
$('#corpo').prepend(html);
}
}
}
};
(function () {
'use strict'
FMPresent.init();
})();