// ori-desktop frontend — plain JavaScript, no bundler. // // The Wails runtime injects two globals into this page: // window.go.main.App.* — the Go methods bound in main.go (each returns a Promise) // window.runtime.* — the Wails runtime API (window title, browser, ...) // Calling them directly (rather than through the generated frontend/wailsjs // wrappers) keeps this frontend free of generated files. // // Loaded as an ES module (see index.html): strict mode and a private scope // come for free, and the script runs once the document has been parsed. const $ = (id) => document.getElementById(id); const els = { connectScreen: $("connect-screen"), viewerScreen: $("viewer-screen"), form: $("connect-form"), url: $("server-url"), check: $("check-btn"), connect: $("connect-btn"), status: $("status"), settingsPath: $("settings-path"), viewerUrl: $("viewer-url"), reload: $("reload-btn"), browser: $("browser-btn"), disconnect: $("disconnect-btn"), frame: $("ori-frame"), }; /** The Go-bound App, or null when the page is opened outside Wails. */ function app() { return window.go?.main?.App || null; } function setStatus(text, kind) { els.status.textContent = text || ""; els.status.className = kind ? `status ${kind}` : "status"; } function setBusy(busy) { els.check.disabled = busy; els.connect.disabled = busy; els.url.disabled = busy; } function setTitle(title) { if (window.runtime?.WindowSetTitle) { window.runtime.WindowSetTitle(title); } document.title = title; } // ---- screens -------------------------------------------------------- function showViewer(baseURL) { els.viewerUrl.textContent = baseURL; els.frame.src = `${baseURL}/`; els.connectScreen.hidden = true; els.viewerScreen.hidden = false; setTitle(`ori — ${baseURL}`); } function showConnect(message, kind) { els.frame.src = "about:blank"; els.viewerScreen.hidden = true; els.connectScreen.hidden = false; setTitle("ori"); setStatus(message, kind); els.url.focus(); els.url.select(); } // ---- actions -------------------------------------------------------- async function check() { const a = app(); if (!a) return; setBusy(true); setStatus(`Checking ${els.url.value.trim()}/healthz …`); try { const rep = await a.CheckHealth(els.url.value); setStatus(rep.detail, rep.ok ? "ok" : "err"); } catch (err) { setStatus(String(err), "err"); } finally { setBusy(false); } } async function connect() { const a = app(); if (!a) return; setBusy(true); setStatus("Connecting …"); try { const base = await a.Connect(els.url.value); els.url.value = base; setStatus(""); showViewer(base); } catch (err) { setStatus(String(err), "err"); } finally { setBusy(false); } } async function openInBrowser() { const a = app(); if (!a) return; try { await a.OpenInBrowser(els.viewerUrl.textContent || els.url.value); } catch (err) { console.error(err); } } /** Reloads the embedded ori page by re-assigning the frame's URL. */ function reloadFrame() { const current = els.frame.src; els.frame.src = current; } // ---- startup -------------------------------------------------------- async function init() { els.form.addEventListener("submit", (e) => { e.preventDefault(); connect(); }); els.check.addEventListener("click", check); els.reload.addEventListener("click", reloadFrame); els.browser.addEventListener("click", openInBrowser); els.disconnect.addEventListener("click", () => showConnect("Disconnected.", ""), ); const a = app(); if (!a) { setStatus( "Wails runtime not found — open this page from the ori-desktop app (wails dev / wails build).", "err", ); setBusy(true); return; } try { const cfg = await a.GetConfig(); els.url.value = cfg.serverUrl || "http://localhost:8888"; } catch (err) { els.url.value = "http://localhost:8888"; setStatus(`Could not read saved settings: ${err}`, "err"); } try { const path = await a.SettingsPath(); els.settingsPath.textContent = path ? `Remembered in ${path}` : ""; } catch (_) { /* cosmetic only */ } // Auto-connect when the remembered server already answers; otherwise // stay on this screen with the reason, so the user can fix the URL. await connect(); } document.addEventListener("DOMContentLoaded", init);