Files

156 lines
5.7 KiB
JavaScript

// /srv/http/eos-modern/public/assets/js/main.js
// 1. Globální funkce pro Toast notifikaci
function showToast(message, isError = false) {
const existingToasts = document.querySelectorAll('.custom-toast');
existingToasts.forEach(t => t.remove());
const toast = document.createElement('div');
toast.className = 'custom-toast';
toast.textContent = message;
const bgColor = isError ? '#e74c3c' : 'var(--primary)';
Object.assign(toast.style, {
position: 'fixed',
bottom: '20px',
left: '50%',
transform: 'translateX(-50%)',
backgroundColor: bgColor,
color: '#fff',
padding: '12px 24px',
borderRadius: '8px',
boxShadow: '0 4px 12px rgba(0,0,0,0.15)',
zIndex: '9999',
transition: 'opacity 0.3s ease, transform 0.3s ease',
fontFamily: 'var(--font-main)',
fontSize: '0.95rem',
opacity: '0',
pointerEvents: 'none'
});
document.body.appendChild(toast);
requestAnimationFrame(() => {
toast.style.opacity = '1';
toast.style.transform = 'translateX(-50%) translateY(0)';
});
setTimeout(() => {
toast.style.opacity = '0';
toast.style.transform = 'translateX(-50%) translateY(10px)';
setTimeout(() => toast.remove(), 300);
}, 3000);
}
document.addEventListener('DOMContentLoaded', () => {
// 2. Tmavý režim (už funguje, necháme to tak)
const themeToggle = document.getElementById('themeToggle');
const html = document.documentElement;
if (themeToggle) {
const icon = themeToggle.querySelector('i');
if (icon) {
const savedTheme = localStorage.getItem('theme') || 'dark';
html.setAttribute('data-theme', savedTheme);
updateIcon(savedTheme);
themeToggle.addEventListener('click', () => {
const currentTheme = html.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
html.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
updateIcon(newTheme);
});
function updateIcon(theme) {
icon.className = theme === 'dark' ? 'fas fa-sun' : 'fas fa-moon';
}
}
}
// 3. Vyhledávání (Fuse.js) - necháme jako bylo
const searchInput = document.getElementById('searchInput');
const searchResults = document.getElementById('searchResults');
let fuse = null;
if (searchInput && searchResults) {
const jsonUrl = '/eos-modern/public/data/articles.json';
fetch(jsonUrl)
.then(res => res.json())
.then(data => {
fuse = new Fuse(data, {
keys: ['title', 'excerpt', 'slug'],
threshold: 0.3,
ignoreLocation: true
});
})
.catch(err => console.error('Chyba JSON:', err));
searchInput.addEventListener('input', (e) => {
const query = e.target.value.trim();
if (query.length < 2) {
searchResults.style.display = 'none';
return;
}
if (!fuse) return;
const results = fuse.search(query);
searchResults.innerHTML = '';
if (results.length > 0) {
searchResults.style.display = 'block';
results.forEach(result => {
const item = result.item;
const link = document.createElement('a');
link.href = `/eos-modern/public/article.php?slug=${item.slug}`;
link.className = 'search-item';
link.innerHTML = `<strong>${item.title}</strong><br><small>${item.excerpt}</small>`;
searchResults.appendChild(link);
});
} else {
searchResults.style.display = 'none';
}
});
document.addEventListener('click', (e) => {
if (!searchInput.contains(e.target) && !searchResults.contains(e.target)) {
searchResults.style.display = 'none';
}
});
}
// 4. Tlačítko Sdílet - ZLEPŠENÁ VERZE
const shareButton = document.getElementById('shareButton');
if (shareButton) {
shareButton.addEventListener('click', async () => {
const currentUrl = window.location.href;
const title = document.title;
// Zkusíme nejprve nativní sdílení (mobil)
if (navigator.share) {
try {
await navigator.share({
title: title,
text: 'Podívej se na tento článek na EndeavourOS CZ!',
url: currentUrl
});
console.log('Sdílení úspěšné (mobile).');
return; // Ukončíme, pokud se podařilo
} catch (err) {
console.warn('Sdílení zrušeno nebo selhalo:', err);
// Pokud selže, přejdeme na fallback
}
}
// Fallback pro Desktop: Zkusíme zkopírovat do schránky
try {
await navigator.clipboard.writeText(currentUrl);
showToast('Odkaz zkopírován do schránky! 📋');
console.log('Odkaz zkopírován (desktop).');
} catch (err) {
console.error('Chyba kopírování:', err);
// Pokud selže i clipboard (často na HTTP), ukážeme chybu
showToast('Nelze zkopírovat. Zkuste to ručně (URL v adresním řádku).', true);
}
});
}
});