-
Notifications
You must be signed in to change notification settings - Fork 24
/
app.js
378 lines (322 loc) · 13.8 KB
/
app.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Set the current page and items per page
let currentPage = 1;
const itemsPerPage = 500;
const apiBaseUrl = 'https://stampchain.io/api/v2/stamps';
let totalNumberOfStamps = 0;
function simpleValidateAddress(address) {
return /^1|^3|^bc1q/.test(address);
}
function isValidCpid(cpid) {
return /^A\d+$/.test(cpid);
}
// Main index page function
function indexPage() {
const urlParams = new URLSearchParams(window.location.search);
const creatorAddress = urlParams.get('creator');
const dropdownValue = urlParams.get('dropdown');
const dropdown = document.getElementById('query-select'); // Correct the ID here
const searchForm = document.getElementById('search-form');
// Event listeners
dropdown.addEventListener('change', () => {
currentPage = 1; // Reset to the first page
fetchDataAndRender(currentPage, creatorAddress, dropdown.value);
});
searchForm.addEventListener('submit', (event) => {
event.preventDefault();
const searchInput = document.getElementById('search-input');
const searchValue = searchInput.value.trim();
fetchDataAndRender(currentPage, searchValue, dropdown.value);
});
// Fetch and render data
fetchDataAndRender(currentPage, creatorAddress, dropdownValue);
function fetchDataAndRender(page, creator, dropdownValue) {
let apiUrl = `${apiBaseUrl}?page=${page}&limit=${itemsPerPage}&sort_order=desc`;
if (creator) {
apiUrl += `&creator=${creator}`;
}
// Use the ident parameter for filtering
if (dropdownValue && dropdownValue !== "ALL") {
apiUrl += `&ident=${dropdownValue}`;
}
fetch(apiUrl)
.then(response => response.json())
.then(data => {
if (currentPage === 1) {
totalNumberOfStamps = data.total;
}
renderData(data.data);
renderPaginationButtons(page, data.totalPages, dropdownValue, creatorAddress);
})
.catch(error => console.error(error));
}
function renderData(data) {
const dataContainer = document.getElementById('data-container');
dataContainer.innerHTML = '';
data.forEach((item) => {
const itemContainer = document.createElement('div');
itemContainer.classList.add('item');
if (item.stamp_url) {
const contentContainer = document.createElement('a'); // Create a container that's clickable
contentContainer.href = `asset.html?tx_hash=${item.tx_hash}`; // Set the URL you want to navigate to
// Check if the URL is an HTML file
if (item.stamp_url.endsWith('.html')) {
// Embed HTML content using an iframe or a div
const iframe = document.createElement('iframe');
iframe.src = item.stamp_url;
iframe.width = '210'; // Set width as needed
iframe.height = '210'; // Set height as needed
iframe.style.border = 'none'; // Optional: remove border
iframe.setAttribute('sandbox', 'allow-scripts allow-same-origin');
contentContainer.appendChild(iframe);
} else {
// Use an img tag for images
const img = document.createElement('img');
img.src = item.stamp_url;
img.width = 210;
img.height = 210;
img.onerror = function () {
this.onerror = null;
this.src = 'images/sad.png';
};
img.style.objectFit = 'contain';
img.style.imageRendering = 'pixelated';
img.style.imageRendering = '-moz-crisp-edges';
img.style.imageRendering = 'crisp-edges';
img.style.backgroundColor = '#000000';
contentContainer.appendChild(img);
}
itemContainer.appendChild(contentContainer); // Append the content container to the itemContainer
}
const stampInfo = document.createElement('pre');
stampInfo.innerText = (String(item.stamp) === '999999999') ? 'Stamp: \u221E' : `Stamp ${item.stamp}`;
itemContainer.appendChild(stampInfo);
const creatorInfo = document.createElement('pre');
creatorInfo.classList.add('creator-info'); // Add a new class for specific styling
const displayedCreator = item.creator_name ? item.creator_name : `${item.creator.slice(0, 5)}...${item.creator.slice(-5)}`;
creatorInfo.innerHTML = `Creator: <span class="normal-case">${displayedCreator}</span>`;
itemContainer.appendChild(creatorInfo);
dataContainer.appendChild(itemContainer);
});
}
function renderPaginationButtons(page, numberOfItems, dropdownValue, creatorAddress) {
const paginationContainerTop = document.getElementById('pagination-container-top');
const paginationContainerBottom = document.getElementById('pagination-container-bottom');
paginationContainerTop.innerHTML = '';
paginationContainerBottom.innerHTML = '';
const totalPages = Math.ceil(totalNumberOfStamps / itemsPerPage);
const firstButtonTop = document.createElement('button');
firstButtonTop.innerText = '<< First';
firstButtonTop.disabled = page === 1;
firstButtonTop.addEventListener('click', () => {
currentPage = 1;
fetchDataAndRender(currentPage, creatorAddress, dropdownValue);
});
const prevButtonTop = document.createElement('button');
prevButtonTop.innerText = '< Previous';
prevButtonTop.disabled = page === 1;
prevButtonTop.addEventListener('click', () => {
currentPage--;
fetchDataAndRender(currentPage, creatorAddress, dropdownValue);
});
const nextButtonTop = document.createElement('button');
nextButtonTop.innerText = 'Next >';
nextButtonTop.disabled = page === totalPages;
nextButtonTop.addEventListener('click', () => {
currentPage++;
fetchDataAndRender(currentPage, creatorAddress, dropdownValue);
});
const lastButtonTop = document.createElement('button');
lastButtonTop.innerText = 'Last >>';
lastButtonTop.disabled = page === totalPages;
lastButtonTop.addEventListener('click', () => {
currentPage = totalPages;
fetchDataAndRender(currentPage, creatorAddress, dropdownValue);
});
const firstButtonBottom = firstButtonTop.cloneNode(true);
const prevButtonBottom = prevButtonTop.cloneNode(true);
const nextButtonBottom = nextButtonTop.cloneNode(true);
const lastButtonBottom = lastButtonTop.cloneNode(true);
// Add new event listeners
firstButtonBottom.addEventListener('click', () => {
currentPage = 1;
fetchDataAndRender(currentPage, creatorAddress, dropdownValue);
});
prevButtonBottom.addEventListener('click', () => {
currentPage--;
fetchDataAndRender(currentPage, creatorAddress, dropdownValue);
});
nextButtonBottom.addEventListener('click', () => {
currentPage++;
fetchDataAndRender(currentPage, creatorAddress, dropdownValue);
});
lastButtonBottom.addEventListener('click', () => {
currentPage = totalPages;
fetchDataAndRender(currentPage, creatorAddress, dropdownValue);
});
// Replace the old buttons in the DOM with the new ones
paginationContainerTop.appendChild(firstButtonTop);
paginationContainerTop.appendChild(prevButtonTop);
paginationContainerTop.appendChild(nextButtonTop);
paginationContainerTop.appendChild(lastButtonTop);
paginationContainerBottom.appendChild(firstButtonBottom);
paginationContainerBottom.appendChild(prevButtonBottom);
paginationContainerBottom.appendChild(nextButtonBottom);
paginationContainerBottom.appendChild(lastButtonBottom);
}
}
function assetPage() {
async function fetchAssetDetails() {
const urlParams = new URLSearchParams(window.location.search);
const asset = urlParams.get('asset');
const cpid = urlParams.get('cpid');
const tx_hash = urlParams.get('tx_hash');
const stampNumber = urlParams.get('stampNumber');
try {
let assetData;
const fetchUrl = new URL(apiBaseUrl);
if (stampNumber) {
fetchUrl.searchParams.append('stamp', stampNumber);
} else if (asset || cpid) {
fetchUrl.searchParams.append('cpid', asset || cpid);
} else if (tx_hash) {
fetchUrl.searchParams.append('tx_hash', tx_hash);
}
const resp = await fetch(fetchUrl);
const json = await resp.json();
if (json.data) {
assetData = json.data;
}
if (assetData) {
displayAssetDetails(assetData);
} else {
console.error('Asset not found');
}
} catch (error) {
console.error(error);
}
}
function displayAssetDetails(data) {
const assetContainer = document.getElementById('asset-container');
const grid = document.createElement('div');
grid.id = 'asset-details-grid';
// Check if the URL is an HTML file
if (data.stamp_url.endsWith('.html')) {
// Create an iframe to embed the HTML content
const iframe = document.createElement('iframe');
iframe.src = data.stamp_url;
iframe.width = '420'; // You can set width as needed
iframe.height = '420'; // You can set height as needed
iframe.style.border = 'none'; // Optional: remove border
iframe.setAttribute('sandbox', 'allow-scripts allow-same-origin');
assetContainer.appendChild(iframe);
} else {
// If it's not an HTML file, assume it's an image
const img = document.createElement('img');
img.src = data.stamp_url;
img.width = 420;
img.height = 420;
img.onerror = function() {
this.onerror = null;
this.src = 'images/sad.png';
};
img.style.objectFit = 'contain';
img.style.imageRendering = 'pixelated';
img.style.imageRendering = '-moz-crisp-edges';
img.style.imageRendering = 'crisp-edges';
img.style.backgroundColor = '#000000';
assetContainer.appendChild(img);
}
// Create a grid container for asset details
const gridContainer = document.createElement('div');
gridContainer.id = 'asset-details-grid'; // Use the ID for styling
gridContainer.style.display = 'grid';
gridContainer.style.gridTemplateColumns = '1fr'; // One-column layout
gridContainer.style.gap = '10px'; // Space between grid items
gridContainer.style.textAlign = 'left';
gridContainer.style.maxWidth = '420px';
gridContainer.style.margin = 'auto';
// Function to create grid items
function createGridItem(label, value) {
const item = document.createElement('div');
item.className = 'grid-item';
item.innerText = `${label}: ${value}`;
return item;
}
// Adding grid items
gridContainer.appendChild(createGridItem('STAMP', data.stamp === '999999999' ? '\u221E' : data.stamp));
if (data.creator_name) {
gridContainer.appendChild(createGridItem('CREATOR NAME', data.creator_name));
}
gridContainer.appendChild(createGridItem('CREATOR', data.creator));
gridContainer.appendChild(createGridItem('CPID', data.cpid));
gridContainer.appendChild(createGridItem('BLOCK INDEX', data.block_index));
gridContainer.appendChild(createGridItem('SUPPLY', data.supply));
gridContainer.appendChild(createGridItem('DIVISIBLE', data.divisible));
gridContainer.appendChild(createGridItem('LOCKED', data.locked));
gridContainer.appendChild(createGridItem('BTC TX', data.tx_hash));
// Adding links
const addLink = (label, url) => {
const link = document.createElement('a');
link.href = url;
link.innerText = label;
link.style.display = 'block'; // Ensure each link is on a new line
gridContainer.appendChild(link);
};
addLink('BLOCKCHAIN.COM TRANSACTION INFORMATION', `https://www.blockchain.com/explorer/transactions/btc/${data.tx_hash}`);
addLink('bitSTART ASSET INFORMATION', `https://bitst.art/${data.tx_hash}`);
addLink('TXN DATA DECODER', `https://jpja.github.io/Electrum-Counterparty/decode_tx.html?tx=${data.tx_hash}`);
addLink('BINARY MEDIA', data.stamp_url);
// Append the grid container to assetContainer
assetContainer.appendChild(gridContainer);
}
function displayDispenserDetails(dispenserData) {
if (dispenserData.length > 0) {
const assetContainer = document.getElementById('asset-container');
const dispenserDetails = document.createElement('pre');
dispenserDetails.innerText = 'Dispensers:\n';
dispenserData.forEach((dispenser, index) => {
dispenserDetails.innerText += `\nDispenser ${index + 1}:\nAsset: ${dispenser.asset}\nAmount: ${dispenser.amount}\nDispenser Address: ${dispenser.dispenser}\n`;
});
assetContainer.appendChild(dispenserDetails);
}
}
fetchAssetDetails();
}
// Initialize the correct page
function init() {
const currentPageName = document.location.pathname.split('/').pop();
if (currentPageName === 'index.html' || currentPageName === '') {
indexPage();
} else if (currentPageName === 'asset.html') {
assetPage();
}
}
// Set up event listeners and any other initialization logic
function setUpEventListeners() {
const searchForm = document.getElementById('search-form');
if (searchForm) {
searchForm.addEventListener('submit', (event) => {
event.preventDefault();
const searchInput = document.getElementById('search-input');
const searchValue = searchInput.value.trim();
// Add your search validation and redirection logic here
// For example:
if (/^\d+$/.test(searchValue)) {
window.location.href = `asset.html?stampNumber=${searchValue}`;
} else if (isValidCpid(searchValue)) {
window.location.href = `asset.html?asset=${searchValue}`;
} else if (/^[a-fA-F0-9]{64}$/.test(searchValue)) {
window.location.href = `asset.html?tx_hash=${searchValue}`;
} else if (simpleValidateAddress(searchValue)) {
window.location.href = `index.html?creator=${searchValue}`;
} else {
console.error('Invalid search input');
}
});
}
}
// Call the init function when the DOM is fully loaded
document.addEventListener('DOMContentLoaded', () => {
init();
setUpEventListeners();
});