Commit 4d60b4f1a6
Unsigned
Layout: unified · split
web/src/pages/browse.astro +55 −44
| @@ -36,48 +36,55 @@ const categories: Category[] = (await fetchData())?.categories; | ||
| 36 | 36 | </Layout> |
| 37 | 37 | |
| 38 | 38 | <script> |
| 39 | // Time for some good ol' fashioned vanilla JS... | |
| 40 | // I reccomend you not to look at this code for too long, it's not pretty. | |
| 41 | document.addEventListener('DOMContentLoaded', function() { | |
| 42 | const searchInput = document.getElementById('searchInput'); | |
| 43 | const categories = document.querySelectorAll('.category'); | |
| 44 | const pressEnterMsg = document.getElementById('press-enter-msg'); | |
| 45 | const noResults = document.querySelector('.no-results'); | |
| 46 | ||
| 47 | if (!pressEnterMsg || !noResults || !searchInput) { | |
| 48 | console.error('Essential element missing!'); | |
| 49 | return; | |
| 50 | } | |
| 51 | ||
| 52 | searchInput.addEventListener('input', (e) => { | |
| 53 | let resultsCount = 0; | |
| 54 | const filter = e.target.value.trim().toLowerCase(); | |
| 55 | pressEnterMsg.style.visibility = filter ? 'visible' : 'hidden'; | |
| 56 | ||
| 57 | categories.forEach((category) => { | |
| 58 | const titleElement = category.querySelector('.category-title'); | |
| 59 | const title = titleElement ? titleElement.textContent?.toLowerCase() : ''; | |
| 60 | const sections = category.querySelectorAll('.section'); | |
| 61 | let sectionCount = 0; | |
| 62 | ||
| 63 | sections.forEach((section) => { | |
| 64 | const sectionTitle = section.textContent?.toLowerCase(); | |
| 65 | if (sectionTitle?.includes(filter)) { | |
| 66 | section.style.display = 'block'; | |
| 67 | sectionCount++; | |
| 68 | } else { | |
| 69 | section.style.display = 'none'; | |
| 70 | } | |
| 71 | }); | |
| 72 | ||
| 73 | category.style.display = title.includes(filter) || sectionCount > 0 ? 'inline-flex' : 'none'; | |
| 74 | resultsCount += title.includes(filter) ? 1 : sectionCount; | |
| 75 | }); | |
| 76 | ||
| 77 | noResults.style.display = resultsCount === 0 ? 'block' : 'none'; | |
| 39 | // Time for some good ol' fashioned vanilla JS... | |
| 40 | // I reccomend you not to look at this code for too long, it's not pretty. | |
| 41 | const filterInput = document.querySelector('input'); | |
| 42 | const categories = document.querySelectorAll<HTMLElement>('.category'); | |
| 43 | const pressEnterMsg = document.getElementById('press-enter-msg') as HTMLElement | null; | |
| 44 | const noResults = document.querySelector('.no-results') as HTMLElement | null; | |
| 45 | ||
| 46 | if (!pressEnterMsg || !noResults) { | |
| 47 | throw new Error('No pressEnterMsg or noResults'); | |
| 48 | }; | |
| 49 | ||
| 50 | filterInput?.addEventListener('input', (e) => { | |
| 51 | let resultsCount = 0; | |
| 52 | const filter = (e.target as HTMLInputElement).value.toLowerCase(); | |
| 53 | if (filter.length > 0) { | |
| 54 | pressEnterMsg.style.visibility = 'visible'; | |
| 55 | } else { | |
| 56 | pressEnterMsg.style.visibility = 'hidden'; | |
| 57 | } | |
| 58 | categories.forEach((category) => { | |
| 59 | const titleElement = category.querySelector('.category-title'); | |
| 60 | const title = titleElement ? titleElement.textContent?.toLowerCase() : ''; | |
| 61 | const sections = category.querySelectorAll<HTMLElement>('.section'); | |
| 62 | let count = 0; | |
| 63 | sections.forEach((section) => { | |
| 64 | const sectionTitle = section.textContent?.toLowerCase(); | |
| 65 | if (sectionTitle?.includes(filter)) { | |
| 66 | section.style.display = 'block'; | |
| 67 | count++; | |
| 68 | resultsCount++; | |
| 69 | } else { | |
| 70 | section.style.display = 'none'; | |
| 71 | } | |
| 78 | 72 | }); |
| 73 | if (title && title.includes(filter)) { | |
| 74 | category.style.display = 'inline-flex'; | |
| 75 | resultsCount++; | |
| 76 | } else if (count === 0) { | |
| 77 | category.style.display = 'none'; | |
| 78 | } | |
| 79 | }); | |
| 80 | noResults.style.display = resultsCount === 0 ? 'block' : 'none'; | |
| 81 | }); | |
| 79 | 82 | |
| 80 | searchInput.addEventListener('keydown', (event) => { | |
| 83 | if (typeof window !== 'undefined') { | |
| 84 | document.addEventListener('DOMContentLoaded', function() { | |
| 85 | const searchInput = document.getElementById('searchInput') as HTMLInputElement | null; | |
| 86 | if (searchInput === null) return; | |
| 87 | searchInput.addEventListener('keydown', function(event) { | |
| 81 | 88 | if (event.key === 'Enter') { |
| 82 | 89 | event.preventDefault(); |
| 83 | 90 | window.location.href = `/search/${encodeURIComponent(searchInput.value.trim())}`; |
| @@ -86,16 +93,20 @@ const categories: Category[] = (await fetchData())?.categories; | ||
| 86 | 93 | searchInput.value = ''; |
| 87 | 94 | searchInput.blur(); |
| 88 | 95 | pressEnterMsg.style.visibility = 'hidden'; |
| 89 | // Reset display styles | |
| 90 | categories.forEach(category => category.style.display = 'inline-flex'); | |
| 91 | document.querySelectorAll('.section').forEach(section => section.style.display = 'block'); | |
| 96 | categories.forEach((category) => { | |
| 97 | category.style.display = 'inline-flex'; | |
| 98 | const sections = category.querySelectorAll<HTMLElement>('.section'); | |
| 99 | sections.forEach((section) => { | |
| 100 | section.style.display = 'block'; | |
| 101 | }); | |
| 102 | }); | |
| 92 | 103 | } |
| 93 | 104 | }); |
| 94 | 105 | }); |
| 106 | } | |
| 95 | 107 | </script> |
| 96 | 108 | |
| 97 | 109 | |
| 98 | ||
| 99 | 110 | <style lang="scss"> |
| 100 | 111 | |
| 101 | 112 | .head-wrap { |