From 3fc87d235a67978cd7bf4cb9dca9aaad777dc840 Mon Sep 17 00:00:00 2001 From: Antoine M Date: Wed, 27 May 2026 18:33:42 +0200 Subject: [PATCH] FEATURE Handling data tab urls --- .../statistiques-collections.php | 19 ++++- .../statistiques-collections/view.js | 73 ++++++++++++++++++- 2 files changed, 85 insertions(+), 7 deletions(-) diff --git a/plugins/carhop-blocks/acf-blocks/statistiques-collections/statistiques-collections.php b/plugins/carhop-blocks/acf-blocks/statistiques-collections/statistiques-collections.php index 6c59661..9112c21 100644 --- a/plugins/carhop-blocks/acf-blocks/statistiques-collections/statistiques-collections.php +++ b/plugins/carhop-blocks/acf-blocks/statistiques-collections/statistiques-collections.php @@ -1,6 +1,6 @@ -
'statistiques-collections content-section ')); ?>> +
'statistiques-collections content-section ', + 'id' => 'statistiques-collections', +)); ?>>
$document_statistic) : ?> + diff --git a/plugins/carhop-blocks/acf-blocks/statistiques-collections/view.js b/plugins/carhop-blocks/acf-blocks/statistiques-collections/view.js index 11f910e..af73828 100644 --- a/plugins/carhop-blocks/acf-blocks/statistiques-collections/view.js +++ b/plugins/carhop-blocks/acf-blocks/statistiques-collections/view.js @@ -5,12 +5,19 @@ document.addEventListener("DOMContentLoaded", function () { if (!tablist) return; const tabsButtons = tablist.querySelectorAll("button"); + if (!tabsButtons.length) return; + + const blockSection = + document.getElementById("statistiques-collections") || + tablist.closest("section.statistiques-collections"); function setActiveTab(currentTab) { tabsButtons.forEach((tab) => { tab.setAttribute("aria-selected", "false"); + tab.setAttribute("tabindex", "-1"); }); currentTab.setAttribute("aria-selected", "true"); + currentTab.setAttribute("tabindex", "0"); } function setActiveTabPanel(currentTabButton) { @@ -18,12 +25,14 @@ document.addEventListener("DOMContentLoaded", function () { const currentTabPanel = document.querySelector( `.statistiques-collections__stats-items #${currentTabPanelId}`, ); + if (!currentTabPanel) return; currentTabPanel.setAttribute("data-active", "true"); animateBar(currentTabPanel); } function animateBar(currentTabPanel) { const bar = currentTabPanel.querySelector(".percentage-bar"); + if (!bar) return; const percentage = bar.getAttribute("data-percentage"); bar.style.width = "0%"; requestAnimationFrame(() => { @@ -31,6 +40,54 @@ document.addEventListener("DOMContentLoaded", function () { }); } + function scrollToBlock() { + if (!blockSection) return; + blockSection.scrollIntoView({ behavior: "smooth", block: "start" }); + } + + function updateUrlTabParam(tab) { + const tabKey = tab.getAttribute("data-tab-key"); + if (!tabKey) return; + + const url = new URL(window.location.href); + url.searchParams.set("tab", tabKey); + url.searchParams.delete("collection"); + url.searchParams.delete("stats_tab"); + + window.history.replaceState(null, "", url); + } + + function activateTab(tab, options = {}) { + const { updateUrl = true } = options; + setActiveTab(tab); + hideAllTabPanels(); + setActiveTabPanel(tab); + if (updateUrl) { + updateUrlTabParam(tab); + } + } + + function getInitialTabFromUrl() { + const params = new URLSearchParams(window.location.search); + const rawValue = + params.get("tab") || params.get("collection") || params.get("stats_tab"); + if (!rawValue) return null; + return rawValue.trim().toLowerCase(); + } + + function findTabButtonByQueryValue(queryValue) { + if (!queryValue) return null; + return Array.from(tabsButtons).find((tab) => { + const tabKey = (tab.getAttribute("data-tab-key") || "") + .trim() + .toLowerCase(); + const tabIndex = (tab.getAttribute("data-tab") || "") + .trim() + .toLowerCase(); + return tabKey === queryValue || tabIndex === queryValue; + }); + } + function hideAllTabPanels() { const tabPanels = document.querySelectorAll( ".statistiques-collections__stats-items .statistiques-collections__stat-item", @@ -42,9 +99,19 @@ document.addEventListener("DOMContentLoaded", function () { tabsButtons.forEach((tab) => { tab.addEventListener("click", () => { - setActiveTab(tab); - hideAllTabPanels(); - setActiveTabPanel(tab); + activateTab(tab); }); }); + + const initialTabValue = getInitialTabFromUrl(); + const initialTabButton = findTabButtonByQueryValue(initialTabValue); + if (initialTabButton) { + activateTab(initialTabButton, { updateUrl: false }); + const scrollAfterLoad = () => setTimeout(() => scrollToBlock(), 200); + if (document.readyState === "complete") { + scrollAfterLoad(); + } else { + window.addEventListener("load", scrollAfterLoad, { once: true }); + } + } });