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