-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
130 lines (119 loc) · 3.46 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
//You can edit ALL of the code here
const rootElem = document.getElementById("root");
const container = document.querySelector(".container");
const nav = document.querySelector(".nav");
const select = document.getElementById("episodes");
const themeSelector = document.querySelector("button");
themeSelector.addEventListener("click", () => {
nav.classList.toggle("dark");
rootElem.classList.toggle("dark");
themeSelector.classList.toggle("dark");
});
let p = document.createElement("p");
nav.append(p);
//run setup
window.onload = setup;
function setup() {
const allEpisodes = getAllEpisodes();
const input = document.querySelector("input");
makePageForEpisodes(allEpisodes);
input.addEventListener("input", ({ target: { value } }) => {
if (value) {
select.selectedIndex = 0;
}
let filteredList = allEpisodes.filter((episode) => {
if (
episode.name.toLocaleLowerCase().includes(value.toLowerCase()) ||
episode.summary.toLocaleLowerCase().includes(value.toLowerCase())
) {
return episode;
}
});
makePageForEpisodes(filteredList);
});
}
function makeEpisodeCard(item) {
let card = createAndAppendElement("section", "", "", "card");
createAndAppendElement("h2", item.name, card);
createAndAppendElement("img", item.image.medium, card);
createAndAppendElement(
"p",
namePadding(item.season, item.number),
card,
"season-data"
);
createAndAppendElement("div", truncate(item.summary), card);
card.setAttribute("id", `${item.id}`);
createAndAppendElement(
"option",
`${namePadding(item.season, item.number)} - ${item.name}`,
select,
"",
item.id
);
return card;
}
//Create elements with optional content, classname addition and append to a parent
function createAndAppendElement(
tag,
content,
parent,
className = null,
value = null
) {
let element = document.createElement(`${tag}`);
if (element) {
if (tag === "img") {
element.src = content;
} else if (content[0] === "<") {
element.innerHTML = content;
} else {
element.innerText = content;
}
}
if (tag === "option") {
element.value = value;
}
if (className) {
element.classList.add(`${className}`);
}
if (parent) {
parent.append(element);
}
return element;
}
function makePageForEpisodes(episodeList) {
p.innerText = `Got ${episodeList.length} episode${
episodeList.length > 1 ? "s" : ""
}`;
p.style.marginLeft = "4%";
p.style.fontWeight = "bold";
container.innerText = "";
for (let episode of episodeList) {
let card = makeEpisodeCard(episode);
select.addEventListener("change", ({ target: { value } }) => {
if (episode.id === parseInt(value)) {
container.innerText = "";
card.classList.add("selected-episode");
let button = createAndAppendElement("a", "back", card);
button.classList.add("btn");
p.innerText = `Got 1 episode`;
button.addEventListener("click", () => {
makePageForEpisodes(episodeList);
});
container.append(card);
}
});
container.append(card);
}
}
//give padding to the season and episode numbers
function namePadding(seasonNum, episodeNum) {
let seasonName = seasonNum < 10 ? `S0${seasonNum}` : `S${seasonNum}`;
let episodeName = episodeNum < 10 ? `E0${episodeNum}` : `E${episodeNum}`;
return `${seasonName}${episodeName}`;
}
//truncate text
function truncate(str) {
return str.length > 375 ? `${str.slice(0, 375)}...` : str;
}