window.gentUI = (() => {
const byId = id => document.getElementById(id);
function el(tag, text, className) {
const node = document.createElement(tag);
if (text != null) node.textContent = String(text);
if (className) node.className = className;
return node;
}
let bridgeSequence = 0, lastBridgeResult = 0;
function notice(id, message, error = false, sequence) {
if (id === "bridge-error") {
const order = sequence ?? ++bridgeSequence;
if (order < lastBridgeResult) return;
lastBridgeResult = order;
}
const field = byId(id);
delete field.dataset.readError;
delete field.dataset.queuedRename;
delete field.dataset.queuedUnregister;
field.textContent = message || "";
field.hidden = !message;
field.classList.toggle("error", error);
}
function readNotice(id, error) {
const field = byId(id);
if (error) {
notice(id, error, true);
field.dataset.readError = "true";
} else if (field.dataset.readError) {
notice(id, "");
}
}
function send(request) {
const sequence = ++bridgeSequence;
return window.gent.send(request).then(reply => {
notice("bridge-error", "", false, sequence);
return reply;
}, () => {
notice("bridge-error", "Gent Desktop could not reach its supervisor.", true, sequence);
throw new Error("Supervisor unavailable");
});
}
function request(op, fields) { return send({ op, ...fields }); }
function button(title, run, style) {
const b = el("button", title, style);
b.type = "button";
b.addEventListener("click", run);
return b;
}
const refreshIcon = document.createElement("template");
refreshIcon.innerHTML = '<svg viewBox="0 0 24 24" width="18" height="18" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 5v6h-6"/><path d="M19.5 10A8 8 0 1 0 18 17"/></svg>';
function addRefreshIcon(control) {
control.append(refreshIcon.content.cloneNode(true));
}
function setupDashboardFullscreen(onEnter) {
const control = byId("dashboard-fullscreen");
control.innerHTML = '<svg viewBox="0 0 24 24" width="18" height="18" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path/></svg>';
const icon = control.querySelector("path");
let active = false, pending = null;
function paint() {
control.title = active ? "Exit dashboard fullscreen" : "Enter dashboard fullscreen";
control.setAttribute("aria-label", control.title);
control.setAttribute("aria-pressed", String(active));
icon.setAttribute("d", active ? "M9 3v6H3m12-6v6h6M3 15h6v6m6 0v-6h6" : "M4 9V4h5m6 0h5v5m0 6v5h-5M9 20H4v-5");
}
function change(next) {
if (pending === next) return;
pending = next;
window.gent.setDashboardFullscreen(next).catch(() => {
if (pending !== next) return;
pending = null;
notice("bridge-error", "Could not change dashboard fullscreen mode.", true);
});
}
control.disabled = !window.gent?.setDashboardFullscreen;
control.addEventListener("click", () => change(!active));
window.addEventListener("gent-message", ({detail}) => {
const next = detail?.dashboard_fullscreen;
if (typeof next !== "boolean") return;
pending = null;
if (active === next) return;
active = next;
if (active) onEnter?.();
document.body.classList.toggle("dashboard-fullscreen", active);
paint();
if (document.hasFocus()) control.focus({preventScroll:true});
});
window.addEventListener("keydown", event => {
if (event.key !== "Escape" || !(active || pending === true)) return;
event.preventDefault();
if (!event.repeat) change(false);
});
paint();
}
return {byId,el,notice,readNotice,send,request,button,addRefreshIcon,setupDashboardFullscreen};
})();