Skip to content

Commit d848c03

Browse files
committed
Make selected tags sharable
1 parent a2df905 commit d848c03

1 file changed

Lines changed: 74 additions & 11 deletions

File tree

_includes/filter_widget.html

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,24 +128,87 @@
128128
</style>
129129

130130
<script>
131-
/* JS remains focused on toggling .filter-item visibility based on currentTag and search query */
132-
window.currentTag = 'all';
131+
/**
132+
* 1. Initial Load: Read URL parameters and apply them
133+
*/
134+
document.addEventListener('DOMContentLoaded', function() {
135+
const params = new URLSearchParams(window.location.search);
136+
const urlTag = params.get('tag') || 'all';
137+
const urlQuery = params.get('q') || '';
138+
139+
// Set initial state
140+
window.currentTag = urlTag;
141+
const searchInput = document.getElementById('search-input');
142+
if (searchInput) searchInput.value = urlQuery;
143+
144+
// Apply filters without pushing to history again
145+
window.updateFilters(false);
146+
});
147+
148+
/**
149+
* 2. Tag Selection Handler
150+
*/
133151
window.applyTagFilter = function(tag) {
134152
window.currentTag = tag;
135-
window.updateFilters();
153+
window.updateFilters(true);
136154
};
137-
window.updateFilters = function() {
138-
const q = document.getElementById('search-input').value.toLowerCase();
155+
156+
/**
157+
* 3. Core Filter Engine
158+
* @param {boolean} updateUrl - If true, syncs state to browser address bar
159+
*/
160+
window.updateFilters = function(updateUrl = true) {
161+
const searchInput = document.getElementById('search-input');
162+
const q = searchInput ? searchInput.value : '';
163+
const qLower = q.toLowerCase();
164+
165+
// Sync state to URL
166+
if (updateUrl) {
167+
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
180+
window.history.replaceState({}, '', url);
181+
}
182+
183+
// Filter Items in DOM
139184
document.querySelectorAll('.filter-item').forEach(item => {
140185
const tags = (item.dataset.tags || "").split(',');
141-
const match = (window.currentTag === 'all' || tags.includes(window.currentTag)) && item.innerText.toLowerCase().includes(q);
142-
item.classList.toggle('is-hidden', !match);
186+
const text = item.innerText.toLowerCase();
187+
188+
const matchesTag = (window.currentTag === 'all' || tags.includes(window.currentTag));
189+
const matchesSearch = text.includes(qLower);
190+
191+
item.classList.toggle('is-hidden', !(matchesTag && matchesSearch));
143192
});
144-
// Update button active states
193+
194+
// Update Button UI States
145195
document.querySelectorAll('.filter-btn').forEach(btn => {
146-
const btnTag = btn.getAttribute('onclick').match(/'([^']+)'/)[1];
147-
btn.classList.toggle('active', btnTag === window.currentTag);
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+
}
148203
});
149204
};
150-
window.resetFilters = function() { document.getElementById('search-input').value = ''; window.applyTagFilter('all'); };
205+
206+
/**
207+
* 4. Reset Handler
208+
*/
209+
window.resetFilters = function() {
210+
const searchInput = document.getElementById('search-input');
211+
if (searchInput) searchInput.value = '';
212+
window.applyTagFilter('all');
213+
};
151214
</script>

0 commit comments

Comments
 (0)