import {getById, writeError, writeDebug, h, span, addClass, div} from '/common.module.js' import {DACTAL} from "/dactal.js" const enterEvent = new KeyboardEvent('keydown', { key: 'Enter', code: 'Enter', keyCode: 13, which: 13, bubbles: true, cancelable: true }); let dactal export async function onLoad() { const input = getById("input") dactal = new DACTAL() fetch("/brp.json").then(async res => { const cdata = await res.json() dactal.load(cdata, "personen") writeDebug("Loaded brp data") await runQuery(input.value) }) input.onkeydown = async (e) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault() await runQuery(input.value) } } input.dispatchEvent(enterEvent) } async function runQuery(query) { const results = getById("results") try { let result = await dactal.query(query) results.replaceChildren(renderValue(result.slice(0, 100))) if (result.length > 100) { results.appendChild(div(span(`Not showing ${result.length - 100} more results.`))) } } catch (e) { writeError(e.message) results.replaceChildren() } } function renderValue(obj) { if (Array.isArray(obj)) { return h("ol", ...obj.map((item) => h("li", renderValue(item))) ) } else if (typeof obj === "object") { return h("ul", ...Object.entries(obj).map(([name, item]) => addClass(h("li", addClass(span(name), "name"), span(" "), renderValue(item)), "dash"))) } else if (typeof obj === "boolean") { return addClass(span(obj), obj ? "json-true" : "json-false") } else if (typeof obj === "number") { return addClass(span(obj), "json-number") } else if (typeof obj === "string") { return span(obj) } }