all repos — honk @ 1a33595d069b578261172d045f946572ebb31e5b

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 flogit(el, how, xid) {
 57	var s = how
 58	if (s[s.length-1] != "e") { s += "e" }
 59	s += "d"
 60	el.innerHTML = s
 61	el.disabled = true
 62	post("/zonkit", encode({"CSRF": csrftoken, "wherefore": how, "what": xid}))
 63}
 64function fillinhonks(xhr) {
 65	var doc = xhr.responseXML
 66	var stash = curpagestate.name + ":" + curpagestate.arg
 67	topxid[stash] = doc.children[0].children[1].children[0].innerText
 68	var srvmsg = doc.children[0].children[1].children[1]
 69	var honks = doc.children[0].children[1].children[2].children
 70
 71	var srvel = document.getElementById("srvmsg")
 72	if (srvel.children[0]) {
 73		srvel.children[0].remove()
 74	}
 75	srvel.prepend(srvmsg)
 76
 77	var frontload = true
 78	if (curpagestate.name == "convoy") {
 79		frontload = false
 80	}
 81
 82	var honksonpage = document.getElementById("honksonpage")
 83	var holder = honksonpage.children[0]
 84	var lenhonks = honks.length
 85	for (var i = honks.length; i > 0; i--) {
 86		if (frontload) {
 87			holder.prepend(honks[i-1])
 88		} else {
 89			holder.append(honks[i-1])
 90		}
 91			
 92	}
 93	relinklinks()
 94	return lenhonks
 95}
 96function hydrargs() {
 97	var name = curpagestate.name
 98	var arg = curpagestate.arg
 99	var args = { "page" : name }
100	if (name == "convoy") {
101		args["c"] = arg
102	} else if (name == "combo") {
103		args["c"] = arg
104	} else if (name == "honker") {
105		args["xid"] = arg
106	}
107	return args
108}
109function refreshupdate(msg) {
110	var el = document.querySelector("#refreshbox p span")
111	if (el) {
112		el.innerHTML = msg
113	}
114}
115function refreshhonks(btn) {
116	btn.innerHTML = "refreshing"
117	btn.disabled = true
118	var args = hydrargs()
119	var stash = curpagestate.name + ":" + curpagestate.arg
120	args["topxid"] = topxid[stash]
121	get("/hydra?" + encode(args), function(xhr) {
122		var lenhonks = fillinhonks(xhr)
123		btn.innerHTML = "refresh"
124		btn.disabled = false
125		refreshupdate(" " + lenhonks + " new")
126	})
127}
128function statechanger(evt) {
129	var data = evt.state
130	if (!data) {
131		return
132	}
133	switchtopage(data.name, data.arg)
134}
135function switchtopage(name, arg) {
136	var stash = curpagestate.name + ":" + curpagestate.arg
137	var honksonpage = document.getElementById("honksonpage")
138	var holder = honksonpage.children[0]
139	holder.remove()
140	var srvel = document.getElementById("srvmsg")
141	var msg = srvel.children[0]
142	if (msg) {
143		msg.remove()
144		servermsgs[stash] = msg
145	}
146
147	honksforpage[stash] = holder
148
149	curpagestate.name = name
150	curpagestate.arg = arg
151	// get the holder for the target page
152	var stash = name + ":" + arg
153	holder = honksforpage[stash]
154	if (holder) {
155		honksonpage.prepend(holder)
156		msg = servermsgs[stash]
157		if (msg) {
158			srvel.prepend(msg)
159		}
160	} else {
161		// or create one and fill it
162		honksonpage.prepend(document.createElement("div"))
163		var args = hydrargs()
164		get("/hydra?" + encode(args), fillinhonks)
165	}
166	refreshupdate("")
167}
168function newpagestate(name, arg) {
169	return { "name": name, "arg": arg }
170}
171function pageswitcher(name, arg) {
172	return function(evt) {
173		var topmenu = document.getElementById("topmenu")
174		topmenu.open = false
175		if (name == curpagestate.name && arg == curpagestate.arg) {
176			return false
177		}
178		switchtopage(name, arg)
179		var url = evt.srcElement.href
180		if (!url) {
181			url = evt.srcElement.parentElement.href
182		}
183		history.pushState(newpagestate(name, arg), "some title", url)
184		window.scrollTo(0, 0)
185		return false
186	}
187}
188function relinklinks() {
189	var els = document.getElementsByClassName("convoylink")
190	while (els.length) {
191		els[0].onclick = pageswitcher("convoy", els[0].text)
192		els[0].classList.remove("convoylink")
193	}
194	els = document.getElementsByClassName("combolink")
195	while (els.length) {
196		els[0].onclick = pageswitcher("combo", els[0].text)
197		els[0].classList.remove("combolink")
198	}
199	els = document.getElementsByClassName("honkerlink")
200	while (els.length) {
201		var el = els[0]
202		var xid = el.getAttribute("data-xid")
203		el.onclick = pageswitcher("honker", xid)
204		el.classList.remove("honkerlink")
205	}
206}
207(function() {
208	var el = document.getElementById("homelink")
209	el.onclick = pageswitcher("home", "")
210	el = document.getElementById("atmelink")
211	el.onclick = pageswitcher("atme", "")
212	el = document.getElementById("firstlink")
213	el.onclick = pageswitcher("first", "")
214	el = document.getElementById("savedlink")
215	el.onclick = pageswitcher("saved", "")
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}