Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
505 lines (445 loc) · 17.5 KB
/
Copy pathapp.js
File metadata and controls
505 lines (445 loc) · 17.5 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// TMDb API Configuration
const API_KEY = ''; // Users need to replace this with their own API key
const BASE_URL = 'https://api.themoviedb.org/3';
const IMAGE_BASE_URL = 'https://image.tmdb.org/t/p/w500';
// State management
let currentMovies = [];
let currentSort = 'popularity';
let currentGenre = 'all';
let allGenres = [];
// DOM Elements
const movieGrid = document.getElementById('movie-grid');
const searchInput = document.getElementById('movie-search');
const searchButton = document.getElementById('search-button');
// Initialize the app
async function init() {
// Check if API key is set
if (API_KEY === 'YOUR_TMDB_API_KEY_HERE') {
showApiKeyMessage();
return;
}
else{
await loadGenres();
createControlsUI();
await loadPopularMovies();
setupEventListeners();
}
}
// Show message when API key is not configured
function showApiKeyMessage() {
movieGrid.innerHTML = `
<div class="api-message">
<h2>🎬 TMDb API Key Required</h2>
<p>To use VibeFlix, you need a free API key from The Movie Database (TMDb).</p>
<ol>
<li>Visit <a href="https://www.themoviedb.org/settings/api" target="_blank" rel="noopener">themoviedb.org/settings/api</a></li>
<li>Create a free account and request an API key</li>
<li>Replace 'YOUR_TMDB_API_KEY_HERE' in app.js with your key</li>
</ol>
</div>
`;
}
// Load genres from TMDb
async function loadGenres() {
try {
const response = await fetch(`${BASE_URL}/genre/movie/list?api_key=${API_KEY}`);
const data = await response.json();
allGenres = data.genres || [];
} catch (error) {
console.error('Error loading genres:', error);
allGenres = [];
}
}
// Create sorting and filtering controls
function createControlsUI() {
const controlsContainer = document.createElement('div');
controlsContainer.id = 'controls-container';
controlsContainer.className = 'controls-container';
// Sort dropdown
const sortContainer = document.createElement('div');
sortContainer.className = 'control-group';
sortContainer.innerHTML = `
<label for="sort-select" class="control-label">
<span class="control-icon">📊</span>
Sort by:
</label>
<select id="sort-select" class="control-select" aria-label="Sort movies">
<option value="popularity">Popularity</option>
<option value="rating">Rating</option>
<option value="release_date">Release Date</option>
<option value="title">Title (A-Z)</option>
</select>
`;
// Genre dropdown
const genreContainer = document.createElement('div');
genreContainer.className = 'control-group';
genreContainer.innerHTML = `
<label for="genre-select" class="control-label">
<span class="control-icon">🎭</span>
Genre:
</label>
<select id="genre-select" class="control-select" aria-label="Filter by genre">
<option value="all">All Genres</option>
</select>
`;
// Add genre options
const genreSelect = genreContainer.querySelector('#genre-select');
allGenres.forEach(genre => {
const option = document.createElement('option');
option.value = genre.id;
option.textContent = genre.name;
genreSelect.appendChild(option);
});
controlsContainer.appendChild(sortContainer);
controlsContainer.appendChild(genreContainer);
// Insert controls before movie grid
const contentArea = document.getElementById('content-area');
contentArea.insertBefore(controlsContainer, movieGrid);
// Setup control event listeners
document.getElementById('sort-select').addEventListener('change', handleSortChange);
document.getElementById('genre-select').addEventListener('change', handleGenreChange);
}
// Setup event listeners
function setupEventListeners() {
searchButton.addEventListener('click', handleSearch);
searchInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
handleSearch();
}
});
}
// Handle search
async function handleSearch() {
const query = searchInput.value.trim();
if (!query) {
await loadPopularMovies();
return;
}
try {
showLoading();
const response = await fetch(
`${BASE_URL}/search/movie?api_key=${API_KEY}&query=${encodeURIComponent(query)}&include_adult=false`
);
const data = await response.json();
currentMovies = data.results || [];
applySortAndFilter();
} catch (error) {
console.error('Error searching movies:', error);
showError('Failed to search movies. Please try again.');
}
}
// Load popular movies
async function loadPopularMovies() {
try {
showLoading();
const response = await fetch(
`${BASE_URL}/movie/popular?api_key=${API_KEY}&page=1`
);
const data = await response.json();
currentMovies = data.results || [];
applySortAndFilter();
} catch (error) {
console.error('Error loading popular movies:', error);
showError('Failed to load movies. Please check your API key.');
}
}
// Handle sort change
function handleSortChange(e) {
currentSort = e.target.value;
applySortAndFilter();
}
// Handle genre filter change
function handleGenreChange(e) {
currentGenre = e.target.value;
applySortAndFilter();
}
// Apply sorting and filtering
function applySortAndFilter() {
let filteredMovies = [...currentMovies];
// Apply genre filter
if (currentGenre !== 'all') {
const genreId = parseInt(currentGenre);
filteredMovies = filteredMovies.filter(movie =>
movie.genre_ids && movie.genre_ids.includes(genreId)
);
}
// Apply sorting
filteredMovies.sort((a, b) => {
switch (currentSort) {
case 'rating':
return (b.vote_average || 0) - (a.vote_average || 0);
case 'release_date':
return new Date(b.release_date || 0) - new Date(a.release_date || 0);
case 'title':
return (a.title || '').localeCompare(b.title || '');
case 'popularity':
default:
return (b.popularity || 0) - (a.popularity || 0);
}
});
displayMovies(filteredMovies);
}
// Display movies in grid
function displayMovies(movies) {
hideProgressBar();
if (!movies || movies.length === 0) {
movieGrid.innerHTML = `
<div class="no-results">
<p class="no-results-icon">🎬</p>
<p class="no-results-text">No movies found</p>
</div>
`;
return;
}
movieGrid.innerHTML = movies.map((movie, index) => createMovieCard(movie, index)).join('');
// Add staggered animation with intersection observer for better performance
const cards = movieGrid.querySelectorAll('.movie-card');
// Use Intersection Observer for fade-in on scroll
const observerOptions = {
root: null,
rootMargin: '0px',
threshold: 0.1
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('fade-in-visible');
observer.unobserve(entry.target);
}
});
}, observerOptions);
cards.forEach((card, index) => {
// Add staggered delay for initial load
card.style.animationDelay = `${index * 0.05}s`;
// Observe for scroll-triggered animations
observer.observe(card);
});
}
// Create movie card HTML
function createMovieCard(movie, index) {
const posterPath = movie.poster_path
? `${IMAGE_BASE_URL}${movie.poster_path}`
: 'https://images.unsplash.com/photo-1536440136628-849c177e76a1?auto=format&fit=crop&w=300&q=80';
const rating = movie.vote_average ? movie.vote_average.toFixed(1) : 'N/A';
const title = movie.title || 'Unknown Title';
const releaseYear = movie.release_date ? new Date(movie.release_date).getFullYear() : '';
// Get genre names
const genreNames = movie.genre_ids
? movie.genre_ids
.map(id => allGenres.find(g => g.id === id)?.name)
.filter(Boolean)
.slice(0, 2)
.join(', ')
: '';
return `
<article class="movie-card"
id="movie-${index}"
role="article"
aria-label="${title}"
data-movie-id="${movie.id}">
<div class="movie-poster-container">
<img src="${posterPath}"
alt="${title} poster"
class="movie-poster"
loading="lazy"
onerror="this.src='https://images.unsplash.com/photo-1536440136628-849c177e76a1?auto=format&fit=crop&w=300&q=80'">
<div class="movie-overlay">
<button class="info-button" onclick="showMovieDetails(${movie.id})" aria-label="View details for ${title}">
<span class="info-icon">ℹ️</span>
<span class="info-text">Details</span>
</button>
</div>
</div>
<div class="movie-info">
<h3 class="movie-title" title="${title}">${title}</h3>
${releaseYear ? `<p class="movie-year">${releaseYear}</p>` : ''}
${genreNames ? `<p class="movie-genres">${genreNames}</p>` : ''}
<div class="movie-rating-container">
<span class="movie-rating" aria-label="Rating ${rating} out of 10">
<span class="star-icon">⭐</span>
<span class="rating-value">${rating}</span>
</span>
</div>
</div>
</article>
`;
}
// Show movie details modal
async function showMovieDetails(movieId) {
// Show loading modal first
const loadingModal = document.createElement('div');
loadingModal.className = 'modal active';
loadingModal.innerHTML = `
<div class="modal-content modal-loading">
<div class="loading-spinner"></div>
<p class="loading-text">Loading movie details...</p>
</div>
`;
document.body.appendChild(loadingModal);
try {
const response = await fetch(`${BASE_URL}/movie/${movieId}?api_key=${API_KEY}`);
const movie = await response.json();
// Remove loading modal
loadingModal.remove();
const modal = document.createElement('div');
modal.className = 'modal';
modal.setAttribute('role', 'dialog');
modal.setAttribute('aria-labelledby', 'modal-title');
modal.setAttribute('aria-modal', 'true');
const posterPath = movie.poster_path
? `${IMAGE_BASE_URL}${movie.poster_path}`
: 'https://images.unsplash.com/photo-1536440136628-849c177e76a1?auto=format&fit=crop&w=500&q=80';
const backdropPath = movie.backdrop_path
? `https://image.tmdb.org/t/p/original${movie.backdrop_path}`
: '';
modal.innerHTML = `
<div class="modal-content" style="${backdropPath ? `background-image: linear-gradient(to bottom, rgba(15, 15, 15, 0.95), rgba(15, 15, 15, 0.98)), url(${backdropPath});` : ''}">
<button class="modal-close" onclick="closeModal()" aria-label="Close modal">×</button>
<div class="modal-body">
<div class="modal-poster">
<img src="${posterPath}" alt="${movie.title} poster">
</div>
<div class="modal-details">
<h2 id="modal-title" class="modal-title">${movie.title}</h2>
${movie.tagline ? `<p class="modal-tagline">"${movie.tagline}"</p>` : ''}
<div class="modal-meta">
<span class="modal-rating">⭐ ${movie.vote_average ? movie.vote_average.toFixed(1) : 'N/A'}</span>
${movie.release_date ? `<span class="modal-year">${new Date(movie.release_date).getFullYear()}</span>` : ''}
${movie.runtime ? `<span class="modal-runtime">${movie.runtime} min</span>` : ''}
</div>
${movie.genres && movie.genres.length > 0 ? `
<div class="modal-genres">
${movie.genres.map(g => `<span class="genre-tag">${g.name}</span>`).join('')}
</div>
` : ''}
${movie.overview ? `
<div class="modal-overview">
<h3>Overview</h3>
<p>${movie.overview}</p>
</div>
` : ''}
${movie.homepage ? `
<a href="${movie.homepage}" target="_blank" rel="noopener" class="modal-link">
Visit Official Website →
</a>
` : ''}
</div>
</div>
</div>
`;
document.body.appendChild(modal);
// Trap focus in modal
const focusableElements = modal.querySelectorAll('button, a');
const firstFocusable = focusableElements[0];
const lastFocusable = focusableElements[focusableElements.length - 1];
firstFocusable.focus();
modal.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
closeModal();
}
if (e.key === 'Tab') {
if (e.shiftKey) {
if (document.activeElement === firstFocusable) {
e.preventDefault();
lastFocusable.focus();
}
} else {
if (document.activeElement === lastFocusable) {
e.preventDefault();
firstFocusable.focus();
}
}
}
});
modal.addEventListener('click', (e) => {
if (e.target === modal) {
closeModal();
}
});
setTimeout(() => modal.classList.add('active'), 10);
} catch (error) {
// Remove any loading modals
document.querySelectorAll('.modal').forEach(m => m.remove());
console.error('Error loading movie details:', error);
// Show error modal
const errorModal = document.createElement('div');
errorModal.className = 'modal active';
errorModal.innerHTML = `
<div class="modal-content modal-error">
<button class="modal-close" onclick="closeModal()" aria-label="Close modal">×</button>
<div class="error-container">
<p class="error-icon">⚠️</p>
<p class="error-text">Failed to load movie details. Please try again.</p>
<button class="retry-button" onclick="closeModal(); showMovieDetails(${movieId})">
Retry
</button>
</div>
</div>
`;
document.body.appendChild(errorModal);
}
}
// Close modal
function closeModal() {
const modal = document.querySelector('.modal');
if (modal) {
modal.classList.remove('active');
setTimeout(() => modal.remove(), 300);
}
}
// Show loading state
function showLoading() {
showProgressBar();
const skeletonCount = 12; // Show 12 skeleton cards
const skeletonCards = Array(skeletonCount).fill(0).map((_, i) => `
<div class="skeleton-card" style="animation-delay: ${i * 0.05}s">
<div class="skeleton-poster"></div>
<div class="skeleton-info">
<div class="skeleton-title"></div>
<div class="skeleton-rating"></div>
</div>
</div>
`).join('');
movieGrid.innerHTML = `
<div class="skeleton-grid" style="grid-column: 1 / -1; display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 25px;">
${skeletonCards}
</div>
`;
}
// Show progress bar
function showProgressBar() {
let progressBar = document.getElementById('progress-bar');
if (!progressBar) {
progressBar = document.createElement('div');
progressBar.id = 'progress-bar';
progressBar.className = 'progress-bar';
document.body.appendChild(progressBar);
}
progressBar.classList.add('active');
}
// Hide progress bar
function hideProgressBar() {
const progressBar = document.getElementById('progress-bar');
if (progressBar) {
progressBar.classList.remove('active');
}
}
// Show error message
function showError(message) {
hideProgressBar();
movieGrid.innerHTML = `
<div class="error-container">
<p class="error-icon">⚠️</p>
<p class="error-text">${message}</p>
</div>
`;
}
// Make functions globally accessible
window.showMovieDetails = showMovieDetails;
window.closeModal = closeModal;
// Initialize app when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}