all repos — honk @ e408e753ee672520e4808fda6e138ec64ec50a98

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