Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
261 lines (194 loc) · 6.51 KB
/
Copy pathscript.js
File metadata and controls
261 lines (194 loc) · 6.51 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
let allShows = [];
let allEpisodes = [];
let currentShowId = null;
const cache = {
shows: null,
episodesByShowId: {},
};
function setup() {
loadShows();
}
function renderShows(showList) {
const root = document.getElementById("root");
root.innerHTML = "";
const title = document.createElement("h1");
title.textContent = "TV Shows";
const showSelect = document.createElement("select");
const defaultOption = document.createElement("option");
defaultOption.value = "";
defaultOption.textContent = "Select a show...";
showSelect.appendChild(defaultOption);
allShows.forEach((show) => {
const option = document.createElement("option");
option.value = show.id;
option.textContent = show.name;
showSelect.appendChild(option);
});
showSelect.addEventListener("change", () => {
const selectedId = showSelect.value;
if (!selectedId) return;
currentShowId = selectedId;
renderEpisodesView();
loadEpisodesForShow(selectedId);
});
const searchInput = document.createElement("input");
searchInput.placeholder = "Search shows...";
const count = document.createElement("p");
const container = document.createElement("div");
container.id = "shows-container";
function renderShowCards(showList) {
container.innerHTML = "";
showList.forEach((show) => {
const card = document.createElement("article");
card.className = "show-card";
card.innerHTML = `
<h2>${show.name}</h2>
<img src="${show.image?.medium || ""}" alt="${show.name}">
<div>${show.summary || ""}</div>
<p><strong>Genres:</strong> ${show.genres.join(", ")}</p>
<p><strong>Status:</strong> ${show.status}</p>
<p><strong>Rating:</strong> ${show.rating?.average || "N/A"}</p>
<p><strong>Runtime:</strong> ${show.runtime || "N/A"} mins</p>
`;
card.addEventListener("click", () => {
currentShowId = show.id;
renderEpisodesView();
loadEpisodesForShow(show.id);
});
container.appendChild(card);
});
count.textContent = `Showing ${showList.length} / ${allShows.length} shows`;
}
searchInput.addEventListener("input", () => {
const term = searchInput.value.toLowerCase();
const filtered = allShows.filter((show) =>
show.name.toLowerCase().includes(term) ||
show.genres.join(" ").toLowerCase().includes(term) ||
(show.summary || "").toLowerCase().includes(term)
);
renderShowCards(filtered);
});
renderShowCards(allShows);
root.append(title, showSelect, searchInput, count, container);
}
function renderEpisodesView() {
const root = document.getElementById("root");
root.innerHTML = "";
const backBtn = document.createElement("button");
backBtn.textContent = "← Back to Shows";
backBtn.addEventListener("click", () => {
renderShows(allShows);
});
const controls = document.createElement("div");
const episodeSelect = document.createElement("select");
episodeSelect.id = "episode-select";
const searchInput = document.createElement("input");
searchInput.id = "search-input";
searchInput.placeholder = "Search episodes...";
const countDisplay = document.createElement("p");
countDisplay.id = "count-display";
const statusMessage = document.createElement("p");
statusMessage.id = "status-message";
const container = document.createElement("div");
container.id = "episodes-container";
controls.append(episodeSelect, searchInput, countDisplay);
root.append(backBtn, controls, statusMessage, container);
episodeSelect.addEventListener("change", updateDisplay);
searchInput.addEventListener("input", updateDisplay);
}
function loadShows() {
if (cache.shows) {
allShows = cache.shows;
renderShows(allShows);
return;
}
fetch("https://api.tvmaze.com/shows")
.then((res) => res.json())
.then((data) => {
allShows = data.sort((a, b) =>
a.name.localeCompare(b.name, undefined, { sensitivity: "base" })
);
cache.shows = allShows;
renderShows();
})
.catch((err) => console.error(err));
}
function loadEpisodesForShow(showId) {
const status = document.getElementById("status-message");
if (cache.episodesByShowId[showId]) {
const status = document.getElementById("status-message");
allEpisodes = cache.episodesByShowId[showId];
populateEpisodeSelector();
updateDisplay();
return;
}
status.textContent = "Loading episodes...";
fetch(`https://api.tvmaze.com/shows/${showId}/episodes`)
.then((res) => res.json())
.then((data) => {
cache.episodesByShowId[showId] = data;
allEpisodes = data;
status.textContent = "";
populateEpisodeSelector();
updateDisplay();
})
.catch(() => {
status.textContent = "Error loading episodes";
});
}
function populateEpisodeSelector() {
const select = document.getElementById("episode-select");
select.innerHTML = "";
const allOpt = document.createElement("option");
allOpt.value = "all";
allOpt.textContent = "All episodes";
select.appendChild(allOpt);
allEpisodes.forEach((ep) => {
const opt = document.createElement("option");
opt.value = ep.id;
opt.textContent = `${getEpisodeCode(ep)} - ${ep.name}`;
select.appendChild(opt);
});
}
function updateDisplay() {
const term = document
.getElementById("search-input")
.value.toLowerCase();
const selected = document.getElementById("episode-select").value;
let filtered = allEpisodes.filter((ep) => {
const summary = ep.summary ? ep.summary.toLowerCase() : "";
return (
ep.name.toLowerCase().includes(term) ||
summary.includes(term)
);
});
if (selected !== "all") {
filtered = filtered.filter((ep) => String(ep.id) === selected);
}
renderEpisodes(filtered);
updateCount(filtered.length, allEpisodes.length);
}
function renderEpisodes(list) {
const container = document.getElementById("episodes-container");
container.innerHTML = "";
list.forEach((ep) => {
const card = document.createElement("article");
card.innerHTML = `
<h2>${ep.name} - ${getEpisodeCode(ep)}</h2>
<img src="${ep.image?.medium || ""}" alt="${ep.name}">
<div>${ep.summary || ""}</div>
`;
container.appendChild(card);
});
}
function updateCount(current, total) {
document.getElementById(
"count-display"
).textContent = `Displaying ${current}/${total} episodes`;
}
function getEpisodeCode(ep) {
const s = String(ep.season).padStart(2, "0");
const n = String(ep.number).padStart(2, "0");
return `S${s}E${n}`;
}
window.onload = setup;