(function () { const INSERT_POSITION = 'beforeend'; const HIDDEN_CLASS = 'hidden'; const insertHTML = (html, target, position = INSERT_POSITION) => { target.insertAdjacentHTML(position, html); }; const padNumber = (value) => String(value).length === 1 ? `0${value}` : value; class CornerBanner { #originalHTML = null; #bannerElement = null; #countdownTime = null; #bannerContainer = null; #closeButton = null; #contentContainer = null; #contentFrame = null; #bannerItem = null; #countdownElement = null; #timerInterval = null; #bannerId = null; #imageElements = null; #storage = localStorage; constructor(bannerContent) { this.#originalHTML = bannerContent.outerHTML; this.#bannerContainer = document.querySelector('.gc-main-content'); } get templateStyle() { return ` `; } get template() { return `
${this.#originalHTML}
`; } get element() { if (!this.#bannerElement) { this.#bannerElement = this.template; } return this.#bannerElement; } init() { // Insert banner styles and HTML insertHTML(this.templateStyle, this.#bannerContainer); insertHTML(this.element, this.#bannerContainer); // Show first banner item this.#showFirstBannerItem(); // Get banner elements this.#bannerContainer = document.querySelector('.ip-banner'); this.#closeButton = this.#bannerContainer.querySelector('.ip-banner__close'); this.#contentContainer = this.#bannerContainer.querySelector('.ip-banner__content'); this.#contentFrame = this.#contentContainer.querySelector('.lite-page.block-set'); // Modify content frame classes this.#contentFrame.classList.add('ip-banner__content-frame'); this.#contentFrame.classList.remove('lite-page'); this.#contentFrame.classList.remove('block-set'); // Get banner item and images this.#bannerItem = this.#bannerContainer.querySelector('.ip-banner-item'); this.#imageElements = this.#bannerContainer.querySelectorAll('.image-box img'); if (this.#bannerItem) { this.#countdownElement = this.#bannerItem.querySelector('.countdown-el'); this.#countdownTime = this.#countdownElement.dataset.time; this.#bannerId = this.#bannerItem.className.split('ip-banner-item-')[1]; if (this.#countdownElement) { this.#startCountdown(this.#countdownTime); } } else { this.#bannerContainer.classList.add(HIDDEN_CLASS); } // Load lazy images if (this.#imageElements.length) { for (const image of this.#imageElements) { image.src = image.dataset.src; } } // Add event listener for close button this.#closeButton.addEventListener('click', this.#handleCloseClick); // Check banner state from localStorage this.#checkBannerState(); } #loadScript(src) { const script = document.createElement('script'); script.setAttribute('src', src); document.body.appendChild(script); } #showFirstBannerItem() { const bannerItems = document.querySelectorAll('.ip-banner-item'); if (bannerItems.length) { for (const item of bannerItems) { item.style = 'display: none !important'; } bannerItems[0].style = 'display: block !important'; } } #startCountdown(endTime) { const [date, time] = endTime.split(' '); const endDateTime = moment(`${date}T${time}:00Z`); const now = moment(new Date()); let timeLeft = endDateTime.valueOf() - now.valueOf(); const intervalMs = 1000; const countdownDisplay = document.querySelector(`.ip-banner-item-${this.#bannerId} .countdown-el`); setInterval(() => { timeLeft = moment.duration(timeLeft - intervalMs, 'milliseconds'); countdownDisplay.textContent = timeLeft - intervalMs > 0 ? `${padNumber(timeLeft.days())}Д : ${padNumber(timeLeft.hours())}Ч : ${padNumber(timeLeft.minutes())}М : ${padNumber(timeLeft.seconds())}С` : '00Д : 00Ч : 00М : 00С'; }, intervalMs); } #handleCloseClick = () => { this.#bannerContainer.classList.add(HIDDEN_CLASS); this.#saveBannerState(); }; #saveBannerState = () => { const bannerId = this.#bannerId; if (bannerId >= 1) { const currentTime = moment(new Date()); const storedState = this.#storage.getItem(`ipBannerStateId-${bannerId}`); let clickCount; if (storedState) { clickCount = JSON.parse(storedState).count; } this.#storage.setItem( `ipBannerStateId-${bannerId}`, JSON.stringify({ id: bannerId, count: clickCount ? ++clickCount : 1, timeCloseClick: currentTime }) ); } }; #checkBannerState() { const storedState = this.#storage.getItem(`ipBannerStateId-${this.#bannerId}`); console.log(`ipBannerStateId-${this.#bannerId}`); if (storedState) { const parsedState = JSON.parse(storedState); const bannerId = parsedState.id; const clickCount = parsedState.count; const tenMinutesInMs = 600000; // 10 minutes if (moment(new Date()).valueOf() - moment(parsedState.timeCloseClick).valueOf() >= tenMinutesInMs && clickCount < 3) { console.log('прошло более 6 часов'); } else { document.querySelector(`.ip-banner-item-${bannerId}`).classList.add(HIDDEN_CLASS); if (document.querySelectorAll('.ip-banner-item')[0].classList.contains('hidden')) { this.#bannerContainer.classList.add(HIDDEN_CLASS); } console.log('прошло менее 6 часов'); } } } } // Check if current page should show banner const shouldShowBanner = () => { const currentPath = location.pathname; const allowedPaths = [ '/teach/control/stream', '/pl/teach/control/lesson', '/sales/control', '/profile', '/src-main', '/src-library/cases' ]; return allowedPaths.some(path => currentPath.indexOf(path) !== -1); }; const bannerDeleted = localStorage.getItem('DeleteCSTBanner'); const isShow = shouldShowBanner(); if (isShow) { fetch('/cst-banner') .then(response => response.text()) .then(html => { return new DOMParser() .parseFromString(html, 'text/html') .querySelector('.lite-page.block-set'); }) .then(bannerContent => { if (isShow && !bannerDeleted) { new CornerBanner(bannerContent).init(); } }) .catch(error => console.log('Failed to fetch page: ', error)); } })();