work-graph-simple/app.js

77 lines
1.8 KiB
JavaScript

import * as FSHandle from "./store-directory-handle.js";
const H =(type, attributes={}, ...children)=> {
const el =document.createElement(type);
Object.entries(attributes).forEach(([name, data])=>{
if(name.startsWith("on"))
{
el.addEventListener(name.substring(2), data);
}
else
{
el.setAttribute(name, data)
}
});
children.forEach(child=>
{
el.appendChild(typeof child == "string" ? document.createTextNode(child) : child);
});
return el;
}
const button = H("button");
const listRoom = H("ul");
async function getFolderHandle(dirHandle)
{
try
{
const workingFolder = await dirHandle.getDirectoryHandle("room");
const module = await import("./data/room.js");
const rooms = module.default(workingFolder);
button.innerText = "change directory";
listRoom.innerHTML = "";
Object.entries(rooms).forEach(([roomID, roomData])=>
{
const listPass = H("ul");
Object.entries(roomData.Pass).forEach(([passID, passData])=>{
listPass.appendChild(H("li", {},
H("button", { onclick(){ passData.load();console.log(roomData.Part); } }, passID)
))
});
listRoom.appendChild(
H("li", {},
H("div", {}, roomID),
listPass
)
);
})
return true;
}
catch (err)
{
button.innerText = "select directory";
console.error("Folder selection cancelled or failed:", err);
return false;
}
}
let handle = false;
handle = await FSHandle.getDirectoryHandle();
await getFolderHandle(handle);
button.addEventListener("click", async()=>
{
const directory = await globalThis.showDirectoryPicker();
await FSHandle.setDirectoryHandle(directory);
await getFolderHandle(directory);
});
document.body.appendChild(button);
document.body.appendChild(listRoom);