this is extremely messy, but allow hotswap between /home and /atme
Ted Unangst tedu@tedunangst.com
Thu, 05 Sep 2019 17:29:40 -0400
4 files changed,
58 insertions(+),
16 deletions(-)
M
honk.go
→
honk.go
@@ -173,9 +173,9 @@ honks = gethonksforme(userid)
} else { honks = gethonksforuser(userid) honks = osmosis(honks, userid) - if len(honks) > 0 { - templinfo["TopXID"] = honks[0].XID - } + } + if len(honks) > 0 { + templinfo["TopXID"] = honks[0].XID } templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r) }@@ -1715,6 +1715,7 @@ posters := mux.Methods("POST").Subrouter()
getters := mux.Methods("GET").Subrouter() getters.HandleFunc("/", homepage) + getters.HandleFunc("/home", homepage) getters.HandleFunc("/front", homepage) getters.HandleFunc("/robots.txt", nomoroboto) getters.HandleFunc("/rss", showrss)
M
views/header.html
→
views/header.html
@@ -14,9 +14,9 @@ <meta name="viewport" content="width=device-width">
</head> <body> <header> -<span><a href="/">honk</a></span> +<span><a id="homelink" href="/">honk</a></span> {{ if .UserInfo }} -<span><a href="/atme">@me</a></span> +<span><a id="atmelink" href="/atme">@me</a></span> <span><a href="/{{ .UserSep }}/{{ .UserInfo.Username }}">{{ .UserInfo.Username }}</a></span> <span><a href="/honkers">honkers</a></span> <span><a href="/c">combos</a></span>
M
views/honkfrags.html
→
views/honkfrags.html
@@ -1,5 +1,7 @@
<div>{{ .TopXID }}</div> {{ $BonkCSRF := .HonkCSRF }} +<div> {{ range .Honks }} {{ template "honk.html" map "Honk" . "BonkCSRF" $BonkCSRF }} {{ end }} +</div>
M
views/honkpage.html
→
views/honkpage.html
@@ -14,31 +14,70 @@ {{ $BonkCSRF := .HonkCSRF }}
{{ if .TopXID }} <div class="info" id="refreshbox"> <script> -var topxid = "{{ .TopXID }}" +var topxid = { "home" : "{{ .TopXID }}" } +var honksforpage = { } +var thispagename = "home" +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("/?topxid=" + escape(topxid), function(xhr) { - var doc = xhr.responseXML - topxid = doc.children[0].children[1].children[0].innerText - var honks = doc.children[0].children[1].children - var mebox = document.getElementById("refreshbox") - var newhonks = honks.length - 1 - for (var i = honks.length; i > 1; i--) { - mebox.insertAdjacentElement('afterend', honks[i-1]) - } + get("/" + thispagename + "?topxid=" + escape(topxid[thispagename]), function(xhr) { + var lenhonks = fillinhonks(xhr) btn.innerHTML = "refresh" btn.disabled = false - btn.parentElement.children[1].innerHTML = " " + newhonks + " new" + 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"> {{ range .Honks }} {{ template "honk.html" map "Honk" . "BonkCSRF" $BonkCSRF }} {{ end }} +</div> </main> {{ if $BonkCSRF }} <script>