all repos — honk @ c71060dae106572af3f352a1273a0ade9588b554

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	var stash = curpagestate.name + ":" + curpagestate.arg
 69	topxid[stash] = doc.children[0].children[1].children[0].innerText
 70	var srvmsg = doc.children[0].children[1].children[1]
 71	var honks = doc.children[0].children[1].children[2].children
 72
 73	var srvel = document.getElementById("srvmsg")
 74	if (srvel.children[0]) {
 75		srvel.children[0].remove()
 76	}
 77	srvel.prepend(srvmsg)
 78
 79	var frontload = true
 80	if (curpagestate.name == "convoy") {
 81		frontload = false
 82	}
 83
 84	var honksonpage = document.getElementById("honksonpage")
 85	var holder = honksonpage.children[0]
 86	var lenhonks = honks.length
 87	for (var i = honks.length; i > 0; i--) {
 88		if (frontload) {
 89			holder.prepend(honks[i-1])
 90		} else {
 91			holder.append(honks[i-1])
 92		}
 93			
 94	}
 95	relinklinks()
 96	return lenhonks
 97}
 98function hydrargs() {
 99	var name = curpagestate.name
100	var arg = curpagestate.arg
101	var args = { "page" : name }
102	if (name == "convoy") {
103		args["c"] = arg
104	} else if (name == "combo") {
105		args["c"] = arg
106	} else if (name == "honker") {
107		args["xid"] = arg
108	}
109	return args
110}
111function refreshhonks(btn) {
112	btn.innerHTML = "refreshing"
113	btn.disabled = true
114	var args = hydrargs()
115	var stash = curpagestate.name + ":" + curpagestate.arg
116	args["topxid"] = topxid[stash]
117	get("/hydra?" + encode(args), function(xhr) {
118		var lenhonks = fillinhonks(xhr)
119		btn.innerHTML = "refresh"
120		btn.disabled = false
121		btn.parentElement.children[1].innerHTML = " " + lenhonks + " new"
122	})
123}
124function statechanger(evt) {
125	var data = evt.state
126	if (!data) {
127		return
128	}
129	switchtopage(data.name, data.arg)
130}
131function switchtopage(name, arg) {
132	var stash = curpagestate.name + ":" + curpagestate.arg
133	var honksonpage = document.getElementById("honksonpage")
134	var holder = honksonpage.children[0]
135	holder.remove()
136	var srvel = document.getElementById("srvmsg")
137	var msg = srvel.children[0]
138	if (msg) {
139		msg.remove()
140		servermsgs[stash] = msg
141	}
142
143	honksforpage[stash] = holder
144
145	curpagestate.name = name
146	curpagestate.arg = arg
147	// get the holder for the target page
148	var stash = name + ":" + arg
149	holder = honksforpage[stash]
150	if (holder) {
151		honksonpage.prepend(holder)
152		msg = servermsgs[stash]
153		if (msg) {
154			srvel.prepend(msg)
155		}
156	} else {
157		// or create one and fill it
158		honksonpage.prepend(document.createElement("div"))
159		var args = hydrargs()
160		get("/hydra?" + encode(args), fillinhonks)
161	}
162}
163function newpagestate(name, arg) {
164	return { "name": name, "arg": arg }
165}
166function pageswitcher(name, arg) {
167	return function(evt) {
168		var topmenu = document.getElementById("topmenu")
169		topmenu.open = false
170		if (name == curpagestate.name && arg == curpagestate.arg) {
171			return false
172		}
173		switchtopage(name, arg)
174		var url = evt.srcElement.href
175		if (!url) {
176			url = evt.srcElement.parentElement.href
177		}
178		history.pushState(newpagestate(name, arg), "some title", url)
179		window.scrollTo(0, 0)
180		return false
181	}
182}
183function relinklinks() {
184	var els = document.getElementsByClassName("convoylink")
185	while (els.length) {
186		els[0].onclick = pageswitcher("convoy", els[0].text)
187		els[0].classList.remove("convoylink")
188	}
189	els = document.getElementsByClassName("combolink")
190	while (els.length) {
191		els[0].onclick = pageswitcher("combo", els[0].text)
192		els[0].classList.remove("combolink")
193	}
194	els = document.getElementsByClassName("honkerlink")
195	while (els.length) {
196		var el = els[0]
197		var xid = el.getAttribute("data-xid")
198		el.onclick = pageswitcher("honker", xid)
199		el.classList.remove("honkerlink")
200	}
201}
202(function() {
203	var el = document.getElementById("homelink")
204	el.onclick = pageswitcher("home", "")
205	el = document.getElementById("atmelink")
206	el.onclick = pageswitcher("atme", "")
207	el = document.getElementById("firstlink")
208	el.onclick = pageswitcher("first", "")
209	relinklinks()
210	window.onpopstate = statechanger
211	history.replaceState(curpagestate, "some title", "")
212})();
213(function() {
214	var el = document.getElementById("donkdescriptor")
215	el.style.display = "none"
216})();
217function showhonkform(elem, rid, hname) {
218	var form = document.getElementById("honkform")
219	form.style = "display: block"
220	if (elem) {
221		form.remove()
222		elem.parentElement.parentElement.parentElement.insertAdjacentElement('beforebegin', form)
223	} else {
224		elem = document.getElementById("honkformhost")
225		elem.insertAdjacentElement('afterend', form)
226	}
227	var ridinput = document.getElementById("ridinput")
228	var honknoise = document.getElementById("honknoise")
229	if (rid) {
230		ridinput.value = rid
231		honknoise.value = "@" + hname + " "
232	}
233	document.getElementById("honknoise").focus()
234}
235function showelement(id) {
236	var el = document.getElementById(id)
237	el.style.display = "block"
238}
239function updatedonker() {
240	var el = document.getElementById("donker")
241	el.children[1].textContent = el.children[0].value.slice(-20)
242	var el = document.getElementById("donkdescriptor")
243	el.style.display = ""
244}
245var checkinprec = 100.0
246var gpsoptions = {
247	enableHighAccuracy: false,
248	timeout: 1000,
249	maximumAge: 0
250};
251function fillcheckin() {
252	if (navigator.geolocation) {
253		navigator.geolocation.getCurrentPosition(function(pos) {
254			showelement("placedescriptor")
255			var el = document.getElementById("placelatinput")
256			el.value = Math.round(pos.coords.latitude * checkinprec) / checkinprec
257			el = document.getElementById("placelonginput")
258			el.value = Math.round(pos.coords.longitude * checkinprec) / checkinprec
259			checkinprec = 10000.0
260			gpsoptions.enableHighAccuracy = true
261			gpsoptions.timeout = 2000
262		}, function(err) {
263			showelement("placedescriptor")
264			el = document.getElementById("placenameinput")
265			el.value = err.message
266		}, gpsoptions)
267	}
268}