-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.html
More file actions
271 lines (236 loc) · 7.77 KB
/
Copy pathindex.html
File metadata and controls
271 lines (236 loc) · 7.77 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Wilayah Indonesia API Demo</title>
<link href="https://cdn.jsdelivr.net/npm/tom-select@2.2.2/dist/css/tom-select.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/tom-select@2.2.2/dist/js/tom-select.complete.min.js"></script>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background-color: #f5f5f5;
color: #333;
margin: 0;
padding: 20px;
display: flex;
justify-content: center;
min-height: 100vh;
}
.container {
background: white;
padding: 2rem;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
width: 100%;
max-width: 600px;
}
h1 { margin-top: 0; color: #2c3e50; text-align: center; }
p { color: #666; text-align: center; margin-bottom: 2rem; }
.form-group { margin-bottom: 1.5rem; }
label { display: block; margin-bottom: 0.5rem; font-weight: 600; font-size: 0.9rem; }
select {
width: 100%;
padding: 0.75rem;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 1rem;
background-color: white;
}
select:disabled { background-color: #f9f9f9; cursor: not-allowed; }
.result {
margin-top: 2rem;
padding: 1rem;
background: #f8f9fa;
border-radius: 4px;
border: 1px solid #e9ecef;
font-family: monospace;
white-space: pre-wrap;
font-size: 0.85rem;
display: none;
}
.result.visible { display: block; }
footer { margin-top: 2rem; text-align: center; font-size: 0.8rem; color: #999; }
a { color: #0366d6; text-decoration: none; }
a:hover { text-decoration: underline; }
</style>
</head>
<body>
<div class="container">
<h1>Wilayah Indonesia</h1>
<p>Demo API statis data wilayah administrasi Indonesia.</p>
<div class="form-group">
<label for="province">Provinsi:</label>
<select id="province">
<option value="">Pilih Provinsi...</option>
</select>
</div>
<div class="form-group">
<label for="regency">Kabupaten/Kota:</label>
<select id="regency" disabled>
<option value="">Pilih Kabupaten/Kota...</option>
</select>
</div>
<div class="form-group">
<label for="district">Kecamatan:</label>
<select id="district" disabled>
<option value="">Pilih Kecamatan...</option>
</select>
</div>
<div class="form-group">
<label for="village">Desa/Kelurahan:</label>
<select id="village" disabled>
<option value="">Pilih Desa/Kelurahan...</option>
</select>
</div>
<div id="result" class="result"></div>
<footer>
<p>Data source: <a href="https://github.com/izzulabadi/api-wilayah-indonesia-2026">izzulabadi/api-wilayah-indonesia-2026</a></p>
</footer>
</div>
<script>
// Base URL - relative to index.html
const API_BASE = 'api';
async function fetchJson(path) {
const res = await fetch(`${API_BASE}${path}`);
if (!res.ok) throw new Error(`Failed to fetch ${path}`);
return res.json();
}
// Initialize Tom Select
const tsConfig = {
create: false,
sortField: { field: "text", direction: "asc" },
placeholder: "Cari..."
};
const selects = {
province: new TomSelect('#province', { ...tsConfig, placeholder: "Pilih Provinsi..." }),
regency: new TomSelect('#regency', { ...tsConfig, placeholder: "Pilih Kabupaten/Kota..." }),
district: new TomSelect('#district', { ...tsConfig, placeholder: "Pilih Kecamatan..." }),
village: new TomSelect('#village', { ...tsConfig, placeholder: "Pilih Desa/Kelurahan..." })
};
// Disable downstream selects initially
selects.regency.disable();
selects.district.disable();
selects.village.disable();
// Cache for data
const cache = {
regencies: {}, // cache per province
districts: {}, // cache per province
villages: {} // cache per province
};
async function loadProvinces() {
try {
const data = await fetchJson('/provinces.json');
populateSelect('province', data);
} catch (e) {
console.error(e);
document.getElementById('result').textContent = 'Error loading provinces. Check console.';
document.getElementById('result').classList.add('visible');
}
}
async function loadRegencies(provinceId) {
if (!cache.regencies[provinceId]) {
try {
// Fetch regencies specific to this province
const data = await fetchJson(`/regencies/${provinceId}.json`);
cache.regencies[provinceId] = data;
} catch (e) {
console.error('Failed to load regencies:', e);
return;
}
}
// No need to filter manually anymore, the API returns only relevant items
populateSelect('regency', cache.regencies[provinceId]);
selects.regency.enable();
}
async function loadDistricts(regencyId) {
if (!cache.districts[regencyId]) {
try {
// Fetch districts specific to this regency
const data = await fetchJson(`/districts/${regencyId}.json`);
cache.districts[regencyId] = data;
} catch (e) {
console.error('Failed to load districts:', e);
return;
}
}
populateSelect('district', cache.districts[regencyId]);
selects.district.enable();
}
async function loadVillages(districtId) {
if (!cache.villages[districtId]) {
try {
// Fetch villages specific to this district
const data = await fetchJson(`/villages/${districtId}.json`);
cache.villages[districtId] = data;
} catch (e) {
console.error('Failed to load villages:', e);
return;
}
}
return cache.villages[districtId];
}
function populateSelect(key, items) {
const select = selects[key];
select.clear();
select.clearOptions();
const options = items.map(item => ({value: item.id, text: item.name}));
select.addOption(options);
select.enable();
}
function resetSelects(fromLevel) {
if (fromLevel === 'province') {
selects.regency.clear();
selects.regency.clearOptions();
selects.regency.disable();
}
if (fromLevel === 'province' || fromLevel === 'regency') {
selects.district.clear();
selects.district.clearOptions();
selects.district.disable();
}
if (fromLevel === 'province' || fromLevel === 'regency' || fromLevel === 'district') {
selects.village.clear();
selects.village.clearOptions();
selects.village.disable();
}
document.getElementById('result').classList.remove('visible');
}
// Event Listeners (Tom Select triggers 'change' on the original element, but we can also listen to the instance)
// Using instance.on('change') is safer.
selects.province.on('change', async (id) => {
resetSelects('province');
if (!id) return;
await loadRegencies(id);
});
selects.regency.on('change', async (id) => {
resetSelects('regency');
if (!id) return;
await loadDistricts(id);
});
selects.district.on('change', async (id) => {
resetSelects('district');
if (!id) return;
const villages = await loadVillages(id);
populateSelect('village', villages);
});
selects.village.on('change', (id) => {
if (!id) return;
// Get text from Tom Select items
const getLabel = (inst) => inst.getItem(inst.getValue()).textContent;
const selected = {
province: getLabel(selects.province),
regency: getLabel(selects.regency),
district: getLabel(selects.district),
village: getLabel(selects.village),
villageId: id
};
const resEl = document.getElementById('result');
resEl.textContent = JSON.stringify(selected, null, 2);
resEl.classList.add('visible');
});
// Init
loadProvinces();
</script>
</body>
</html>