all repos — honk @ 86a2d571fe0e893ba60cc53ea8b884d30dc3870e

my fork of honk

views/honkpage.js (view raw)

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