feat: implement tabbed category navigation with card layout for content browsing

This commit is contained in:
Greg 2025-05-25 20:47:34 +02:00
parent 8cff71e0ce
commit 10265692a1

View File

@ -5,22 +5,44 @@
<div class="tab{{ if eq $i 0 }} active{{ end }}" data-tab="{{ $cat }}">{{ $cat }}</div>
{{ end }}
</div>
<div class="cards" id="cards">
{{ $currentCat := .Title }}
{{ range .Site.RegularPages }}
{{ if and (.Params.category) (or (eq $currentCat "All") (eq .Params.category $currentCat)) }}
{{ partial "card.html" . }}
{{ end }}
{{ end }}
</div>
<script>
// Tab switching with "All" default view
// (same as index.html)
document.addEventListener('DOMContentLoaded', function() {
const tabs = document.querySelectorAll('.tab');
const cards = document.getElementById('cards');
const allCards = Array.from(cards.children);
<div class="cards-container">
{{ range $cat := slice "Books" "Movies" "TV Series" "Recipes" "YouTube" "Music" "Concerts" }}
<div class="category-cards" id="category-{{ $cat | urlize }}">
{{ range where $.Site.RegularPages "Params.category" $cat }}
{{ if not (eq .Params.draft true) }}
{{ partial "card.html" . }}
{{ end }}
{{ end }}
</div>
{{ end }}
<!-- All cards section (shown by default) -->
<div class="category-cards active" id="category-all">
{{ range .Site.RegularPages }}
{{ if not (eq .Params.draft true) }}
{{ partial "card.html" . }}
{{ end }}
{{ end }}
</div>
</div>
<style>
.cards-container {
width: 100%;
}
.category-cards {
display: none; /* Hide all category sections by default */
}
.category-cards.active {
display: flex;
flex-wrap: wrap;
gap: 20px;
justify-content: center;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
// Add an "All" tab at the beginning
const tabsContainer = document.getElementById('tabs');
const allTab = document.createElement('div');
@ -30,26 +52,45 @@ document.addEventListener('DOMContentLoaded', function() {
tabsContainer.prepend(allTab);
// Make other tabs inactive initially
tabs.forEach(tab => tab.classList.remove('active'));
document.querySelectorAll('.tab').forEach(tab => {
tab.classList.remove('active');
});
allTab.classList.add('active');
// Add click handler for all tabs (including the new All tab)
// Add click handler for all tabs
document.querySelectorAll('.tab').forEach(tab => {
tab.addEventListener('click', function() {
const selectedCategory = this.getAttribute('data-tab');
// Update active tab
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
this.classList.add('active');
const cat = this.getAttribute('data-tab');
allCards.forEach(card => {
if (cat === 'all') {
card.style.display = '';
} else {
// Add a data-category attribute to make filtering more reliable
const category = card.getAttribute('data-category');
card.style.display = (category === cat) ? '' : 'none';
}
// Hide all category sections
document.querySelectorAll('.category-cards').forEach(section => {
section.classList.remove('active');
});
// Show the selected category section
if (selectedCategory === 'all') {
document.getElementById('category-all').classList.add('active');
} else {
// Convert to URL-friendly format for element ID matching
const sectionId = 'category-' + selectedCategory.toLowerCase().replace(/\s+/g, '-');
const section = document.getElementById(sectionId);
if (section) {
section.classList.add('active');
} else {
// Fallback to showing all if category not found
document.getElementById('category-all').classList.add('active');
console.log('Category section not found:', sectionId);
}
}
});
});
// Trigger the "All" tab by default
allTab.click();
});
</script>
{{ end }}