/** * Скрипт отвечает за обработку модулей иконок в блоках меню ME500 * Проверяет есть ли на странице или в проекте модули Корзины, ЛК, Поиска и Изранное * для взаимодействия с ними при клике на иконки из блоков меню * * Если модули не подключены, выводим соответствующее сообщение с ошибкой на экран */ function t_menuWidgets__init(recId) { const rec = document.querySelector('#rec' + recId); if (!rec) return; const menuEl = rec.querySelector('[data-menu="yes"]'); if (!menuEl) return; const dataHideWidgets = menuEl.getAttribute('data-menu-widgeticons-hide'); const isHideWidgetsOnPage = dataHideWidgets && dataHideWidgets === 'yes'; const dataShowTextWidgets = menuEl.getAttribute('data-menu-widgeticon-textshow'); const isShowTextWidgets = dataShowTextWidgets && dataShowTextWidgets === 'yes'; t_menuWidgets__handledSearchIcon(rec, menuEl); t_menuWidgets__handledCartIcon(rec, menuEl); t_menuWidgets__handledWishlistIcon(rec, menuEl); t_menuWidgets__handledMembersIcon(rec, menuEl); if (isHideWidgetsOnPage) { t_menuWidgets__hideWidgetButtons(recId, menuEl); window.addEventListener( 'resize', t_throttle(function () { t_menuWidgets__hideWidgetButtons(recId, menuEl); }, 200) ); } if (isShowTextWidgets) t_menuWidgets__showWidgetsText(menuEl); } function t_menuWidgets__handledSearchIcon(rec, menuEl) { const searchEl = document.querySelector('.t985'); const showSearchIcon = menuEl.getAttribute('data-menu-widgeticon-search') === 'yes'; const searchIcons = rec.querySelectorAll('.t-menuwidgeticons__link_search'); if (searchEl && showSearchIcon && searchEl.getAttribute('data-search-hook')) { const searchHook = searchEl.getAttribute('data-search-hook'); searchIcons.forEach(function (button) { button.href = searchHook; }); } if (!searchEl && showSearchIcon) { const searchErrorText = window.browserLang === 'RU' ? 'Ошибка: На странице отсутствует виджет поиска (блок T985). Пожалуйста, добавьте его.' : 'Error: search widget (block T985) is not added to this page.'; searchIcons.forEach(function (icon) { icon.addEventListener('click', function (event) { event.preventDefault(); if (!document.querySelector('.t-menuwidgeticons__search-error')) { t_menuWidgets__showError('search', searchErrorText); } }); }); } } function t_menuWidgets__handledCartIcon(rec, menuEl) { const cartEl = document.querySelector('.t706'); const showCartIcon = menuEl.getAttribute('data-menu-widgeticon-cart') === 'yes'; if (!cartEl && showCartIcon) { const cartIcons = rec.querySelectorAll('.t-menuwidgeticons__link_cart'); const cartErrorText = window.browserLang === 'RU' ? 'Ошибка: На странице отсутствует виджет корзины (блок ST100). Пожалуйста, добавьте его.' : 'Error: сart widget (block ST100) is not added to this page.'; cartIcons.forEach(function (icon) { icon.addEventListener('click', function (event) { event.preventDefault(); if (!document.querySelector('.t-menuwidgeticons__cart-error')) { t_menuWidgets__showError('cart', cartErrorText); } }); }); } } function t_menuWidgets__handledWishlistIcon(rec, menuEl) { const wishlistEl = document.querySelector('.t1002'); const showWishlistIcon = menuEl.getAttribute('data-menu-widgeticon-wishlist') === 'yes'; if (!wishlistEl && showWishlistIcon) { const wishlistIcons = rec.querySelectorAll('.t-menuwidgeticons__link_wishlist'); const wishlistErrorText = window.browserLang === 'RU' ? 'Ошибка: На странице отсутствует виджет избранного (блок ST110). Пожалуйста, добавьте его.' : 'Error: favorites widget (block ST110) is not added to this page.'; wishlistIcons.forEach(function (icon) { icon.addEventListener('click', function (event) { event.preventDefault(); if (!document.querySelector('.t-menuwidgeticons__favorites-error')) { t_menuWidgets__showError('favorites', wishlistErrorText); } }); }); } } function t_menuWidgets__handledMembersIcon(rec, menuEl) { const buyerDashboardScript = document.querySelector('script[src*="tilda-buyer-dashboard"]'); const lkDashboardScript = document.querySelector('script[src*="tilda-lk-dashboard"]'); const showMembersIcon = menuEl.getAttribute('data-menu-widgeticon-members') === 'yes'; if (!window.tildaMembers && !buyerDashboardScript && !lkDashboardScript && showMembersIcon) { const memberIcons = rec.querySelectorAll('.t-menuwidgeticons__link_members'); const membersErrorText = window.browserLang === 'RU' ? 'Ошибка: В проекте отсутствует модуль Личного кабинета. Пожалуйста, подключите его.' : 'Error: there is no Members module in the project. Please connect it.'; memberIcons.forEach(function (icon) { icon.addEventListener('click', function (event) { event.preventDefault(); if (!document.querySelector('.t-menuwidgeticons__members-error')) { t_menuWidgets__showError('members', membersErrorText); } }); }); } } function t_menuWidgets__getVisibleMenu(rec) { const windowWidth = window.innerWidth; const screenMin = rec.getAttribute('data-screen-min'); const screenMax = rec.getAttribute('data-screen-max'); if (screenMin && windowWidth < parseInt(screenMin, 10)) return false; if (screenMax && windowWidth > parseInt(screenMax, 10)) return false; return true; } function t_menuWidgets__hideWidgetButtons(recId, menuEl) { const rec = document.querySelector('#rec' + recId); const hideWidgetClass = 't-menuwidgeticons__button_hidden_' + recId; const isVisibleMenu = t_menuWidgets__getVisibleMenu(rec); const icons = [ { element: document.querySelector('.t985__search-widget-button'), isExsists: menuEl.getAttribute('data-menu-widgeticon-search') === 'yes', }, { element: document.querySelector('.t706__carticon'), isExsists: menuEl.getAttribute('data-menu-widgeticon-cart') === 'yes', }, { element: document.querySelector('.t1002__wishlisticon'), isExsists: menuEl.getAttribute('data-menu-widgeticon-wishlist') === 'yes', }, ]; icons.forEach(function (icon) { if (!icon.element || !icon.isExsists) return; isVisibleMenu ? icon.element.classList.add(hideWidgetClass) : icon.element.classList.remove(hideWidgetClass); }); } function t_menuWidgets__showError(type, text) { const shownErrors = document.querySelectorAll('.t-menuwidgeticons__error'); let errorTopPos; if (shownErrors.length > 0) { const lastError = shownErrors[shownErrors.length - 1]; errorTopPos = parseInt(window.getComputedStyle(lastError).top) + parseInt(window.getComputedStyle(lastError).height) + 20; } const html = t_menuWidgets__getErrorHtml(type, text); document.body.classList.add('t-menuwidgeticons__body-error_showed'); document.body.insertAdjacentHTML('beforeend', html); const currentError = document.querySelector('.t-menuwidgeticons__' + type + '-error'); setTimeout(function () { currentError.style.transition = 'opacity .4s, right .4s'; currentError.style.opacity = 1; currentError.style.right = '20px'; if (errorTopPos) currentError.style.top = errorTopPos + 'px'; }, 0); setTimeout(function () { t_menuWidgets_closeError(currentError); }, 3400); const errorCloseButton = currentError.querySelector('.t-menuwidgeticons__error-close'); if (!errorCloseButton) return; errorCloseButton.addEventListener('click', function () { t_menuWidgets_closeError(currentError); }); } function t_menuWidgets__getErrorHtml(type, text) { let html = ''; html += '
'; html += '
×
'; html += '
' + text + '
'; html += '
'; return html; } function t_menuWidgets_closeError(errorElement) { if (!errorElement) return; errorElement.style.opacity = 0; errorElement.style.right = '-350px'; setTimeout(function () { if (errorElement.parentNode !== null) { errorElement.parentNode.removeChild(errorElement); document.body.classList.remove('t-menuwidgeticons__body-error_showed'); } }, 300); } function t_menuWidgets__showWidgetsText(menuEl) { const translation = { cart: { EN: 'Cart', RU: 'Корзина', FR: 'Panier', DE: 'Warenkorb', ES: 'Carrito', PT: 'Carrinho', UK: 'Кошик', JA: 'カート', ZH: '购物车', PL: 'Koszyk', KK: 'Себет', IT: 'Carrello', LV: 'Grozs', }, wishlist: { EN: 'Favorites', RU: 'Избранное', FR: 'Favoris', DE: 'Favoriten', ES: 'Favoritos', PT: 'Favoritos', UK: 'Вибране', JA: 'お気に入り', ZH: '收藏夹', PL: 'Ulubione', KK: 'Таңдаулылар', IT: 'Preferiti', LV: 'Iecienītie', }, search: { EN: 'Search', RU: 'Поиск', FR: 'Rechercher', DE: 'Suche', ES: 'Buscar', PT: 'Buscar', UK: 'Пошук', JA: '検索', ZH: '搜索', PL: 'Szukaj', KK: 'Іздеу', IT: 'Cerca', LV: 'Meklēt', }, members: { EN: 'Sign In', RU: 'Войти', FR: 'Se connecter', DE: 'Anmelden', ES: 'Iniciar sesión', PT: 'Entrar', UK: 'Увійти', JA: 'サインイン', ZH: '登录', PL: 'Zaloguj się', KK: 'Кіру', IT: 'Accedi', LV: 'Pieteikties', }, home: { EN: 'Home', RU: 'Главная', FR: 'Accueil', DE: 'Startseite', ES: 'Inicio', PT: 'Início', UK: 'Головна', JA: 'ホーム', ZH: '首页', PL: 'Strona główna', KK: 'Басты бет', IT: 'Home', LV: 'Sākums', }, catalog: { EN: 'Catalog', RU: 'Каталог', FR: 'Catalogue', DE: 'Katalog', ES: 'Catálogo', PT: 'Catálogo', UK: 'Каталог', JA: 'カタログ', ZH: '产品目录', PL: 'Katalog', KK: 'Каталог', IT: 'Catalogo', LV: 'Katalogs', }, }; const allRecords = document.getElementById('allrecords'); if (!allRecords) return; let lang = (window.navigator.userLanguage || window.navigator.language).toUpperCase().slice(0, 2); // Переопределение языка на основе атрибута if (!lang) { const projectLang = allRecords.getAttribute('data-tilda-project-lang'); if (projectLang) lang = projectLang; } // Отмечены разрешенные языки. По умолчанию используется английский const acceptedLangs = ['EN', 'RU', 'FR', 'DE', 'ES', 'PT', 'UK', 'JA', 'ZH', 'PL', 'KK', 'IT', 'LV']; if (acceptedLangs.indexOf(lang) === -1) { lang = 'EN'; } const SEARCH_CLASS = 't-menuwidgeticons__link_'; const icons = menuEl.querySelectorAll('.t-menuwidgeticons__link'); icons.forEach(function (icon) { const iconСlasses = icon.className.split(' '); const typeClass = iconСlasses.find(function (iconСlass) { return iconСlass.indexOf(SEARCH_CLASS) !== -1 ? iconСlass : false; }); const type = typeClass.replace(SEARCH_CLASS, ''); const text = translation[type][lang]; const html = t_menuWidgets__getWidgetsTextHTML(text, type); icon.insertAdjacentHTML('beforeend', html); }); } function t_menuWidgets__getWidgetsTextHTML(text, type) { let html = ''; html += ''; html += text; html += ''; return html; }