generated from CodeYourFuture/tv-show-dom-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
317 lines (284 loc) · 10.5 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
311
312
313
314
315
316
317
//import episode card to render the data
import createEpisodeCard from "./episodecard.js";
//import all shows
import getAllShows from "./shows.js";
let allShowEpisodes = [];
let allEpisodes = [];
let allAvailableShows = [];
let userSelectedShow = "";
allAvailableShows = getAllShows();
//display episodes
async function getAllEpisodes() {
let names = localStorage.getItem("showname");
let id;
let shows = allAvailableShows.find((show) => show.name === names);
if (shows !== undefined) {
id = shows.id;
}
const response = await fetch(`https://api.tvmaze.com/shows/${id}/episodes`);
const data = await response.json();
let episodes = data.filter(
(episodes) => episodes.summary !== "" && episodes.image !== null
);
return episodes;
}
//Setup
async function setup() {
allEpisodes = await getAllEpisodes();
let completeEpisodes = allEpisodes.filter((episode) => {
if (episode.image !== null && episode.summary !== null) {
return episode;
}
});
allAvailableShows = getAllShows(); //all shows
makePageForEpisodes(); // initial page for all episodes
createEpisodeCard(completeEpisodes); //single episode card for available / matched episode
createOptions(completeEpisodes); //load episodes in select
showOptions(allAvailableShows); //load shows in select
let show = document.getElementById("shows");
show.dispatchEvent(new Event("change"));
}
// header body and footer styling and data presentation
function makePageForEpisodes() {
const rootElem = document.getElementById("root"); //root div with ul
const header = document.createElement("header");
document.body.prepend(header);
header.classList.add("header");
const heading = document.createElement("h1");
heading.innerText = "TV Shows";
const img = document.createElement("img");
img.src = "favicon.ico";
img.classList.add("img");
img.alt = "tv logo";
header.append(img, heading);
const searchContainer = document.createElement("div"); //search input text and display
const displayNumbers = document.createElement("p");
displayNumbers.id = "display";
const search = document.createElement("input");
const searchLabel = document.createElement("label");
searchLabel.classList.add("label");
searchLabel.innerText = "Search Episode";
search.type = "text";
search.placeholder = "Enter key word";
search.id = "searchBox";
searchContainer.classList.add("inputs");
searchContainer.append(searchLabel, search, displayNumbers);
document.body.insertBefore(searchContainer, rootElem);
const selectLabel = document.createElement("label");
selectLabel.innerText = "Select Episode";
selectLabel.classList.add("label");
const selectLabelOptions = document.createElement("label");
selectLabelOptions.innerText = "Select Show";
selectLabelOptions.classList.add("label");
const select = document.createElement("select");
//select display for options of episodes available
const showNames = document.createElement("select");
showNames.classList.add("select");
showNames.id = "shows";
select.classList.add("select");
const selectContainer = document.createElement("div");
const button = document.createElement("button");
button.style.display = "none";
button.classList.add("button");
button.innerText = "Show all Episodes";
const buttonShows = document.createElement("a");
buttonShows.classList.add("button");
buttonShows.innerText = "Present all Shows";
buttonShows.href = "index.html";
buttonShows.target = "_self";
selectContainer.append(
selectLabel,
select,
selectLabelOptions,
showNames,
button,
buttonShows
);
document.body.insertBefore(selectContainer, searchContainer); //select and search appended to the body
selectContainer.classList.add("selection");
const footer = document.createElement("footer");
footer.classList.add("footer");
const site = document.createElement("p");
site.innerText = "@copyright";
const link = document.createElement("a");
link.id = "linkTag";
link.href = "https://tvmaze.com/api#licensing";
link.target = "_blank";
link.innerText = "TVMaze.com";
//display footer
document.body.appendChild(footer);
footer.appendChild(site);
footer.appendChild(link);
//button event to display all episodes
button.addEventListener("click", async () => {
let totalEpisodes = await getAllEpisodesFetched();
let completeEpisodes = totalEpisodes.filter((episode) => {
if (episode.image !== null && episode.summary !== null) {
return episode;
}
});
allShowEpisodes = completeEpisodes;
allEpisodes = allShowEpisodes.map((episode) => episode);
const screen = document.getElementById("root");
const list = document.querySelector(".main");
screen.removeChild(list);
const display = document.getElementById("display");
display.innerText = `displaying ${completeEpisodes.length} of ${completeEpisodes.length} episodes`;
const options = document.querySelector(".select");
removeAllChildNodes(options);
createEpisodeCard(allEpisodes); //single episode card for available / matched episode
createOptions(allEpisodes);
});
searchContainer.addEventListener("input", searchList); //event listener search
select.addEventListener("change", selectList); //event listener select
showNames.addEventListener("change", selectShows);
}
// populate Episode options when episode are searched or all episodes if no search made
function createOptions(episodeList) {
const selectOptions = document.querySelector("select");
const search = document.getElementById("searchBox");
search.innerText = "";
episodeList.forEach((episode) => {
const option = document.createElement("option");
option.value = `${episode.season
.toString()
.padStart(3, "S0")}${episode.number.toString().padStart(3, "E0")} - ${
episode.name
}`;
option.innerText = `${episode.season
.toString()
.padStart(3, "S0")}${episode.number.toString().padStart(3, "E0")} - ${
episode.name
}`;
selectOptions.appendChild(option);
});
}
//shows
function showOptions(showList) {
const selectOptions = document.getElementById("shows");
showList = showList.sort((a, b) => {
if (a.name.toLowerCase() < b.name.toLowerCase()) return -1;
if (a.name.toLowerCase() > b.name.toLowerCase()) return 1;
return 0;
});
showList.forEach((show) => {
const option = document.createElement("option");
option.value = `${show.name}`;
option.innerText = `${show.name}`;
selectOptions.appendChild(option);
});
}
//search event listener's call back function
async function searchList(e) {
const screen = document.getElementById("root");
const list = document.querySelector(".main");
screen.removeChild(list);
const displayNumbers = document.getElementById("display");
const options = document.querySelector(".select");
removeAllChildNodes(options);
const userInput = e.target.value.toLowerCase();
let totalEpisodes = await getAllEpisodesFetched();
let completeEpisodes = totalEpisodes.filter((episode) => {
if (episode.image !== null && episode.summary !== null) {
return episode;
}
});
const allMatchedEpisodes = allEpisodes.filter((episode) => {
if (episode.image !== null && episode.summary !== null) {
if (
episode.name.toLowerCase().includes(userInput) ||
episode.summary.toLowerCase().includes(userInput)
)
return episode;
}
});
displayNumbers.innerText = `displaying ${allMatchedEpisodes.length} / ${completeEpisodes.length} episodes`;
createEpisodeCard(allMatchedEpisodes);
createOptions(allMatchedEpisodes);
}
//select event listener's call back function
async function selectList(e) {
const search = document.getElementById("searchBox");
search.value = "";
const button = document.querySelector(".button");
button.style.display = "flex";
const displayNumbers = document.getElementById('display');
displayNumbers.innerText = '';
const selected = allEpisodes.filter((episode) => {
if (
e.target.value ===
`${episode.season.toString().padStart(3, "S0")}${episode.number
.toString()
.padStart(3, "E0")} - ${episode.name}`
) {
return episode;
}
});
const screen = document.getElementById("root");
const list = document.querySelector(".main");
screen.removeChild(list);
createEpisodeCard(selected);
}
async function getAllEpisodesFetched() {
let id;
allAvailableShows = getAllShows();
let shows = allAvailableShows.find((show) => show.name === userSelectedShow);
if (shows !== undefined) {
id = shows.id;
} else {
let names = localStorage.getItem("showname");
let shows = allAvailableShows.find((show) => show.name === names);
if (shows !== undefined) {
id = shows.id;
}
}
let url = `https://api.tvmaze.com/shows/${id}/episodes`;
let res = fetch(url)
.then((res) => res.json())
.catch((error) => console.log("connection Failed", error));
return res;
}
//new show select
async function selectShows(e) {
let chosenShowName = localStorage.getItem("chosenShow");
if (chosenShowName) {
userSelectedShow = chosenShowName;
localStorage.removeItem("chosenShow");
const showFound = document.getElementById("shows");
const showGiven = Array.from(showFound);
showGiven.filter((show, index) => {
if (show.value === chosenShowName) {
showGiven[index].setAttribute("selected", "selected");
}
});
} else {
userSelectedShow = e.target.value;
}
const displayNumbers = document.getElementById("display");
const inputBox = document.getElementById("searchBox");
inputBox.value = "";
displayNumbers.display='flex'
let totalEpisodes = await getAllEpisodesFetched();
let completeEpisodes = totalEpisodes.filter((episode) => {
if (episode.image !== null && episode.summary !== null) {
return episode;
}
});
allShowEpisodes = completeEpisodes;
displayNumbers.innerText = `displaying ${completeEpisodes.length} of ${completeEpisodes.length} episodes`;
allEpisodes = allShowEpisodes.map((episode) => episode);
const screen = document.getElementById("root");
const list = document.querySelector(".main");
screen.removeChild(list);
const options = document.querySelector(".select");
removeAllChildNodes(options);
createEpisodeCard(allEpisodes); //single episode card for available / matched episode
createOptions(allEpisodes);
}
//remove options dynamically
function removeAllChildNodes(parent) {
while (parent.firstChild) {
parent.removeChild(parent.firstChild);
}
}
window.onload = setup; // on window load setup call