-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscripts.js
180 lines (150 loc) · 8.27 KB
/
scripts.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
let allLoadedPokemon = [];
let currentPokemon;
let loadCounter = 15;
let listOfPokemonNames = []; // loaded all names for search use
let isLoading = false; // don`t allow a new iteration before an existing one is finished
function render() {
displayOverview();
fillPokemonNames();
}
async function displayOverview() { // main card loading function
if (!isLoading) { // doesnt trigger if content is already being loaded into container
isLoading = true;
let content = document.getElementById('overview');
content.innerHTML = '';
await loadContent();
isLoading = false;
}
else { // retry if isLoading was still true
setTimeout(() => {
displayOverview();
}, 50);
}
}
async function loadContent() { // loader based on numeric
for (let i = 1; i < loadCounter; i++) { // load pokemon from api
let url = `https://pokeapi.co/api/v2/pokemon/${i}`;
let response = await fetch(url); //waiting so the function doesnt continue without this input
currentPokemon = await response.json(); // to json so we have a file type we can work with
allLoadedPokemon.push(currentPokemon);
renderPokemonOverview(); // builds the different pokecards
}
}
function renderPokemonOverview() {
let pokemonEntryBuild = ''; // predefined and empty so it can be filled later
let Types = currentPokemon["types"].length; // checking if one or two types are defined
if (Types < 2) { // use the builder for one or two skills, depending on Types.length
pokemonEntryBuild += cardGeneratorOne(pokemonEntryBuild)
}
else {
pokemonEntryBuild += cardGeneratorTwo(pokemonEntryBuild)
}
document.getElementById('overview').innerHTML += pokemonEntryBuild; // let the different elements be in place before you display it
}
async function fillPokemonNames() {
let url = `https://pokeapi.co/api/v2/pokemon/?limit=151`; // 151 is the number of first gen pokemon
let response = await fetch(url); //waiting so the function doesnt continue without this input
let responseJsonNames = await response.json(); // to json so we have a file type we can work with
for (let i = 0; i < responseJsonNames['results'].length; i++) {
let name = responseJsonNames['results'][i];
listOfPokemonNames.push(name);
}
}
async function searchPokemon() {
let search = document.getElementById('search').value;
let content = document.getElementById('overview');
content.innerHTML = '';
search = search.toLowerCase();
if (search == "") { // go back to the initial listing if no search is active
displayOverview();
return
}
if (!isLoading) {
isLoading = true;
await loadContentSearch(search);
isLoading = false; // sends the searched phrase to loader
}
else { // retry if isLoading was still true
setTimeout(() => {
searchPokemon();
}, 50);
}
}
async function loadContentSearch(search) { // loader based on word search (not numeric)
for (let i = 0; i < listOfPokemonNames.length; i++) {
let pokemon = listOfPokemonNames[i];
if (pokemon['name'].includes(search)) {
let url = `${pokemon['url']}`;
let response = await fetch(url);
currentPokemon = await response.json();
allLoadedPokemon.push(currentPokemon);
renderPokemonOverview();
}
}
}
async function loadPokemon(name) {
showCard();
let url = 'https://pokeapi.co/api/v2/pokemon/' + name; // to lowercase to make all characters small
let response = await fetch(url); // wait for the fetch
currentPokemon = await response.json(); //declare api/json to be used later
renderPokemonInfo(); // display pokedex on clicked pokemon for more details
}
function showCard() {
document.getElementById('pokedex').classList.remove('d-none');
}
function hideCard() {
document.getElementById('pokedex').classList.add('d-none');
}
function hideLoader() {
document.getElementById('loader-container').remove();
}
// when add pokemon is triggered, adds 15 more and renders
function addMorePokemon() {
loadCounter = loadCounter + 15;
displayOverview();
}
// when we are in the bottom of the page then load 15 pokemon more
window.onscroll = function () {
if (window.scrollY + window.innerHeight >= document.body.clientHeight) {
addMorePokemon();
}
};
// shows a variety of info on selected pokemon
function renderPokemonInfo() {
document.getElementById('pokemon-name').innerHTML = currentPokemon['name'].toUpperCase();
document.getElementById('img-pokedex').src = currentPokemon["sprites"]["front_default"]; //its the .img so src =
document.getElementById('skill-hp').innerHTML = currentPokemon["stats"][0]["base_stat"];
document.getElementById('skill-attack').innerHTML = currentPokemon["stats"][1]["base_stat"];
document.getElementById('skill-defence').innerHTML = currentPokemon["stats"][2]["base_stat"];
document.getElementById('skill-speed').innerHTML = currentPokemon["stats"][5]["base_stat"];
document.getElementById('type-1').innerHTML = currentPokemon["types"]["0"]["type"]["name"];
document.getElementById('id').innerHTML = '#' + currentPokemon["id"];
document.getElementById('height').innerHTML = 'Height:' + '         ' + currentPokemon["height"] * 10 + `cm`;
document.getElementById('weight').innerHTML = 'Weight:' + '         ' + currentPokemon["weight"] / 10 + `kg`;
document.getElementById('pokemon-name-container').classList.add('bg-${currentPokemon["types"]["0"]["type"]["name"]');
}
// returns html to function (1 skill)
function cardGeneratorOne(pokemonEntryBuild) {
return `
<div id="wrapper" onclick="loadPokemon('${currentPokemon['name']}')" class="card-wrapper bg-${currentPokemon["types"]["0"]["type"]["name"]}">
<img src="${currentPokemon["sprites"]["other"]["dream_world"]["front_default"]}" class="card-image " alt="${currentPokemon['name']}">
<div class="card-description">
<h2 class="card-title">${currentPokemon['name'].substring(0, 1).toUpperCase() + currentPokemon['name'].substring(1)} </h2>
<p class="card-text"> ${currentPokemon["types"]["0"]["type"]["name"]}</p>
<div class="card-id"><span class="id-card"># ${currentPokemon["id"]}</span></div>
</div>
</div>`
}
// returns html to function (2 skills)
function cardGeneratorTwo(pokemonEntryBuild) {
return `
<div id="wrapper" onclick="loadPokemon('${currentPokemon['name']}')" class="card-wrapper bg-${currentPokemon["types"]["0"]["type"]["name"]}">
<img src="${currentPokemon["sprites"]["other"]["dream_world"]["front_default"]}" class="card-image " alt="${currentPokemon['name']}">
<div class="card-description">
<h2 class="card-title">${currentPokemon['name'].substring(0, 1).toUpperCase() + currentPokemon['name'].substring(1)} </h2>
<p class="card-text"> ${currentPokemon["types"]["0"]["type"]["name"]}</p>
<p class="card-text">${currentPokemon["types"]["1"]["type"]["name"]}</p>
<div class="card-id"><span class="id-card"># ${currentPokemon["id"]}</span></div>
</div>
</div>`
}