← Back
Editing: page-ofertas.js
// Elementos globais const buttons = document.querySelectorAll(".estado-btn"); const encartesCards = document.querySelector(".encartes-cards"); const encartes = document.querySelectorAll(".encarte-item"); const fallback = document.querySelector(".encarte-vazio"); function filtrarEncartesPorEstado(estadoSelecionado) { if (!encartesCards) return; encartesCards.classList.add("fade"); setTimeout(() => { let algumVisivel = false; encartes.forEach((encarte) => { const estados = encarte.getAttribute("data-estado")?.split(",") || []; if (estados.includes(estadoSelecionado)) { encarte.style.display = "block"; algumVisivel = true; } else { encarte.style.display = "none"; } }); if (fallback) { fallback.style.display = algumVisivel ? "none" : "block"; } encartesCards.classList.remove("fade"); }, 350); } function configurarBotoesEstado() { buttons.forEach((btn) => { btn.addEventListener("click", function () { buttons.forEach((b) => b.classList.remove("active")); this.classList.add("active"); const estado = this.getAttribute("data-estado"); filtrarEncartesPorEstado(estado); }); }); } function verificarSeHaEncartesVisiveis() { let algumVisivel = false; encartes.forEach((encarte) => { if (encarte.style.display !== "none") { algumVisivel = true; } }); if (fallback) { fallback.style.display = algumVisivel ? "none" : "block"; } } function configurarObserverFallback() { const observer = new MutationObserver(() => { verificarSeHaEncartesVisiveis(); }); if (encartesCards) { observer.observe(encartesCards, { childList: true, subtree: true, attributes: true, attributeFilter: ["style"], }); } } function initEncarteButtons() { const buttons = document.querySelectorAll(".encarte-button"); buttons.forEach((button) => { button.addEventListener("click", function () { const encarteId = this.getAttribute("data-encarte-id"); const encarteItem = this.closest(".encarte-item"); const thumb = encarteItem.querySelector("._df_thumb"); if (thumb) { thumb.click(); } else { console.error( "Elemento _df_thumb não encontrado para o encarte ID: " + encarteId ); } }); }); } function initContentMutationObserver() { const observer = new MutationObserver(() => { const bookTitles = document.querySelectorAll(".df-book-title"); bookTitles.forEach((title) => { if (title.textContent === "Open Book") { title.textContent = "Abrir Encarte"; } }); const bookImages = document.querySelectorAll('img[alt="Open Book"]'); bookImages.forEach((img) => { img.alt = "Abrir Encarte"; }); const thumbDivs = document.querySelectorAll( 'div._df_thumb[title="Open Book"]' ); thumbDivs.forEach((div) => { div.title = "Abrir Encarte"; }); if (bookTitles.length && bookImages.length && thumbDivs.length) { observer.disconnect(); } }); observer.observe(document.body, { childList: true, subtree: true, }); } function initEncarteCloseOnOutsideClick() { document.addEventListener("click", function (event) { const pages = document.querySelectorAll(".df-page"); const contents = document.querySelectorAll(".df-page-content"); const closeButton = document.querySelector(".df-lightbox-close"); const uiContainer = document.querySelector(".df-ui"); if (!pages.length || !contents.length || !closeButton || !uiContainer) return; const clickedInsideViewer = Array.from(pages).some((page) => page.contains(event.target)) || Array.from(contents).some((content) => content.contains(event.target)) || uiContainer.contains(event.target); if (!clickedInsideViewer) { closeButton.click(); } }); } function initScrollWarningDismiss() { const warning = document.querySelector(".scroll-warning"); if (!warning) return; warning.style.transition = "opacity 0.5s ease-out, visibility 0.5s ease-out"; window.addEventListener("scroll", function () { if (window.scrollY > 120) { warning.style.opacity = "0"; warning.style.visibility = "hidden"; } else { warning.style.opacity = "1"; warning.style.visibility = "visible"; } }); } function initFiltroSupermercado() { const selects = document.querySelectorAll( "#supermercado-select, #supermercado-go-select" ); if (!selects.length) return; selects.forEach(function (select) { select.addEventListener("change", function () { const supermercadoSelecionado = this.value; const encartes = document.querySelectorAll(".encarte-item"); let algumEncarteVisivel = false; encartes.forEach(function (encarte) { const supermercados = encarte.getAttribute("data-supermercado"); if (!supermercados) { encarte.style.display = "none"; return; } const listaSupermercados = supermercados.split(","); const corresponde = supermercadoSelecionado === "" || listaSupermercados.includes(supermercadoSelecionado); if (corresponde) { encarte.style.display = "block"; algumEncarteVisivel = true; } else { encarte.style.display = "none"; } }); const aviso = document.querySelector(".encarte-vazio"); if (aviso) { aviso.style.display = algumEncarteVisivel ? "none" : "block"; } }); }); } function trocarSelectEstado() { const botoesEstado = document.querySelectorAll(".estado-btn"); const selectMG = document.getElementById("supermercado-select"); const labelMG = document.querySelector("label[for='supermercado-select']"); const wrapperGO = document.getElementById("goias-select-wrapper"); if (!botoesEstado.length) return; botoesEstado.forEach(function (botao) { botao.addEventListener("click", function () { const estadoSelecionado = this.getAttribute("data-estado"); botoesEstado.forEach((btn) => btn.classList.remove("active")); this.classList.add("active"); if (estadoSelecionado === "Minas Gerais") { if (selectMG) selectMG.style.display = "block"; if (labelMG) labelMG.style.display = "block"; if (wrapperGO) wrapperGO.style.display = "none"; } else if (estadoSelecionado === "Goias") { if (selectMG) selectMG.style.display = "none"; if (labelMG) labelMG.style.display = "none"; if (wrapperGO) wrapperGO.style.display = "block"; } }); }); } function inicializarEncartes() { configurarBotoesEstado(); filtrarEncartesPorEstado("Goias"); configurarObserverFallback(); verificarSeHaEncartesVisiveis(); initEncarteButtons(); initContentMutationObserver(); initEncarteCloseOnOutsideClick(); initScrollWarningDismiss(); initFiltroSupermercado(); trocarSelectEstado(); } inicializarEncartes();
Save File
Cancel