-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
272 lines (223 loc) · 7.72 KB
/
Copy pathscript.js
File metadata and controls
272 lines (223 loc) · 7.72 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
// URLs
let EPISODES_URL = "https://api.tvmaze.com/shows/82/episodes";
let SHOWS_URL = "https://api.tvmaze.com/shows";
// Cache for all episodes
const EPISODE_CACHE = {};
// Array of episodes
let EPISODES = [];
let SHOWS = [];
// Elements
const displayEps = document.getElementById("display-info");
const searchTerm = document.getElementById("search");
const select = document.getElementById("ep-select");
const showSelect = document.getElementById("show-select");
const backToShows = document.querySelector(".back-to-shows");
// Event listeners
searchTerm.addEventListener("keyup", applyFilters);
select.addEventListener("change", applyFilters);
showSelect.addEventListener("change", showChange);
backToShows.addEventListener("click", showAllShows);
// Go back to all shows by hiding ep view and un-hiding shows view
function showAllShows() {
window.scrollTo(0, 0);
document.getElementById("ep-view").style.display = "none";
document.getElementById("ep-select").style.display = "none";
document.querySelector(".back-to-shows").style.display = "none";
document.querySelector(".show-container").style.display = "";
document.querySelector(".show-select").style.display = "none";
searchTerm.value = "";
renderShows(SHOWS);
}
// Fetch all shows
async function fetchShows() {
const response = await fetch(SHOWS_URL);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data.sort((a, b) =>
a.name.toLowerCase().localeCompare(b.name.toLowerCase()),
);
}
// Fetch all episodes
async function fetchEpisodes() {
const response = await fetch(EPISODES_URL);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
}
// Filtering func
function applyFilters() {
const query = searchTerm.value.toLowerCase();
const showContainer = document.querySelector(".show-container");
if (showContainer.style.display !== "none") {
// Filter Shows
const filteredShows = SHOWS.filter(
(show) =>
show.name.toLowerCase().includes(query) ||
show.genres.join(" ").toLowerCase().includes(query) ||
show.summary.toLowerCase().includes(query),
);
renderShows(filteredShows);
} else {
// Filter Episodes
const selectedEp = select.value;
let filteredEps = EPISODES.filter(
(ep) =>
ep.name.toLowerCase().includes(query) ||
(ep.summary || "").toLowerCase().includes(query),
);
if (selectedEp !== "all-episodes") {
filteredEps = filteredEps.filter((ep) => ep.id === Number(selectedEp));
}
renderEpisodes(filteredEps);
}
}
// Creating each episode card
function createEpCard(episode) {
const epCard = document.getElementById("ep-card").content.cloneNode(true);
const code = seasonAndEpisodeFormat(
String(episode.season),
String(episode.number),
);
const title = epCard.querySelector("h2");
const image = epCard.querySelector("img");
const summary = epCard.querySelector("p");
title.textContent = `${episode.name} - ${code}`;
image.src = episode.image.medium || "./images/no-image.png";
image.alt = `${episode.name} (${code})`;
summary.innerHTML =
episode.summary || "No summary available for this episode.";
return epCard;
}
// Creating each show card
function createShowCard(show) {
const showCard = document.getElementById("show-card").content.cloneNode(true);
const showSection = showCard.querySelector(".show-section");
const title = showCard.querySelector(".show-title");
const image = showCard.querySelector(".show-img");
const summary = showCard.querySelector(".show-summary");
const genres = showCard.querySelector(".show-genres");
const status = showCard.querySelector(".show-status");
const rating = showCard.querySelector(".show-rating");
const runtime = showCard.querySelector(".show-runtime");
title.textContent = show.name;
image.src = show.image.medium || "./images/no-image.png";
image.alt = show.name;
summary.innerHTML = show.summary;
genres.textContent = show.genres.join(", ");
status.textContent = show.status;
rating.textContent = show.rating.average;
runtime.textContent = show.runtime;
showSection.addEventListener("click", () => {
showSelect.value = show.id;
showChange();
});
return showCard;
}
// Formatting season and episode
function seasonAndEpisodeFormat(season, episode) {
season = season.padStart(2, "0");
episode = episode.padStart(2, "0");
return `S${season}E${episode}`;
}
// Rendering episodes
function renderEpisodes(list) {
const container = document.querySelector(".ep-container");
const template = document.getElementById("ep-card");
container.innerHTML = "";
container.appendChild(template);
for (const episode of list) {
container.appendChild(createEpCard(episode));
}
displayEps.textContent = `Displaying ${list.length} / ${EPISODES.length} episodes`;
}
// Rendering shows
function renderShows(list) {
const container = document.querySelector(".show-container");
const template = document.getElementById("show-card");
container.innerHTML = "";
container.appendChild(template);
for (const show of list) {
container.appendChild(createShowCard(show));
}
displayEps.textContent = `Found ${list.length} / ${SHOWS.length} shows`;
}
// Populate ep options
function populateEpOptions() {
select.innerHTML = `<option value="all-episodes">Show all episodes</option>`;
EPISODES.forEach((ep) => {
const option = document.createElement("option");
const code = seasonAndEpisodeFormat(String(ep.season), String(ep.number));
option.value = ep.id;
option.textContent = `${code} - ${ep.name}`;
select.appendChild(option);
});
}
// Populate select with show options
function populateShowOptions() {
showSelect.innerHTML = "";
SHOWS.forEach((show) => {
const option = document.createElement("option");
option.value = show.id;
option.textContent = show.name;
showSelect.appendChild(option);
});
populateEpOptions();
}
// Handling show change
async function showChange() {
window.scrollTo(0, 0);
const showId = showSelect.value;
const showContainer = document.querySelector(".show-container");
const epView = document.getElementById("ep-view");
if (EPISODE_CACHE[showId]) {
EPISODES = EPISODE_CACHE[showId];
updateUIForEpisodes();
return;
}
displayEps.textContent = "Loading episodes...";
try {
const response = await fetch(`${SHOWS_URL}/${showId}/episodes`);
const data = await response.json();
EPISODE_CACHE[showId] = data;
EPISODES = data;
updateUI();
} catch (error) {
displayEps.textContent = `Error: ${error.message}`;
}
}
// Updating UI
function updateUI() {
const showContainer = document.querySelector(".show-container");
const epView = document.getElementById("ep-view");
searchTerm.value = "";
showContainer.style.display = "none";
document.querySelector(".back-to-shows").style.display = "";
epView.style.display = "block";
document.getElementById("ep-select").style.display = "";
populateEpOptions();
renderEpisodes(EPISODES);
}
// Initialize
async function init() {
displayEps.textContent = "Loading...";
document.getElementById("ep-view").style.display = "none";
document.querySelector(".ep-select").style.display = "none";
document.querySelector(".back-to-shows").style.display = "none";
document.querySelector(".show-select").style.display = "none";
setTimeout(async function () {
try {
const allShows = await fetchShows();
SHOWS = allShows;
populateShowOptions();
renderShows(SHOWS);
displayEps.textContent = `Found ${SHOWS.length} shows`;
} catch (error) {
displayEps.textContent = `Error: ${error.message}`;
}
}, 1000);
}
// Initial render
window.onload = init;