prompt_template/web/js/main.js

53 lines
1.4 KiB
JavaScript
Raw Normal View History

/**
* Haupt-Initialisierung: Event-Listener, Hash-Filter, App-Start.
*/
let allTemplates = [];
// Search-Event
document.getElementById('search').addEventListener('input', (e) => {
currentQuery = e.target.value.toLowerCase();
applyFilters();
});
// Close modal on overlay click
document.getElementById('modal').addEventListener('click', (e) => {
if (e.target === e.currentTarget) closeModal();
});
// Escape key closes modal
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') { closeEditModal(); closeModal(); }
});
// Hash -> type filter
window.addEventListener('hashchange', () => {
currentType = parseTypeFromHash();
applyFilters();
});
// Nav clicks set the hash; hashchange drives filtering
document.querySelectorAll('.nav a').forEach(link => {
link.addEventListener('click', (e) => {
e.preventDefault();
const target = link.getAttribute('href') || '#';
if (window.location.hash === target || (target === '#' && window.location.hash === '')) {
return; // same target, no hashchange would fire
}
window.location.hash = target;
});
});
// Initial load
loadTemplates().then(t => {
allTemplates = t;
window.allTemplates = t;
currentType = parseTypeFromHash();
applyFilters();
}).catch(e => {
console.error('Failed to load templates:', e);
allTemplates = [];
window.allTemplates = [];
applyFilters();
});