generated from CodeYourFuture/tv-show-dom-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
310 lines (253 loc) · 9.55 KB
/
script.js
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
let episodeContainer;
let allShows = [];
let allEpisodes = [];
let isShowingShowsListing = true;
async function setup() {
allShows = await getAllShows();
const rootElem = document.getElementById("root");
const showSelect = createShowSelect(allShows);
const episodeSelect = createEpisodeSelect();
episodeSelect.style.display = "none"; // Hide episode selector initially
episodeContainer = document.createElement("div");
episodeContainer.classList.add("episode-container");
const showsListingContainer = document.createElement("div");
showsListingContainer.classList.add("shows-listing-container");
const backButton = document.createElement("button");
backButton.textContent = "Back to Shows Listing";
backButton.classList.add("back-button");
backButton.style.display = "none";
backButton.addEventListener("click", () => {
showsListingContainer.style.display = "block";
episodeContainer.style.display = "none";
backButton.style.display = "none";
episodeSelect.style.display = "none";
searchContainer.style.display = "none";
isShowingShowsListing = true;
showSelect.value = "";
episodeSelect.value = "all";
searchInput.value = "";
matchedCount.textContent = "";
});
const matchedCount = document.createElement("div");
matchedCount.setAttribute("id", "matchedCount");
matchedCount.classList.add("matched-count");
const searchContainer = document.createElement("div");
searchContainer.classList.add("search-container");
searchContainer.style.display = "none"; // Hide search container initially
const searchInput = document.createElement("input");
searchInput.setAttribute("type", "text");
searchInput.setAttribute("id", "searchInput");
searchInput.setAttribute("placeholder", "Search episodes");
searchInput.addEventListener("input", updateSearchResults);
searchContainer.appendChild(searchInput);
searchContainer.appendChild(matchedCount);
rootElem.appendChild(showSelect);
rootElem.appendChild(episodeSelect);
rootElem.appendChild(backButton);
rootElem.appendChild(searchContainer);
rootElem.appendChild(showsListingContainer);
rootElem.appendChild(episodeContainer);
showSelect.addEventListener("change", async () => {
const selectedShowId = showSelect.value;
const episodeList = await fetchEpisodeList(selectedShowId);
allEpisodes = episodeList;
makePageForEpisodes(allEpisodes, episodeContainer);
populateEpisodeSelect(allEpisodes);
episodeSelect.style.display = "block"; // Show episode selector for the selected show
searchContainer.style.display = "block"; // Show search container for the selected show
if (isShowingShowsListing) {
showsListingContainer.style.display = "none";
episodeContainer.style.display = "block";
backButton.style.display = "block";
episodeSelect.value = "all"; // Reset episode selector value
isShowingShowsListing = false;
}
});
episodeSelect.addEventListener("change", () => {
const selectedEpisodeId = episodeSelect.value;
if (selectedEpisodeId === "all") {
displayAllEpisodes();
} else {
highlightSelectedEpisode(selectedEpisodeId);
}
});
// Create shows listing
const showsListing = document.createElement("div");
showsListing.classList.add("shows-listing");
allShows.forEach((show) => {
const showCard = document.createElement("div");
showCard.classList.add("show-card");
const showName = document.createElement("h2");
showName.textContent = show.name;
const showImage = document.createElement("img");
showImage.setAttribute("src", show.image.medium);
showImage.setAttribute("alt", show.name);
const showSummary = document.createElement("div");
showSummary.innerHTML = show.summary;
const showGenres = document.createElement("p");
showGenres.textContent = `Genres: ${show.genres.join(", ")}`;
const showStatus = document.createElement("p");
showStatus.textContent = `Status: ${show.status}`;
const showRating = document.createElement("p");
showRating.textContent = `Rating: ${show.rating.average}`;
const showRuntime = document.createElement("p");
showRuntime.textContent = `Runtime: ${show.runtime} minutes`;
showCard.appendChild(showName);
showCard.appendChild(showImage);
showCard.appendChild(showSummary);
showCard.appendChild(showGenres);
showCard.appendChild(showStatus);
showCard.appendChild(showRating);
showCard.appendChild(showRuntime);
showsListing.appendChild(showCard);
});
showsListingContainer.appendChild(showsListing);
// Show the shows listing by default
showsListingContainer.style.display = "block";
episodeContainer.style.display = "none";
backButton.style.display = "none";
isShowingShowsListing = true;
}
async function getAllShows() {
const response = await fetch("https://api.tvmaze.com/shows");
const shows = await response.json();
return shows;
}
async function fetchEpisodeList(showId) {
const response = await fetch(
`https://api.tvmaze.com/shows/${showId}/episodes`
);
const episodeList = await response.json();
return episodeList;
}
function createShowSelect(shows) {
const selectInput = document.createElement("select");
selectInput.setAttribute("id", "showSelect");
const defaultOption = document.createElement("option");
defaultOption.textContent = "Select a show";
defaultOption.setAttribute("value", "");
defaultOption.disabled = true;
defaultOption.selected = true;
selectInput.appendChild(defaultOption);
const sortedShows = shows.sort((a, b) =>
a.name.localeCompare(b.name, "en", { sensitivity: "base" })
);
sortedShows.forEach((show) => {
const option = document.createElement("option");
option.textContent = show.name;
option.setAttribute("value", show.id);
selectInput.appendChild(option);
});
return selectInput;
}
function createEpisodeSelect() {
const selectInput = document.createElement("select");
selectInput.setAttribute("id", "episodeSelect");
return selectInput;
}
function makePageForEpisodes(episodeList, episodeContainer) {
episodeContainer.innerHTML = "";
episodeList.forEach((episode) => {
const card = document.createElement("div");
card.classList.add("card");
card.setAttribute("data-episode-id", episode.id);
const title = document.createElement("h2");
title.innerHTML = `${episode.name} - S${zeroPad(episode.season)}E${zeroPad(
episode.number
)}`;
const image = document.createElement("img");
image.setAttribute("src", episode.image.medium);
image.setAttribute("alt", `Poster for ${episode.name}`);
const summary = document.createElement("div");
const highlightedSummary = highlightMatchedWords(
episode.summary,
searchInput.value.toLowerCase()
);
summary.innerHTML = highlightedSummary;
card.appendChild(title);
card.appendChild(image);
card.appendChild(summary);
episodeContainer.appendChild(card);
});
}
function populateEpisodeSelect(episodeList) {
const episodeSelect = document.getElementById("episodeSelect");
episodeSelect.innerHTML = "";
const allOption = document.createElement("option");
allOption.textContent = "All Episodes";
allOption.setAttribute("value", "all");
episodeSelect.appendChild(allOption);
episodeList.forEach((episode) => {
const option = document.createElement("option");
option.textContent = `S${zeroPad(episode.season)}E${zeroPad(
episode.number
)} - ${episode.name}`;
option.setAttribute("value", episode.id);
episodeSelect.appendChild(option);
});
}
function displayAllEpisodes() {
const episodeCards = document.querySelectorAll(".card");
episodeCards.forEach((card) => {
card.style.display = "block";
});
updateMatchedCount(episodeCards);
}
function highlightSelectedEpisode(episodeId) {
const episodeCards = document.querySelectorAll(".card");
episodeCards.forEach((card) => {
if (card.getAttribute("data-episode-id") === episodeId) {
card.style.display = "block";
card.scrollIntoView({ behavior: "smooth", block: "center" });
} else {
card.style.display = "none";
}
});
updateMatchedCount(episodeCards);
}
function updateSearchResults(event) {
const searchInput = document.getElementById("searchInput");
const searchTerm = searchInput.value.toLowerCase();
const episodeCards = document.querySelectorAll(".card");
episodeCards.forEach((card) => {
const episodeName = card.querySelector("h2").textContent.toLowerCase();
const episodeSummary = card.querySelector("div");
if (
episodeName.includes(searchTerm) ||
episodeSummary.textContent.toLowerCase().includes(searchTerm)
) {
card.style.display = "block";
const highlightedSummary = highlightMatchedWords(
episodeSummary.innerHTML,
searchTerm
);
episodeSummary.innerHTML = highlightedSummary;
} else {
card.style.display = "none";
}
});
updateMatchedCount(episodeCards);
}
function highlightMatchedWords(content, searchTerm) {
// not working properly :(
if (!searchTerm) {
return content;
}
const regex = new RegExp(`\\b${searchTerm}\\b`, "gi");
const highlightedContent = content.replaceAll(
regex,
(match) => `<span class="highlight">${match}</span>`
);
return highlightedContent;
}
function updateMatchedCount(episodeCards) {
const matchedCount = document.getElementById("matchedCount");
const count = Array.from(episodeCards).filter(
(card) => card.style.display !== "none"
).length;
matchedCount.textContent = `${count} episode(s) matched`;
}
function zeroPad(num) {
return num.toString().padStart(2, "0");
}
setup();