-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
639 lines (587 loc) · 25.5 KB
/
index.html
File metadata and controls
639 lines (587 loc) · 25.5 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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>INAPROC API Data Downloader</title>
<style>
body { font-family: Arial, sans-serif; margin: 24px; }
.panel { max-width: 1100px; margin: 0 auto; }
label { display: block; margin-bottom: 6px; font-weight: 600; }
.param-label { display: flex; align-items: center; gap: 4px; }
input, select, button { padding: 8px; font-size: 14px; }
input[type="text"], input[type="number"] { width: 40ch; }
#apiUrl { width: 48ch; }
.field { margin-bottom: 12px; }
.hint-mini {
display: inline-flex;
align-items: center;
justify-content: center;
width: 16px;
height: 16px;
padding: 0;
border: 1px solid #999;
border-radius: 50%;
background: transparent;
font-size: 11px;
line-height: 1;
vertical-align: middle;
}
.hint-text { margin-top: 4px; font-size: 12px; font-weight: 400; color: #444; }
table { border-collapse: collapse; width: 100%; margin-top: 12px; }
th, td { border: none; padding: 6px 8px; text-align: left; vertical-align: top; }
th { background: #f0f0f0; }
.table-wrap { max-height: 60vh; overflow: auto; border: none; }
#info { margin-top: 8px; display: flex; gap: 12px; align-items: center; }
.title { text-align: center; font-weight: 700; }
.layout-table { width: 100%; border-collapse: separate; border-spacing: 12px; table-layout: fixed; }
.layout-table td { vertical-align: top; padding: 8px; }
.left-col, .right-col { width: 50%; }
.sidebox { border: 1px dashed #ddd; padding: 12px; }
.sidebox h2 { font-weight: 700; margin: 0 0 6px; }
.sidebox p { margin: 6px 0; }
.sidebox .small { font-size: 11px; }
.footer { margin-top: 10px; font-size: 12px; color: #666; text-align: center; }
.divider { margin: 30px 0 5px; }
.divider span { display: inline-block; padding: 0 8px; color: #000000; }
.divider:before, .divider:after {
content: "";
display: inline-block;
vertical-align: middle;
width: 60px;
border-top: 1px dashed rgb(0, 0, 0)
}
.divider:before { margin-right: 8px; }
.divider:after { margin-left: 8px; }
@media (max-width: 900px) {
body { margin: 16px; }
input[type="text"], input[type="number"], #apiUrl { width: 100%; }
.layout-table, .layout-table tr, .layout-table td { display: block; width: 100%; }
.layout-table { border-spacing: 0; }
.layout-table td { padding: 0; }
.left-col, .right-col { width: 100%; }
.sidebox { margin-top: 12px; }
}
</style>
</head>
<body>
<main class="panel">
<h1 class="title">INAPROC API Data Downloader</h1>
<table class="layout-table">
<tr>
<td class="left-col">
<!-- API token -->
<div class="field">
<label for="token">Token API</label>
<input id="token" type="text" placeholder="inprceaxxxxxxxxdea437a1ae" size="48" />
</div>
<!-- Endpoint selector -->
<div class="field">
<label for="apiUrl">Endpoints</label>
<select id="apiUrl">
<!-- E-Katalog endpoints -->
<option value="https://data.inaproc.id/api/v1/ekatalog/paket-e-purchasing">
/api/v1/ekatalog/paket-e-purchasing
</option>
<option value="https://data.inaproc.id/api/v1/ekatalog/penyedia-detail">
/api/v1/ekatalog/penyedia-detail
</option>
<!-- RUP endpoints -->
<option value="https://data.inaproc.id/api/v1/rup/master-satker">
/api/v1/rup/master-satker
</option>
<option value="https://data.inaproc.id/api/v1/rup/paket-anggaran-penyedia">
/api/v1/rup/paket-anggaran-penyedia
</option>
<option value="https://data.inaproc.id/api/v1/rup/paket-anggaran-swakelola">
/api/v1/rup/paket-anggaran-swakelola
</option>
<option value="https://data.inaproc.id/api/v1/rup/paket-penyedia-terumumkan">
/api/v1/rup/paket-penyedia-terumumkan
</option>
<option value="https://data.inaproc.id/api/v1/rup/paket-swakelola-terumumkan">
/api/v1/rup/paket-swakelola-terumumkan
</option>
<option value="https://data.inaproc.id/api/v1/rup/program-master">
/api/v1/rup/program-master
</option>
<!-- Tender endpoints -->
<option value="https://data.inaproc.id/api/v1/tender/jadwal-tahapan-non-tender">
/api/v1/tender/jadwal-tahapan-non-tender
</option>
<option value="https://data.inaproc.id/api/v1/tender/jadwal-tahapan-tender">
/api/v1/tender/jadwal-tahapan-tender
</option>
<option value="https://data.inaproc.id/api/v1/tender/non-tender-ekontrak-kontrak">
/api/v1/tender/non-tender-ekontrak-kontrak
</option>
<option value="https://data.inaproc.id/api/v1/tender/non-tender-pengumuman">
/api/v1/tender/non-tender-pengumuman
</option>
<option value="https://data.inaproc.id/api/v1/tender/non-tender-selesai">
/api/v1/tender/non-tender-selesai
</option>
<option value="https://data.inaproc.id/api/v1/tender/pencatatan-non-tender">
/api/v1/tender/pencatatan-non-tender
</option>
<option value="https://data.inaproc.id/api/v1/tender/pencatatan-non-tender-realisasi">
/api/v1/tender/pencatatan-non-tender-realisasi
</option>
<option value="https://data.inaproc.id/api/v1/tender/pencatatan-swakelola">
/api/v1/tender/pencatatan-swakelola
</option>
<option value="https://data.inaproc.id/api/v1/tender/pencatatan-swakelola-realisasi">
/api/v1/tender/pencatatan-swakelola-realisasi
</option>
<option value="https://data.inaproc.id/api/v1/tender/pengumuman">
/api/v1/tender/pengumuman
</option>
<option value="https://data.inaproc.id/api/v1/tender/peserta-tender">
/api/v1/tender/peserta-tender
</option>
<option value="https://data.inaproc.id/api/v1/tender/tender-ekontrak-kontrak">
/api/v1/tender/tender-ekontrak-kontrak
</option>
<option value="https://data.inaproc.id/api/v1/tender/tender-selesai-nilai">
/api/v1/tender/tender-selesai-nilai
</option>
</select>
</div>
<div class="divider"><span>Parameter</span></div>
<!-- Query parameters (shown based on endpoint) -->
<div id="fieldKodeKlpd" class="field">
<label for="kodeKlpd" class="param-label">Kode KLPD <button type="button" class="hint-mini" data-help-target="helpKodeKlpd" aria-expanded="false" aria-label="Cara isi Kode KLPD">?</button></label>
<div id="helpKodeKlpd" class="hint-text" hidden>Kode instansi bisa dilihat pada SiRUP, contoh: D123.</div>
<input id="kodeKlpd" type="text" placeholder="D123" />
</div>
<div id="fieldKodePenyedia" class="field" style="display: none;">
<label for="kodePenyedia" class="param-label">Kode Penyedia <button type="button" class="hint-mini" data-help-target="helpKodePenyedia" aria-expanded="false" aria-label="Cara isi Kode Penyedia">?</button></label>
<div id="helpKodePenyedia" class="hint-text" hidden>Kode penyedia bisa dilihat dari API "/api/v1/ekatalog/paket-e-purchasing", contoh format: 01J8PR1TAZG60XXXXXXXW1NB.</div>
<input id="kodePenyedia" type="text" placeholder="01J8PR1TAZG60XXXXXXXW1NB" />
</div>
<div id="fieldKodeTender" class="field" style="display: none;">
<label for="kodeTender" class="param-label">Kode Tender <button type="button" class="hint-mini" data-help-target="helpKodeTender" aria-expanded="false" aria-label="Cara isi Kode Tender">?</button></label>
<div id="helpKodeTender" class="hint-text" hidden>Kode tender bisa dilihat dari API "/api/v1/tender/tender-selesai-nilai", contoh format: 10040XXXX00 </div>
<input id="kodeTender" type="text" placeholder="100002345" />
</div>
<div id="fieldTahun" class="field">
<label for="tahun" class="param-label">Tahun <button type="button" class="hint-mini" data-help-target="helpTahun" aria-expanded="false" aria-label="Cara isi Tahun">?</button></label>
<div id="helpTahun" class="hint-text" hidden>Isi tahun data yang ingin diambil, contoh format: 2025 atau 2026.</div>
<input id="tahun" type="number" placeholder="2025" />
</div>
<!-- Debug toggle (no token logging) -->
<div class="field">
<label>
<input id="debug" type="checkbox" />
Debug logging
</label>
</div>
<!-- Actions -->
<div class="actions">
<button id="showBtn">Show Data</button>
<button id="downloadBtn">Download Data</button>
</div>
<!-- Results table (hidden until data exists) -->
</td>
<td class="right-col">
<div class="sidebox">
<h2>Tata Cara Penggunaan</h2>
<p>1) Isi Token API yang didapatkan dari https://data.inaproc.id/portal/.</p>
<p>2) Pilih endpoint data yang ingin diambil, pastikan Token API punya akses ke endpoint tersebut.</p>
<p>3) Isi parameter yang tersedia.</p>
<p>4) Klik Show Data untuk menampilkan data atau Download Data untuk mengunduh CSV.</p>
<p class="small">Debug logging membantu melihat request/response. Buka DevTools (F12), lalu lihat tab Console.</p>
</div>
<div class="sidebox">
<h2>Source Code</h2>
<p>
Kode sumber halaman ini tersedia di
<a href="https://github.com/deniganda/INAPROC-API-Downloader" target="_blank" rel="noopener">
GitHub</a>.
</p>
<p class="small">Silakan buka repositori untuk mengunduh <em>source code</em> dan menjalankannya secara lokal.</p>
</div>
</td>
</tr>
<tr>
<td colspan="2">
<div id="info">
<span id="status"></span>
</div>
<div id="tableWrap" class="table-wrap" style="display: none;">
<table id="table"></table>
</div>
</td>
</tr>
<tr>
<td colspan="2">
<div class="footer">Halaman ini merupakan <em>proof of concept</em>. <br />Demi keamanan API Token, jalankan website ini secara lokal di komputer atau server yang terpercaya.</div>
</td>
</tr>
</table>
</main>
<script>
// Random limit for first page or retries when cursor is missing.
function getRandomLimit() {
return Math.floor(Math.random() * (100 - 80 + 1)) + 80;
}
// Build query params based on the selected endpoint.
function getParams(config, cursorOverride, limitOverride) {
const kodeKlpd = document.getElementById("kodeKlpd").value.trim();
const tahun = document.getElementById("tahun").value.trim();
const kodePenyedia = document.getElementById("kodePenyedia").value.trim();
const kodeTender = document.getElementById("kodeTender").value.trim();
const limitValue = typeof limitOverride === "number" ? limitOverride : getRandomLimit();
const cursor = typeof cursorOverride === "string" ? cursorOverride : "";
const params = new URLSearchParams();
if (config.useKodeKlpd && kodeKlpd) params.set("kode_klpd", kodeKlpd);
if (config.useKodePenyedia && kodePenyedia) params.set("kode_penyedia", kodePenyedia);
if (config.useKodeTender && kodeTender) params.set("kode_tender", kodeTender);
if (config.useTahun && tahun) params.set("tahun", tahun);
if (config.useLimit) params.set("limit", String(limitValue));
if (config.useCursor && cursor) params.set("cursor", cursor);
return params;
}
// Normalize cursor/has_more fields from various response shapes.
function extractCursor(data) {
const meta = data?.meta || data?.pagination || {};
const rawHasMore = meta?.has_more ?? data?.has_more;
let hasMore = false;
if (typeof rawHasMore === "string") {
hasMore = rawHasMore.toLowerCase() === "true";
} else {
hasMore = Boolean(rawHasMore);
}
const rawCursor = meta?.cursor ?? meta?.next_cursor ?? data?.cursor ?? data?.next_cursor ?? data?.cursor_next;
return {
next: rawCursor ? String(rawCursor) : "",
hasMore,
limit: meta?.limit,
meta
};
}
// Toggle buttons during fetch.
function setBusy(isBusy) {
document.getElementById("showBtn").disabled = isBusy;
document.getElementById("downloadBtn").disabled = isBusy;
}
// Update status text.
function setStatus(text) {
document.getElementById("status").textContent = text;
}
function setCount() {}
// Endpoint parameter configuration.
const ENDPOINT_CONFIG = {
// E-Katalog endpoints
"/api/v1/ekatalog/paket-e-purchasing": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
"/api/v1/ekatalog/penyedia-detail": { useKodePenyedia: true, useLimit: false, useCursor: false },
// RUP endpoints
"/api/v1/rup/master-satker": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
"/api/v1/rup/paket-anggaran-penyedia": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
"/api/v1/rup/paket-anggaran-swakelola": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
"/api/v1/rup/paket-penyedia-terumumkan": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
"/api/v1/rup/paket-swakelola-terumumkan": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
"/api/v1/rup/program-master": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
// Tender endpoints
"/api/v1/tender/jadwal-tahapan-non-tender": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
"/api/v1/tender/jadwal-tahapan-tender": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
"/api/v1/tender/non-tender-ekontrak-kontrak": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
"/api/v1/tender/non-tender-pengumuman": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
"/api/v1/tender/non-tender-selesai": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
"/api/v1/tender/pencatatan-non-tender": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
"/api/v1/tender/pencatatan-non-tender-realisasi": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
"/api/v1/tender/pencatatan-swakelola": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
"/api/v1/tender/pencatatan-swakelola-realisasi": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
"/api/v1/tender/pengumuman": { useKodeTender: true, useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
"/api/v1/tender/peserta-tender": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
"/api/v1/tender/tender-ekontrak-kontrak": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true },
"/api/v1/tender/tender-selesai-nilai": { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true }
};
function getPathFromUrl(url) {
try {
return new URL(url).pathname;
} catch (err) {
return url;
}
}
function getEndpointConfig() {
const apiUrl = document.getElementById("apiUrl").value.trim();
const path = getPathFromUrl(apiUrl);
return ENDPOINT_CONFIG[path] || { useKodeKlpd: true, useTahun: true, useLimit: true, useCursor: true };
}
function updateFieldVisibility(config) {
document.getElementById("fieldKodeKlpd").style.display = config.useKodeKlpd ? "block" : "none";
document.getElementById("fieldKodePenyedia").style.display = config.useKodePenyedia ? "block" : "none";
document.getElementById("fieldKodeTender").style.display = config.useKodeTender ? "block" : "none";
document.getElementById("fieldTahun").style.display = config.useTahun ? "block" : "none";
}
// Cached results for reuse between Show and Download.
let lastItems = null;
let lastSignature = "";
// Cache key to reuse fetched data across Show/Download.
function getSignature() {
const apiUrl = document.getElementById("apiUrl").value.trim();
const config = getEndpointConfig();
const kodeKlpd = document.getElementById("kodeKlpd").value.trim();
const tahun = document.getElementById("tahun").value.trim();
const kodePenyedia = document.getElementById("kodePenyedia").value.trim();
const kodeTender = document.getElementById("kodeTender").value.trim();
return [
apiUrl,
config.useKodeKlpd ? kodeKlpd : "",
config.useKodePenyedia ? kodePenyedia : "",
config.useKodeTender ? kodeTender : "",
config.useTahun ? tahun : ""
].join("|");
}
// Fetch all pages using cursor pagination, with retry on missing cursor.
async function fetchAllItems() {
const token = document.getElementById("token").value.trim();
const tableEl = document.getElementById("table");
const tableWrapEl = document.getElementById("tableWrap");
const debug = document.getElementById("debug").checked;
// Reset UI state before fetching.
setStatus("");
tableEl.innerHTML = "";
tableWrapEl.style.display = "none";
setBusy(true);
const apiUrl = document.getElementById("apiUrl").value.trim();
const config = getEndpointConfig();
// Validate required inputs.
if (!token) {
setStatus("Please enter a token.");
setBusy(false);
return null;
}
if (!apiUrl) {
setStatus("Please enter an API URL.");
setBusy(false);
return null;
}
try {
let cursor = "";
let hasMore = true;
let lockedLimit = null;
const allItems = [];
setStatus("Fetching data...");
if (debug) {
console.log("[debug] start fetch");
}
const maxCursorRetries = 5; // change this to control retry attempts
while (hasMore) {
let retryCount = 0;
let data = null;
let resOk = false;
// Retry loop when API reports more pages but no cursor.
let missingCursorFail = false;
while (retryCount <= maxCursorRetries) {
const usedLimit = lockedLimit === null ? getRandomLimit() : lockedLimit;
const params = getParams(config, cursor, usedLimit);
const url = `${apiUrl}?${params.toString()}`;
if (debug) {
console.log("[debug] request", url);
}
const res = await fetch(url, {
method: "GET",
headers: {
"Authorization": `Bearer ${token}`
}
});
data = await res.json();
resOk = res.ok;
// Abort on HTTP error.
if (!resOk) {
setStatus("Getting data failed.");
if (debug) {
console.log("[debug] error", res.status, res.statusText);
}
return null;
}
const cursors = extractCursor(data);
const nextCursor = cursors.next || "";
const wantsMore = config.useCursor ? Boolean(cursors.hasMore) : false;
if (debug) {
console.log("[debug] meta", cursors.meta);
}
// Retry if cursor is missing but API says there are more pages.
if (wantsMore && !nextCursor) {
retryCount += 1;
if (debug) {
console.log("[debug] missing cursor, retry", retryCount);
}
if (retryCount > maxCursorRetries) {
missingCursorFail = true;
break;
}
await new Promise((resolve) => setTimeout(resolve, 5000));
continue;
}
// Advance cursor and lock limit once a cursor appears.
cursor = nextCursor;
hasMore = wantsMore;
if (lockedLimit === null && nextCursor) {
lockedLimit = typeof cursors.limit === "number" ? cursors.limit : usedLimit;
if (debug && lockedLimit !== null) {
console.log("[debug] lock limit", lockedLimit);
}
}
break;
}
// Fail when cursor never appears.
if (missingCursorFail) {
setStatus("Getting data failed.");
return null;
}
if (!data || !resOk) {
setStatus("Getting data failed.");
return null;
}
// Collect data from the current page.
const items = data?.data || data?.items || data?.results || [];
if (Array.isArray(items) && items.length) {
allItems.push(...items);
}
if (debug) {
console.log("[debug] page", {
got: Array.isArray(items) ? items.length : 0,
total: allItems.length,
hasMore,
cursor: cursor || ""
});
}
if (!cursor && hasMore) {
hasMore = false;
if (debug) {
console.log("[debug] stop: hasMore true but cursor empty");
}
}
}
// Final status after pagination ends.
if (allItems.length) {
setStatus(`Fetched ${allItems.length} rows.`);
if (debug) {
console.log("[debug] done", { total: allItems.length });
}
return allItems;
}
setStatus("No items found.");
if (debug) {
console.log("[debug] done", { total: 0 });
}
return [];
} catch (err) {
setStatus("Getting data failed.");
if (debug) {
console.log("[debug] exception", err);
}
return null;
} finally {
// Re-enable buttons even on failure.
setBusy(false);
}
}
// Build CSV and trigger download.
function downloadCsv(items) {
if (!items.length) return;
const columns = Object.keys(items[0]);
const escapeCell = (value) => {
const text = value === null || value === undefined ? "" : String(value);
return `"${text.replace(/"/g, "\"\"")}"`;
};
const rows = [
columns.map(escapeCell).join(","),
...items.map((item) => columns.map((col) => escapeCell(item[col])).join(","))
];
const blob = new Blob([rows.join("\n")], { type: "text/csv;charset=utf-8;" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "data.csv";
document.body.appendChild(link);
link.click();
link.remove();
URL.revokeObjectURL(url);
}
// Render a simple table from items.
function renderTable(items) {
const tableEl = document.getElementById("table");
const tableWrapEl = document.getElementById("tableWrap");
if (!items.length) {
tableWrapEl.style.display = "none";
return;
}
tableWrapEl.style.display = "block";
const columns = Object.keys(items[0]);
const thead = document.createElement("thead");
const headRow = document.createElement("tr");
for (const col of columns) {
const th = document.createElement("th");
th.textContent = col;
headRow.appendChild(th);
}
thead.appendChild(headRow);
const tbody = document.createElement("tbody");
for (const item of items) {
const tr = document.createElement("tr");
for (const col of columns) {
const td = document.createElement("td");
const value = item[col];
td.textContent = value === null || value === undefined ? "" : String(value);
tr.appendChild(td);
}
tbody.appendChild(tr);
}
tableEl.innerHTML = "";
tableEl.appendChild(thead);
tableEl.appendChild(tbody);
}
// Show Data button handler.
document.getElementById("showBtn").addEventListener("click", async () => {
const items = await fetchAllItems();
if (items) {
lastItems = items;
lastSignature = getSignature();
renderTable(items);
}
});
// Download button handler with cache reuse.
document.getElementById("downloadBtn").addEventListener("click", async () => {
const signature = getSignature();
if (lastItems && lastSignature === signature && lastItems.length) {
downloadCsv(lastItems);
setStatus(`Downloaded ${lastItems.length} rows.`);
return;
}
const items = await fetchAllItems();
if (items && items.length) {
lastItems = items;
lastSignature = signature;
downloadCsv(items);
setStatus(`Downloaded ${items.length} rows.`);
}
});
// Initialize field visibility on load and when endpoint changes.
updateFieldVisibility(getEndpointConfig());
document.getElementById("apiUrl").addEventListener("change", () => {
updateFieldVisibility(getEndpointConfig());
});
// Toggle hint text on click/tap (mobile-friendly).
const hints = document.querySelectorAll(".hint-mini[data-help-target]");
const helps = document.querySelectorAll(".hint-text");
hints.forEach((btn) => {
btn.addEventListener("click", () => {
const id = btn.getAttribute("data-help-target");
const panel = id ? document.getElementById(id) : null;
if (!panel) return;
const open = panel.hidden;
helps.forEach((el) => { el.hidden = true; });
hints.forEach((b) => b.setAttribute("aria-expanded", "false"));
if (open) {
panel.hidden = false;
btn.setAttribute("aria-expanded", "true");
}
});
});
</script>
</body>
</html>