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}}{{.Flash}}
{{end}} {{if .Error}}{{.Error}}
{{end}} - {{template "content" .}} +