-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgallery2.html
More file actions
232 lines (195 loc) · 4.58 KB
/
gallery2.html
File metadata and controls
232 lines (195 loc) · 4.58 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
230
231
232
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>replit</title>
<style>
* {
box-sizing: border-box;
}
/* On mouse-over, add a deeper shadow */
.card:hover {
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
}
html,
body {
height: 100vh;
margin: 0;
width: 100vw;
}
body {
display: flex;
flex-direction: column;
background-color: gainsboro;
}
nav {
background-color: blueviolet;
flex-grow: 1;
}
nav>h1 {
text-align: center;
color: white;
margin-bottom: 0;
}
article {
display: flex;
justify-content: flex-start;
padding: 10px;
align-items: center;
gap: 40px;
height: 60px;
}
button {
border: none;
box-shadow: -1px 10px 11px -7px rgba(0, 0, 0, 0.65);
height: 25px;
width: 120px;
border-radius: 10px;
color: white;
background-color: blueviolet;
}
button:hover {
background-color: white;
color: blueviolet;
text-decoration: 1px solid blueviolet;
}
button:disabled {
background-color: dimgrey;
color: linen;
opacity: 1;
}
button:disabled:hover {
background-color: dimgrey;
color: linen;
opacity: 1;
}
main {
display: flex;
flex-direction: row;
padding: 5px;
flex-grow: 10;
max-height: 80vh;
}
section {
padding: 5px;
display: flex;
flex-grow: 8;
flex-wrap: wrap;
gap: 10px;
align-items: center;
flex-direction: row;
align-items: center;
max-height: 100%;
overflow-y: scroll;
}
aside {
flex-grow: 3;
min-width: 300px;
padding: 10px;
gap: 10px;
display: flex;
flex-direction: column;
max-height: 100%;
overflow-y: scroll;
overflow-x: hidden;
}
.item {
height: 100px;
width: 100%;
background-color: white;
border-radius: 10px;
padding: 5px;
text-align: left;
}
.card{
padding: 5px;
text-align: center;
background-color: white;
border-radius: 15px;
flex-grow: 1;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
}
</style>
</head>
<body>
<nav>
<h1>Poke Gallery</h1>
</nav>
<main>
<section id="result">
</section>
<aside id="favourites">
</aside>
</main>
<article>
<button id="prev" onclick="getDetails(-8)">Prev</button>
<button id="next" onclick="getDetails(8)">Next</button>
</article>
</body>
<script>
let result = document.querySelector('#result');
let next = document.querySelector('#next');
let prev = document.querySelector('#prev');
let curIndex = 1;
let favourites = [];
let data = [];
function showLoading() {
result.innerHTML = "<h1 style='text-align: center'>Loading...</h1>";
next.disabled = true;
prev.disabled = true;
}
function addFavourite(id) {
let item = data.find(rec => rec.id == id);
favourites.push(item);
renderFavourites()
}
function removeFavourite(id) {
let index = favourites.findIndex(rec => rec.id == id);
favourites.splice(index, 1);
renderFavourites()
}
function renderPokemon() {
let html = '';
let result = document.querySelector('#result');
for (let rec of data) {
html += `<div class="card">
<img style="height:100px; width:100px" src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${rec.id}.png" alt="Avatar" style="width:100%">
<p>Name: ${rec.name}</p>
<button onclick="addFavourite(${rec.id})">Add to Favourites</button>
</div>`;
}
result.innerHTML = html;
//make buttons clickable again
next.disabled = false;
prev.disabled = false;
}
function renderFavourites() {
let favourites_list = document.querySelector('#favourites');
let html = "";
for (let rec of favourites) {
html += `
<div class="item">
<p>Pokemon: ${rec.name}</p>
<button onclick="removeFavourite(${rec.id})">Remove</button>
</div>
`;
}
favourites_list.innerHTML = html;
}
async function getDetails(offset) {
curIndex += offset;
let end = curIndex + 8;
let item, response;
data = [];
showLoading();
for (let i = curIndex; i < end; i++) {
response = await fetch(`https://pokedextr.uwista.dev/pokemon/${i}`);
item = await response.json();
data.push(item);
}
renderPokemon();
}
getDetails(0);
</script>
</html>