From 220e7586d0f53e08ee6ff7eda8da73fb8f66beed Mon Sep 17 00:00:00 2001 From: epoberezkin Date: Thu, 2 Apr 2026 23:05:21 +0000 Subject: [PATCH] deploy: a83779f7c960459b0053b97faa9eb99cc3ea66d6 --- js/directory.js | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/js/directory.js b/js/directory.js index 42403e279d..d008370342 100644 --- a/js/directory.js +++ b/js/directory.js @@ -18,16 +18,53 @@ async function initDirectory() { const topBtn = document.querySelector('#top-pagination .top'); const searchInput = document.getElementById('search'); allEntries = listing.entries - renderEntries('top', bySortPriority, topBtn) - searchInput.addEventListener('input', (e) => renderEntries('top', bySortPriority, topBtn, e.target.value.trim())); + + applyHash(); + + searchInput.addEventListener('input', (e) => renderEntries('top', bySortPriority, topBtn, e.target.value.trim(), true)); liveBtn.addEventListener('click', () => renderEntries('live', byActiveAtDesc, liveBtn)); newBtn.addEventListener('click', () => renderEntries('new', byCreatedAtDesc, newBtn)); topBtn.addEventListener('click', () => renderEntries('top', bySortPriority, topBtn)); + window.addEventListener('popstate', applyHash); + + function applyHash() { + const hash = location.hash; + let mode, comparator, btn, search = ''; + switch (hash) { + case '#active': + mode = 'live'; + comparator = byActiveAtDesc; + btn = liveBtn; + case '#new': + mode = 'new'; + comparator = byCreatedAtDesc; + btn = newBtn; + default: + mode = 'top'; + comparator = bySortPriority; + btn = topBtn; + try { + if (hash.startsWith('#q=')) { + search = decodeURIComponent(hash.slice(3)); + if (search) searchInput.value = search; + } + } catch(e) {} + } + currentSortMode = ''; + currentSearch = ''; + currentPage = 1; + renderEntries(mode, comparator, btn, search); + } function renderEntries(mode, comparator, btn, search = '') { if (currentSortMode === mode && search == currentSearch) return; currentSortMode = mode; - if (location.hash) location.hash = ''; + const hash = search ? '#q=' + encodeURIComponent(search) + : mode === 'live' ? '#active' + : mode === 'new' ? '#new' + : ''; + const url = hash || (location.pathname + location.search); + history.replaceState(null, '', url); liveBtn.classList.remove('active'); newBtn.classList.remove('active'); topBtn.classList.remove('active');