-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
229 lines (205 loc) · 6.62 KB
/
app.js
File metadata and controls
229 lines (205 loc) · 6.62 KB
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
// DOM Objects (Document Object Model)
const mainScreen = document.querySelector('.main-screen');
// Pokemon Name
const pokeName = document.querySelector('.poke-name');
// Poke Dex ID
const pokeId = document.querySelector('.poke-id');
// front image
const pokeFrontImage = document.querySelector('.poke-front-image');
// back image
const pokeBackImage = document.querySelector('.poke-back-image');
// Type Of Pokemon (element type; pokemon can have one or two!)
const pokeTypeOne = document.querySelector('.poke-type-one');
const pokeTypeTwo = document.querySelector('.poke-type-two');
// Weight
const pokeWeight = document.querySelector('.poke-weight');
// Height
const pokeHeight = document.querySelector('.poke-height');
// List Of pokemon (20 at a time)
const pokeListItems = document.querySelectorAll('.list-item');
// PokeDex Previous List Button
const dexPrev = document.querySelector('.left-button');
// PokeDex Next List Button
const dexNext = document.querySelector('.right-button');
// Switch Between Normal and shiny versions
const shinySwitch = document.querySelector('.shiny');
const rightPad = document.querySelector('.right');
const leftPad = document.querySelector('.left');
const midPad = document.querySelector('.middle');
const aButton = document.querySelector('.a-button');
// Element Types / variables
const TYPES = [
'normal', 'fighting', 'flying', 'poison', 'ground',
'rock', 'bug', 'ghost', 'steel', 'fire', 'water',
'grass', 'electric', 'psychic', 'ice', 'dragon',
'dark', 'fairy'
];
var nextUrl = null;
var prevUrl = null;
var shySwitch = 0;
var pokeCurr = null;
// Functions
function capitalize(str) {
return str[0].toUpperCase() + str.slice(1);
}
function resetScreen() {
mainScreen.classList.remove('hide');
for (const type of TYPES) {
mainScreen.classList.remove(type);
}
}
// Right PokeDex Screen
function fetchPokeList(url) {
fetch(url)
.then(res => res.json())
.then(data => {
const { results, previous, next } = data;
nextUrl = next;
prevUrl = previous;
console.log(results);
for (var i = 0; i < pokeListItems.length; i++) {
const pokeListItem = pokeListItems[i];
const resultData = results[i];
if (resultData) {
const { name, url } = resultData;
const urlArray = url.split('/');
const id = urlArray[urlArray.length - 2];
pokeListItem.textContent = id + '. ' + capitalize(name);
} else {
pokeListItem.textContent = '';
}
}
});
}
// Left PokeDex Screen
function fetchPokeMon(id) {
fetch(`https://pokeapi.co/api/v2/pokemon/${id}`)
.then(res => {
return res.json();
})
.then(data => {
resetScreen();
console.log(data);
mainScreen.classList.remove('hide');
// Type Of Pokemon (element type)
const datatype = data['types'];
const dataFirstType = datatype[0];
const dataSecondType = datatype[1];
pokeTypeOne.textContent = capitalize(dataFirstType['type']['name']);
// If the Pokemon has A second Element type
if (dataSecondType) {
pokeTypeTwo.classList.remove('hide');
pokeTypeTwo.textContent = capitalize(dataSecondType['type']['name']);
} else {
pokeTypeTwo.classList.add('hide');
pokeTypeTwo.textContent = '';
}
// Screen Color Of Element
mainScreen.classList.add(dataFirstType['type']['name']);
// Pokemon Details
// Name
pokeName.textContent = capitalize(data['name']);
// Poke Dex ID
pokeId.textContent = '#' + data['id'].toString().padStart(3, '0');
// Weight
pokeWeight.textContent = data['weight'] + " hg";
// Height
pokeHeight.textContent = data['height'] + " dm";
// front image
if (data['sprites']['front_default'] || data['sprites']['front_shiny']) {
pokeFrontImage.classList.remove('hide');
if (shySwitch == 0) {
pokeFrontImage.src = data['sprites']['front_default'];
} else {
if (data['sprites']['front_shiny']) {
pokeFrontImage.src = data['sprites']['front_shiny'];
} else {
pokeBackImage.classList.add('hide');
}
}
} else {
pokeFrontImage.classList.add('hide');
}
// back image
if (data['sprites']['back_default'] || data['sprites']['back_shiny']) {
pokeBackImage.classList.remove('hide');
if (shySwitch == 0) {
pokeBackImage.src = data['sprites']['back_default'];
} else {
if (data['sprites']['back_shiny']) {
pokeBackImage.src = data['sprites']['back_shiny'];
} else {
pokeBackImage.classList.add('hide');
}
}
} else {
pokeBackImage.classList.add('hide');
}
});
}
function nextClick() {
if (nextUrl) {
fetchPokeList(nextUrl);
}
};
function prevClick() {
if (prevClick) {
fetchPokeList(prevUrl);
}
};
function listClick(e) {
if (!e.target) {
return;
}
const listItem = e.target;
if (!listItem.textContent) {
return;
}
const id = listItem.textContent.split('.')[0];
pokeCurr = id;
fetchPokeMon(id);
};
function switchShy(e) {
console.log(e);
console.log(shySwitch);
if (shySwitch == 0) {
shySwitch++;
shinySwitch.classList.add('grass');
console.log(shySwitch);
} else {
shySwitch--;
shinySwitch.classList.remove('grass');
console.log(shySwitch);
}
fetchPokeMon(pokeCurr);
};
function nextPoke() {
// console.log("right!");
if (pokeCurr != 10220) {
pokeCurr++;
fetchPokeMon(pokeCurr);
}
}
function prevPoke() {
// console.log("left!");
if (pokeCurr != 1) {
pokeCurr--;
fetchPokeMon(pokeCurr);
}
}
function dexOn() {
console.log("!");
}
// Events
dexNext.addEventListener('click', nextClick);
dexPrev.addEventListener('click', prevClick);
for (const pokeListItem of pokeListItems) {
pokeListItem.addEventListener('click', listClick);
}
shinySwitch.addEventListener('click', switchShy);
rightPad.addEventListener('click', nextPoke);
leftPad.addEventListener('click', prevPoke);
// midPad.addEventListener('click', shySwitch);
aButton.addEventListener('click', dexOn);
// Initializing App
fetchPokeList('https://pokeapi.co/api/v2/pokemon');