all repos — honk @ 1089b58adb19709aa52311fa118e5d77897f75e2

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