-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
204 lines (180 loc) · 7.4 KB
/
app.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
let pokemonObjectsArray = [];
let currentLanguage = "en";
let scrollHeight;
(async() => {
const fetchJson = file => fetch(file).then(r => r.json()).catch(console.log);
pokemonObjectsArray = await fetchJson("assets/json/pokemon-objects.json");
generateList(pokemonObjectsArray);
scrollHeight = document.querySelector(".simplebar-content").clientHeight;
listInteractivity();
selectActivePokemon(pokemonObjectsArray[24]); //24 contains Pikachu, the mascot of Pokemon, to open with a recognizable character.
printSelectedLanguage(currentLanguage);
})();
const pokeSelect = () => document.querySelector(".simplebar-content");
const generateList = array => {
array.forEach(({
id,
name
}) => {
const newPoke = document.createElement("li");
newPoke.innerHTML = `<span>${id}</span> <span class="pokedex__list-item--name-of-pokemon">${name[currentLanguage]}</span>`;
pokeSelect().append(newPoke);
newPoke.tabIndex = -1;
})
};
const parseIntIndex = index => parseInt(index, 10) - 1;
printPokemonNameCurrentLanguage = () => {
for (let li of liAll) {
num = parseIntIndex(li.innerText);
li.children[1].innerHTML = pokemonObjectsArray[num].name[currentLanguage];
}
}
const search = document.querySelector("#search");
let liAll;
search.addEventListener("input", () => {
for (let li of liAll) {
if (!li.innerText.includes(search.value.toUpperCase())) {
li.classList.add("u-display-none");
} else {
li.classList.remove("u-display-none");
}
}
});
const listInteractivity = () => {
let liAll = document.querySelectorAll("li");
for (let li of liAll) {
li.addEventListener("click", () => {
num = parseIntIndex(li.innerText);
selectActivePokemon(pokemonObjectsArray[num]);
});
}
}
const entryNumber = document.querySelector("#entry-number");
const entryName = document.querySelector("#entry-name");
const entrySprite = document.querySelector("#entry-sprite");
const entrySpecies = document.querySelector("#entry-species");
const entryHeight = document.querySelector("#entry-height");
const entryWeight = document.querySelector("#entry-weight");
const entryFlavorText = document.querySelector("#entry-flavor-text");
let entryAudioFile;
let currentActivePokemon;
const selectActivePokemon = (poke) => {
entryNumber.lastChild.textContent = poke.id;
entryName.textContent = poke.name[currentLanguage];
entrySprite.src = poke.image;
entrySpecies.lastChild.textContent = ` ${poke.species[currentLanguage]}`;
if (currentLanguage === "en") {
entryHeight.lastChild.textContent = ` ${poke.height.imperial}`;
entryWeight.lastChild.textContent = ` ${poke.weight.imperial}`;
} else {
entryHeight.lastChild.textContent = ` ${poke.height.metric}`;
entryWeight.lastChild.textContent = ` ${poke.weight.metric}`;
}
entryFlavorText.innerText = poke.entry[currentLanguage];
entryAudioFile = new Audio(poke.audioFile);
focusPoke(poke);
currentActivePokemon = parseIntIndex(poke.id);;
};
const focusPoke = poke => {
search.value = "";
liAll = document.querySelectorAll("li");
for (let li of liAll) {
li.classList.remove("u-display-none");
li.classList.remove("u-border-color-white");
};
const focusedPoke = pokeSelect().children[parseIntIndex(poke.id)];
focusedPoke.classList.add("u-border-color-white");
focusedPoke.focus();
resetPokedexPaneDisplay(window.innerWidth);
};
const entryAudioButton = document.querySelector("#entry-audio-button");
window.addEventListener("DOMContentLoaded", () => {
entryAudioButton.addEventListener("click", () => {
entryAudioFile.play();
});
});
const entryNumberHeading = document.querySelector("#entry-number-heading");
const entrySpeciesHeading = document.querySelector("#entry-species-heading");
const entryHeightHeading = document.querySelector("#entry-height-heading");
const entryWeightHeading = document.querySelector("#entry-weight-heading");
let multilingualHeadings;
(async() => {
const fetchJson = file => fetch(file).then(r => r.json()).catch(console.log);
return multilingualHeadings = await fetchJson("assets/json/multilingual-headings.json");
})();
const printAllHeadings = () => {
for (let heading in multilingualHeadings) {
if (heading.includes("languageSelect")) {
let language = heading.replace("languageSelect", "").toLowerCase();
let languageButton = document.querySelector(`[data-language=${language}]`);
languageButton.textContent = multilingualHeadings[heading][currentLanguage];
} else {
const passedInHeading = eval(heading);
if (passedInHeading.placeholder) {
passedInHeading.placeholder = multilingualHeadings[heading][currentLanguage];
} else {
passedInHeading.textContent = multilingualHeadings[heading][currentLanguage];
}
}
}
}
const printSelectedLanguage = (selectedLanguage) => {
currentLanguage = selectedLanguage;
selectActivePokemon(pokemonObjectsArray[currentActivePokemon]);
printPokemonNameCurrentLanguage();
printAllHeadings();
}
const languageSelectButtons = document.querySelectorAll("[data-language]");
languageSelectButtons.forEach((button) => {
button.addEventListener("click", () => {
const language = button.getAttribute("data-language");
printSelectedLanguage(language);
});
});
//cardBall Rotation
window.addEventListener("DOMContentLoaded", () => {
const pokeSelect = document.querySelector(".simplebar-content-wrapper");
function transformCardBall (scrollTop, scrollHeight, clientHeight) {
const degrees = Math.round((scrollTop / (scrollHeight - clientHeight)) * 360);
document.documentElement.style.setProperty("--deg", `${ degrees }deg`);
}
pokeSelect.addEventListener("scroll", e => {
transformCardBall(e.target.scrollTop, scrollHeight, e.target.clientHeight);
});
});
//Responsiveness
const toggleButton = document.querySelector("#toggle-button");
const searchPane = document.querySelector("#search-pane");
const entryPane = document.querySelector("#entry-pane");
window.addEventListener("DOMContentLoaded", () => {
toggleButton.addEventListener("click", () => {
searchPane.classList.toggle("u-display-block");
entryPane.classList.toggle("u-display-none");
});
});
tabPortMediaQuery = 900;
const resetPokedexPaneDisplay = (width) => {
if (width <= tabPortMediaQuery) {
searchPane.classList.remove("u-display-block");
entryPane.classList.remove("u-display-none");
}
}
let prevWidth = window.innerWidth;
window.addEventListener("DOMContentLoaded", () => {
window.addEventListener("resize", () => {
currentWidth = window.innerWidth
if (currentWidth <= tabPortMediaQuery) { //To small...
if (prevWidth >= tabPortMediaQuery) { //...from big
searchPane.classList.remove("u-display-block");
entryPane.classList.remove("u-display-none");
}
if (prevWidth <= tabPortMediaQuery) { //...from small
return prevWidth = currentWidth;
}
} else { //To big from big & from small to big
searchPane.classList.remove("u-display-block");
entryPane.classList.remove("u-display-none");
}
prevWidth = currentWidth;
})
});