all repos — honk @ 36f3f3f5a152e856e2f495155825a1e05e0522cc

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