all repos — honk @ 072be529fbbea94b584bc9cebe984d002b8ea625

my fork of honk

views/honkpage.js (view raw)

  1{{ $BonkCSRF := .HonkCSRF }}
  2function encode(hash) {
  3        var s = []
  4        for (var key in hash) {
  5                var val = hash[key]
  6                s.push(escape(key) + "=" + escape(val))
  7        }
  8        return s.join("&")
  9}
 10function post(url, data) {
 11	var x = new XMLHttpRequest()
 12	x.open("POST", url)
 13	x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
 14	x.send(data)
 15}
 16function get(url, whendone) {
 17	var x = new XMLHttpRequest()
 18	x.open("GET", url)
 19	x.responseType = "document"
 20	x.onload = function() { whendone(x) }
 21	x.send()
 22}
 23function bonk(el, xid) {
 24	el.innerHTML = "bonked"
 25	el.disabled = true
 26	post("/bonk", "CSRF={{ $BonkCSRF }}&xid=" + escape(xid))
 27}
 28function unbonk(el, xid) {
 29	el.innerHTML = "unbonked"
 30	el.disabled = true
 31	post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=unbonk&what=" + escape(xid))
 32}
 33function muteit(el, convoy) {
 34	el.innerHTML = "muted"
 35	el.disabled = true
 36	post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=zonvoy&what=" + escape(convoy))
 37	var els = document.querySelectorAll('article.honk')
 38	for (var i = 0; i < els.length; i++) {
 39		var e = els[i]
 40		if (e.getAttribute("data-convoy") == convoy) {
 41			e.remove()
 42		}
 43	}
 44}
 45function zonkit(el, xid) {
 46	el.innerHTML = "zonked"
 47	el.disabled = true
 48	post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=zonk&what=" + escape(xid))
 49	var p = el
 50	while (p && p.tagName != "ARTICLE") {
 51		p = p.parentElement
 52	}
 53	if (p) {
 54		p.remove()
 55	}
 56}
 57function ackit(el, xid) {
 58	el.innerHTML = "acked"
 59	el.disabled = true
 60	post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=ack&what=" + escape(xid))
 61}
 62function deackit(el, xid) {
 63	el.innerHTML = "deacked"
 64	el.disabled = true
 65	post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=deack&what=" + escape(xid))
 66}
 67var topxid = { "{{ .PageName }}" : "{{ .TopXID }}" }
 68var honksforpage = { }
 69var thispagename = "{{ .PageName }}"
 70function fillinhonks(xhr) {
 71	var doc = xhr.responseXML
 72	topxid[thispagename] = doc.children[0].children[1].children[0].innerText
 73	var honks = doc.children[0].children[1].children[1].children
 74	var honksonpage = document.getElementById("honksonpage")
 75	var holder = honksonpage.children[0]
 76	var lenhonks = honks.length
 77	for (var i = honks.length; i > 0; i--) {
 78		holder.prepend(honks[i-1])
 79	}
 80	relinkconvoys()
 81	return lenhonks
 82}
 83function refreshhonks(btn) {
 84	btn.innerHTML = "refreshing"
 85	btn.disabled = true
 86	var args = { "page" : thispagename }
 87	args["topxid"] = topxid[thispagename]
 88	get("/hydra?" + encode(args), function(xhr) {
 89		var lenhonks = fillinhonks(xhr)
 90		btn.innerHTML = "refresh"
 91		btn.disabled = false
 92		btn.parentElement.children[1].innerHTML = " " + lenhonks + " new"
 93	})
 94}
 95function statechanger(evt) {
 96	var name = evt.state
 97	if (!name) {
 98		return
 99	}
100	switchtopage(name)
101}
102function switchtopage(name, evt) {
103	var honksonpage = document.getElementById("honksonpage")
104	var holder = honksonpage.children[0]
105	holder.remove()
106	if (thispagename != "convoy") {
107		honksforpage[thispagename] = holder
108	}
109	thispagename = name
110	holder = honksforpage[name]
111	if (holder) {
112		honksonpage.prepend(holder)
113	} else {
114		honksonpage.prepend(document.createElement("div"))
115		var args = { "page" : name }
116		if (name == "convoy") {
117			var c = evt.srcElement.text
118			args["c"] = c
119		} else {
120			args["topxid"] = topxid[name]
121		}
122		get("/hydra?" + encode(args), fillinhonks)
123	}
124}
125function pageswitcher(name) {
126	return function(evt) {
127		if (name == thispagename) {
128			return false
129		}
130		switchtopage(name, evt)
131		var url = evt.srcElement.href
132		history.pushState(name, "some title", url)
133		return false
134	}
135}
136function relinkconvoys() {
137	var els = document.getElementsByClassName("convoylink")
138	for (var i = 0; i < els.length; i++) {
139		els[i].onclick = pageswitcher("convoy")
140	}
141}
142(function() {
143	var el = document.getElementById("homelink")
144	el.onclick = pageswitcher("home")
145	var el = document.getElementById("atmelink")
146	el.onclick = pageswitcher("atme")
147	relinkconvoys()
148	window.onpopstate = statechanger
149	history.replaceState(thispagename, "some title", thispagename)
150})();