start handling js like an asset

This commit is contained in:
Ted Unangst 2019-09-18 22:17:50 -04:00
parent 2d19f53cb8
commit 58395536d1
4 changed files with 28 additions and 22 deletions

View File

@ -51,10 +51,10 @@ import (
"humungus.tedunangst.com/r/webs/httpsig"
)
var savedstyleparams = make(map[string]string)
var savedassetparams = make(map[string]string)
func getstyleparam(file string) string {
if p, ok := savedstyleparams[file]; ok {
func getassetparam(file string) string {
if p, ok := savedassetparams[file]; ok {
return p
}
data, err := ioutil.ReadFile(file)

View File

@ -10,7 +10,6 @@
{{ template "honkform.html" . }}
{{ end }}
</div>
{{ $BonkCSRF := .HonkCSRF }}
{{ if .TopXID }}
<div class="info" id="refreshbox">
<p><button onclick="refreshhonks(this)">refresh</button><span></span>
@ -18,14 +17,19 @@
{{ end }}
<div id="honksonpage">
<div>
{{ $BonkCSRF := .HonkCSRF }}
{{ range .Honks }}
{{ template "honk.html" map "Honk" . "BonkCSRF" $BonkCSRF }}
{{ end }}
</div>
</div>
</main>
{{ if $BonkCSRF }}
{{ if .HonkCSRF }}
<script>
{{ template "honkpage.js" . }}
var csrftoken = {{ .HonkCSRF }}
var topxid = { "{{ .PageName }}" : "{{ .TopXID }}" }
var honksforpage = { }
var thispagename = "{{ .PageName }}"
</script>
<script src="/honkpage.js{{ .JSParam }}"></script>
{{ end }}

View File

@ -1,4 +1,3 @@
{{ $BonkCSRF := .HonkCSRF }}
function encode(hash) {
var s = []
for (var key in hash) {
@ -23,17 +22,17 @@ function get(url, whendone) {
function bonk(el, xid) {
el.innerHTML = "bonked"
el.disabled = true
post("/bonk", "CSRF={{ $BonkCSRF }}&xid=" + escape(xid))
post("/bonk", encode({"CSRF": csrftoken, "xid": xid}))
}
function unbonk(el, xid) {
el.innerHTML = "unbonked"
el.disabled = true
post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=unbonk&what=" + escape(xid))
post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "unbonk", "what": xid}))
}
function muteit(el, convoy) {
el.innerHTML = "muted"
el.disabled = true
post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=zonvoy&what=" + escape(convoy))
post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "zonvoy", "what": convoy}))
var els = document.querySelectorAll('article.honk')
for (var i = 0; i < els.length; i++) {
var e = els[i]
@ -45,7 +44,7 @@ function muteit(el, convoy) {
function zonkit(el, xid) {
el.innerHTML = "zonked"
el.disabled = true
post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=zonk&what=" + escape(xid))
post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "zonk", "what": xid}))
var p = el
while (p && p.tagName != "ARTICLE") {
p = p.parentElement
@ -57,16 +56,13 @@ function zonkit(el, xid) {
function ackit(el, xid) {
el.innerHTML = "acked"
el.disabled = true
post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=ack&what=" + escape(xid))
post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "ack", "what": xid}))
}
function deackit(el, xid) {
el.innerHTML = "deacked"
el.disabled = true
post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=deack&what=" + escape(xid))
post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "deack", "what": 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

18
web.go
View File

@ -73,8 +73,9 @@ func getuserstyle(u *login.UserInfo) template.CSS {
func getInfo(r *http.Request) map[string]interface{} {
u := login.GetUserInfo(r)
templinfo := make(map[string]interface{})
templinfo["StyleParam"] = getstyleparam("views/style.css")
templinfo["LocalStyleParam"] = getstyleparam("views/local.css")
templinfo["StyleParam"] = getassetparam("views/style.css")
templinfo["LocalStyleParam"] = getassetparam("views/local.css")
templinfo["JSParam"] = getassetparam("views/honkpage.js")
templinfo["UserStyle"] = getuserstyle(u)
templinfo["ServerName"] = serverName
templinfo["IconName"] = iconName
@ -1334,6 +1335,10 @@ func servecss(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/css; charset=utf-8")
w.Write([]byte(s))
}
func serveasset(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "max-age=7776000")
http.ServeFile(w, r, "views"+r.URL.Path)
}
func servehtml(w http.ResponseWriter, r *http.Request) {
templinfo := getInfo(r)
err := readviews.Execute(w, r.URL.Path[1:]+".html", templinfo)
@ -1449,10 +1454,10 @@ func serve() {
"views/honkpage.js",
)
if !debug {
s := "views/style.css"
savedstyleparams[s] = getstyleparam(s)
s = "views/local.css"
savedstyleparams[s] = getstyleparam(s)
assets := []string{"views/style.css", "views/local.css", "views/honkpage.js"}
for _, s := range assets {
savedassetparams[s] = getassetparam(s)
}
}
bitethethumbs()
@ -1485,6 +1490,7 @@ func serve() {
getters.HandleFunc("/style.css", servecss)
getters.HandleFunc("/local.css", servecss)
getters.HandleFunc("/honkpage.js", serveasset)
getters.HandleFunc("/about", servehtml)
getters.HandleFunc("/login", servehtml)
posters.HandleFunc("/dologin", login.LoginFunc)