all repos — honk @ fa6917a048b9c40caa63396291e74da2ca144da8

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 honks = doc.children[0].children[1].children[1].children
 71	var honksonpage = document.getElementById("honksonpage")
 72	var holder = honksonpage.children[0]
 73	var lenhonks = honks.length
 74	for (var i = honks.length; i > 0; i--) {
 75		holder.prepend(honks[i-1])
 76	}
 77	relinklinks()
 78	return lenhonks
 79}
 80function hydrargs() {
 81	var name = curpagestate.name
 82	var arg = curpagestate.arg
 83	var args = { "page" : name }
 84	if (name == "convoy") {
 85		args["c"] = arg
 86	} else if (name == "combo") {
 87		console.log("loading combo " + arg)
 88		args["c"] = arg
 89	} else if (name == "honker") {
 90		console.log("loading honker " + arg)
 91		args["xid"] = arg
 92	}
 93	return args
 94}
 95function refreshhonks(btn) {
 96	btn.innerHTML = "refreshing"
 97	btn.disabled = true
 98	var args = hydrargs()
 99	var stash = curpagestate.name + ":" + curpagestate.arg
100	args["topxid"] = topxid[stash]
101	get("/hydra?" + encode(args), function(xhr) {
102		var lenhonks = fillinhonks(xhr)
103		btn.innerHTML = "refresh"
104		btn.disabled = false
105		btn.parentElement.children[1].innerHTML = " " + lenhonks + " new"
106	})
107}
108function statechanger(evt) {
109	var data = evt.state
110	if (!data) {
111		return
112	}
113	switchtopage(data.name, data.arg)
114}
115function switchtopage(name, arg) {
116	var honksonpage = document.getElementById("honksonpage")
117	var holder = honksonpage.children[0]
118	holder.remove()
119	// if not convoy, save current page
120	if (curpagestate.name != "convoy") {
121		var stash = curpagestate.name + ":" + curpagestate.arg
122		honksforpage[stash] = holder
123	}
124	curpagestate.name = name
125	curpagestate.arg = arg
126	// get the holder for the target page
127	var stash = name + ":" + arg
128	holder = honksforpage[stash]
129	if (holder) {
130		honksonpage.prepend(holder)
131	} else {
132		// or create one and fill it
133		honksonpage.prepend(document.createElement("div"))
134		var args = hydrargs()
135		get("/hydra?" + encode(args), fillinhonks)
136	}
137}
138function newpagestate(name, arg) {
139	return { "name": name, "arg": arg }
140}
141function pageswitcher(name, arg) {
142	return function(evt) {
143		var topmenu = document.getElementById("topmenu")
144		topmenu.open = false
145		console.log("switching to", name +":"+arg)
146		if (name == curpagestate.name && arg == curpagestate.arg) {
147			console.log("skipping nav")
148			return false
149		}
150		switchtopage(name, arg)
151		var url = evt.srcElement.href
152		history.pushState(newpagestate(name, arg), "some title", url)
153		window.scrollTo(0, 0)
154		return false
155	}
156}
157function relinklinks() {
158	var els = document.getElementsByClassName("convoylink")
159	while (els.length) {
160		els[0].onclick = pageswitcher("convoy", els[0].text)
161		els[0].classList.remove("convoylink")
162	}
163	els = document.getElementsByClassName("combolink")
164	while (els.length) {
165		els[0].onclick = pageswitcher("combo", els[0].text)
166		els[0].classList.remove("combolink")
167	}
168	els = document.getElementsByClassName("honkerlink")
169	while (els.length) {
170		var el = els[0]
171		var xid = el.getAttribute("data-xid")
172		el.onclick = pageswitcher("honker", xid)
173		el.classList.remove("honkerlink")
174	}
175}
176(function() {
177	var el = document.getElementById("homelink")
178	el.onclick = pageswitcher("home", "")
179	el = document.getElementById("atmelink")
180	el.onclick = pageswitcher("atme", "")
181	el = document.getElementById("firstlink")
182	el.onclick = pageswitcher("first", "")
183	relinklinks()
184	window.onpopstate = statechanger
185	history.replaceState(curpagestate, "some title", "")
186})();
187(function() {
188	var el = document.getElementById("donkdescriptor")
189	el.style.display = "none"
190})();
191function showhonkform(elem, rid, hname) {
192	var form = document.getElementById("honkform")
193	form.style = "display: block"
194	if (elem) {
195		form.remove()
196		elem.parentElement.parentElement.parentElement.insertAdjacentElement('beforebegin', form)
197	} else {
198		elem = document.getElementById("honkformhost")
199		elem.insertAdjacentElement('afterend', form)
200	}
201	var ridinput = document.getElementById("ridinput")
202	var honknoise = document.getElementById("honknoise")
203	if (rid) {
204		ridinput.value = rid
205		honknoise.value = "@" + hname + " "
206	}
207	document.getElementById("honknoise").focus()
208}
209function showelement(id) {
210	var el = document.getElementById(id)
211	el.style.display = "block"
212}
213function updatedonker() {
214	var el = document.getElementById("donker")
215	el.children[1].textContent = el.children[0].value.slice(-20)
216	var el = document.getElementById("donkdescriptor")
217	el.style.display = ""
218}
219var checkinprec = 500.0
220function fillcheckin() {
221	if (navigator.geolocation) {
222		navigator.geolocation.getCurrentPosition(function(pos) {
223			showelement("placedescriptor")
224			var el = document.getElementById("placelatinput")
225			el.value = Math.round(pos.coords.latitude * checkinprec) / checkinprec
226			el = document.getElementById("placelonginput")
227			el.value = Math.round(pos.coords.longitude * checkinprec) / checkinprec
228			checkinprec = 10000.0
229		}, function(err) {
230			showelement("placedescriptor")
231			el = document.getElementById("placenameinput")
232			el.value = err.message
233		})
234	}
235}