Files
sodino/public/js/banner-frontend.js

66 lines
2.2 KiB
JavaScript

(function () {
const closeButtons = document.querySelectorAll('.sodino-banner-close');
const clicked = new Set();
function getCookie(name) {
const value = '; ' + document.cookie;
const parts = value.split('; ' + name + '=');
if (parts.length === 2) {
return parts.pop().split(';').shift();
}
return null;
}
function sendClick(bannerId) {
if (!bannerId || clicked.has(bannerId) || !window.sodinoBannerFrontend) {
return;
}
clicked.add(bannerId);
fetch(window.sodinoBannerFrontend.ajaxUrl, {
method: 'POST',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
body: 'action=sodino_banner_click&banner_id=' + encodeURIComponent(bannerId) + '&nonce=' + encodeURIComponent(window.sodinoBannerFrontend.nonce),
});
}
document.addEventListener('click', function (event) {
const target = event.target.closest('.sodino-banner-link');
if (target) {
const bannerId = target.dataset.bannerId;
sendClick(bannerId);
}
});
closeButtons.forEach(function (button) {
button.addEventListener('click', function () {
const wrapper = this.closest('.sodino-banner-wrap');
if (!wrapper) {
return;
}
wrapper.style.display = 'none';
const bannerId = wrapper.dataset.bannerId;
if (bannerId) {
document.cookie = 'sodino_banner_' + bannerId + '=hidden; path=/; max-age=' + 60 * 60 * 24;
}
});
});
document.querySelectorAll('.sodino-banner-wrap').forEach(function (banner) {
const bannerId = banner.dataset.bannerId;
if (getCookie('sodino_banner_' + bannerId) === 'hidden') {
banner.style.display = 'none';
}
if (banner.classList.contains('sodino-banner-popup')) {
setTimeout(function () {
if (getCookie('sodino_banner_' + bannerId) !== 'hidden') {
banner.style.display = 'block';
}
}, 2000);
}
});
})();