diff --git a/pkgs/router-webui/main.go b/pkgs/router-webui/main.go index e586fcf..7fa9dca 100644 --- a/pkgs/router-webui/main.go +++ b/pkgs/router-webui/main.go @@ -368,7 +368,7 @@ func securityHeaders(next http.Handler) http.Handler { type page struct { Title string CSRF string - Refresh int // seconds; 0 = none + Live int // seconds between updates of the content without a reload (live.js); 0 = none Flash string Error string LoggedIn bool @@ -495,9 +495,9 @@ func (a *app) overview(w http.ResponseWriter, r *http.Request, sess session) { Data: overviewData{Status: st, Dashboard: a.dashboardData(r.URL.Query().Get("range"))}} switch { case st.Applying || st.ConfirmRunning: - p.Refresh = 2 + p.Live = 2 case st.RollbackIn > 0: - p.Refresh = 10 + p.Live = 10 } a.render(w, "overview", p) } diff --git a/pkgs/router-webui/static/live.js b/pkgs/router-webui/static/live.js new file mode 100644 index 0000000..2e6cfb5 --- /dev/null +++ b/pkgs/router-webui/static/live.js @@ -0,0 +1,38 @@ +// A page whose #live element has data-live="N" is fetched again every N seconds +// while it says so, and only the content is replaced (no reload, so the scroll +// position and the log's place stay). A log that was at its end follows new text. +(function () { + function follow(root) { + root.querySelectorAll("pre.log").forEach(function (pre) { pre.scrollTop = pre.scrollHeight; }); + } + var live = document.getElementById("live"); + if (!live) return; + follow(live); + function poll() { + var secs = parseInt(live.getAttribute("data-live"), 10); + if (!secs) return; + setTimeout(function () { + fetch(location.pathname + location.search,{ credentials: "same-origin", headers: { "X-Live": "1" } }) + .then(function (r) { + if (r.redirected || !r.ok) throw new Error("not the page"); + return r.text(); + }) + .then(function (html) { + var next = new DOMParser().parseFromString(html, "text/html").getElementById("live"); + if (!next) throw new Error("no content"); + var old = live.querySelector("pre.log"); + var atEnd = !old || old.scrollTop + old.clientHeight >= old.scrollHeight - 8; + var kept = old ? old.scrollTop : 0; + live.innerHTML = next.innerHTML; + if (next.getAttribute("data-live")) live.setAttribute("data-live", next.getAttribute("data-live")); + else live.removeAttribute("data-live"); + var pre = live.querySelector("pre.log"); + if (pre) pre.scrollTop = atEnd ? pre.scrollHeight : kept; + }) + // The web UI restarts when an upgrade is switched on: keep trying. + .catch(function () {}) + .then(poll); + }, secs * 1000); + } + poll(); +})(); diff --git a/pkgs/router-webui/templates/layout.html b/pkgs/router-webui/templates/layout.html index 7888564..676cf6e 100644 --- a/pkgs/router-webui/templates/layout.html +++ b/pkgs/router-webui/templates/layout.html @@ -3,7 +3,6 @@ -{{if .Refresh}}{{end}} {{.Title}} · router @@ -41,8 +40,9 @@

{{.Title}}

{{if .Flash}}

{{.Flash}}

{{end}} {{if .Error}}

{{.Error}}

{{end}} - {{template "content" .}} +
{{template "content" .}}
+ diff --git a/pkgs/router-webui/templates/upgrade.html b/pkgs/router-webui/templates/upgrade.html index 3f3f008..377f244 100644 --- a/pkgs/router-webui/templates/upgrade.html +++ b/pkgs/router-webui/templates/upgrade.html @@ -26,7 +26,7 @@ {{if $d.Updating}}

Updating…

The router is installing a new version of nixos-router.

{{else if $d.Run.Running}} -

Working…

This page refreshes on its own. The web UI restarts when an upgrade is switched on.

+

Working…

This page updates on its own. The web UI restarts when an upgrade is switched on.

{{else if $d.Pending}}

Waiting for confirmation

A new system is active and not confirmed yet. Confirm it on the Overview, or the router goes back to the diff --git a/pkgs/router-webui/update.go b/pkgs/router-webui/update.go index 7a63834..d8a01b2 100644 --- a/pkgs/router-webui/update.go +++ b/pkgs/router-webui/update.go @@ -265,7 +265,7 @@ func (a *app) updatePage(w http.ResponseWriter, r *http.Request, sess session) { d := a.updateData() p := page{Title: "Update", CSRF: sess.csrf, LoggedIn: true, Data: d, Flash: r.URL.Query().Get("msg")} if d.Status.Running { - p.Refresh = 3 + p.Live = 3 } a.render(w, "update", p) } diff --git a/pkgs/router-webui/upgrade.go b/pkgs/router-webui/upgrade.go index bfcb259..92de249 100644 --- a/pkgs/router-webui/upgrade.go +++ b/pkgs/router-webui/upgrade.go @@ -194,7 +194,7 @@ func (a *app) upgradePage(w http.ResponseWriter, r *http.Request, sess session) d := a.upgradeData() p := page{Title: "Packages", CSRF: sess.csrf, LoggedIn: true, Data: d, Flash: r.URL.Query().Get("msg")} if d.Run.Running { - p.Refresh = 3 + p.Live = 3 } a.render(w, "upgrade", p) }