all repos — honk @ 95a60dea408cd956190b04edd9ff05e728248bf0

my fork of honk

views/honkpage.js (view raw)

  1var csrftoken = ""
  2var honksforpage = { }
  3var curpagestate = { name: "", arg : "" }
  4var tophid = { }
  5var servermsgs = { }
  6
  7function encode(hash) {
  8	var s = []
  9	for (var key in hash) {
 10		var val = hash[key]
 11		s.push(encodeURIComponent(key) + "=" + encodeURIComponent(val))
 12	}
 13	return s.join("&")
 14}
 15function post(url, data) {
 16	var x = new XMLHttpRequest()
 17	x.open("POST", url)
 18	x.timeout = 30 * 1000
 19	x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
 20	x.send(data)
 21}
 22function get(url, whendone, errfunction) {
 23	var x = new XMLHttpRequest()
 24	x.open("GET", url)
 25	x.timeout = 15 * 1000
 26	x.responseType = "json"
 27	x.onload = function() { whendone(x) }
 28	if (errfunction) {
 29		x.ontimeout = function(e) { errfunction(" timed out") }
 30		x.onerror = function(e) { errfunction(" error") }
 31	}
 32	x.send()
 33}
 34function bonk(el, xid) {
 35	el.innerHTML = "bonked"
 36	el.disabled = true
 37	post("/bonk", encode({"js": "2", "CSRF": csrftoken, "xid": xid}))
 38	return false
 39}
 40function unbonk(el, xid) {
 41	el.innerHTML = "unbonked"
 42	el.disabled = true
 43	post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "unbonk", "what": xid}))
 44}
 45function muteit(el, convoy) {
 46	el.innerHTML = "muted"
 47	el.disabled = true
 48	post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "zonvoy", "what": convoy}))
 49	var els = document.querySelectorAll('article.honk')
 50	for (var i = 0; i < els.length; i++) {
 51		var e = els[i]
 52		if (e.getAttribute("data-convoy") == convoy) {
 53			e.remove()
 54		}
 55	}
 56}
 57function zonkit(el, xid) {
 58	el.innerHTML = "zonked"
 59	el.disabled = true
 60	post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "zonk", "what": xid}))
 61	var p = el
 62	while (p && p.tagName != "ARTICLE") {
 63		p = p.parentElement
 64	}
 65	if (p) {
 66		p.remove()
 67	}
 68}
 69function flogit(el, how, xid) {
 70	var s = how
 71	if (s[s.length-1] != "e") { s += "e" }
 72	s += "d"
 73	if (s == "untaged") s = "untagged"
 74	if (s == "reacted") s = "badonked"
 75	el.innerHTML = s
 76	el.disabled = true
 77	post("/zonkit", encode({"CSRF": csrftoken, "wherefore": how, "what": xid}))
 78}
 79
 80var lehonkform = document.getElementById("honkform")
 81var lehonkbutton = document.getElementById("honkingtime")
 82
 83function oldestnewest(btn) {
 84	var els = document.getElementsByClassName("glow")
 85	if (els.length) {
 86		els[els.length-1].scrollIntoView({ behavior: "smooth" })
 87	}
 88}
 89function removeglow() {
 90	var els = document.getElementsByClassName("glow")
 91	while (els.length) {
 92		els[0].classList.remove("glow")
 93	}
 94}
 95
 96function fillinhonks(xhr, glowit) {
 97	var resp = xhr.response
 98	var stash = curpagestate.name + ":" + curpagestate.arg
 99	tophid[stash] = resp.Tophid
100	var doc = document.createElement( 'div' );
101	doc.innerHTML = resp.Srvmsg
102	var srvmsg = doc
103	doc = document.createElement( 'div' );
104	doc.innerHTML = resp.Honks
105	var honks = doc.children
106
107	var mecount = document.getElementById("mecount")
108	if (resp.MeCount) {
109		mecount.innerHTML = "(" + resp.MeCount + ")"
110	} else {
111		mecount.innerHTML = ""
112	}
113	var chatcount = document.getElementById("chatcount")
114	if (resp.ChatCount) {
115		chatcount.innerHTML = "(" + resp.ChatCount + ")"
116	} else {
117		chatcount.innerHTML = ""
118	}
119
120	var srvel = document.getElementById("srvmsg")
121	while (srvel.children[0]) {
122		srvel.children[0].remove()
123	}
124	srvel.prepend(srvmsg)
125
126	var frontload = true
127	if (curpagestate.name == "convoy") {
128		frontload = false
129	}
130
131	var honksonpage = document.getElementById("honksonpage")
132	var holder = honksonpage.children[0]
133	var lenhonks = honks.length
134	for (var i = honks.length; i > 0; i--) {
135		var h = honks[frontload ? i-1 : 0]
136		if (glowit)
137			h.classList.add("glow")
138		if (frontload) {
139			holder.prepend(h)
140		} else {
141			holder.append(h)
142		}
143	}
144	relinklinks()
145	return lenhonks
146}
147function hydrargs() {
148	var name = curpagestate.name
149	var arg = curpagestate.arg
150	var args = { "page" : name }
151	if (name == "convoy") {
152		args["c"] = arg
153	} else if (name == "combo") {
154		args["c"] = arg
155	} else if (name == "honker") {
156		args["xid"] = arg
157	} else if (name == "user") {
158		args["uname"] = arg
159	}
160	return args
161}
162function refreshupdate(msg) {
163	var el = document.querySelector("#refreshbox p span")
164	if (el) {
165		el.innerHTML = msg
166	}
167}
168function refreshhonks(btn) {
169	removeglow()
170	btn.innerHTML = "refreshing"
171	btn.disabled = true
172	var args = hydrargs()
173	var stash = curpagestate.name + ":" + curpagestate.arg
174	args["tophid"] = tophid[stash]
175	get("/hydra?" + encode(args), function(xhr) {
176		btn.innerHTML = "refresh"
177		btn.disabled = false
178		if (xhr.status == 200) {
179			var lenhonks = fillinhonks(xhr, true)
180			refreshupdate(" " + lenhonks + " new")
181		} else {
182			refreshupdate(" status: " + xhr.status)
183		}
184	}, function(err) {
185		btn.innerHTML = "refresh"
186		btn.disabled = false
187		refreshupdate(err)
188	})
189}
190function statechanger(evt) {
191	var data = evt.state
192	if (!data) {
193		return
194	}
195	switchtopage(data.name, data.arg)
196}
197function switchtopage(name, arg) {
198	var stash = curpagestate.name + ":" + curpagestate.arg
199	var honksonpage = document.getElementById("honksonpage")
200	var holder = honksonpage.children[0]
201	holder.remove()
202	var srvel = document.getElementById("srvmsg")
203	var msg = srvel.children[0]
204	if (msg) {
205		msg.remove()
206		servermsgs[stash] = msg
207	}
208	showelement("refreshbox")
209
210	honksforpage[stash] = holder
211
212	curpagestate.name = name
213	curpagestate.arg = arg
214	// get the holder for the target page
215	stash = name + ":" + arg
216	holder = honksforpage[stash]
217	if (holder) {
218		honksonpage.prepend(holder)
219		msg = servermsgs[stash]
220		if (msg) {
221			srvel.prepend(msg)
222		}
223	} else {
224		// or create one and fill it
225		honksonpage.prepend(document.createElement("div"))
226		var args = hydrargs()
227		get("/hydra?" + encode(args), function(xhr) {
228			if (xhr.status == 200) {
229				fillinhonks(xhr, false)
230			} else {
231				refreshupdate(" status: " + xhr.status)
232			}
233		}, function(err) {
234			refreshupdate(err)
235		})
236	}
237	refreshupdate("")
238}
239function newpagestate(name, arg) {
240	return { "name": name, "arg": arg }
241}
242function pageswitcher(name, arg) {
243	return function(evt) {
244		var topmenu = document.getElementById("topmenu")
245		topmenu.open = false
246		if (name == curpagestate.name && arg == curpagestate.arg) {
247			return false
248		}
249		switchtopage(name, arg)
250		var url = evt.srcElement.href
251		if (!url) {
252			url = evt.srcElement.parentElement.href
253		}
254		history.pushState(newpagestate(name, arg), "some title", url)
255		window.scrollTo(0, 0)
256		return false
257	}
258}
259function relinklinks() {
260	var els = document.getElementsByClassName("convoylink")
261	while (els.length) {
262		els[0].onclick = pageswitcher("convoy", els[0].text)
263		els[0].classList.remove("convoylink")
264	}
265	els = document.getElementsByClassName("combolink")
266	while (els.length) {
267		els[0].onclick = pageswitcher("combo", els[0].text)
268		els[0].classList.remove("combolink")
269	}
270	els = document.getElementsByClassName("honkerlink")
271	while (els.length) {
272		var el = els[0]
273		var xid = el.getAttribute("data-xid")
274		el.onclick = pageswitcher("honker", xid)
275		el.classList.remove("honkerlink")
276	}
277	els = document.getElementsByClassName("donklink")
278	while (els.length) {
279		let el = els[0]
280		el.onclick = function() {
281			el.classList.remove("donk")
282			el.onclick = null
283			return false
284		}
285		el.classList.remove("donklink")
286	}
287
288	els = document.querySelectorAll("#honksonpage article button")
289	els.forEach(function(el) {
290		var honk = el.closest("article")
291		var convoy = honk.dataset.convoy
292		var hname = honk.dataset.hname
293		var xid = honk.dataset.xid
294		var id = Number(honk.dataset.id)
295
296		if (!(id > 0)) {
297			console.error("could not determine honk id")
298			return
299		}
300
301		if (el.classList.contains("unbonk")) {
302			el.onclick = function() {
303				unbonk(el, xid);
304			}
305		} else if (el.classList.contains("bonk")) {
306			el.onclick = function() {
307				bonk(el, xid)
308			}
309		} else if (el.classList.contains("honkback")) {
310			el.onclick = function() {
311				return showhonkform(el, xid, hname)
312			}
313		} else if (el.classList.contains("mute")) {
314			el.onclick = function() {
315				muteit(el, convoy);
316			}
317		} else if (el.classList.contains("evenmore")) {
318			var more = document.querySelector("#evenmore"+id);
319			el.onclick = function() {
320				more.classList.toggle("hide");
321			}
322		} else if (el.classList.contains("zonk")) {
323			el.onclick = function() {
324				zonkit(el, xid);
325			}
326		} else if (el.classList.contains("flogit-deack")) {
327			el.onclick = function() {
328				flogit(el, "deack", xid);
329			}
330		} else if (el.classList.contains("flogit-ack")) {
331			el.onclick = function() {
332				flogit(el, "ack", xid);
333			}
334		} else if (el.classList.contains("flogit-unsave")) {
335			el.onclick = function() {
336				flogit(el, "unsave", xid);
337			}
338		} else if (el.classList.contains("flogit-save")) {
339			el.onclick = function() {
340				flogit(el, "save", xid);
341			}
342		} else if (el.classList.contains("flogit-untag")) {
343			el.onclick = function() {
344				flogit(el, "untag", xid);
345			}
346		} else if (el.classList.contains("flogit-react")) {
347			el.onclick = function() {
348				flogit(el, "react", xid);
349			}
350		}
351	})
352}
353function showhonkform(elem, rid, hname) {
354	var form = lehonkform
355	form.style = "display: block"
356	if (elem) {
357		form.remove()
358		elem.parentElement.parentElement.parentElement.insertAdjacentElement('beforebegin', form)
359	} else {
360		hideelement(lehonkbutton)
361		elem = document.getElementById("honkformhost")
362		elem.insertAdjacentElement('afterend', form)
363	}
364	var ridinput = document.getElementById("ridinput")
365	var honknoise = document.getElementById("honknoise")
366	if (rid) {
367		ridinput.value = rid
368		if (hname) {
369			honknoise.value = hname + " "
370		} else {
371			honknoise.value = ""
372		}
373	} else {
374		ridinput.value = ""
375		honknoise.value = ""
376	}
377	var updateinput = document.getElementById("updatexidinput")
378	updateinput.value = ""
379	honknoise.focus()
380	return false
381}
382function cancelhonking() {
383	hideelement(lehonkform)
384	showelement(lehonkbutton)
385}
386function showelement(el) {
387	if (typeof(el) == "string")
388		el = document.getElementById(el)
389	if (!el) return
390	el.style.display = "block"
391}
392function hideelement(el) {
393	if (typeof(el) == "string")
394		el = document.getElementById(el)
395	if (!el) return
396	el.style.display = "none"
397}
398function updatedonker(ev) {
399	var el = ev.target.parentElement
400	el.children[1].textContent = el.children[0].value.slice(-20)
401	el = el.nextSibling
402	el.value = ""
403	el = el.parentElement.nextSibling
404	el.style.display = ""
405}
406var checkinprec = 100.0
407var gpsoptions = {
408	enableHighAccuracy: false,
409	timeout: 1000,
410	maximumAge: 0
411};
412function fillcheckin() {
413	if (navigator.geolocation) {
414		navigator.geolocation.getCurrentPosition(function(pos) {
415			showelement("placedescriptor")
416			var el = document.getElementById("placelatinput")
417			el.value = Math.round(pos.coords.latitude * checkinprec) / checkinprec
418			el = document.getElementById("placelonginput")
419			el.value = Math.round(pos.coords.longitude * checkinprec) / checkinprec
420			checkinprec = 10000.0
421			gpsoptions.enableHighAccuracy = true
422			gpsoptions.timeout = 2000
423		}, function(err) {
424			showelement("placedescriptor")
425			var el = document.getElementById("placenameinput")
426			el.value = err.message
427		}, gpsoptions)
428	}
429}
430
431function scrollnexthonk() {
432	var honks = document.getElementsByClassName("honk");
433	for (var i = 0; i < honks.length; i++) {
434		var h = honks[i];
435		var b = h.getBoundingClientRect();
436		if (b.top > 1.0) {
437			h.scrollIntoView()
438			var a = h.querySelector(".actions summary")
439			if (a) a.focus({ preventScroll: true })
440			break
441		}
442	}
443}
444
445function scrollprevioushonk() {
446	var honks = document.getElementsByClassName("honk");
447	for (var i = 1; i < honks.length; i++) {
448		var b = honks[i].getBoundingClientRect();
449		if (b.top > -1.0) {
450			honks[i-1].scrollIntoView()
451			var a = honks[i-1].querySelector(".actions summary")
452			if (a) a.focus({ preventScroll: true })
453			break
454		}
455	}
456}
457
458function hotkey(e) {
459	if (e.ctrlKey || e.altKey)
460		return
461	if (e.code == "Escape") {
462		var menu = document.getElementById("topmenu")
463		menu.open = false
464		return
465	}
466	if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement)
467		return
468
469	switch (e.code) {
470	case "KeyR":
471		refreshhonks(document.getElementById("honkrefresher"));
472		break;
473	case "KeyS":
474		oldestnewest(document.getElementById("newerscroller"));
475		break;
476	case "KeyJ":
477		scrollnexthonk();
478		break;
479	case "KeyK":
480		scrollprevioushonk();
481		break;
482	case "KeyM":
483		var menu = document.getElementById("topmenu")
484		if (!menu.open) {
485			menu.open = true
486			menu.querySelector("a").focus()
487		} else {
488			menu.open = false
489		}
490		break
491	case "Slash":
492		document.getElementById("topmenu").open = true
493		document.getElementById("searchbox").focus()
494		e.preventDefault()
495		break
496	}
497}
498
499document.addEventListener("keydown", hotkey)
500
501function addemu(elem) {
502	const data = elem.alt
503	const box = document.getElementById("honknoise");
504	box.value += data;
505}
506function loademus() {
507	var div = document.getElementById("emupicker")
508	var request = new XMLHttpRequest()
509	request.open('GET', '/emus')
510	request.onload = function() {
511		div.innerHTML = request.responseText
512		div.querySelectorAll(".emu").forEach(function(el) {
513			el.onclick = function() {
514				addemu(el)
515			}
516		})
517	}
518	if (div.style.display === "none") {
519		div.style.display = "block";
520	} else {
521		div.style.display = "none";
522	}
523	request.send()
524}
525
526// init
527(function() {
528	var me = document.currentScript;
529	csrftoken = me.dataset.csrf
530	curpagestate.name = me.dataset.pagename
531	curpagestate.arg = me.dataset.pagearg
532	tophid[curpagestate.name + ":" + curpagestate.arg] = me.dataset.tophid
533	servermsgs[curpagestate.name + ":" + curpagestate.arg] = me.dataset.srvmsg
534
535	var el = document.getElementById("homelink")
536	el.onclick = pageswitcher("home", "")
537	el = document.getElementById("atmelink")
538	el.onclick = pageswitcher("atme", "")
539	el = document.getElementById("firstlink")
540	el.onclick = pageswitcher("first", "")
541	el = document.getElementById("savedlink")
542	el.onclick = pageswitcher("saved", "")
543	el = document.getElementById("longagolink")
544	el.onclick = pageswitcher("longago", "")
545
546	var totop = document.querySelector(".nophone")
547	if (totop) {
548		totop.onclick = function() {
549			window.scrollTo(0,0)
550		}
551	}
552
553	var refreshbox = document.getElementById("refreshbox")
554	if (refreshbox) {
555		refreshbox.querySelectorAll("button").forEach(function(el) {
556			if (el.classList.contains("refresh")) {
557				el.onclick = function() {
558					refreshhonks(el)
559				}
560			} else if (el.classList.contains("scrolldown")) {
561				el.onclick = function() {
562					oldestnewest(el)
563				}
564			}
565		})
566
567		if (me.dataset.srvmsg == "one honk maybe more") {
568			hideelement(refreshbox)
569		}
570	}
571
572	var td = document.getElementById("timedescriptor")
573	document.getElementById("addtimebutton").onclick = function() {
574		td.classList.toggle("hide")
575	}
576	document.getElementById("honkingtime").onclick = function() {
577		return showhonkform()
578	}
579	document.getElementById("checkinbutton").onclick = fillcheckin
580	document.getElementById("emuload").onclick = loademus
581	document.querySelector("#donker input").onchange = updatedonker
582	document.querySelector("button[name=cancel]").onclick = cancelhonking
583
584	relinklinks()
585	window.onpopstate = statechanger
586	history.replaceState(curpagestate, "some title", "")
587
588	hideelement("donkdescriptor")
589})();