-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
348 lines (326 loc) · 9.6 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
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
const pokemonName = document.querySelector(".pokemon-name");
const pokemonNumber = document.querySelector(".pokemon-number");
const pokemonImage = document.querySelector(".pokemon-image");
const INF = document.querySelector(".image-not-found");
const form = document.querySelector(".form");
const input = document.querySelector(".input-search");
const prev = document.querySelector(".btn-prev");
const next = document.querySelector(".btn-next");
const checkbox = document.getElementById("checkbox");
const cards = document.querySelector(".layout-cards");
const pokedex = document.querySelector(".layout-pokedex");
const vai = document.querySelector(".btn-nextCard");
const vem = document.querySelector(".btn-prevCard");
const cardP = document.querySelector(".grid-container");
const cardPI = document.querySelector(".dots");
const buttonsCard = document.querySelector(".buttonsCard");
const pokemonImageCard = document.querySelector(".pokemon-imageCard");
// const abbrPokemonName = document.querySelector(".abbr-pokemon-name");
const abbrPokemonName = document.createElement("abbr");
const PokemonData = document.querySelector(".pokemon-data");
let searchPokemon = 1;
checkbox.addEventListener("change", function () {
pokedex.classList.toggle("hide");
cards.classList.toggle("hide");
});
const fetchPokemon = async (pokemon) => {
const APIResponse = await fetch(
`https://pokeapi.co/api/v2/pokemon/${pokemon}`
);
if (APIResponse.status === 200) {
const data = await APIResponse.json();
return data;
}
};
const fetchPokemonURL = async (url) => {
const APIResponse = await fetch(url);
if (APIResponse.status === 200) {
const data = await APIResponse.json();
return data;
}
};
const renderPokemon = async (pokemon) => {
pokemonImage.src = "images/carregando.gif";
pokemonName.innerHTML = "Loading...";
INF.style.display = "none";
pokemonNumber.innerHTML = "";
const data = await fetchPokemon(pokemon);
if (data) {
pokemonImage.style.display = "block";
searchPokemon = data.id;
pokemonName.innerHTML = data.name;
if (data.name.length > 13) {
console.log(data.name);
abbrPokemonName.appendChild(pokemonName);
abbrPokemonName.setAttribute("title", data.name);
PokemonData.appendChild(abbrPokemonName);
console.log(abbrPokemonName);
} else {
abbrPokemonName.innerHTML = "";
PokemonData.appendChild(pokemonName);
}
pokemonNumber.innerHTML = data.id;
if (
data["sprites"]["versions"]["generation-v"]["black-white"]["animated"][
"front_default"
] === null
) {
if (data.id < 899) {
pokemonImage.src =
data["sprites"]["versions"]["generation-v"]["black-white"][
"front_default"
];
} else {
pokemonImage.src = "";
INF.style.display = "block";
}
} else {
pokemonImage.src =
data["sprites"]["versions"]["generation-v"]["black-white"]["animated"][
"front_default"
];
}
input.value = "";
} else {
pokemonName.innerHTML = "Not found";
pokemonNumber.innerHTML = "";
pokemonImage.style.display = "none";
searchPokemon = 0;
}
};
form.addEventListener("submit", (event) => {
event.preventDefault();
renderPokemon(input.value.toLowerCase());
input.value = "";
});
prev.addEventListener("click", () => {
if (searchPokemon > 1) {
searchPokemon--;
renderPokemon(searchPokemon);
} else if (searchPokemon === 0) {
searchPokemon = 905;
renderPokemon(searchPokemon);
}
});
next.addEventListener("click", () => {
if (searchPokemon < 905) {
searchPokemon++;
renderPokemon(searchPokemon);
} else if (searchPokemon > 905) {
searchPokemon = 1;
renderPokemon(searchPokemon);
}
});
const cores = (type) => {
let gridColor = [];
for (let i = 0; i < type.length; i++) {
switch (type[i]) {
case "normal":
gridColor.push("rgba(168, 168, 124, 1)");
break;
case "fighting":
gridColor.push("rgba(223, 9, 46, 1)");
break;
case "flying":
gridColor.push("rgba(172, 145, 236, 1)");
break;
case "poison":
gridColor.push("rgba(167, 64, 157, 1)");
break;
case "ground":
gridColor.push("rgba(227, 192, 113, 1)");
break;
case "rock":
gridColor.push("rgba(186, 160, 69, 1)");
break;
case "bug":
gridColor.push("rgba(166, 184, 58, 1)");
break;
case "ghost":
gridColor.push("rgba(115, 89, 149, 1)");
break;
case "steel":
gridColor.push("rgba(185, 184, 207, 1)");
break;
case "fire":
gridColor.push("rgba(248, 127, 61, 1)");
break;
case "water":
gridColor.push("rgba(102, 145, 236, 1)");
break;
case "grass":
gridColor.push("rgba(106, 200, 92, 1)");
break;
case "electric":
gridColor.push("rgba(251, 208, 74, 1)");
break;
case "psychic":
gridColor.push("rgba(255, 87, 136, 1)");
break;
case "ice":
gridColor.push("rgba(144, 216, 216, 1)");
break;
case "dragon":
gridColor.push("rgba(119, 59, 242, 1)");
break;
case "dark":
gridColor.push("rgba(114, 88, 74, 1)");
break;
case "fairy":
gridColor.push("rgba(245, 153, 172, 1)");
break;
}
}
return gridColor;
};
const coresPokebolas = (type) => {
let gridColor = [];
for (let i = 0; i < type.length; i++) {
switch (type[i]) {
case "normal":
gridColor.push("rgba(168, 168, 124, 0.5)");
break;
case "fighting":
gridColor.push("rgba(223, 9, 46, 0.5)");
break;
case "flying":
gridColor.push("rgba(172, 145, 236, 0.5)");
break;
case "poison":
gridColor.push("rgba(167, 64, 157, 0.5)");
break;
case "ground":
gridColor.push("rgba(227, 192, 113, 0.5)");
break;
case "rock":
gridColor.push("rgba(186, 160, 69, 0.5)");
break;
case "bug":
gridColor.push("rgba(166, 184, 58, 0.5)");
break;
case "ghost":
gridColor.push("rgba(115, 89, 149, 0.5)");
break;
case "steel":
gridColor.push("rgba(185, 184, 207, 0.5)");
break;
case "fire":
gridColor.push("rgba(248, 127, 61, 0.5)");
break;
case "water":
gridColor.push("rgba(102, 145, 236, 0.5)");
break;
case "grass":
gridColor.push("rgba(106, 200, 92, 0.5)");
break;
case "electric":
gridColor.push("rgba(251, 208, 74, 0.5)");
break;
case "psychic":
gridColor.push("rgba(255, 87, 136, 0.5)");
break;
case "ice":
gridColor.push("rgba(144, 216, 216, 0.5)");
break;
case "dragon":
gridColor.push("rgba(119, 59, 242, 0.5)");
break;
case "dark":
gridColor.push("rgba(114, 88, 74, 0.5)");
break;
case "fairy":
gridColor.push("rgba(245, 153, 172, 0.5)");
break;
}
}
return gridColor;
};
let numero = 0;
vai.addEventListener("click", () => {
numero = numero + 21;
viewCards();
window.scrollTo(0, 0);
});
vem.addEventListener("click", () => {
if (numero >= 21) {
numero = numero - 21;
viewCards();
window.scrollTo(0, 0);
}
});
const NumberFormat = (n) => {
if (n < 10) {
return `00${n}`;
} else if (n < 100) {
return `0${n}`;
} else {
return `${n}`;
}
};
const Pokebolas = () => {
// coresPokebolas();
let a;
return (a = `
<div class="geral">
<div class="pokebola tam1">
<div class="detalhe"></div>
</div>
</div>
`);
};
const viewCards = async () => {
cardPI.style.display = "block";
buttonsCard.style.display = "none";
let url = `https://pokeapi.co/api/v2/pokemon?offset=${numero}&limit=21`;
let data;
const APIResponse = await fetch(url);
if (APIResponse.status === 200) {
data = await APIResponse.json();
}
let a = "";
console.log("url", url);
if (data) {
for (let index = 0; index < data.results.length; index++) {
const data1 = await fetchPokemonURL(data.results[index].url);
const type = data1.types.map((t) => {
return t.type.name;
});
let cor = cores(type);
let img =
data1["sprites"]["versions"]["generation-v"]["black-white"][
"front_default"
];
// console.log(data1);
a =
a +
// `<div onmouseover="overCard(${index})" onmouseout="outCard( ${index})" class="grid-item" style="background: linear-gradient(to bottom, ${
// cor[0]
// }, #F0F0F0, ${cor.length === 2 ? cor[1] : "#F0F0F0"});">
`<div class="grid-item" style="background: linear-gradient(${
cor[0]
} 0%,#FFFFFF 45%, #FFFFFF 55%, ${
cor.length === 2 ? cor[1] : "#FFFFFF"
} 100%); ">
<div class="div-numPokemon">
<img src="images/pokebola.png" class="pokebola-mini" alt="Pokemon"/>
<p class="numPokemon">${NumberFormat(data1.id)}</p>
</div>
<div class="type">
<p class="p-tipo">${type[0]}</p>
<p class=${type.length == 2 ? "p-tipo" : ""}>${
type.length == 2 ? type[1] : ""
}</p>
</div>
<div class="img-info">
<p class="p-titulo">${data1.name}</p>
<img id="${index}" src="${img}" class="pokemon-imageCard" alt="Pokemon"/><br/>
</div>
</div>`;
}
buttonsCard.style.display = "flex";
cardPI.style.display = "none";
cardP.innerHTML = a;
}
};
viewCards();
renderPokemon(searchPokemon);