/**
* Main initialization and event handlers
*/
// Initialize on DOM load
document.addEventListener('DOMContentLoaded', () => {
initializeForms();
loadProviders();
loadDownloads();
setInterval(loadDownloads, 1000);
// Load home content (recommendations & releases)
loadHomeContent();
});
/**
* Initialize form event listeners
*/
function initializeForms() {
// Anime search form
const animeSearchInput = document.getElementById('animeSearchInput');
if (animeSearchInput) {
animeSearchInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
handleAnimeSearch();
}
});
}
// Series search form
const seriesSearchInput = document.getElementById('seriesSearchInput');
if (seriesSearchInput) {
seriesSearchInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
handleSeriesSearch();
}
});
}
// Direct download form
const downloadForm = document.getElementById('downloadForm');
if (downloadForm) {
downloadForm.addEventListener('submit', handleDirectDownload);
}
}
/**
* Load providers dynamically (legacy support)
* Note: This is kept for compatibility but the new interface uses static tabs
*/
async function loadProviders() {
try {
const data = await getProvidersInfo();
// Update supported hosts badges (if element exists)
const hostsContainer = document.querySelector('.supported-hosts');
if (hostsContainer) {
hostsContainer.innerHTML = '';
Object.values(data.file_hosts).forEach(host => {
const badge = document.createElement('span');
badge.className = 'host-badge';
badge.textContent = `${host.icon} ${host.name}`;
hostsContainer.appendChild(badge);
});
}
} catch (error) {
console.error('Error loading providers:', error);
}
}
/**
* Create anime provider tab content
*/
function createAnimeTabContent(providerId, provider) {
return `
`;
}
/**
* Create series provider tab content
*/
function createSeriesTabContent(providerId, provider) {
return `
`;
}
/**
* Handle load provider episodes
*/
async function handleLoadProviderEpisodes(providerId) {
const animeUrl = document.getElementById(`${providerId}UrlInput`).value;
if (!animeUrl) {
alert('Veuillez entrer une URL d\'anime');
return;
}
try {
const data = await loadEpisodes(animeUrl, null);
if (data.episodes && data.episodes.length > 0) {
const select = document.getElementById(`${providerId}EpisodeSelect`);
select.innerHTML = '';
data.episodes.forEach(ep => {
const option = document.createElement('option');
option.value = ep.url;
option.textContent = `Épisode ${ep.episode}`;
select.appendChild(option);
});
document.getElementById(`${providerId}EpisodeSelector`).style.display = 'flex';
} else {
alert('Aucun épisode trouvé');
}
} catch (error) {
console.error('Error loading episodes:', error);
alert('Erreur lors du chargement des épisodes');
}
}
/**
* Handle download provider episode
*/
async function handleDownloadProviderEpisode(providerId) {
const episodeUrl = document.getElementById(`${providerId}EpisodeSelect`).value;
if (!episodeUrl) {
alert('Veuillez sélectionner un épisode');
return;
}
try {
await downloadEpisode(episodeUrl);
document.getElementById(`${providerId}EpisodeSelect`).value = '';
loadDownloads();
} catch (error) {
console.error('Download error:', error);
alert('Erreur lors du téléchargement');
}
}
/**
* Switch between tabs (Modernized to Alpine.js)
*/
function switchTab(tabName) {
console.log('Switching tab to:', tabName);
window.dispatchEvent(new CustomEvent('set-tab', { detail: { tab: tabName } }));
}
// Handle URL hash on page load
if (window.location.hash) {
const hash = window.location.hash.substring(1);
if (hash === 'watchlist' || hash === 'anime' || hash === 'series' || hash === 'providers') {
switchTab(hash);
}
}
// Listen for hash changes
window.addEventListener('hashchange', function() {
if (window.location.hash) {
const hash = window.location.hash.substring(1);
if (hash === 'watchlist' || hash === 'anime' || hash === 'series' || hash === 'providers') {
switchTab(hash);
}
}
});