| @@ -248,20 +248,18 @@ async function runQuery(query, lang) { |
| 248 |
248 |
} |
| 249 |
249 |
} |
| 250 |
250 |
|
| 251 |
|
-function renderValue(obj) { |
|
251 |
+ |
|
252 |
+ |
|
253 |
+ |
|
254 |
+ |
|
255 |
+ |
|
256 |
+const PAGE_SIZE = 10 |
|
257 |
+ |
|
258 |
+function renderValue(obj, depth = 0) { |
| 252 |
259 |
if (Array.isArray(obj)) { |
| 253 |
|
- return h("ol", |
| 254 |
|
- obj.map((item) => h("li", [renderValue(item)])) |
| 255 |
|
- ) |
| 256 |
|
- } else if (typeof obj === "object") { |
| 257 |
|
- return h("ul", |
| 258 |
|
- Object.entries(obj).map(([name, item]) => |
| 259 |
|
- h("li", {class: "dash"}, |
| 260 |
|
- [ |
| 261 |
|
- h("span", {class: "name"}, name), |
| 262 |
|
- t(" "), |
| 263 |
|
- renderValue(item) |
| 264 |
|
- ]))) |
|
260 |
+ return renderList(obj, depth) |
|
261 |
+ } else if (obj !== null && typeof obj === "object") { |
|
262 |
+ return renderContainer("ul", Object.entries(obj), ([name, item]) => renderEntry(name, item, depth)) |
| 265 |
263 |
} else if (typeof obj === "boolean") { |
| 266 |
264 |
return h("span", {class: obj ? "json-true" : "json-false"}) |
| 267 |
265 |
} else if (typeof obj === "number") { |
| @@ -269,4 +267,95 @@ function renderValue(obj) { |
| 269 |
267 |
} else if (typeof obj === "string") { |
| 270 |
268 |
return h("span", obj) |
| 271 |
269 |
} |
|
270 |
+} |
|
271 |
+ |
|
272 |
+function renderEntry(name, item, depth) { |
|
273 |
+ return h("li", {class: "dash"}, [ |
|
274 |
+ h("span", {class: "name"}, name), |
|
275 |
+ t(" "), |
|
276 |
+ renderValue(item, depth + 1) |
|
277 |
+ ]) |
|
278 |
+} |
|
279 |
+ |
|
280 |
+ |
|
281 |
+ |
|
282 |
+ |
|
283 |
+function renderList(items, depth) { |
|
284 |
+ const renderItem = (item) => h("li", [renderValue(item, depth + 1)]) |
|
285 |
+ |
|
286 |
+ if (depth === 0) { |
|
287 |
+ return h("ol", items.map(renderItem)) |
|
288 |
+ } |
|
289 |
+ |
|
290 |
+ const container = h("div") |
|
291 |
+ appendListPage(container, items, renderItem, 0) |
|
292 |
+ return container |
|
293 |
+} |
|
294 |
+ |
|
295 |
+function appendListPage(container, items, renderItem, offset) { |
|
296 |
+ const page = items.slice(offset, offset + PAGE_SIZE) |
|
297 |
+ const ol = h("ol", page.map(renderItem)) |
|
298 |
+ if (offset > 0) { |
|
299 |
+ ol.setAttribute("start", String(offset + 1)) |
|
300 |
+ } |
|
301 |
+ container.appendChild(ol) |
|
302 |
+ |
|
303 |
+ const nextOffset = offset + page.length |
|
304 |
+ const rest = items.length - nextOffset |
|
305 |
+ if (rest === 0) { |
|
306 |
+ return |
|
307 |
+ } |
|
308 |
+ |
|
309 |
+ const details = h("details") |
|
310 |
+ details.appendChild(h("summary", `${rest} more…`)) |
|
311 |
+ details.addEventListener("toggle", () => { |
|
312 |
+ if (details.open) { |
|
313 |
+ details.remove() |
|
314 |
+ appendListPage(container, items, renderItem, nextOffset) |
|
315 |
+ } |
|
316 |
+ }, {once: true}) |
|
317 |
+ |
|
318 |
+ container.appendChild(details) |
|
319 |
+} |
|
320 |
+ |
|
321 |
+ |
|
322 |
+ |
|
323 |
+ |
|
324 |
+ |
|
325 |
+ |
|
326 |
+ |
|
327 |
+ |
|
328 |
+function renderContainer(tag, items, renderItem) { |
|
329 |
+ const container = h(tag, []) |
|
330 |
+ appendContainerPage(container, items, renderItem, 0) |
|
331 |
+ return container |
|
332 |
+} |
|
333 |
+ |
|
334 |
+function appendContainerPage(container, items, renderItem, offset) { |
|
335 |
+ const page = items.slice(offset, offset + PAGE_SIZE) |
|
336 |
+ for (const item of page) { |
|
337 |
+ container.appendChild(renderItem(item)) |
|
338 |
+ } |
|
339 |
+ |
|
340 |
+ const nextOffset = offset + page.length |
|
341 |
+ const rest = items.length - nextOffset |
|
342 |
+ if (rest === 0) { |
|
343 |
+ return |
|
344 |
+ } |
|
345 |
+ |
|
346 |
+ const details = h("details") |
|
347 |
+ details.appendChild(h("summary", `${rest} more…`)) |
|
348 |
+ |
|
349 |
+ const moreItem = h("li", {class: "dash"}, [details]) |
|
350 |
+ |
|
351 |
+ |
|
352 |
+ |
|
353 |
+ details.addEventListener("toggle", () => { |
|
354 |
+ if (details.open) { |
|
355 |
+ moreItem.remove() |
|
356 |
+ appendContainerPage(container, items, renderItem, nextOffset) |
|
357 |
+ } |
|
358 |
+ }, {once: true}) |
|
359 |
+ |
|
360 |
+ container.appendChild(moreItem) |
| 272 |
361 |
} |
| No newline at end of file |