Files
docs.a2v.space/public/js/search.js
T
Агальцов Антон 690abf379c daily update 2026-03-29
2026-03-30 00:02:10 +03:00

91 lines
3.0 KiB
JavaScript

// Search functionality using Fuse.js
document.addEventListener('DOMContentLoaded', function() {
const searchInput = document.getElementById('search-input');
const searchForm = document.querySelector('.search-form');
const searchResults = document.getElementById('search-results');
const resultsList = document.getElementById('results-list');
const closeSearch = document.getElementById('close-search');
if (!searchInput) return;
let fuse;
// Load search index
fetch('/index.json')
.then(response => response.json())
.then(data => {
fuse = new Fuse(data, {
keys: ['title', 'content', 'tags'],
includeScore: true,
threshold: 0.3,
ignoreLocation: true,
minMatchCharLength: 2
});
})
.catch(err => console.error('Failed to load search index:', err));
function performSearch(query) {
if (!fuse || !query.trim()) {
searchResults.style.display = 'none';
return;
}
const results = fuse.search(query, { limit: 10 });
if (results.length === 0) {
resultsList.innerHTML = '<li>Ничего не найдено</li>';
} else {
resultsList.innerHTML = results.map(result => {
const item = result.item;
const sectionLabels = {
'diary': 'Дневник',
'kb': 'База знаний',
'plans': 'Планы',
'guides': 'Инструкции',
'creds': 'Креды'
};
const label = sectionLabels[item.section] || item.section;
return `<li>
<a href="${item.url}">
<strong>${item.title}</strong>
<small class="text-muted d-block">${label}</small>
</a>
</li>`;
}).join('');
}
searchResults.style.display = 'block';
}
// Debounce function
let debounceTimer;
function debounce(func, delay) {
return function() {
const context = this;
const args = arguments;
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => func.apply(context, args), delay);
};
}
searchInput.addEventListener('input', debounce(function(e) {
performSearch(e.target.value);
}, 200));
searchForm.addEventListener('submit', function(e) {
e.preventDefault();
performSearch(searchInput.value);
});
closeSearch.addEventListener('click', function() {
searchResults.style.display = 'none';
});
// Close search when clicking outside
document.addEventListener('click', function(e) {
if (!searchForm.contains(e.target) && !searchResults.contains(e.target)) {
searchResults.style.display = 'none';
}
});
});