class PopupManager { constructor() { this.cookieName = 'popup_closed'; this.cookieExpiry = 365; this.cookiePolicyName = 'cookie_policy_accepted'; this.init(); this.initCookiePolicy(); } init() { if (this.isPopupClosed()) return; setTimeout(() => this.loadPopupContent(), 10000); } // Инициализация cookie policy попапа initCookiePolicy() { if (this.isCookiePolicyAccepted()) return; setTimeout(() => this.showCookiePolicy(), 100); } isPopupClosed() { return document.cookie.split(';').some(cookie => cookie.trim().startsWith(this.cookieName + '=') ); } isCookiePolicyAccepted() { return document.cookie.split(';').some(cookie => cookie.trim().startsWith(this.cookiePolicyName + '=') ); } setPopupClosed() { const date = new Date(); date.setTime(date.getTime() + (this.cookieExpiry * 24 * 60 * 60 * 1000)); document.cookie = `${this.cookieName}=true;expires=${date.toUTCString()};path=/`; } setCookiePolicyAccepted() { const date = new Date(); date.setTime(date.getTime() + (this.cookieExpiry * 24 * 60 * 60 * 1000)); document.cookie = `${this.cookiePolicyName}=true;expires=${date.toUTCString()};path=/`; } loadPopupContent() { const timestamp = new Date().getTime(); $.getJSON(`/popup/random?t=${timestamp}`) .done(data => { if (data.success) this.showPopup(data.content); }) .fail(error => { console.error('Error loading popup:', error); }); } showPopup(content) { const popupContainer = $( `` ); popupContainer.find('.close-popup').click(() => this.closePopup()); popupContainer.click((e) => { if (e.target === popupContainer[0]) this.closePopup(); }); $('body').append(popupContainer); setTimeout(() => popupContainer.addClass('active'), 100); } // Показ cookie policy попапа showCookiePolicy() { const cookiePolicyHTML = ` `; const cookieContainer = $(cookiePolicyHTML); cookieContainer.find('.accept-cookies').click(() => this.closeCookiePolicy()); $('body').append(cookieContainer); setTimeout(() => cookieContainer.addClass('active'), 100); } closePopup() { const popup = $('#popup-container'); popup.removeClass('active'); setTimeout(() => popup.remove(), 300); this.setPopupClosed(); } // Закрытие cookie policy попапа closeCookiePolicy() { const cookiePolicy = $('#cookie-policy-container'); cookiePolicy.removeClass('active'); setTimeout(() => cookiePolicy.remove(), 300); this.setCookiePolicyAccepted(); } } // Initialize $(function() { new PopupManager(); });