Skip to content

Commit c8efdb1

Browse files
committed
Show not found on url with not existing tags
1 parent d848c03 commit c8efdb1

1 file changed

Lines changed: 46 additions & 51 deletions

File tree

_includes/filter_widget.html

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
</button>
4646
{% endfor %}
4747
</div>
48+
<div id="no-results" class="no-results-message is-hidden">
49+
Please select a tag to view items.
50+
</div>
4851
</div>
4952

5053
<style>
@@ -125,90 +128,82 @@
125128
.count-pill { background: rgba(0,0,0,0.08); padding: 1px 6px; border-radius: 10px; margin-left: 8px; font-size: 0.8em; }
126129
.filter-btn.active .count-pill { background: rgba(255,255,255,0.2); }
127130

131+
.no-results-message {
132+
margin: 20px 0;
133+
padding: 2rem;
134+
text-align: center;
135+
background: #fff9e6; /* Light amber background */
136+
border: 1px solid #ffe58f; /* Subtle border */
137+
border-radius: 8px;
138+
color: #856404; /* Darker amber text */
139+
font-weight: bold;
140+
font-size: 1.1em;
141+
}
142+
/* Ensure this is at the bottom of your style block */
143+
.is-hidden { display: none !important; }
128144
</style>
129145

130146
<script>
131-
/**
132-
* 1. Initial Load: Read URL parameters and apply them
133-
*/
147+
/** 1. On Load: Set state from URL and run filter **/
134148
document.addEventListener('DOMContentLoaded', function() {
135149
const params = new URLSearchParams(window.location.search);
136-
const urlTag = params.get('tag') || 'all';
137-
const urlQuery = params.get('q') || '';
150+
window.currentTag = params.get('tag'); // No fallback here ensures empty if missing
138151

139-
// Set initial state
140-
window.currentTag = urlTag;
141152
const searchInput = document.getElementById('search-input');
142-
if (searchInput) searchInput.value = urlQuery;
153+
if (searchInput) searchInput.value = params.get('q') || '';
143154

144-
// Apply filters without pushing to history again
145-
window.updateFilters(false);
155+
window.updateFilters(false); // Initial run
146156
});
147157

148-
/**
149-
* 2. Tag Selection Handler
150-
*/
158+
/** 2. Click Handler **/
151159
window.applyTagFilter = function(tag) {
152160
window.currentTag = tag;
153161
window.updateFilters(true);
154162
};
155163

156-
/**
157-
* 3. Core Filter Engine
158-
* @param {boolean} updateUrl - If true, syncs state to browser address bar
159-
*/
164+
/** 3. Core Logic: URL Sync + Visibility + No Results Message **/
160165
window.updateFilters = function(updateUrl = true) {
161-
const searchInput = document.getElementById('search-input');
162-
const q = searchInput ? searchInput.value : '';
166+
const q = document.getElementById('search-input').value || '';
163167
const qLower = q.toLowerCase();
164168

165-
// Sync state to URL
169+
// Sync Address Bar
166170
if (updateUrl) {
167171
const url = new URL(window.location);
168-
if (window.currentTag === 'all') {
169-
url.searchParams.delete('tag');
170-
} else {
171-
url.searchParams.set('tag', window.currentTag);
172-
}
173-
174-
if (q) {
175-
url.searchParams.set('q', q);
176-
} else {
177-
url.searchParams.delete('q');
178-
}
179-
// Update address bar without reloading
172+
window.currentTag ? url.searchParams.set('tag', window.currentTag) : url.searchParams.delete('tag');
173+
q ? url.searchParams.set('q', q) : url.searchParams.delete('q');
180174
window.history.replaceState({}, '', url);
181175
}
182176

183-
// Filter Items in DOM
177+
let visibleCount = 0;
178+
179+
// Logic: Only show if a tag is active (currentTag is truthy) AND search matches
184180
document.querySelectorAll('.filter-item').forEach(item => {
185181
const tags = (item.dataset.tags || "").split(',');
186-
const text = item.innerText.toLowerCase();
187-
188-
const matchesTag = (window.currentTag === 'all' || tags.includes(window.currentTag));
189-
const matchesSearch = text.includes(qLower);
182+
const matchesTag = (window.currentTag === 'all') || (window.currentTag && tags.includes(window.currentTag));
183+
const matchesSearch = item.innerText.toLowerCase().includes(qLower);
190184

191-
item.classList.toggle('is-hidden', !(matchesTag && matchesSearch));
185+
const isVisible = !!(matchesTag && matchesSearch);
186+
item.classList.toggle('is-hidden', !isVisible);
187+
if (isVisible) visibleCount++;
192188
});
193189

194-
// Update Button UI States
190+
// Handle No Results Message
191+
const noResultsEl = document.getElementById('no-results');
192+
if (noResultsEl) {
193+
noResultsEl.classList.toggle('is-hidden', visibleCount > 0);
194+
noResultsEl.innerText = !window.currentTag ? "Please select a tag to view items." : "No items match your criteria.";
195+
}
196+
197+
// Sync Button UI
195198
document.querySelectorAll('.filter-btn').forEach(btn => {
196-
// Extract the tag name from the onclick attribute: applyTagFilter('tag-name')
197-
const onclickAttr = btn.getAttribute('onclick') || "";
198-
const match = onclickAttr.match(/'([^']+)'/);
199-
if (match) {
200-
const btnTag = match[1];
201-
btn.classList.toggle('active', btnTag === window.currentTag);
202-
}
199+
const btnTag = btn.getAttribute('onclick').match(/'([^']+)'/)[1];
200+
btn.classList.toggle('active', btnTag === window.currentTag);
203201
});
204202
};
205203

206-
/**
207-
* 4. Reset Handler
208-
*/
204+
/** 4. Reset **/
209205
window.resetFilters = function() {
210-
const searchInput = document.getElementById('search-input');
211-
if (searchInput) searchInput.value = '';
206+
document.getElementById('search-input').value = '';
212207
window.applyTagFilter('all');
213208
};
214209
</script>

0 commit comments

Comments
 (0)