all repos — honk @ 1dab6623e5cfb202c33a976d3dd63f6f89f6473a

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	} else if (name == "user") {
152		args["uname"] = arg
153	}
154	return args
155}
156function refreshupdate(msg) {
157	var el = document.querySelector("#refreshbox p span")
158	if (el) {
159		el.innerHTML = msg
160	}
161}
162function refreshhonks(btn) {
163	removeglow()
164	btn.innerHTML = "refreshing"
165	btn.disabled = true
166	var args = hydrargs()
167	var stash = curpagestate.name + ":" + curpagestate.arg
168	args["tophid"] = tophid[stash]
169	get("/hydra?" + encode(args), function(xhr) {
170		btn.innerHTML = "refresh"
171		btn.disabled = false
172		if (xhr.status == 200) {
173			var lenhonks = fillinhonks(xhr, true)
174			refreshupdate(" " + lenhonks + " new")
175		} else {
176			refreshupdate(" status: " + xhr.status)
177		}
178	}, function(xhr, e) {
179		btn.innerHTML = "refresh"
180		btn.disabled = false
181		refreshupdate(" timed out")
182	})
183}
184function statechanger(evt) {
185	var data = evt.state
186	if (!data) {
187		return
188	}
189	switchtopage(data.name, data.arg)
190}
191function switchtopage(name, arg) {
192	var stash = curpagestate.name + ":" + curpagestate.arg
193	var honksonpage = document.getElementById("honksonpage")
194	var holder = honksonpage.children[0]
195	holder.remove()
196	var srvel = document.getElementById("srvmsg")
197	var msg = srvel.children[0]
198	if (msg) {
199		msg.remove()
200		servermsgs[stash] = msg
201	}
202	showelement("refreshbox")
203
204	honksforpage[stash] = holder
205
206	curpagestate.name = name
207	curpagestate.arg = arg
208	// get the holder for the target page
209	var stash = name + ":" + arg
210	holder = honksforpage[stash]
211	if (holder) {
212		honksonpage.prepend(holder)
213		msg = servermsgs[stash]
214		if (msg) {
215			srvel.prepend(msg)
216		}
217	} else {
218		// or create one and fill it
219		honksonpage.prepend(document.createElement("div"))
220		var args = hydrargs()
221		get("/hydra?" + encode(args), function(xhr) {
222			if (xhr.status == 200) {
223				var lenhonks = fillinhonks(xhr, false)
224			} else {
225				refreshupdate(" status: " + xhr.status)
226			}
227		}, function(xhr, e) {
228			refreshupdate(" timed out")
229		})
230	}
231	refreshupdate("")
232}
233function newpagestate(name, arg) {
234	return { "name": name, "arg": arg }
235}
236function pageswitcher(name, arg) {
237	return function(evt) {
238		var topmenu = document.getElementById("topmenu")
239		topmenu.open = false
240		if (name == curpagestate.name && arg == curpagestate.arg) {
241			return false
242		}
243		switchtopage(name, arg)
244		var url = evt.srcElement.href
245		if (!url) {
246			url = evt.srcElement.parentElement.href
247		}
248		history.pushState(newpagestate(name, arg), "some title", url)
249		window.scrollTo(0, 0)
250		return false
251	}
252}
253function relinklinks() {
254	var els = document.getElementsByClassName("convoylink")
255	while (els.length) {
256		els[0].onclick = pageswitcher("convoy", els[0].text)
257		els[0].classList.remove("convoylink")
258	}
259	els = document.getElementsByClassName("combolink")
260	while (els.length) {
261		els[0].onclick = pageswitcher("combo", els[0].text)
262		els[0].classList.remove("combolink")
263	}
264	els = document.getElementsByClassName("honkerlink")
265	while (els.length) {
266		var el = els[0]
267		var xid = el.getAttribute("data-xid")
268		el.onclick = pageswitcher("honker", xid)
269		el.classList.remove("honkerlink")
270	}
271}
272(function() {
273	var el = document.getElementById("homelink")
274	el.onclick = pageswitcher("home", "")
275	el = document.getElementById("atmelink")
276	el.onclick = pageswitcher("atme", "")
277	el = document.getElementById("firstlink")
278	el.onclick = pageswitcher("first", "")
279	el = document.getElementById("savedlink")
280	el.onclick = pageswitcher("saved", "")
281	el = document.getElementById("longagolink")
282	el.onclick = pageswitcher("longago", "")
283	relinklinks()
284	window.onpopstate = statechanger
285	history.replaceState(curpagestate, "some title", "")
286})();
287(function() {
288	hideelement("donkdescriptor")
289})();
290function showhonkform(elem, rid, hname) {
291	var form = lehonkform
292	form.style = "display: block"
293	if (elem) {
294		form.remove()
295		elem.parentElement.parentElement.parentElement.insertAdjacentElement('beforebegin', form)
296	} else {
297		hideelement(lehonkbutton)
298		elem = document.getElementById("honkformhost")
299		elem.insertAdjacentElement('afterend', form)
300	}
301	var ridinput = document.getElementById("ridinput")
302	if (rid) {
303		ridinput.value = rid
304		if (hname) {
305			honknoise.value = hname + " "
306		} else {
307			honknoise.value = ""
308		}
309	} else {
310		ridinput.value = ""
311		honknoise.value = ""
312	}
313	var updateinput = document.getElementById("updatexidinput")
314	updateinput.value = ""
315	document.getElementById("honknoise").focus()
316	return false
317}
318function cancelhonking() {
319	hideelement(lehonkform)
320	showelement(lehonkbutton)
321}
322function showelement(el) {
323	if (typeof(el) == "string")
324		el = document.getElementById(el)
325	if (!el) return
326	el.style.display = "block"
327}
328function hideelement(el) {
329	if (typeof(el) == "string")
330		el = document.getElementById(el)
331	if (!el) return
332	el.style.display = "none"
333}
334function updatedonker() {
335	var el = document.getElementById("donker")
336	el.children[1].textContent = el.children[0].value.slice(-20)
337	var el = document.getElementById("donkdescriptor")
338	el.style.display = ""
339	var el = document.getElementById("saveddonkxid")
340	el.value = ""
341}
342var checkinprec = 100.0
343var gpsoptions = {
344	enableHighAccuracy: false,
345	timeout: 1000,
346	maximumAge: 0
347};
348function fillcheckin() {
349	if (navigator.geolocation) {
350		navigator.geolocation.getCurrentPosition(function(pos) {
351			showelement("placedescriptor")
352			var el = document.getElementById("placelatinput")
353			el.value = Math.round(pos.coords.latitude * checkinprec) / checkinprec
354			el = document.getElementById("placelonginput")
355			el.value = Math.round(pos.coords.longitude * checkinprec) / checkinprec
356			checkinprec = 10000.0
357			gpsoptions.enableHighAccuracy = true
358			gpsoptions.timeout = 2000
359		}, function(err) {
360			showelement("placedescriptor")
361			el = document.getElementById("placenameinput")
362			el.value = err.message
363		}, gpsoptions)
364	}
365}