all repos — honk @ 7fc65173054ff234db65c78f211c7369c567eeff

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