-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbrowse.html
More file actions
197 lines (175 loc) · 6.27 KB
/
Copy pathbrowse.html
File metadata and controls
197 lines (175 loc) · 6.27 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Browse</title>
<style>
* { box-sizing: border-box; }
body { font-family: sans-serif; margin: 0; }
nav { background-color: blueviolet; padding: 10px; display: flex; align-items: center; gap: 15px; }
nav a { color: white; text-decoration: none; border: 2px solid white; padding: 5px 14px; text-align: center; }
nav h1 { color: white; margin: 0; flex: 1; }
nav span { color: white; }
main { display: flex; height: calc(100vh - 80px); }
#content-area {
flex: 1;
display: flex;
flex-wrap: wrap;
gap: 10px;
padding: 15px;
align-content: flex-start;
overflow-y: auto;
}
.card {
border: 1px solid #ccc;
padding: 10px;
width: 140px;
text-align: center;
background: white;
}
.card img { width: 80px; height: 80px; }
.card p { margin: 3px 0; font-size: 0.9rem; text-transform: capitalize; }
.card button { margin-top: 5px; padding: 3px 8px; cursor: pointer; }
#bookmarks {
width: 240px;
border-left: 1px solid #ccc;
padding: 10px;
overflow-y: auto;
}
#bookmarks h2 { margin: 0 0 10px 0; font-size: 1rem; }
.bookmark-item { border: 1px solid #eee; padding: 8px; margin-bottom: 6px; }
.bookmark-item img { width: 40px; height: 40px; vertical-align: middle; }
.pagination { display: flex; gap: 8px; padding: 10px; align-items: center; background: #f5f5f5; flex-wrap: wrap; }
.pagination button, .page-btn { padding: 5px 10px; cursor: pointer; border: 1px solid #ccc; background: white; }
.page-btn.active { background: blueviolet; color: white; border-color: blueviolet; }
.pagination button:disabled { opacity: 0.5; }
</style>
</head>
<body>
<nav>
<a href="index.html">Home</a>
<h1>Browse</h1>
<span id="bookmark-count">0 Bookmarked</span>
</nav>
<main>
<div id="content-area">Loading...</div>
<div id="bookmarks">
<h2>Bookmarks</h2>
<button onclick="clearBookmarks()">Clear All</button>
<div id="bookmarks-list"><p>None yet.</p></div>
</div>
</main>
<div class="pagination">
<button onclick="goToPage(currentPage - 1)" id="prev-btn">Previous</button>
<div id="page-numbers"></div>
<button onclick="goToPage(currentPage + 1)" id="next-btn">Next</button>
</div>
<script>
let allPokemon = [];
let bookmarks = [];
let currentPage = 1;
let itemsPerPage = 12;
function renderContent() {
const content = document.querySelector('#content-area');
const start = (currentPage - 1) * itemsPerPage;
const pageData = allPokemon.slice(start, start + itemsPerPage);
let html = '';
for (let pokemon of pageData) {
const isBookmarked = bookmarks.some(b => b.id === pokemon.id);
html += `<div class="card">
<img src="${pokemon.image}" alt="${pokemon.name}">
<p><strong>${pokemon.name}</strong></p>
<p>${pokemon.type1}${pokemon.type2 ? ' / ' + pokemon.type2 : ''}</p>
<button onclick="toggleBookmark(${pokemon.id})">${isBookmarked ? '★ Remove' : '☆ Save'}</button>
</div>`;
}
content.innerHTML = html;
updatePagination();
}
function renderBookmarks() {
const list = document.querySelector('#bookmarks-list');
const count = document.querySelector('#bookmark-count');
count.textContent = `${bookmarks.length} Bookmarked`;
if (bookmarks.length === 0) {
list.innerHTML = '<p>None yet.</p>';
return;
}
let html = '';
for (let pokemon of bookmarks) {
html += `<div class="bookmark-item">
<img src="${pokemon.image}" alt="${pokemon.name}">
<span style="text-transform:capitalize">${pokemon.name}</span>
<button onclick="removeBookmark(${pokemon.id})">Remove</button>
</div>`;
}
list.innerHTML = html;
}
function toggleBookmark(id) {
const pokemon = allPokemon.find(p => p.id === id);
if (!pokemon) return;
const index = bookmarks.findIndex(b => b.id === id);
if (index === -1) {
bookmarks.push(pokemon);
} else {
bookmarks.splice(index, 1);
}
renderContent();
renderBookmarks();
}
function removeBookmark(id) {
const index = bookmarks.findIndex(b => b.id === id);
if (index !== -1) {
bookmarks.splice(index, 1);
renderContent();
renderBookmarks();
}
}
function clearBookmarks() {
bookmarks = [];
renderContent();
renderBookmarks();
}
function updatePagination() {
const totalPages = Math.ceil(allPokemon.length / itemsPerPage);
const pageNumbers = document.querySelector('#page-numbers');
const prevBtn = document.querySelector('#prev-btn');
const nextBtn = document.querySelector('#next-btn');
prevBtn.disabled = currentPage === 1;
nextBtn.disabled = currentPage === totalPages;
let html = '';
let startPage = Math.max(1, currentPage - 2);
let endPage = Math.min(totalPages, currentPage + 2);
if (startPage > 1) {
html += `<button class="page-btn" onclick="goToPage(1)">1</button>`;
if (startPage > 2) html += `<span>...</span>`;
}
for (let i = startPage; i <= endPage; i++) {
html += `<button class="page-btn ${i === currentPage ? 'active' : ''}" onclick="goToPage(${i})">${i}</button>`;
}
if (endPage < totalPages) {
if (endPage < totalPages - 1) html += `<span>...</span>`;
html += `<button class="page-btn" onclick="goToPage(${totalPages})">${totalPages}</button>`;
}
pageNumbers.innerHTML = html;
}
function goToPage(page) {
const totalPages = Math.ceil(allPokemon.length / itemsPerPage);
if (page < 1 || page > totalPages) return;
currentPage = page;
renderContent();
}
async function loadPokemon() {
for (let i = 1; i <= 50; i++) {
const response = await fetch(`https://pokedextr.uwista.dev/pokemon/${i}`);
const data = await response.json();
allPokemon.push(data);
if (i % 12 === 0 || i === 1) renderContent();
}
renderContent();
renderBookmarks();
}
loadPokemon();
</script>
</body>
</html>