all repos — honk @ 7d553a86e910350664063b0865c465fe8b722a6c

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.timeout = 30 * 1000
 13	x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
 14	x.send(data)
 15}
 16function get(url, whendone, whentimedout) {
 17	var x = new XMLHttpRequest()
 18	x.open("GET", url)
 19	x.timeout = 15 * 1000
 20	x.responseType = "json"
 21	x.onload = function() { whendone(x) }
 22	if (whentimedout) {
 23		x.ontimeout = function(e) { whentimedout(x, e) }
 24	}
 25	x.send()
 26}
 27function bonk(el, xid) {
 28	el.innerHTML = "bonked"
 29	el.disabled = true
 30	post("/bonk", encode({"js": "2", "CSRF": csrftoken, "xid": xid}))
 31	return false
 32}
 33function unbonk(el, xid) {
 34	el.innerHTML = "unbonked"
 35	el.disabled = true
 36	post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "unbonk", "what": xid}))
 37}
 38function muteit(el, convoy) {
 39	el.innerHTML = "muted"
 40	el.disabled = true
 41	post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "zonvoy", "what": convoy}))
 42	var els = document.querySelectorAll('article.honk')
 43	for (var i = 0; i < els.length; i++) {
 44		var e = els[i]
 45		if (e.getAttribute("data-convoy") == convoy) {
 46			e.remove()
 47		}
 48	}
 49}
 50function zonkit(el, xid) {
 51	el.innerHTML = "zonked"
 52	el.disabled = true
 53	post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "zonk", "what": xid}))
 54	var p = el
 55	while (p && p.tagName != "ARTICLE") {
 56		p = p.parentElement
 57	}
 58	if (p) {
 59		p.remove()
 60	}
 61}
 62function flogit(el, how, xid) {
 63	var s = how
 64	if (s[s.length-1] != "e") { s += "e" }
 65	s += "d"
 66	if (s == "untaged") s = "untagged"
 67	if (s == "reacted") s = "badonked"
 68	el.innerHTML = s
 69	el.disabled = true
 70	post("/zonkit", encode({"CSRF": csrftoken, "wherefore": how, "what": xid}))
 71}
 72
 73var lehonkform = document.getElementById("honkform")
 74var lehonkbutton = document.getElementById("honkingtime")
 75
 76function oldestnewest(btn) {
 77	var els = document.getElementsByClassName("glow")
 78	if (els.length) {
 79		els[els.length-1].scrollIntoView()
 80	}
 81}
 82function removeglow() {
 83	var els = document.getElementsByClassName("glow")
 84	while (els.length) {
 85		els[0].classList.remove("glow")
 86	}
 87}
 88
 89function fillinhonks(xhr, glowit) {
 90	var resp = xhr.response
 91	var stash = curpagestate.name + ":" + curpagestate.arg
 92	tophid[stash] = resp.Tophid
 93	var doc = document.createElement( 'div' );
 94	doc.innerHTML = resp.Srvmsg
 95	var srvmsg = doc
 96	doc = document.createElement( 'div' );
 97	doc.innerHTML = resp.Honks
 98	var honks = doc.children
 99
100	var mecount = document.getElementById("mecount")
101	if (resp.MeCount) {
102		mecount.innerHTML = "(" + resp.MeCount + ")"
103	} else {
104		mecount.innerHTML = ""
105	}
106	var chatcount = document.getElementById("chatcount")
107	if (resp.ChatCount) {
108		chatcount.innerHTML = "(" + resp.ChatCount + ")"
109	} else {
110		chatcount.innerHTML = ""
111	}
112
113	var srvel = document.getElementById("srvmsg")
114	while (srvel.children[0]) {
115		srvel.children[0].remove()
116	}
117	srvel.prepend(srvmsg)
118
119	var frontload = true
120	if (curpagestate.name == "convoy") {
121		frontload = false
122	}
123
124	var honksonpage = document.getElementById("honksonpage")
125	var holder = honksonpage.children[0]
126	var lenhonks = honks.length
127	for (var i = honks.length; i > 0; i--) {
128		var h = honks[i-1]
129		if (glowit)
130			h.classList.add("glow")
131		if (frontload) {
132			holder.prepend(h)
133		} else {
134			holder.append(h)
135		}
136			
137	}
138	relinklinks()
139	return lenhonks
140}
141function hydrargs() {
142	var name = curpagestate.name
143	var arg = curpagestate.arg
144	var args = { "page" : name }
145	if (name == "convoy") {
146		args["c"] = arg
147	} else if (name == "combo") {
148		args["c"] = arg
149	} else if (name == "honker") {
150		args["xid"] = arg
151	}
152	return args
153}
154function refreshupdate(msg) {
155	var el = document.querySelector("#refreshbox p span")
156	if (el) {
157		el.innerHTML = msg
158	}
159}
160function refreshhonks(btn) {
161	removeglow()
162	btn.innerHTML = "refreshing"
163	btn.disabled = true
164	var args = hydrargs()
165	var stash = curpagestate.name + ":" + curpagestate.arg
166	args["tophid"] = tophid[stash]
167	get("/hydra?" + encode(args), function(xhr) {
168		btn.innerHTML = "refresh"
169		btn.disabled = false
170		if (xhr.status == 200) {
171			var lenhonks = fillinhonks(xhr, true)
172			refreshupdate(" " + lenhonks + " new")
173		} else {
174			refreshupdate(" status: " + xhr.status)
175		}
176	}, function(xhr, e) {
177		btn.innerHTML = "refresh"
178		btn.disabled = false
179		refreshupdate(" timed out")
180	})
181}
182function statechanger(evt) {
183	var data = evt.state
184	if (!data) {
185		return
186	}
187	switchtopage(data.name, data.arg)
188}
189function switchtopage(name, arg) {
190	var stash = curpagestate.name + ":" + curpagestate.arg
191	var honksonpage = document.getElementById("honksonpage")
192	var holder = honksonpage.children[0]
193	holder.remove()
194	var srvel = document.getElementById("srvmsg")
195	var msg = srvel.children[0]
196	if (msg) {
197		msg.remove()
198		servermsgs[stash] = msg
199	}
200	showelement("refreshbox")
201
202	honksforpage[stash] = holder
203
204	curpagestate.name = name
205	curpagestate.arg = arg
206	// get the holder for the target page
207	var stash = name + ":" + arg
208	holder = honksforpage[stash]
209	if (holder) {
210		honksonpage.prepend(holder)
211		msg = servermsgs[stash]
212		if (msg) {
213			srvel.prepend(msg)
214		}
215	} else {
216		// or create one and fill it
217		honksonpage.prepend(document.createElement("div"))
218		var args = hydrargs()
219		get("/hydra?" + encode(args), function(xhr) {
220			if (xhr.status == 200) {
221				var lenhonks = fillinhonks(xhr, false)
222			} else {
223				refreshupdate(" status: " + xhr.status)
224			}
225		}, function(xhr, e) {
226			refreshupdate(" timed out")
227		})
228	}
229	refreshupdate("")
230}
231function newpagestate(name, arg) {
232	return { "name": name, "arg": arg }
233}
234function pageswitcher(name, arg) {
235	return function(evt) {
236		var topmenu = document.getElementById("topmenu")
237		topmenu.open = false
238		if (name == curpagestate.name && arg == curpagestate.arg) {
239			return false
240		}
241		switchtopage(name, arg)
242		var url = evt.srcElement.href
243		if (!url) {
244			url = evt.srcElement.parentElement.href
245		}
246		history.pushState(newpagestate(name, arg), "some title", url)
247		window.scrollTo(0, 0)
248		return false
249	}
250}
251function relinklinks() {
252	var els = document.getElementsByClassName("convoylink")
253	while (els.length) {
254		els[0].onclick = pageswitcher("convoy", els[0].text)
255		els[0].classList.remove("convoylink")
256	}
257	els = document.getElementsByClassName("combolink")
258	while (els.length) {
259		els[0].onclick = pageswitcher("combo", els[0].text)
260		els[0].classList.remove("combolink")
261	}
262	els = document.getElementsByClassName("honkerlink")
263	while (els.length) {
264		var el = els[0]
265		var xid = el.getAttribute("data-xid")
266		el.onclick = pageswitcher("honker", xid)
267		el.classList.remove("honkerlink")
268	}
269}
270(function() {
271	var el = document.getElementById("homelink")
272	el.onclick = pageswitcher("home", "")
273	el = document.getElementById("atmelink")
274	el.onclick = pageswitcher("atme", "")
275	el = document.getElementById("firstlink")
276	el.onclick = pageswitcher("first", "")
277	el = document.getElementById("savedlink")
278	el.onclick = pageswitcher("saved", "")
279	el = document.getElementById("longagolink")
280	el.onclick = pageswitcher("longago", "")
281	relinklinks()
282	window.onpopstate = statechanger
283	history.replaceState(curpagestate, "some title", "")
284})();
285(function() {
286	hideelement("donkdescriptor")
287})();
288function showhonkform(elem, rid, hname) {
289	var form = lehonkform
290	form.style = "display: block"
291	if (elem) {
292		form.remove()
293		elem.parentElement.parentElement.parentElement.insertAdjacentElement('beforebegin', form)
294	} else {
295		hideelement(lehonkbutton)
296		elem = document.getElementById("honkformhost")
297		elem.insertAdjacentElement('afterend', form)
298	}
299	var ridinput = document.getElementById("ridinput")
300	if (rid) {
301		ridinput.value = rid
302		if (hname) {
303			honknoise.value = hname + " "
304		} else {
305			honknoise.value = ""
306		}
307	} else {
308		ridinput.value = ""
309		honknoise.value = ""
310	}
311	var updateinput = document.getElementById("updatexidinput")
312	updateinput.value = ""
313	document.getElementById("honknoise").focus()
314	return false
315}
316function cancelhonking() {
317	hideelement(lehonkform)
318	showelement(lehonkbutton)
319}
320function showelement(el) {
321	if (typeof(el) == "string")
322		el = document.getElementById(el)
323	if (!el) return
324	el.style.display = "block"
325}
326function hideelement(el) {
327	if (typeof(el) == "string")
328		el = document.getElementById(el)
329	if (!el) return
330	el.style.display = "none"
331}
332function updatedonker() {
333	var el = document.getElementById("donker")
334	el.children[1].textContent = el.children[0].value.slice(-20)
335	var el = document.getElementById("donkdescriptor")
336	el.style.display = ""
337	var el = document.getElementById("saveddonkxid")
338	el.value = ""
339}
340var checkinprec = 100.0
341var gpsoptions = {
342	enableHighAccuracy: false,
343	timeout: 1000,
344	maximumAge: 0
345};
346function fillcheckin() {
347	if (navigator.geolocation) {
348		navigator.geolocation.getCurrentPosition(function(pos) {
349			showelement("placedescriptor")
350			var el = document.getElementById("placelatinput")
351			el.value = Math.round(pos.coords.latitude * checkinprec) / checkinprec
352			el = document.getElementById("placelonginput")
353			el.value = Math.round(pos.coords.longitude * checkinprec) / checkinprec
354			checkinprec = 10000.0
355			gpsoptions.enableHighAccuracy = true
356			gpsoptions.timeout = 2000
357		}, function(err) {
358			showelement("placedescriptor")
359			el = document.getElementById("placenameinput")
360			el.value = err.message
361		}, gpsoptions)
362	}
363}