import {getById, writeError, writeDebug, h, t} 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(h("div", [h("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]) => h("li", {class: "dash"}, [ h("span", {class: "name"}, name), t(" "), renderValue(item) ]))) } else if (typeof obj === "boolean") { return h("span", {class: obj ? "json-true" : "json-false"}) } else if (typeof obj === "number") { return h("span", {class: "json-number"}, obj.toString()) } else if (typeof obj === "string") { return h("span", obj) } }