diff --git a/pos/static/js/catalog.js b/pos/static/js/catalog.js index 68f04dc..059950d 100644 --- a/pos/static/js/catalog.js +++ b/pos/static/js/catalog.js @@ -62,6 +62,44 @@ var currentDetailPart = null; var detailQty = 1; var isOffline = false; + var skipPush = false; // flag to avoid pushing state on popstate + + // ─── Browser History Management ─── + function pushNavState() { + if (skipPush) return; + var s = JSON.parse(JSON.stringify(nav)); + s.page = currentPage; + history.pushState(s, '', '/pos/catalog'); + } + + window.addEventListener('popstate', function (e) { + if (e.state) { + skipPush = true; + nav.level = e.state.level || 'brands'; + nav.brand = e.state.brand; + nav.model = e.state.model; + nav.year = e.state.year; + nav.engine = e.state.engine; + nav.category = e.state.category; + nav.group = e.state.group; + currentPage = e.state.page || 1; + + // Reload the correct level + if (nav.level === 'brands') loadBrands(); + else if (nav.level === 'models') loadModels(); + else if (nav.level === 'years') loadYears(); + else if (nav.level === 'engines') loadEngines(); + else if (nav.level === 'categories') loadCategories(); + else if (nav.level === 'groups') loadGroups(); + else if (nav.level === 'parts') loadParts(currentPage); + else loadBrands(); + + skipPush = false; + } + }); + + // Push initial state + history.replaceState(JSON.parse(JSON.stringify(nav)), '', '/pos/catalog'); // ─── Cart State ─── var cartItems = JSON.parse(localStorage.getItem('pos_cart') || '[]'); @@ -143,6 +181,7 @@ function resetNav() { nav.level = 'brands'; + pushNavState(); nav.brand = nav.model = nav.year = nav.engine = nav.category = nav.group = null; } @@ -176,6 +215,7 @@ function loadBrands() { nav.level = 'brands'; + pushNavState(); updateBreadcrumb(); levelTitle.textContent = 'Selecciona una marca'; setupLevelFilter(true); @@ -209,6 +249,7 @@ function loadModels() { nav.level = 'models'; + pushNavState(); updateBreadcrumb(); levelTitle.textContent = 'Modelos de ' + nav.brand.name; setupLevelFilter(true); @@ -235,6 +276,7 @@ function loadYears() { nav.level = 'years'; + pushNavState(); updateBreadcrumb(); levelTitle.textContent = nav.brand.name + ' ' + nav.model.name + ' — Anios'; setupLevelFilter(false); @@ -261,6 +303,7 @@ function loadEngines() { nav.level = 'engines'; + pushNavState(); updateBreadcrumb(); levelTitle.textContent = nav.brand.name + ' ' + nav.model.name + ' ' + nav.year.year + ' — Motor'; setupLevelFilter(false); @@ -298,6 +341,7 @@ function loadCategories() { nav.level = 'categories'; + pushNavState(); updateBreadcrumb(); levelTitle.textContent = 'Categorias de partes'; setupLevelFilter(true); @@ -325,6 +369,7 @@ function loadGroups() { nav.level = 'groups'; + pushNavState(); updateBreadcrumb(); levelTitle.textContent = nav.category.name; setupLevelFilter(true); @@ -352,6 +397,7 @@ function loadParts(page) { nav.level = 'parts'; + pushNavState(); currentPage = page || 1; updateBreadcrumb(); levelTitle.textContent = nav.group.name; @@ -932,6 +978,7 @@ nav.year = { id: parseInt(vsYear.value), year: yearText }; nav.engine = { id_mye: parseInt(myeId), name: engineText }; nav.level = 'categories'; + pushNavState(); loadCategories(); @@ -953,6 +1000,7 @@ vsClear.style.display = 'none'; nav.level = 'brands'; nav.brand = null; nav.model = null; nav.year = null; nav.engine = null; nav.category = null; nav.group = null; currentPage = 1; + pushNavState(); loadBrands(); }