-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
412 lines (352 loc) · 13.7 KB
/
index.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
const pokemon_container = document.getElementById('pokemon_container');
const search_bar = document.getElementById('search_bar');
const search_error = document.getElementById('search_error');
const generation_container = document.getElementById('generation_container');
const pokedex_name = document.getElementById('pokedex_name');
const loading = document.getElementById('loading');
// In milliseconds
const card_anim_speed = 1000;
let timeout = null;
let generations = null;
// Helps keep track of the current gen for the buttons
let current_generation = 1;
// Number is used for staggering the animation
let number = 0;
let promises = null;
let anim_timeouts = [];
const changeGeneration = async (url, new_generation, button_id) => {
current_generation = new_generation;
// Clear disabled from each generation button
const children = Array.from(generation_container.children);
children.forEach(child => {
const button = Array.from(child.children)[0];
if (button.id == button_id) {
// Disable the active generation button
button.disabled = true;
button.style.color = '#fff';
button.style.cursor = 'default';
} else {
// Enable the others
button.disabled = false;
button.style.color = '#999';
button.style.cursor = 'pointer';
}
});
search_bar.value = '';
try {
const result = await fetch(url);
if (result.ok) {
const generation = await result.json();
const limit = generation.pokemon_species.length;
const offset = generation.pokemon_species[0].url.slice(-4, -1) - 1;
const name = generation.main_region.name[0].toUpperCase() + generation.main_region.name.slice(1);
pokedex_name.innerHTML = `${name} Pokédex`;
pokemon_container.innerHTML = '';
search_error.textContent = '';
getAllPokemon(`https://pokeapi.co/api/v2/pokemon/?limit=${limit}&offset=${offset}`, current_generation);
}
} catch(e) {errorShake(e);}
}
const getGenerations = async (url) => {
try {
const result = await fetch(url);
if (result.ok) {
generations = await result.json();
for (let number = 0; number < generations.count; number++) {
const generation_button = document.createElement('div');
generation_button.classList.add('generation_button_container');
generation_button.innerHTML = `
<button type="button" id="generation_button_${number + 1}" onclick="changeGeneration('${generations.results[number].url}', ${number + 1}, 'generation_button_${number + 1}')">${number + 1}</button>`;
if ((number + 1) == 1) {
const button = Array.from(generation_button.children)[0];
button.disabled = true;
button.style.color = '#fff';
button.style.cursor = 'default';
}
generation_container.appendChild(generation_button);
}
}
} catch(e) {errorShake(e);}
}
// These are all the colors I use for the background of each pokemon card
const type_colors = {
normal:'#a8a878',
fire:'#f08030',
water:'#6890f0',
grass:'#78c850',
electric:'#f8d030',
ice:'#98d8d8',
fighting:'#c03028',
poison:'#a040a0',
ground:'#e0c068',
flying:'#a890f0',
psychic:'#f85888',
bug:'#a8b820',
rock:'#b8a038',
ghost:'#705898',
dark:'#705848',
dragon:'#7038f8',
steel:'#b8b8d0',
fairy:'#f0b6bc'
}
// Uses the pokeAPI to get a limited amount within an offset
const getAllPokemon = async (url, generation) => {
// Show the loading Pikachu
loading.style.display = 'block';
try {
const result = await fetch(url);
if (result.ok) {
const pokemon = await result.json();
const all_pokemon = pokemon.results.map((a_pokemon) => {
return getPokemon(a_pokemon.name);
});
promises = await Promise.all(all_pokemon);
number = 0;
promises.forEach(pokemon => {
if (current_generation == generation) {
createPokemonCard(pokemon);
}
});
} else {errorShake(`No results found for ${id}...`);}
} catch(e) {errorShake(e);}
// Hide the loading Pikachu
loading.style.display = 'none';
}
// Uses the pokeAPI to get a Pokemon
getPokemon = async (id) => {
const url = `https://pokeapi.co/api/v2/pokemon/${id}`;
try {
const result = await fetch(url);
if (result.ok) {
const pokemon = await result.json();
return Promise.resolve(pokemon);
} else {errorShake(`No results found for ${id}...`);}
} catch(e) {errorShake(e);}
}
// Shake the search bar when the search fails and show error message
function errorShake(error_text) {
search_error.textContent = error_text;
search_bar.classList.add('error_shake');
setTimeout(function() {
search_bar.classList.remove('error_shake');
}, 500);
}
// Waits for one second to see if the user stops typing,
// then searches for the Pokemon
const searchPokemon = async () => {
clearTimeout(timeout);
search_error.textContent = '';
timeout = setTimeout(function() {
const search_value = search_bar.value.toLowerCase();
const pokemon_cards = Array.from(pokemon_container.children);
let search_results = [];
if (search_value != '') {
for (let index = 0; index < promises.length; index++) {
// First search for names
if (promises[index].name.search(search_value) != -1) {
search_results.push(index);
}
// Then search for types
for (let type_index = 0; type_index < promises[index].types.length; type_index++)
if (promises[index].types[type_index].type.name.search(search_value) != -1) {
search_results.push(index);
}
}
// Clear the animation timeouts
anim_timeouts.forEach(anim_timeout => {
clearTimeout(anim_timeout);
});
// Next hide the cards
for (let index = 0; index < pokemon_cards.length; index++) {
if (pokemon_cards[index].classList.contains('move_card_up')) {
pokemon_cards[index].classList.remove('move_card_up');
pokemon_cards[index].style.animationPlayState = 'paused';
}
pokemon_cards[index].style.display = 'none';
}
anim_timeouts = [];
number = 0;
// Then show the few results
search_results.forEach(index => {
addMoveUpAnim(pokemon_cards[index]);
});
// If search bar is cleared, then show all pokemon in current gen
} else {
// Clear the animation timeouts
anim_timeouts.forEach(anim_timeout => {
clearTimeout(anim_timeout);
});
anim_timeouts = [];
number = 0;
pokemon_cards.forEach(pokemon_card => {
addMoveUpAnim(pokemon_card);
});
}
}, 1000);
}
function addMoveUpAnim(pokemon_card) {
pokemon_card.style.display = 'flex';
pokemon_card.classList.add('move_card_up');
// Add the animation to the cards
let anim_timeout = setTimeout(() => {
pokemon_card.style.animationPlayState = 'running';
pokemon_card.style.display = 'flex';
setTimeout(() => {
pokemon_card.classList.remove('move_card_up');
pokemon_card.style.animationPlayState = 'paused';
}, card_anim_speed);
}, number * 100);
anim_timeouts.push(anim_timeout);
number += 1;
}
function createPokemonCard(pokemon) {
const pokemon_card = document.createElement('div');
pokemon_card.classList.add('pokemon_card');
const types = pokemon.types.map(element => element.type.name);
let type_innerHTML = `<span class='type'>${types[0].toUpperCase()}</span>`;
if (types.length > 1) {
type_innerHTML += `<span class='type'>${types[1].toUpperCase()}</span>`;
}
const female_index = pokemon.species.name.search('-f')
const male_index = pokemon.species.name.search('-m')
let name = '';
// Very special case for the Nidoran family
if (female_index != -1 || male_index != -1) {
// Went for species name instead of pokemon.name because some Pokémon have difficult names to account for (eg #386)
if (female_index != -1) {
name = pokemon.species.name[0].toUpperCase() + pokemon.species.name.slice(1, -2) + '♀';
} else {
name = pokemon.species.name[0].toUpperCase() + pokemon.species.name.slice(1, -2) + '♂';
}
} else {
name = pokemon.species.name[0].toUpperCase() + pokemon.species.name.slice(1);
}
let sprite = pokemon['sprites']['versions']['generation-viii']['icons']['front_default'];
// Some Pokemon do not have a generation 8 sprite, replace with the ten questions pokemon
if (sprite == null) {
sprite = `images/unknown.png`;
}
// Calculate the total stats of the pokemon
let total_stats = 0;
pokemon['stats'].forEach(stat => {
total_stats += stat.base_stat;
});
let stats_innerHTML = `
<table class='stats'>
<tr>
<th>
<div class='stat_box hp'></div>
HP
</th>
<th>${pokemon['stats'][0].base_stat}</th>
<th>
<div class='stat_box sp_atk'></div>
SP. ATK
</th>
<th>${pokemon['stats'][3].base_stat}</th>
</tr>
<tr>
<th>
<div class='stat_box atk'></div>
ATK
</th>
<th>${pokemon['stats'][1].base_stat}</th>
<th>
<div class='stat_box sp_def'></div>
SP. DEF
</th>
<th>${pokemon['stats'][4].base_stat}</th>
</tr>
<tr>
<th>
<div class='stat_box def'></div>
DEF
</th>
<th>${pokemon['stats'][2].base_stat}</th>
<th>
<div class='stat_box speed'></div>
SPEED
</th>
<th>${pokemon['stats'][5].base_stat}</th>
</tr>
<tr>
<th class='total'>TOTAL</th>
<th>${total_stats}</th>
<th></th>
<th>
<a class='bulb_link' href='https://bulbapedia.bulbagarden.net/wiki/${name}_(Pokémon)' target='_blank'>
<img src='images/bulbapedia.png'>
</a>
</th>
</tr>
</table>
`;
const pokemon_innerHTML = `
<div class='pokemon_card_inner' onclick='flipCard(this)'>
<div class='pokemon_card_front'>
<div class='info'>
<div class='basic_info'>
<span class='number'>#${('000' + pokemon.id).slice (-3)}</span>
<div class='sprite_and_name'>
<div class='sprite_container'>
<img class='sprite' src='
${sprite}
'>
</div>
<h3 class='name'>${name}</h3>
</div>
</div>
<div class='types'>
${type_innerHTML}
</div>
</div>
<div class='img-container'>
<img src='${pokemon['sprites']['other']['official-artwork']['front_default']}'>
</div>
</div>
<div class='pokemon_card_back'>
${stats_innerHTML}
</div>
</div>
`;
pokemon_card.innerHTML = pokemon_innerHTML;
// Color the front and back with a gradient if more than one type is present
const pokemon_card_front = Array.from(Array.from(pokemon_card.children)[0].children)[0];
const pokemon_card_back = Array.from(Array.from(pokemon_card.children)[0].children)[1];
if (types.length > 1) {
pokemon_card_front.style = `background: -webkit-linear-gradient(
to right,
${type_colors[types[0]]},
${type_colors[types[1]]})`;
pokemon_card_back.style = `background: -webkit-linear-gradient(
to right,
${type_colors[types[0]]},
${type_colors[types[1]]})`;
pokemon_card_front.style = `background: linear-gradient(
to right,
${type_colors[types[0]]},
${type_colors[types[1]]})`;
pokemon_card_back.style = `background: linear-gradient(
to right,
${type_colors[types[0]]},
${type_colors[types[1]]})`;
} else {
pokemon_card_front.style = `background:${type_colors[types[0]]}`;
pokemon_card_back.style = `background:${type_colors[types[0]]}`;
}
pokemon_container.appendChild(pokemon_card);
addMoveUpAnim(pokemon_card);
}
function flipCard(card) {
if (card.style.transform == 'rotateX(180deg)') {
card.style.transform = '';
} else {
card.style.transform = 'rotateX(180deg)';
}
}
// Clear the search bar from previous visits
search_bar.value = '';
// Get the generations
getGenerations('https://pokeapi.co/api/v2/generation/');
// Get the first generation
getAllPokemon(`https://pokeapi.co/api/v2/pokemon/?limit=151&offset=0`, 1);