all repos — honk @ 6ecc669f4147dcebfa16cff95c06e8a47318435f

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