all repos — honk @ 2d8b2b0737d38e7e5a19a0c597642082e2ac8d1c

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 {
 90		var stash = name + ":" + arg
 91		args["topxid"] = topxid[stash]
 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	var topmenu = document.getElementById("topmenu")
138	topmenu.open = false
139}
140function newpagestate(name, arg) {
141	return { "name": name, "arg": arg }
142}
143function pageswitcher(name, arg) {
144	return function(evt) {
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	for (var i = 0; i < els.length; i++) {
160		els[i].onclick = pageswitcher("convoy", els[i].text)
161	}
162	els = document.getElementsByClassName("combolink")
163	for (var i = 0; i < els.length; i++) {
164		els[i].onclick = pageswitcher("combo", els[i].text)
165	}
166}
167(function() {
168	var el = document.getElementById("homelink")
169	el.onclick = pageswitcher("home", "")
170	el = document.getElementById("atmelink")
171	el.onclick = pageswitcher("atme", "")
172	el = document.getElementById("firstlink")
173	el.onclick = pageswitcher("first", "")
174	relinklinks()
175	window.onpopstate = statechanger
176	history.replaceState(curpagestate, "some title", "")
177})();
178(function() {
179	var el = document.getElementById("donkdescriptor")
180	el.style.display = "none"
181})();
182function showhonkform(elem, rid, hname) {
183	var form = document.getElementById("honkform")
184	form.style = "display: block"
185	if (elem) {
186		form.remove()
187		elem.parentElement.parentElement.parentElement.insertAdjacentElement('beforebegin', form)
188	} else {
189		elem = document.getElementById("honkformhost")
190		elem.insertAdjacentElement('afterend', form)
191	}
192	var ridinput = document.getElementById("ridinput")
193	var honknoise = document.getElementById("honknoise")
194	if (rid) {
195		ridinput.value = rid
196		honknoise.value = "@" + hname + " "
197	}
198	document.getElementById("honknoise").focus()
199}
200function updatedonker() {
201	var el = document.getElementById("donker")
202	el.children[1].textContent = el.children[0].value.slice(-20)
203	var el = document.getElementById("donkdescriptor")
204	el.style.display = ""
205}
206var checkinprec = 500.0
207function fillcheckin() {
208	if (navigator.geolocation) {
209		navigator.geolocation.getCurrentPosition(function(pos) {
210			var el = document.getElementById("placedescriptor")
211			el.style.display = "block"
212			el = document.getElementById("placelatinput")
213			el.value = Math.round(pos.coords.latitude * checkinprec) / checkinprec
214			el = document.getElementById("placelonginput")
215			el.value = Math.round(pos.coords.longitude * checkinprec) / checkinprec
216			checkinprec = 10000.0
217		}, function(err) {
218			var el = document.getElementById("placedescriptor")
219			el.style.display = "block"
220			el = document.getElementById("placenameinput")
221			el.value = err.message
222		})
223	}
224}