all repos — honk @ 48ac9639a4f9d65bd227055f14b54fbf938b5fe6

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	var elmentions = document.getElementById("mentions")
261	if (rid) {
262		ridinput.value = rid
263		elmentions.value = hname
264	} else {
265		ridinput.value = ""
266		honknoise.value = ""
267	}
268	var updateinput = document.getElementById("updatexidinput")
269	updateinput.value = ""
270	document.getElementById("honknoise").focus()
271	return false
272}
273function cancelhonking() {
274	hideelement(lehonkform)
275	showelement(lehonkbutton)
276}
277function showelement(el) {
278	if (typeof(el) == "string")
279		el = document.getElementById(el)
280	if (!el) return
281	el.style.display = "block"
282}
283function hideelement(el) {
284	if (typeof(el) == "string")
285		el = document.getElementById(el)
286	if (!el) return
287	el.style.display = "none"
288}
289function updatedonker() {
290	var el = document.getElementById("donker")
291	el.children[1].textContent = el.children[0].value.slice(-20)
292	var el = document.getElementById("donkdescriptor")
293	el.style.display = ""
294	var el = document.getElementById("saveddonkxid")
295	el.value = ""
296}
297var checkinprec = 100.0
298var gpsoptions = {
299	enableHighAccuracy: false,
300	timeout: 1000,
301	maximumAge: 0
302};
303function fillcheckin() {
304	if (navigator.geolocation) {
305		navigator.geolocation.getCurrentPosition(function(pos) {
306			showelement("placedescriptor")
307			var el = document.getElementById("placelatinput")
308			el.value = Math.round(pos.coords.latitude * checkinprec) / checkinprec
309			el = document.getElementById("placelonginput")
310			el.value = Math.round(pos.coords.longitude * checkinprec) / checkinprec
311			checkinprec = 10000.0
312			gpsoptions.enableHighAccuracy = true
313			gpsoptions.timeout = 2000
314		}, function(err) {
315			showelement("placedescriptor")
316			el = document.getElementById("placenameinput")
317			el.value = err.message
318		}, gpsoptions)
319	}
320}