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