all repos — honk @ v0.9.3

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	el = document.getElementById("longagolink")
242	el.onclick = pageswitcher("longago", "")
243	relinklinks()
244	window.onpopstate = statechanger
245	history.replaceState(curpagestate, "some title", "")
246})();
247(function() {
248	hideelement("donkdescriptor")
249})();
250function showhonkform(elem, rid, hname) {
251	var form = lehonkform
252	form.style = "display: block"
253	if (elem) {
254		form.remove()
255		elem.parentElement.parentElement.parentElement.insertAdjacentElement('beforebegin', form)
256	} else {
257		hideelement(lehonkbutton)
258		elem = document.getElementById("honkformhost")
259		elem.insertAdjacentElement('afterend', form)
260	}
261	var ridinput = document.getElementById("ridinput")
262	if (rid) {
263		ridinput.value = rid
264		honknoise.value = hname + " "
265	} else {
266		ridinput.value = ""
267		honknoise.value = ""
268	}
269	var updateinput = document.getElementById("updatexidinput")
270	updateinput.value = ""
271	document.getElementById("honknoise").focus()
272	return false
273}
274function cancelhonking() {
275	hideelement(lehonkform)
276	showelement(lehonkbutton)
277}
278function showelement(el) {
279	if (typeof(el) == "string")
280		el = document.getElementById(el)
281	if (!el) return
282	el.style.display = "block"
283}
284function hideelement(el) {
285	if (typeof(el) == "string")
286		el = document.getElementById(el)
287	if (!el) return
288	el.style.display = "none"
289}
290function updatedonker() {
291	var el = document.getElementById("donker")
292	el.children[1].textContent = el.children[0].value.slice(-20)
293	var el = document.getElementById("donkdescriptor")
294	el.style.display = ""
295	var el = document.getElementById("saveddonkxid")
296	el.value = ""
297}
298var checkinprec = 100.0
299var gpsoptions = {
300	enableHighAccuracy: false,
301	timeout: 1000,
302	maximumAge: 0
303};
304function fillcheckin() {
305	if (navigator.geolocation) {
306		navigator.geolocation.getCurrentPosition(function(pos) {
307			showelement("placedescriptor")
308			var el = document.getElementById("placelatinput")
309			el.value = Math.round(pos.coords.latitude * checkinprec) / checkinprec
310			el = document.getElementById("placelonginput")
311			el.value = Math.round(pos.coords.longitude * checkinprec) / checkinprec
312			checkinprec = 10000.0
313			gpsoptions.enableHighAccuracy = true
314			gpsoptions.timeout = 2000
315		}, function(err) {
316			showelement("placedescriptor")
317			el = document.getElementById("placenameinput")
318			el.value = err.message
319		}, gpsoptions)
320	}
321}