all repos — honk @ fcc26c183da6f7be9f2baa1016ad56f3c8438773

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