-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
156 lines (137 loc) · 5.4 KB
/
script.js
File metadata and controls
156 lines (137 loc) · 5.4 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
let currentPage = 1;
let totalResults = 0;
let allMovies = [];
let filteredMovies = [];
const apiKey = 'xxxxx'; // Your OMDb API Key
document.getElementById('search-button').addEventListener('click', () => {
currentPage = 1;
fetchMovies(currentPage);
});
document.getElementById('next-button').addEventListener('click', () => {
if (currentPage * 10 < totalResults) {
currentPage++;
displayMovieList(filteredMovies.slice((currentPage - 1) * 10, currentPage * 10));
updatePaginationButtons();
}
});
document.getElementById('prev-button').addEventListener('click', () => {
if (currentPage > 1) {
currentPage--;
displayMovieList(filteredMovies.slice((currentPage - 1) * 10, currentPage * 10));
updatePaginationButtons();
}
});
document.getElementById('genre-filter').addEventListener('change', filterMovies);
document.getElementById('sort-options').addEventListener('change', sortMovies);
function fetchMovies(page) {
const movieTitle = document.getElementById('movie-search').value;
const url = `http://www.omdbapi.com/?s=${movieTitle}&apikey=${apiKey}&page=${page}`;
fetch(url)
.then(response => response.json())
.then(data => {
if (data.Response === "True") {
totalResults = parseInt(data.totalResults);
allMovies = data.Search;
fetchAllMovieDetails();
} else {
alert("No movies found!");
}
})
.catch(error => console.error('Error fetching data:', error));
}
function fetchAllMovieDetails() {
filteredMovies = [];
let promises = allMovies.map(movie => {
return fetch(`http://www.omdbapi.com/?i=${movie.imdbID}&apikey=${apiKey}`)
.then(response => response.json())
.then(data => {
if (data.Response === "True") {
filteredMovies.push(data);
}
});
});
Promise.all(promises).then(() => {
populateGenreFilter();
sortMovies();
});
}
function populateGenreFilter() {
const genreFilter = document.getElementById('genre-filter');
let genres = new Set();
filteredMovies.forEach(movie => {
if (movie.Genre) {
movie.Genre.split(", ").forEach(genre => genres.add(genre));
}
});
genreFilter.innerHTML = '<option value="">All Genres</option>';
genres.forEach(genre => {
const option = document.createElement('option');
option.value = genre;
option.innerText = genre;
genreFilter.appendChild(option);
});
}
function filterMovies() {
const genre = document.getElementById('genre-filter').value;
if (genre === "") {
filteredMovies = [...allMovies];
} else {
filteredMovies = allMovies.filter(movie => movie.Genre.includes(genre));
}
sortMovies();
}
function sortMovies() {
const sortOption = document.getElementById('sort-options').value;
if (sortOption === "highestRating") {
filteredMovies.sort((a, b) => parseFloat(b.imdbRating) - parseFloat(a.imdbRating));
} else if (sortOption === "lowestRating") {
filteredMovies.sort((a, b) => parseFloat(a.imdbRating) - parseFloat(b.imdbRating));
} else if (sortOption === "mostVotes") {
filteredMovies.sort((a, b) => parseInt(b.imdbVotes.replace(/,/g, '')) - parseInt(a.imdbVotes.replace(/,/g, '')));
} else if (sortOption === "leastVotes") {
filteredMovies.sort((a, b) => parseInt(a.imdbVotes.replace(/,/g, '')) - parseInt(b.imdbVotes.replace(/,/g, '')));
}
displayMovieList(filteredMovies.slice((currentPage - 1) * 10, currentPage * 10));
updatePaginationButtons();
}
function displayMovieList(movies) {
const movieList = document.getElementById('movie-list');
movieList.innerHTML = '';
movies.forEach(movie => {
const movieItem = document.createElement('div');
movieItem.classList.add('movie-item');
movieItem.innerHTML = `
<img class="lazy" data-src="${movie.Poster !== 'N/A' ? movie.Poster : ''}" alt="${movie.Title} Poster" />
<div class="movie-content">
<h3 class="movie-title">${movie.Title} (${movie.Year})</h3>
<div class="movie-details" id="details-${movie.imdbID}">
<p><strong>Rating:</strong> ${movie.imdbRating}</p>
<p><strong>Total Votes:</strong> ${movie.imdbVotes}</p>
<p><strong>Genre:</strong> ${movie.Genre}</p>
<p><strong>Plot:</strong> ${movie.Plot}</p>
<p><strong>Cast:</strong> ${movie.Actors}</p>
</div>
</div>
`;
movieList.appendChild(movieItem);
});
// Lazy load the images
const lazyImages = document.querySelectorAll('img.lazy');
lazyImages.forEach(image => loadImage(image));
}
function loadImage(image) {
const observer = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
image.src = image.dataset.src;
image.classList.remove('lazy');
observer.unobserve(image);
}
});
});
observer.observe(image);
}
function updatePaginationButtons() {
document.getElementById('prev-button').disabled = currentPage === 1;
document.getElementById('next-button').disabled = currentPage * 10 >= filteredMovies.length;
}