all repos — honk @ c89d4c002fdd483fd82ea8dd63988f48d0d958e3

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	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	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		els[0].onclick = pageswitcher("honker", els[0].text)
171		els[0].classList.remove("honkerlink")
172	}
173}
174(function() {
175	var el = document.getElementById("homelink")
176	el.onclick = pageswitcher("home", "")
177	el = document.getElementById("atmelink")
178	el.onclick = pageswitcher("atme", "")
179	el = document.getElementById("firstlink")
180	el.onclick = pageswitcher("first", "")
181	relinklinks()
182	window.onpopstate = statechanger
183	history.replaceState(curpagestate, "some title", "")
184})();
185(function() {
186	var el = document.getElementById("donkdescriptor")
187	el.style.display = "none"
188})();
189function showhonkform(elem, rid, hname) {
190	var form = document.getElementById("honkform")
191	form.style = "display: block"
192	if (elem) {
193		form.remove()
194		elem.parentElement.parentElement.parentElement.insertAdjacentElement('beforebegin', form)
195	} else {
196		elem = document.getElementById("honkformhost")
197		elem.insertAdjacentElement('afterend', form)
198	}
199	var ridinput = document.getElementById("ridinput")
200	var honknoise = document.getElementById("honknoise")
201	if (rid) {
202		ridinput.value = rid
203		honknoise.value = "@" + hname + " "
204	}
205	document.getElementById("honknoise").focus()
206}
207function showelement(id) {
208	var el = document.getElementById(id)
209	el.style.display = "block"
210}
211function updatedonker() {
212	var el = document.getElementById("donker")
213	el.children[1].textContent = el.children[0].value.slice(-20)
214	var el = document.getElementById("donkdescriptor")
215	el.style.display = ""
216}
217var checkinprec = 500.0
218function fillcheckin() {
219	if (navigator.geolocation) {
220		navigator.geolocation.getCurrentPosition(function(pos) {
221			showelement("placedescriptor")
222			var 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			showelement("placedescriptor")
229			el = document.getElementById("placenameinput")
230			el.value = err.message
231		})
232	}
233}