all repos — honk @ 526c2ef45b0142a38c78fa2e848cc1abd2573181

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