generated from CodeYourFuture/tv-show-dom-project
-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
251 lines (246 loc) · 8.79 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
// You can edit ALL of the code here
function setup() {
const tvShows = getAllShows();
tvShowsDropdown(tvShows);
makePageForShows(tvShows);
}
var count = 0;
//function for tv shows dropdown
function tvShowsDropdown(tvShows) {
//dropdown for tv shows
const dropdownTV = document.getElementsByClassName("form-select")[0];
//show select must list shows in alphabetical order, case-insensitive.
tvShows.sort((a, b) => {
if (a.name.toLowerCase() < b.name.toLowerCase()) {
return -1;
}
if (a.name.toLowerCase() > b.name.toLowerCase()) {
return 1;
}
return 0;
});
//add shows to dropdown
tvShows.forEach((show) => {
const option = document.createElement("option");
option.value = show.id;
option.textContent = show.name;
dropdownTV.appendChild(option);
});
dropdownTV.addEventListener("change", (event) => {
const rowElem = document.getElementsByClassName("row")[0];
rowElem.innerHTML = "";
const selectedShow = tvShows.find(
(show) => show.id === Number(event.target.value)
);
//clear the episodes dropdown
const dropdownEpisodes = document.getElementsByClassName("form-select")[1];
dropdownEpisodes.innerHTML = "";
//add all episodes to dropdown
const allEpisodes = document.createElement("option");
allEpisodes.value = "All Episodes";
allEpisodes.textContent = "All Episodes";
dropdownEpisodes.appendChild(allEpisodes);
fetch(`https://api.tvmaze.com/shows/${selectedShow.id}/episodes`).then(
(response) =>
response.json().then((data) => {
count++;
console.log(count);
makePageForEpisodes(data);
dropdown(data);
searchBar(data);
})
);
});
}
//function for episodes dropdown
function dropdown(episodeList) {
const dropdown = document.getElementsByClassName("form-select")[1];
dropdown.addEventListener("change", (event) => {
const selected = event.target.value;
const filteredEpisodes = episodeList.filter((episode) => {
return episode.id === parseInt(selected);
});
const rowElem = document.getElementsByClassName("row")[0];
rowElem.innerHTML = "";
makePageForEpisodes(filteredEpisodes);
});
//Selecting All Episodes option will bring all episodes back
dropdown.addEventListener("change", (event) => {
if (event.target.value === "All Episodes") {
const rowElem = document.getElementsByClassName("row")[0];
rowElem.innerHTML = "";
makePageForEpisodes(episodeList);
}
});
}
//function for search bar
function searchBar(episodeList) {
const episodeText = document.getElementById("basic-addon2");
episodeText.textContent = `Got ${episodeList.length} episode(s)`;
const searchBar = document.getElementById("search");
searchBar.addEventListener("keyup", (event) => {
const searchTerm = event.target.value.toLowerCase();
let search = episodeList.filter((episode) => {
return (
episode.name.toLowerCase().includes(searchTerm) ||
episode.summary.toLowerCase().includes(searchTerm)
);
});
const rowElem = document.getElementsByClassName("row")[0];
rowElem.innerHTML = "";
episodeText.textContent = `Displaying ${search.length}/${episodeList.length} episode(s)`;
makePageForEpisodes(search);
});
}
//function for creating elements
function makePageForEpisodes(episodeList) {
episodeList.forEach((episode) => {
const rowElem = document.getElementsByClassName("row")[0];
//create dropdown for the episodes
const dropdown = document.getElementsByClassName("form-select")[1];
const option = document.createElement("option");
option.value = episode.id;
option.textContent = `S${episode.season
.toString()
.padStart(2, "0")}E${episode.number.toString().padStart(2, "0")} ${
episode.name
}`;
dropdown.appendChild(option);
//create grid style bootstrap cards
//create col
const colElem = document.createElement("div");
colElem.classList.add("col");
rowElem.appendChild(colElem);
//div card
const divElem = document.createElement("div");
divElem.classList.add("card");
divElem.classList.add("h-100");
divElem.classList.add("text-center");
divElem.style.width = "25rem";
colElem.appendChild(divElem);
//img
const imgElem = document.createElement("img");
imgElem.classList.add("card-img-top");
if (episode.image !== null) {
imgElem.src = episode.image.medium;
} else {
imgElem.src =
"https://via.placeholder.com/210x295/ffffff/666666/?text=TV";
}
divElem.appendChild(imgElem);
//div-body
const divBodyElem = document.createElement("div");
divBodyElem.classList.add("card-body");
divElem.appendChild(divBodyElem);
//title
const episodeTitle = document.createElement("h5");
episodeTitle.classList.add("card-title");
episodeTitle.textContent = episode.name;
divBodyElem.appendChild(episodeTitle);
//summary
const episodeSummary = document.createElement("p");
episodeSummary.classList.add("card-text");
episodeSummary.innerHTML = episode.summary;
divBodyElem.appendChild(episodeSummary);
//card footer
const cardFooter = document.createElement("div");
cardFooter.classList.add("card-footer");
cardFooter.classList.add("text-muted");
divElem.appendChild(cardFooter);
//ul for episode code
const episodeCode = document.createElement("ul");
episodeCode.classList.add("list-group");
episodeCode.classList.add("list-group-flush");
cardFooter.appendChild(episodeCode);
//episode code
const episodeCodeElem = document.createElement("li");
episodeCodeElem.classList.add("list-group-item");
episodeCodeElem.textContent = `S${episode.season
.toString()
.padStart(2, "0")}E${episode.number.toString().padStart(2, "0")}`;
episodeCode.appendChild(episodeCodeElem);
//episode url
const sourceElem = document.createElement("li");
sourceElem.classList.add("list-group-item");
sourceElem.textContent = "Source: TVMaze.com";
episodeCode.appendChild(sourceElem);
//button for url
const urlButton = document.createElement("a");
urlButton.classList.add("btn");
urlButton.classList.add("btn-primary");
urlButton.href = episode.url;
urlButton.target = "_blank";
urlButton.textContent = "Episode Page";
cardFooter.appendChild(urlButton);
});
}
//make page for TV shows
function makePageForShows(showList) {
showList.forEach((show) => {
const rowElem = document.getElementsByClassName("row")[0];
//create col
const colElem = document.createElement("div");
colElem.classList.add("col");
rowElem.appendChild(colElem);
//div card
const divElem = document.createElement("div");
divElem.classList.add("card");
divElem.classList.add("h-100");
divElem.classList.add("text-center");
divElem.style.width = "25rem";
colElem.appendChild(divElem);
//img
const imgElem = document.createElement("img");
imgElem.classList.add("card-img-top");
if (show.image !== null) {
imgElem.src = show.image.medium;
} else {
imgElem.src =
"https://via.placeholder.com/210x295/ffffff/666666/?text=TV";
}
divElem.appendChild(imgElem);
//div-body
const divBodyElem = document.createElement("div");
divBodyElem.classList.add("card-body");
divElem.appendChild(divBodyElem);
//title
const showTitle = document.createElement("h5");
showTitle.classList.add("card-title");
showTitle.textContent = show.name;
divBodyElem.appendChild(showTitle);
//summary
const showSummary = document.createElement("p");
showSummary.classList.add("card-text");
showSummary.innerHTML = show.summary;
divBodyElem.appendChild(showSummary);
//card footer
const cardFooter = document.createElement("div");
cardFooter.classList.add("card-footer");
cardFooter.classList.add("text-muted");
divElem.appendChild(cardFooter);
//ul for show code
const showCode = document.createElement("ul");
showCode.classList.add("list-group");
showCode.classList.add("list-group-flush");
cardFooter.appendChild(showCode);
//show code
const showCodeElem = document.createElement("li");
showCodeElem.classList.add("list-group-item");
showCodeElem.textContent = `${show.id}`;
showCode.appendChild(showCodeElem);
//show url
const sourceElem = document.createElement("li");
sourceElem.classList.add("list-group-item");
sourceElem.textContent = "Source: TVMaze.com";
showCode.appendChild(sourceElem);
//button for url
const urlButton = document.createElement("a");
urlButton.classList.add("btn");
urlButton.classList.add("btn-primary");
urlButton.href = show.url;
urlButton.target = "_blank";
urlButton.textContent = "Show Page";
cardFooter.appendChild(urlButton);
});
}
window.onload = setup;