38 lines
1.4 KiB
JavaScript
38 lines
1.4 KiB
JavaScript
const input = document.querySelector('#search');
|
|
const sections = [...document.querySelectorAll('article section')];
|
|
const status = document.querySelector('#search-status');
|
|
const links = [...document.querySelectorAll('nav a')];
|
|
|
|
input.addEventListener('input', () => {
|
|
const query = input.value.trim().toLocaleLowerCase('ru');
|
|
let count = 0;
|
|
sections.forEach((section, index) => {
|
|
const matches = section.textContent.toLocaleLowerCase('ru').includes(query);
|
|
section.hidden = !matches;
|
|
links[index].hidden = !matches;
|
|
if (matches) count++;
|
|
});
|
|
status.hidden = !query;
|
|
status.textContent = count ? `Найдено разделов: ${count}` : 'Ничего не найдено. Попробуйте другой запрос.';
|
|
});
|
|
|
|
document.addEventListener('keydown', (event) => {
|
|
if (event.key === '/' && !event.ctrlKey && !event.metaKey && !['INPUT', 'TEXTAREA'].includes(document.activeElement.tagName) && !document.activeElement.isContentEditable) {
|
|
event.preventDefault();
|
|
input.focus();
|
|
}
|
|
if (event.key === 'Escape' && document.activeElement === input) {
|
|
input.value = '';
|
|
input.dispatchEvent(new Event('input'));
|
|
}
|
|
});
|
|
|
|
function updateNavigation() {
|
|
links.forEach(link => {
|
|
if (link.hash === location.hash) link.setAttribute('aria-current', 'location');
|
|
else link.removeAttribute('aria-current');
|
|
});
|
|
}
|
|
window.addEventListener('hashchange', updateNavigation);
|
|
updateNavigation();
|