all repos — honk @ 552c9898818450dd43dbfe145f6dcdb059e8b416

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