';
}
}
function render() {
if (!_items.length) {
_results.innerHTML = '
Ничего не найдено
';
return;
}
// Group by type
const groups = {};
for (const item of _items) {
if (!groups[item.type]) groups[item.type] = [];
groups[item.type].push(item);
}
let html = '';
let idx = 0;
for (const [type, items] of Object.entries(groups)) {
html += `
${LABELS[type] || type}
`;
for (const item of items) {
const iconCls = `gs-icon-${type}`;
const iconName = ICONS[type] || 'bookmark';
html += `
${esc(item.title)}
${item.subtitle ? `
${esc(item.subtitle)}
` : ''}
`;
idx++;
}
}
_results.innerHTML = html;
// Lucide icons
if (window.lucide) lucide.createIcons({ nodes: _results.querySelectorAll('[data-lucide]') });
}
function navigate(dir) {
const total = _items.length;
if (!total) return;
const prev = _results.querySelector('.gs-item.active');
if (prev) prev.classList.remove('active');
_activeIdx = (_activeIdx + dir + total) % total;
const next = _results.querySelector(`.gs-item[data-idx="${_activeIdx}"]`);
if (next) { next.classList.add('active'); next.scrollIntoView({ block: 'nearest' }); }
}
function open() {
build();
_overlay.classList.add('open');
_input.value = '';
_results.innerHTML = '
Начните вводить для поиска
';
_items = [];
_activeIdx = -1;
setTimeout(() => _input.focus(), 50);
}
function close() {
if (_overlay) _overlay.classList.remove('open');
}
// Global shortcut: Ctrl+K
document.addEventListener('keydown', e => {
if ((e.ctrlKey || e.metaKey) && e.key === 'k') {
e.preventDefault();
if (_overlay?.classList.contains('open')) close();
else open();
}
});
// Expose
window.lsSearchOpen = open;
window.lsSearchClose = close;
})();