-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
363 lines (313 loc) · 12.3 KB
/
main.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
let cards = [];
let param = "";
let selectOption = 'name'
let currentPage = 1;
let totalCount = 0;
function rotateCard(event) {
const card = this;
const cardRect = card.getBoundingClientRect();
const mouseX = event.pageX - cardRect.left;
const mouseY = event.pageY - cardRect.top;
const halfWidth = cardRect.width / 2;
const halfHeight = cardRect.height / 2;
const rotateX = -(mouseY - halfHeight) / 10;
const rotateY = -(halfWidth - mouseX) / 10;
card.style.transform = `perspective(1000px) rotateX(${rotateX}deg) rotateY(${rotateY}deg)`;
}
// function to reset the card rotation
function resetCard() {
const card = this
card.style.transform = 'perspective(1000px) rotateX(0) rotateY(0)';
}
function showImages(event, currentPage) {
const imageContainer = document.getElementById("image-container");
const loadingElement = document.getElementById("loading");
while (imageContainer.firstChild) {
imageContainer.removeChild(imageContainer.firstChild);
}
const pokemon = document.getElementById("input");
let param = pokemon.value;
if (!param) {
return;
}
const exportbtn = document.getElementById("exportExcell");
exportbtn.onclick = async function () {
loadingElement.style.display = "block";
const response = await createExcel();
loadingElement.style.display = "none";
const blob = new Blob([response.body], { type: response.headers['Content-Type'] });
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = document.getElementById("input").value + '.xlsx';;
link.click();
}
const loadingGif = document.getElementById("loading-gif");
loadingElement.style.display = "block";
const lupe = document.getElementsByClassName("fas fa-search")[0];
event.preventDefault();
loadingGif.style.display = "inline-block";
lupe.style.display = "none";
param = "\"" + param + '*' + "\""
const url = "https://api.pokemontcg.io/v2/cards?q=" + selectOption + ":" + param + "&page=" + currentPage;
const headers = {
"x-api-key": "46b0dc2d-5668-4467-93f7-acfd30d2c085",
};
let responsePrincipal = fetch(url, {
headers,
})
.then((response) => response.json())
.then((data) => {
// Add a "count" attribute to each card object and initialize it to 0
data.data.forEach((card) => {
card.count = 0;
});
cards = data.data;
totalCount = data.totalCount;
if (totalCount > 250) {
paging.style.display = 'block';
}
const cardContainer = document.createElement("div");
cardContainer.id = "card-container";
document.getElementById("image-container").appendChild(cardContainer);
// Create a card and count input box for each card in the response
data.data.forEach((card) => {
const cardDiv = document.createElement("div");
cardDiv.classList.add("cardBox");
const nameAndCountContainer = document.createElement("div");
nameAndCountContainer.classList.add("nameAndCountContainer");
const name = document.createElement("h2");
name.textContent = card.name
name.classList.add("card-name");
const code = document.createElement("h3");
code.textContent = "(" + card.number + '/' + card.set.printedTotal + ")";
code.classList.add("code-name");
const countInput = document.createElement("input");
countInput.type = "number";
countInput.id = card.id;
countInput.min = 0;
countInput.value = 0;
countInput.step = 1;
countInput.classList.add("count-input");
const incrementBtn = document.createElement("button");
incrementBtn.textContent = "+";
incrementBtn.classList.add("count-btn", "count-increment");
incrementBtn.addEventListener("click", () => {
countInput.stepUp();
card.count = parseInt(countInput.value);
});
const decrementBtn = document.createElement("button");
decrementBtn.textContent = "-";
decrementBtn.classList.add("count-btn", "count-decrement");
decrementBtn.addEventListener("click", () => {
countInput.stepDown();
card.count = parseInt(countInput.value);
});
const countWrapper = document.createElement("div");
countWrapper.classList.add("count-wrapper");
const btnWrapper = document.createElement("div");
btnWrapper.classList.add("btn-wrapper");
btnWrapper.appendChild(decrementBtn);
btnWrapper.appendChild(incrementBtn);
countWrapper.appendChild(countInput);
countWrapper.appendChild(btnWrapper);
nameAndCountContainer.appendChild(name);
nameAndCountContainer.appendChild(code);
nameAndCountContainer.appendChild(countWrapper);
const img = document.createElement("img");
img.classList.add("cards");
img.src = card.images.small;
img.addEventListener('mousemove', rotateCard);
img.addEventListener('mouseout', resetCard);
cardDiv.appendChild(nameAndCountContainer);
cardDiv.appendChild(img);
cardContainer.appendChild(cardDiv);
});
lupe.style.display = "inline-block";
loadingGif.style.display = "none";
loadingElement.style.display = "none";
if (data.data.length == 0) {
showSnackbar("No result for this query.");
} else {
showSnackbar("All cards loaded!");
exportbtn.style.display = "inline-block";
}
})
.catch((error) => {
console.error("Error:", error);
lupe.style.display = "inline-block";
loadingGif.style.display = "none";
showSnackbar("Error loading cards");
});
};
window.onload = function () {
var width = screen.width;
var height = screen.height;
const nameRadio = document.getElementById('name');
const artistRadio = document.getElementById('artist');
const setRadio = document.getElementById('set');
const input = document.getElementById('input');
const paging = document.getElementById('paging')
const prevBtn = document.getElementById("prev");
const nextBtn = document.getElementById("next");
paging.style.display = 'none';
prevBtn.addEventListener('click', () => {
currentPage -= 1;
showImages(event, currentPage);
});
nextBtn.addEventListener('click', () => {
currentPage += 1;
showImages(event, currentPage);
});
nameRadio.addEventListener('click', () => {
selectOption = 'name'
input.style.display = 'flex';
});
artistRadio.addEventListener('click', () => {
selectOption = 'artist'
input.style.display = 'flex';
});
setRadio.addEventListener('click', () => {
selectOption = 'set.name'
input.style.display = 'flex';
});
const bg = document.getElementById("bg");
const bgImage = {
small: [
"images/image1small.jpg",
"images/image2small.jpg",
"images/image3small.jpg",
],
medium: [
"images/image1medium.jpg",
"images/image2medium.jpg",
"images/image3medium.jpg",
],
big: [
"images/image1big.jpg",
"images/image2big.jpg",
"images/image3big.jpg",
],
};
function setRandomImage() {
const screenWidth = window.innerWidth;
let imagesArray;
if (screenWidth <= 640) {
imagesArray = bgImage.small;
} else if (screenWidth > 640 && screenWidth <= 1024) {
imagesArray = bgImage.medium;
} else {
imagesArray = bgImage.big;
}
const randomIndex = Math.floor(Math.random() * imagesArray.length);
bg.style.backgroundImage = `url(${imagesArray[randomIndex]})`;
}
setRandomImage();
};
async function createExcel() {
if (!cards) {
console.error("cards is not available yet");
showSnackbar("Error loading cards");
return;
}
if (typeof cards !== "object" || !Array.isArray(cards)) {
console.error("cards is not an array");
showSnackbar("Error loading cards");
return;
}
const imageUrls = cards.map((item) => item.images.small);
const imageBuffers = await Promise.all(
imageUrls.map(async (url) => {
const response = await fetch(url, { mode: 'no-cors' });
const buffer = await response.arrayBuffer();
return new Uint8Array(buffer);
})
);
const filteredData = cards.map((item) => {
let flavorText = item.flavorText;
if (flavorText === null || flavorText === undefined) {
if (Array.isArray(item.rules)) {
flavorText = item.rules.join(" ");
console.log(flavorText);
} else {
flavorText = "";
}
}
return {
ID: item.id,
Name: item.name,
"Number (First number in the bottom of the card)": item.number,
"Total Printed (second number in the bottom of the card)": item.set.printedTotal,
Artist: item.artist,
Rarity: item.rarity,
"Flavor Text": flavorText,
Set: item.set.name,
Count: item.count,
};
});
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet(document.getElementById("input").value);
worksheet.columns = [
{ header: "ID", key: "ID", width: 15 },
{ header: "Name", key: "Name", width: 25 },
{
header: "Nº Right",
key: "Number (First number in the bottom of the card)",
width: 10,
},
{
header: "Nº Left",
key: "Total Printed (second number in the bottom of the card)",
width: 10,
},
{ header: "Artist", key: "Artist", width: 25 },
{ header: "Rarity", key: "Rarity", width: 25 },
{ header: "Flavor Text", key: "Flavor Text", width: 25 },
{ header: "Set", key: "Set", width: 25 },
{ header: "Count", key: "Count", width: 10 },
{ header: "Image", key: "Image", width: 10 },
];
worksheet.getRow(1).eachCell((cell) => {
cell.font = { bold: true };
});
filteredData.forEach((item, index) => {
worksheet.addRow(item);
const imageId = workbook.addImage({
buffer: imageBuffers[index],
extension: "png",
});
worksheet.addImage(imageId, {
tl: { col: 9, row: index + 1 }, // Position the image in the cell
br: { col: 12, row: index + 2 }, // End position of the image
editAs: "oneCell",
});
const row = worksheet.getRow(index + 2);
row.height = 250; // Increase the row height to make the cell and image bigger
});
worksheet.eachRow((row) => {
row.eachCell((cell) => {
cell.alignment = {
vertical: 'middle',
};
});
});
const buffer = await workbook.xlsx.writeBuffer();
const fileName = document.getElementById("input").value + ".xlsx";
const response = {
headers: {
'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'Content-Disposition': `attachment; filename=${fileName}`
},
statusCode: 200,
body: buffer
};
showSnackbar("Exported!");
return response;
}
function showSnackbar(mesage) {
var snackbar = document.getElementById("snackbar");
snackbar.textContent = mesage;
snackbar.className = "show";
setTimeout(function () {
snackbar.className = snackbar.className.replace("show", "");
}, 3000);
}