checkpoint of js load and switch
Ted Unangst tedu@tedunangst.com
Mon, 16 Sep 2019 15:15:32 -0400
4 files changed,
185 insertions(+),
134 deletions(-)
M
views/honk.html
→
views/honk.html
@@ -32,7 +32,7 @@ {{ end }}
{{ end }} <br> {{ if $bonkcsrf }} -<span style="margin-left: 1em;" class="clip">convoy: <a href="/t?c={{ .Convoy }}">{{ .Convoy }}</a></span> +<span style="margin-left: 1em;" class="clip">convoy: <a class="convoylink" href="/t?c={{ .Convoy }}">{{ .Convoy }}</a></span> {{ else }} <span style="margin-left: 1em;" class="clip">convoy: {{ .Convoy }}</span> {{ end }}
M
views/honkpage.html
→
views/honkpage.html
@@ -13,138 +13,19 @@ </div>
{{ $BonkCSRF := .HonkCSRF }} {{ if .TopXID }} <div class="info" id="refreshbox"> -<script> -var topxid = { "{{ .PageName }}" : "{{ .TopXID }}" } -var honksforpage = { } -var thispagename = "{{ .PageName }}" -function fillinhonks(xhr) { - var doc = xhr.responseXML - topxid[thispagename] = doc.children[0].children[1].children[0].innerText - var honks = doc.children[0].children[1].children[1].children - var honksonpage = document.getElementById("honksonpage") - var lenhonks = honks.length - for (var i = honks.length; i > 0; i--) { - honksonpage.prepend(honks[i-1]) - } - return lenhonks -} -function refreshhonks(btn) { - btn.innerHTML = "refreshing" - btn.disabled = true - get("/" + thispagename + "?topxid=" + escape(topxid[thispagename]), function(xhr) { - var lenhonks = fillinhonks(xhr) - btn.innerHTML = "refresh" - btn.disabled = false - btn.parentElement.children[1].innerHTML = " " + lenhonks + " new" - }) -} -function pageswitcher(name) { - return function(evt) { - if (name == thispagename) { - return false - } - var honksonpage = document.getElementById("honksonpage") - var holder = document.createElement("div"); - while (honksonpage.children.length) { - holder.prepend(honksonpage.children[0]) - } - honksforpage[thispagename] = holder - thispagename = name - holder = honksforpage[name] - if (holder) { - while (holder.children.length) { - honksonpage.prepend(holder.children[0]) - } - } else { - get("/" + name + "?topxid=" + escape(topxid[thispagename]), function(xhr) { - fillinhonks(xhr) - }) - } - return false - } -} -(function() { - var el = document.getElementById("homelink") - el.onclick = pageswitcher("home") - var el = document.getElementById("atmelink") - el.onclick = pageswitcher("atme") -})(); -</script> <p><button onclick="refreshhonks(this)">refresh</button><span></span> </div> {{ end }} <div id="honksonpage"> +<div> {{ range .Honks }} {{ template "honk.html" map "Honk" . "BonkCSRF" $BonkCSRF }} {{ end }} </div> +</div> </main> {{ if $BonkCSRF }} <script> -function encode(hash) { - var s = [] - for (var key in hash) { - var val = hash[key] - s.push(escape(key) + "=" + escape(val)) - } - return s.join("&") -} -function post(url, data) { - var x = new XMLHttpRequest() - x.open("POST", url) - x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") - x.send(data) -} -function get(url, whendone) { - var x = new XMLHttpRequest() - x.open("GET", url) - x.responseType = "document" - x.onload = function() { whendone(x) } - x.send() -} -function bonk(el, xid) { - el.innerHTML = "bonked" - el.disabled = true - post("/bonk", "CSRF={{ $BonkCSRF }}&xid=" + escape(xid)) -} -function unbonk(el, xid) { - el.innerHTML = "unbonked" - el.disabled = true - post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=unbonk&what=" + escape(xid)) -} -function muteit(el, convoy) { - el.innerHTML = "muted" - el.disabled = true - post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=zonvoy&what=" + escape(convoy)) - var els = document.querySelectorAll('article.honk') - for (var i = 0; i < els.length; i++) { - var e = els[i] - if (e.getAttribute("data-convoy") == convoy) { - e.remove() - } - } -} -function zonkit(el, xid) { - el.innerHTML = "zonked" - el.disabled = true - post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=zonk&what=" + escape(xid)) - var p = el - while (p && p.tagName != "ARTICLE") { - p = p.parentElement - } - if (p) { - p.remove() - } -} -function ackit(el, xid) { - el.innerHTML = "acked" - el.disabled = true - post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=ack&what=" + escape(xid)) -} -function deackit(el, xid) { - el.innerHTML = "deacked" - el.disabled = true - post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=deack&what=" + escape(xid)) -} +{{ template "honkpage.js" . }} </script> {{ end }}
A
views/honkpage.js
@@ -0,0 +1,139 @@
+{{ $BonkCSRF := .HonkCSRF }} +function encode(hash) { + var s = [] + for (var key in hash) { + var val = hash[key] + s.push(escape(key) + "=" + escape(val)) + } + return s.join("&") +} +function post(url, data) { + var x = new XMLHttpRequest() + x.open("POST", url) + x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") + x.send(data) +} +function get(url, whendone) { + var x = new XMLHttpRequest() + x.open("GET", url) + x.responseType = "document" + x.onload = function() { whendone(x) } + x.send() +} +function bonk(el, xid) { + el.innerHTML = "bonked" + el.disabled = true + post("/bonk", "CSRF={{ $BonkCSRF }}&xid=" + escape(xid)) +} +function unbonk(el, xid) { + el.innerHTML = "unbonked" + el.disabled = true + post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=unbonk&what=" + escape(xid)) +} +function muteit(el, convoy) { + el.innerHTML = "muted" + el.disabled = true + post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=zonvoy&what=" + escape(convoy)) + var els = document.querySelectorAll('article.honk') + for (var i = 0; i < els.length; i++) { + var e = els[i] + if (e.getAttribute("data-convoy") == convoy) { + e.remove() + } + } +} +function zonkit(el, xid) { + el.innerHTML = "zonked" + el.disabled = true + post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=zonk&what=" + escape(xid)) + var p = el + while (p && p.tagName != "ARTICLE") { + p = p.parentElement + } + if (p) { + p.remove() + } +} +function ackit(el, xid) { + el.innerHTML = "acked" + el.disabled = true + post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=ack&what=" + escape(xid)) +} +function deackit(el, xid) { + el.innerHTML = "deacked" + el.disabled = true + post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=deack&what=" + escape(xid)) +} +var topxid = { "{{ .PageName }}" : "{{ .TopXID }}" } +var honksforpage = { } +var thispagename = "{{ .PageName }}" +function fillinhonks(xhr) { + var doc = xhr.responseXML + topxid[thispagename] = doc.children[0].children[1].children[0].innerText + var honks = doc.children[0].children[1].children[1].children + var honksonpage = document.getElementById("honksonpage") + var holder = honksonpage.children[0] + var lenhonks = honks.length + for (var i = honks.length; i > 0; i--) { + holder.prepend(honks[i-1]) + } + relinkconvoys() + return lenhonks +} +function refreshhonks(btn) { + btn.innerHTML = "refreshing" + btn.disabled = true + var args = { "page" : thispagename } + args["topxid"] = topxid[thispagename] + get("/hydra?" + encode(args), function(xhr) { + var lenhonks = fillinhonks(xhr) + btn.innerHTML = "refresh" + btn.disabled = false + btn.parentElement.children[1].innerHTML = " " + lenhonks + " new" + }) +} +function pageswitcher(name) { + return function(evt) { + if (name == thispagename) { + return false + } + var honksonpage = document.getElementById("honksonpage") + var holder = honksonpage.children[0] + holder.remove() + if (thispagename != "convoy") { + honksforpage[thispagename] = holder + } + + thispagename = name + holder = honksforpage[name] + if (holder) { + honksonpage.prepend(holder) + } else { + honksonpage.prepend(document.createElement("div")) + var args = { "page" : name } + if (name == "convoy") { + console.log("convoy page") + var c = evt.srcElement.text + args["c"] = c + } else { + args["topxid"] = topxid[name] + } + get("/hydra?" + encode(args), fillinhonks) + } + return false + } +} +function relinkconvoys() { + var els = document.getElementsByClassName("convoylink") + for (var i = 0; i < els.length; i++) { + els[i].onclick = pageswitcher("convoy") + } +} +(function() { + console.log("ok") + var el = document.getElementById("homelink") + el.onclick = pageswitcher("home") + var el = document.getElementById("atmelink") + el.onclick = pageswitcher("atme") + relinkconvoys() +})();
M
web.go
→
web.go
@@ -107,17 +107,6 @@ templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r)
} tname := "honkpage.html" - if topxid := r.FormValue("topxid"); topxid != "" { - for i, h := range honks { - if h.XID == topxid { - honks = honks[0:i] - break - } - } - log.Printf("topxid %d frags", len(honks)) - tname = "honkfrags.html" - } - reverbolate(userid, honks) templinfo["Honks"] = honks@@ -1357,6 +1346,46 @@ fmt.Fprintf(w, "Disallow: /%s/%s/%s/\n", userSep, u.Username, honkSep)
} } +func webhydra(w http.ResponseWriter, r *http.Request) { + u := login.GetUserInfo(r) + userid := u.UserID + templinfo := getInfo(r) + templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r) + page := r.FormValue("page") + var honks []*Honk + switch page { + case "atme": + honks = gethonksforme(userid) + case "home": + honks = gethonksforuser(userid) + honks = osmosis(honks, userid) + case "convoy": + c := r.FormValue("c") + honks = gethonksbyconvoy(userid, c) + default: + http.NotFound(w, r) + } + if len(honks) > 0 { + templinfo["TopXID"] = honks[0].XID + } + if topxid := r.FormValue("topxid"); topxid != "" { + for i, h := range honks { + if h.XID == topxid { + honks = honks[0:i] + break + } + } + log.Printf("topxid %d frags", len(honks)) + } + reverbolate(userid, honks) + templinfo["Honks"] = honks + w.Header().Set("Content-Type", "text/html; charset=utf-8") + err := readviews.Execute(w, "honkfrags.html", templinfo) + if err != nil { + log.Printf("frag error: %s", err) + } +} + func serve() { db := opendatabase() login.Init(db)@@ -1384,6 +1413,7 @@ "views/login.html",
"views/xzone.html", "views/header.html", "views/onts.html", + "views/honkpage.js", ) if !debug { s := "views/style.css"@@ -1448,6 +1478,7 @@ loggedin.HandleFunc("/c/{name:[[:alnum:]]+}", showcombo)
loggedin.HandleFunc("/c", showcombos) loggedin.HandleFunc("/t", showconvoy) loggedin.HandleFunc("/q", showsearch) + loggedin.HandleFunc("/hydra", webhydra) loggedin.Handle("/submithonker", login.CSRFWrap("submithonker", http.HandlerFunc(submithonker))) err = http.Serve(listener, mux)