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