This commit is contained in:
epoberezkin
2026-04-02 23:05:21 +00:00
parent 00c75f34a8
commit 220e7586d0

View File

@@ -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');