all repos — honk @ 8c83d43769dcc56c595892bb38d338267492ce8e

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({"CSRF": csrftoken, "xid": xid}))
 26}
 27function unbonk(el, xid) {
 28	el.innerHTML = "unbonked"
 29	el.disabled = true
 30	post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "unbonk", "what": xid}))
 31}
 32function muteit(el, convoy) {
 33	el.innerHTML = "muted"
 34	el.disabled = true
 35	post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "zonvoy", "what": convoy}))
 36	var els = document.querySelectorAll('article.honk')
 37	for (var i = 0; i < els.length; i++) {
 38		var e = els[i]
 39		if (e.getAttribute("data-convoy") == convoy) {
 40			e.remove()
 41		}
 42	}
 43}
 44function zonkit(el, xid) {
 45	el.innerHTML = "zonked"
 46	el.disabled = true
 47	post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "zonk", "what": xid}))
 48	var p = el
 49	while (p && p.tagName != "ARTICLE") {
 50		p = p.parentElement
 51	}
 52	if (p) {
 53		p.remove()
 54	}
 55}
 56function ackit(el, xid) {
 57	el.innerHTML = "acked"
 58	el.disabled = true
 59	post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "ack", "what": xid}))
 60}
 61function deackit(el, xid) {
 62	el.innerHTML = "deacked"
 63	el.disabled = true
 64	post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "deack", "what": xid}))
 65}
 66function fillinhonks(xhr) {
 67	var doc = xhr.responseXML
 68	var stash = curpagestate.name + ":" + curpagestate.arg
 69	topxid[stash] = doc.children[0].children[1].children[0].innerText
 70	var srvmsg = doc.children[0].children[1].children[1]
 71	var honks = doc.children[0].children[1].children[2].children
 72
 73	var srvel = document.getElementById("srvmsg")
 74	if (srvel.children[0]) {
 75		srvel.children[0].remove()
 76	}
 77	srvel.prepend(srvmsg)
 78
 79	var honksonpage = document.getElementById("honksonpage")
 80	var holder = honksonpage.children[0]
 81	var lenhonks = honks.length
 82	for (var i = honks.length; i > 0; i--) {
 83		holder.prepend(honks[i-1])
 84	}
 85	relinklinks()
 86	return lenhonks
 87}
 88function hydrargs() {
 89	var name = curpagestate.name
 90	var arg = curpagestate.arg
 91	var args = { "page" : name }
 92	if (name == "convoy") {
 93		args["c"] = arg
 94	} else if (name == "combo") {
 95		args["c"] = arg
 96	} else if (name == "honker") {
 97		args["xid"] = arg
 98	}
 99	return args
100}
101function refreshhonks(btn) {
102	btn.innerHTML = "refreshing"
103	btn.disabled = true
104	var args = hydrargs()
105	var stash = curpagestate.name + ":" + curpagestate.arg
106	args["topxid"] = topxid[stash]
107	get("/hydra?" + encode(args), function(xhr) {
108		var lenhonks = fillinhonks(xhr)
109		btn.innerHTML = "refresh"
110		btn.disabled = false
111		btn.parentElement.children[1].innerHTML = " " + lenhonks + " new"
112	})
113}
114function statechanger(evt) {
115	var data = evt.state
116	if (!data) {
117		return
118	}
119	switchtopage(data.name, data.arg)
120}
121function switchtopage(name, arg) {
122	var stash = curpagestate.name + ":" + curpagestate.arg
123	var honksonpage = document.getElementById("honksonpage")
124	var holder = honksonpage.children[0]
125	holder.remove()
126	var srvel = document.getElementById("srvmsg")
127	var msg = srvel.children[0]
128	if (msg) {
129		msg.remove()
130		servermsgs[stash] = msg
131	}
132
133	honksforpage[stash] = holder
134
135	curpagestate.name = name
136	curpagestate.arg = arg
137	// get the holder for the target page
138	var stash = name + ":" + arg
139	holder = honksforpage[stash]
140	if (holder) {
141		honksonpage.prepend(holder)
142		msg = servermsgs[stash]
143		if (msg) {
144			srvel.prepend(msg)
145		}
146	} else {
147		// or create one and fill it
148		honksonpage.prepend(document.createElement("div"))
149		var args = hydrargs()
150		get("/hydra?" + encode(args), fillinhonks)
151	}
152}
153function newpagestate(name, arg) {
154	return { "name": name, "arg": arg }
155}
156function pageswitcher(name, arg) {
157	return function(evt) {
158		var topmenu = document.getElementById("topmenu")
159		topmenu.open = false
160		console.log("switching to", name +":"+arg)
161		if (name == curpagestate.name && arg == curpagestate.arg) {
162			console.log("skipping nav")
163			return false
164		}
165		switchtopage(name, arg)
166		var url = evt.srcElement.href
167		if (!url) {
168			url = evt.srcElement.parentElement.href
169		}
170		history.pushState(newpagestate(name, arg), "some title", url)
171		window.scrollTo(0, 0)
172		return false
173	}
174}
175function relinklinks() {
176	var els = document.getElementsByClassName("convoylink")
177	while (els.length) {
178		els[0].onclick = pageswitcher("convoy", els[0].text)
179		els[0].classList.remove("convoylink")
180	}
181	els = document.getElementsByClassName("combolink")
182	while (els.length) {
183		els[0].onclick = pageswitcher("combo", els[0].text)
184		els[0].classList.remove("combolink")
185	}
186	els = document.getElementsByClassName("honkerlink")
187	while (els.length) {
188		var el = els[0]
189		var xid = el.getAttribute("data-xid")
190		el.onclick = pageswitcher("honker", xid)
191		el.classList.remove("honkerlink")
192	}
193}
194(function() {
195	var el = document.getElementById("homelink")
196	el.onclick = pageswitcher("home", "")
197	el = document.getElementById("atmelink")
198	el.onclick = pageswitcher("atme", "")
199	el = document.getElementById("firstlink")
200	el.onclick = pageswitcher("first", "")
201	relinklinks()
202	window.onpopstate = statechanger
203	history.replaceState(curpagestate, "some title", "")
204})();
205(function() {
206	var el = document.getElementById("donkdescriptor")
207	el.style.display = "none"
208})();
209function showhonkform(elem, rid, hname) {
210	var form = document.getElementById("honkform")
211	form.style = "display: block"
212	if (elem) {
213		form.remove()
214		elem.parentElement.parentElement.parentElement.insertAdjacentElement('beforebegin', form)
215	} else {
216		elem = document.getElementById("honkformhost")
217		elem.insertAdjacentElement('afterend', form)
218	}
219	var ridinput = document.getElementById("ridinput")
220	var honknoise = document.getElementById("honknoise")
221	if (rid) {
222		ridinput.value = rid
223		honknoise.value = "@" + hname + " "
224	}
225	document.getElementById("honknoise").focus()
226}
227function showelement(id) {
228	var el = document.getElementById(id)
229	el.style.display = "block"
230}
231function updatedonker() {
232	var el = document.getElementById("donker")
233	el.children[1].textContent = el.children[0].value.slice(-20)
234	var el = document.getElementById("donkdescriptor")
235	el.style.display = ""
236}
237var checkinprec = 100.0
238var gpsoptions = {
239	enableHighAccuracy: false,
240	timeout: 1000,
241	maximumAge: 0
242};
243function fillcheckin() {
244	if (navigator.geolocation) {
245		navigator.geolocation.getCurrentPosition(function(pos) {
246			showelement("placedescriptor")
247			var el = document.getElementById("placelatinput")
248			el.value = Math.round(pos.coords.latitude * checkinprec) / checkinprec
249			el = document.getElementById("placelonginput")
250			el.value = Math.round(pos.coords.longitude * checkinprec) / checkinprec
251			checkinprec = 10000.0
252			gpsoptions.enableHighAccuracy = true
253			gpsoptions.timeout = 2000
254		}, function(err) {
255			showelement("placedescriptor")
256			el = document.getElementById("placenameinput")
257			el.value = err.message
258		}, gpsoptions)
259	}
260}