-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
204 lines (178 loc) · 6.02 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
const pokeImages = document.querySelector(".pokeImages");
const pokeName = document.querySelector(".pokeName");
const pokeTypeOne = document.querySelector("#pokeTypeOne");
const pokeTypeTwo = document.querySelector("#pokeTypeTwo");
const pokeWeight = document.querySelector(".pokeWeight");
const pokePlatform = document.querySelector(".pokePlatform");
const pokeBtn = document.getElementById("pokeBtn").addEventListener("click", () => {
enterValue();
clearValue();
});
let input = document.getElementById("inputSearch");
input.addEventListener("keyup", function (event) {
if (event.key === "Enter") {
event.preventDefault();
document.getElementById("pokeBtn").click();
}
});
function enterValue() {
let pokeSearch = document.getElementById("inputSearch").value.trim().toLowerCase();
let pokeSearchReady = pokeSearch.replaceAll(" ", "-");
if (pokeSearch === "") {
Swal.fire({
title: "Please enter a pokemon name...",
width: 600,
padding: "3em",
heightAuto: false,
color: "#fff",
background: "#fff url(./images/alertBackground.png)",
backdrop: `rgba(0,0,123,0.4)
url("./images/pikachugif.gif")
left bottom
no-repeat`,
});
return;
} else {
pokedex();
UIDisplay();
}
async function pokedex() {
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${pokeSearchReady}`);
const allPokedex = await response.json();
return allPokedex;
}
pokedex()
.then((allPokedex) => {
getPokeImages(allPokedex);
let pokeBaseStats = [];
for (let pokeStatsData of allPokedex.stats) {
pokeBaseStats.push(pokeStatsData.base_stat);
}
let pokeTypes = [];
for (let testtype of allPokedex.types) {
pokeTypes.push(testtype.type.name);
}
if (pokeTypes.length === 2) {
pokeTypeOne.removeAttribute("class");
pokeTypeTwo.removeAttribute("class");
var typesOne = pokeTypes[0];
var typesTwo = pokeTypes[1];
pokeTypeOne.innerHTML = typesOne[0].toUpperCase() + typesOne.substring(1);
pokeTypeTwo.innerHTML = typesTwo[0].toUpperCase() + typesTwo.substring(1);
pokeTypeOne.classList.add(typesOne);
pokeTypeTwo.classList.add(typesTwo);
} else {
pokeTypeOne.removeAttribute("class");
pokeTypeTwo.removeAttribute("class");
pokeTypeTwo.innerHTML = "";
let typesOne = pokeTypes[0];
pokeTypeOne.innerHTML = typesOne[0].toUpperCase() + typesOne.substring(1);
pokeTypeOne.classList.add(typesOne);
}
// UI
let getPokemonName = allPokedex.name;
let pokeNameSplit = getPokemonName.split("-");
for (let i = 0; i < pokeNameSplit.length; i++) {
pokeNameSplit[i] = pokeNameSplit[i][0].toUpperCase() + pokeNameSplit[i].substr(1);
}
let pokemonNameUpperCase = pokeNameSplit.join(" ");
pokeName.innerHTML = `<span class="weightColor">Name: </span>` + pokemonNameUpperCase;
pokeWeight.innerHTML = `<span class="weightColor">Weight: </span>` + allPokedex.weight;
// Chart.js Data update
myChart.config.data.datasets[0].data = pokeBaseStats;
myChart.update();
})
.catch(() => {
pokemonNotFound();
myChart.config.data.datasets[0].data = [null, null, null, null, null, null];
myChart.update();
});
}
// #################### Poke images ####################
function getPokeImages(allPokedex) {
let pokeNumber = allPokedex.id;
console.log(pokeNumber);
pokeImages.innerHTML = `<img src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/official-artwork/${pokeNumber}.png" />`;
pokePlatform.innerHTML = `<img src="./images/platform.png" />`;
}
function pokemonNotFound() {
pokeTypeOne.removeAttribute("class");
pokeTypeTwo.removeAttribute("class");
pokeTypeOne.innerHTML = "";
pokeTypeTwo.innerHTML = "";
pokeImages.innerHTML = '<img src="./images/questionmark.png" />';
pokePlatform.innerHTML = `<img src="./images/platform.png" />`;
pokeName.innerHTML = "Name: ---";
pokeWeight.innerHTML = "Weight: ---";
}
function clearValue() {
inputSearch.value = "";
}
function UIDisplay() {
document.getElementById("pokeGraphicDisplay").style.display = "block";
document.getElementById("infoDisplay").style.display = "flex";
}
// #################### Chart.JS ####################
const ctx = document.getElementById("myChart");
const myChart = new Chart(ctx, {
type: "polarArea",
data: {
labels: ["Hp", "Attack", "Defance", "Special-Attack", "Special-Defance", "Speed"],
datasets: [
{
data: [null, null, null, null, null, null],
backgroundColor: [
"rgba(255, 99, 132, 0.6)",
"rgba(54, 162, 235, 0.6)",
"rgba(255, 206, 86, 0.6)",
"rgba(75, 192, 192, 0.6)",
"rgba(153, 102, 255, 0.6)",
"rgba(255, 159, 64, 0.6)",
],
borderColor: [
"rgba(255, 99, 132, 1)",
"rgba(54, 162, 235, 1)",
"rgba(255, 206, 86, 1)",
"rgba(75, 192, 192, 1)",
"rgba(153, 102, 255, 1)",
"rgba(255, 159, 64, 1)",
],
borderWidth: 2,
},
],
},
options: {
plugins: {
datalabels: {
color: "red",
},
legend: {
labels: {
color: "white",
font: {
size: 13,
},
},
},
},
scales: {
r: {
max: 255,
ticks: {
backdropColor: "rgba(255, 255, 255, 0)",
color: "white",
stepSize: 50,
},
},
},
},
});
// #################### Tippy.JS ####################
tippy("#tippyJS", {
content: 'When searching for a custom form, search for <br/> "name mega x/y" or "name gmax"!',
arrow: true,
placement: "left",
animation: "fade",
allowHTML: true,
theme: "light",
});