<script>
document.addEventListener('DOMContentLoaded', function() {
setTimeout(function() {
initCategoryFilter();
}, 1000);
function initCategoryFilter() {
const categoryLinks = document.querySelectorAll('.category-link');
categoryLinks.forEach(link => {
link.addEventListener('click', function(e) {
e.preventDefault();
const categoryName = this.textContent.trim();
const categoryUrl = this.getAttribute('href');
setActiveCategory(this);
if (categoryName === 'All') {
showAllProducts();
} else {
filterProductsByCategory(categoryUrl, categoryName);
}
updateURL(categoryUrl);
});
});
}
function setActiveCategory(clickedLink) {
document.querySelectorAll('.category-link').forEach(link => {
link.classList.remove('active');
});
clickedLink.classList.add('active');
}
function showAllProducts() {
const productItems = document.querySelectorAll('.product-list-item');
productItems.forEach((item, index) => {
setTimeout(() => {
item.style.display = 'block';
item.style.animation = 'fadeInProduct 0.3s ease-out';
}, index * 50);
});
updateProductCount();
}
async function filterProductsByCategory(categoryUrl, categoryName) {
showLoader();
try {
const response = await fetch(categoryUrl + '?format=json');
const data = await response.json();
const categoryProductIds = data.items ? data.items.map(item => {
const urlParts = item.fullUrl.split('/');
return urlParts[urlParts.length - 1];
}) : [];
const allProductItems = document.querySelectorAll('.product-list-item');
allProductItems.forEach((item, index) => {
const productLink = item.querySelector('.product-list-item-link');
const productUrl = productLink.getAttribute('href');
const productId = productUrl.split('/').pop();
const shouldShow = categoryProductIds.some(id =>
productUrl.includes(id) ||
productId.includes(id) ||
id.includes(productId)
);
setTimeout(() => {
if (shouldShow) {
item.style.display = 'block';
item.style.animation = 'fadeInProduct 0.3s ease-out';
} else {
item.style.animation = 'fadeOutProduct 0.2s ease-out';
setTimeout(() => {
item.style.display = 'none';
}, 200);
}
}, index * 30);
});
setTimeout(() => {
updateProductCount();
hideLoader();
}, 500);
} catch (error) {
console.log('Error filtering products:', error);
hideLoader();
}
}
function updateProductCount() {
// Remove product count functionality
}
function updateURL(categoryUrl) {
history.pushState(null, '', categoryUrl);
}
function showLoader() {
let loader = document.querySelector('.category-filter-loader');
if (!loader) {
loader = document.createElement('div');
loader.className = 'category-filter-loader';
loader.innerHTML = '<div class="loader-spinner"></div>';
const container = document.querySelector('.product-list-container');
if (container) {
container.appendChild(loader);
}
}
loader.style.display = 'flex';
}
function hideLoader() {
const loader = document.querySelector('.category-filter-loader');
if (loader) {
loader.style.display = 'none';
}
}
window.addEventListener('popstate', function() {
location.reload();
});
});
</script>
<style>
@keyframes fadeInProduct {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes fadeOutProduct {
from {
opacity: 1;
transform: translateY(0);
}
to {
opacity: 0;
transform: translateY(-20px);
}
}
.category-link {
cursor: pointer !important;
transition: all 0.2s ease !important;
text-decoration: none !important;
display: block !important;
padding: 8px 12px !important;
}
.category-link.active {
font-weight: bold !important;
}
.category-item {
list-style: none !important;
margin: 4px 0 !important;
}
.nested-category-tree-wrapper ul {
padding-left: 0 !important;
margin: 0 !important;
}
.category-filter-loader {
position: absolute !important;
top: 50% !important;
left: 50% !important;
transform: translate(-50%, -50%) !important;
z-index: 100 !important;
display: none !important;
align-items: center !important;
justify-content: center !important;
background: rgba(255, 255, 255, 0.9) !important;
width: 100% !important;
height: 100% !important;
}
.loader-spinner {
width: 30px !important;
height: 30px !important;
border: 3px solid #f3f3f3 !important;
border-top: 3px solid #1976d2 !important;
border-radius: 50% !important;
animation: spin 1s linear infinite !important;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.product-list-container {
position: relative !important;
}
@media (max-width: 768px) {
.product-count {
font-size: 12px !important;
margin: 15px 0 !important;
padding: 8px !important;
}
.loader-spinner {
width: 25px !important;
height: 25px !important;
}
.category-link {
padding: 6px 10px !important;
font-size: 14px !important;
}
}
</style>