-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs.js
More file actions
302 lines (267 loc) · 13.7 KB
/
js.js
File metadata and controls
302 lines (267 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Mobile Menu Toggle
const mobileMenuBtn = document.getElementById('mobileMenuBtn');
const navLinks = document.getElementById('navLinks');
mobileMenuBtn.addEventListener('click', () => {
navLinks.classList.toggle('active');
});
// API Configuration
const API_BASE_URL = 'https://api.jikan.moe/v4';
let currentSlide = 0;
let featuredAnime = [];
const API_RATE_LIMIT_DELAY = 1000; // Delay 1 detik antara request untuk menghindari rate limit
let lastRequestTime = 0;
// Fetch Popular Anime
async function fetchPopularAnime() {
try {
const response = await fetch(`${API_BASE_URL}/top/anime?filter=airing&limit=8`);
const data = await response.json();
displayAnime(data.data, 'popularAnime');
} catch (error) {
console.error('Error fetching popular anime:', error);
document.getElementById('popularAnime').innerHTML = `
<div class="card" style="text-align: center; padding: 2rem;">
<i class="fas fa-exclamation-triangle" style="font-size: 2rem; color: var(--danger-color); margin-bottom: 1rem;"></i>
<p>Failed to load popular anime. Please try again later.</p>
</div>
`;
}
}
// Fetch Top Rated Anime
async function fetchTopAnime() {
try {
const response = await fetch(`${API_BASE_URL}/top/anime?limit=8`);
const data = await response.json();
displayAnime(data.data, 'topAnime');
} catch (error) {
console.error('Error fetching top anime:', error);
document.getElementById('topAnime').innerHTML = `
<div class="card" style="text-align: center; padding: 2rem;">
<i class="fas fa-exclamation-triangle" style="font-size: 2rem; color: var(--danger-color); margin-bottom: 1rem;"></i>
<p>Failed to load top anime. Please try again later.</p>
</div>
`;
}
}
// Fetch Upcoming Anime
async function fetchUpcomingAnime() {
try {
const response = await fetch(`${API_BASE_URL}/seasons/upcoming?limit=8`);
const data = await response.json();
displayAnime(data.data, 'upcomingAnime');
} catch (error) {
console.error('Error fetching upcoming anime:', error);
document.getElementById('upcomingAnime').innerHTML = `
<div class="card" style="text-align: center; padding: 2rem;">
<i class="fas fa-exclamation-triangle" style="font-size: 2rem; color: var(--danger-color); margin-bottom: 1rem;"></i>
<p>Failed to load upcoming anime. Please try again later.</p>
</div>
`;
}
}
// Fetch Featured Anime for Slider
async function fetchFeaturedAnime() {
try {
const response = await fetch(`${API_BASE_URL}/top/anime?filter=airing&limit=5`);
const data = await response.json();
featuredAnime = data.data;
initSlider();
} catch (error) {
console.error('Error fetching featured anime:', error);
document.querySelector('.slider-container').innerHTML = `
<div class="card" style="text-align: center; padding: 2rem;">
<i class="fas fa-exclamation-triangle" style="font-size: 2rem; color: var(--danger-color); margin-bottom: 1rem;"></i>
<p>Failed to load featured anime. Please try again later.</p>
</div>
`;
}
}
// Display Anime in Gallery
function displayAnime(animeList, containerId) {
const container = document.getElementById(containerId);
container.innerHTML = '';
animeList.forEach(anime => {
const animeCard = document.createElement('div');
animeCard.className = 'anime-card';
animeCard.innerHTML = `
<img src="${anime.images.jpg.large_image_url}" alt="${anime.title}" class="anime-card-img">
<div class="anime-card-content">
<h3 class="anime-card-title">${anime.title}</h3>
<div class="anime-card-meta">
<span>${anime.type} • ${anime.episodes || '?'} eps</span>
<span class="anime-card-score"><i class="fas fa-star"></i> ${anime.score || 'N/A'}</span>
</div>
<p class="anime-card-synopsis">${anime.synopsis ? anime.synopsis.substring(0, 150) + '...' : 'No synopsis available.'}</p>
<button class="anime-card-btn" onclick="showAnimeDetails(${anime.mal_id})">View Details</button>
</div>
`;
container.appendChild(animeCard);
});
}
// Initialize Slider
function initSlider() {
const slider = document.getElementById('featuredSlider');
const dotsContainer = document.getElementById('sliderDots');
slider.innerHTML = '';
dotsContainer.innerHTML = '';
featuredAnime.forEach((anime, index) => {
// Create slider item
const sliderItem = document.createElement('div');
sliderItem.className = 'slider-item';
sliderItem.innerHTML = `
<img src="${anime.images.jpg.large_image_url}" alt="${anime.title}" class="slider-img">
<div class="slider-content">
<h2 class="slider-title">${anime.title}</h2>
<div class="slider-meta">
<span class="slider-meta-item"><i class="fas fa-tv"></i> ${anime.type}</span>
<span class="slider-meta-item"><i class="fas fa-star"></i> ${anime.score || 'N/A'}</span>
<span class="slider-meta-item"><i class="fas fa-calendar-alt"></i> ${anime.aired?.string || 'Unknown'}</span>
</div>
<p class="slider-description">${anime.synopsis ? anime.synopsis.substring(0, 200) + '...' : ''}</p>
<button class="slider-btn" onclick="showAnimeDetails(${anime.mal_id})">View Details</button>
</div>
`;
slider.appendChild(sliderItem);
// Create dot
const dot = document.createElement('div');
dot.className = `slider-dot ${index === 0 ? 'active' : ''}`;
dot.addEventListener('click', () => {
goToSlide(index);
});
dotsContainer.appendChild(dot);
});
// Set initial slide
goToSlide(0);
}
// Go to specific slide
function goToSlide(index) {
const slides = document.querySelectorAll('.slider-item');
const dots = document.querySelectorAll('.slider-dot');
if (index >= slides.length) index = 0;
if (index < 0) index = slides.length - 1;
currentSlide = index;
slides.forEach((slide, i) => {
slide.style.transform = `translateX(${100 * (i - index)}%)`;
});
dots.forEach((dot, i) => {
dot.classList.toggle('active', i === index);
});
}
// Next slide
function nextSlide() {
goToSlide(currentSlide + 1);
}
// Previous slide
function prevSlide() {
goToSlide(currentSlide - 1);
}
// Show anime details in modal
async function showAnimeDetails(animeId) {
try {
const modal = document.getElementById('animeModal');
const response = await fetch(`${API_BASE_URL}/anime/${animeId}/full`);
const { data } = await response.json();
// Set basic info
document.getElementById('modalTitle').textContent = data.title;
document.getElementById('modalBackdrop').src = data.images.jpg.large_image_url;
document.getElementById('modalPoster').src = data.images.jpg.image_url;
document.getElementById('modalDescription').textContent = data.synopsis || 'No synopsis available.';
// Set meta info
const metaContainer = document.getElementById('modalMeta');
metaContainer.innerHTML = `
<div class="modal-meta-item">
<i class="fas fa-tv"></i> ${data.type || 'Unknown'}
</div>
<div class="modal-meta-item">
<i class="fas fa-star"></i> ${data.score || 'N/A'}
</div>
<div class="modal-meta-item">
<i class="fas fa-calendar-alt"></i> ${data.aired?.string || 'Unknown date'}
</div>
<div class="modal-meta-item">
<i class="fas fa-clock"></i> ${data.duration || 'Unknown duration'}
</div>
`;
// Set stats
const statsContainer = document.getElementById('modalStats');
statsContainer.innerHTML = `
<div class="stat-item">
<div class="stat-value">${data.rank || 'N/A'}</div>
<div class="stat-label">Rank</div>
</div>
<div class="stat-item">
<div class="stat-value">${data.popularity || 'N/A'}</div>
<div class="stat-label">Popularity</div>
</div>
<div class="stat-item">
<div class="stat-value">${data.members?.toLocaleString() || 'N/A'}</div>
<div class="stat-label">Members</div>
</div>
<div class="stat-item">
<div class="stat-value">${data.favorites?.toLocaleString() || 'N/A'}</div>
<div class="stat-label">Favorites</div>
</div>
`;
// Set trailer if available
const trailerSection = document.getElementById('trailerSection');
const trailer = document.getElementById('modalTrailer');
if (data.trailer?.embed_url) {
trailer.src = data.trailer.embed_url;
trailerSection.style.display = 'block';
} else {
trailerSection.style.display = 'none';
}
// Show modal
modal.style.display = 'block';
document.body.style.overflow = 'hidden';
} catch (error) {
console.error('Error fetching anime details:', error);
alert('Failed to load anime details. Please try again later.');
}
}
// Close modal
function closeModal() {
const modal = document.getElementById('animeModal');
modal.style.display = 'none';
document.body.style.overflow = 'auto';
// Pause trailer if it's playing
const trailer = document.getElementById('modalTrailer');
trailer.src = '';
}
// Event Listeners
document.getElementById('prevSlide').addEventListener('click', prevSlide);
document.getElementById('nextSlide').addEventListener('click', nextSlide);
document.getElementById('modalClose').addEventListener('click', closeModal);
// Close modal when clicking outside
window.addEventListener('click', (e) => {
const modal = document.getElementById('animeModal');
if (e.target === modal) {
closeModal();
}
});
// Keyboard navigation for slider
document.addEventListener('keydown', (e) => {
if (e.key === 'ArrowRight') {
nextSlide();
} else if (e.key === 'ArrowLeft') {
prevSlide();
} else if (e.key === 'Escape') {
closeModal();
}
});
// Auto-advance slider
let slideInterval = setInterval(nextSlide, 5000);
// Pause auto-advance when hovering over slider
const sliderContainer = document.querySelector('.slider-container');
sliderContainer.addEventListener('mouseenter', () => {
clearInterval(slideInterval);
});
sliderContainer.addEventListener('mouseleave', () => {
slideInterval = setInterval(nextSlide, 5000);
});
// Initialize the page
document.addEventListener('DOMContentLoaded', () => {
fetchPopularAnime();
fetchTopAnime();
fetchUpcomingAnime();
fetchFeaturedAnime();
});