Blog/Components/Pages/Query.razor.js 1.7 K · 63 lines · raw · history

1 import {getById, h, span, addClass, div} from '/common.module.js'
2 import {DACTAL} from "/dactal.js"
3
4 const enterEvent = new KeyboardEvent('keydown', {
5 key: 'Enter',
6 code: 'Enter',
7 keyCode: 13,
8 which: 13,
9 bubbles: true,
10 cancelable: true
11 });
12
13 let dactal
14
15 export async function onLoad() {
16 const input = getById("input")
17 dactal = new DACTAL()
18
19 const brp = await fetch("/brp.json")
20 const cdata = await brp.json()
21
22 dactal.data = {
23 personen: cdata
24 }
25
26 input.onkeydown = async (e) => {
27 if (e.key === "Enter" && !e.shiftKey) {
28 e.preventDefault()
29 let result = await dactal.query(input.value)
30
31 const results = getById("results")
32 results.replaceChildren(renderValue(result.slice(0, 100)))
33
34 if (result.length > 100) {
35 results.appendChild(div(span(`Not showing ${result.length - 100} more results.`)))
36 }
37 }
38 }
39
40 input.dispatchEvent(enterEvent)
41 }
42
43 function renderValue(obj) {
44 if (Array.isArray(obj)) {
45 return h("ol",
46 ...obj.map((item) => h("li", renderValue(item)))
47 )
48 } else if (typeof obj === "object") {
49 return h("ul",
50 ...Object.entries(obj).map(([name, item]) =>
51 addClass(h("li",
52 addClass(span(name), "name"),
53 span(" "),
54 renderValue(item)),
55 "dash")))
56 } else if (typeof obj === "boolean") {
57 return addClass(span(obj), obj ? "json-true" : "json-false")
58 } else if (typeof obj === "number") {
59 return addClass(span(obj), "json-number")
60 } else if (typeof obj === "string") {
61 return span(obj)
62 }
63 }