all repos — honk @ 36ef97d65f05da2974d350425df73dd7a4140b4c

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