diff options
| author | Sivaldo <sivaldodavi@disroot.org> | 2026-06-13 19:20:23 -0300 |
|---|---|---|
| committer | Sivaldo <sivaldodavi@disroot.org> | 2026-06-13 19:20:23 -0300 |
| commit | 7316238f16655bd127b04476bbf7864b27f940f9 (patch) | |
| tree | 61f25290b3e5fd14894e52754cfe6dd4e85845ef /static/home-filter.js | |
Diffstat (limited to 'static/home-filter.js')
| -rw-r--r-- | static/home-filter.js | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/static/home-filter.js b/static/home-filter.js new file mode 100644 index 0000000..b2d1a47 --- /dev/null +++ b/static/home-filter.js @@ -0,0 +1,72 @@ +/** + * Home Filter — filtra disciplinas conforme você digita + * Só funciona quando há itens [data-filterable] na página + */ +document.addEventListener('DOMContentLoaded', () => { + const items = document.querySelectorAll('[data-filterable]'); + if (!items.length) return; + + // Cria campo de busca + const wrap = document.createElement('div'); + wrap.className = 'filter-container'; + wrap.innerHTML = ` + <input + type="search" + id="home-filter" + class="filter-input" + placeholder="Buscar disciplina…" + aria-label="Filtrar disciplinas" + autocomplete="off" + spellcheck="false" + > + <span class="filter-count" id="filter-count" aria-live="polite"></span> + `; + + // Insere antes do disc-grid dentro do <main> + const grid = document.querySelector('.disc-grid'); + if (grid) { + grid.parentNode.insertBefore(wrap, grid); + } else { + const main = document.querySelector('main'); + if (main) main.insertBefore(wrap, main.firstChild); + } + + const input = document.getElementById('home-filter'); + const count = document.getElementById('filter-count'); + + // Normaliza texto para comparação (remove acentos, maiúsculas) + function normalize(str) { + return str.normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLowerCase(); + } + + input.addEventListener('input', () => { + const q = normalize(input.value.trim()); + let visible = 0; + + items.forEach(item => { + const text = normalize(item.textContent); + const match = !q || text.includes(q); + item.style.display = match ? '' : 'none'; + if (match) visible++; + }); + + count.textContent = q ? `${visible} resultado${visible !== 1 ? 's' : ''}` : ''; + }); + + // Esc limpa e desfoca + input.addEventListener('keydown', e => { + if (e.key === 'Escape') { + input.value = ''; + input.dispatchEvent(new Event('input')); + input.blur(); + } + }); + + // Foco automático na home com / ou Ctrl+K + document.addEventListener('keydown', e => { + if ((e.key === '/' || (e.ctrlKey && e.key === 'k')) && document.activeElement !== input) { + e.preventDefault(); + input.focus(); + } + }); +}); |
