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
171 honksforpage[stash] = holder
172
173 curpagestate.name = name
174 curpagestate.arg = arg
175 // get the holder for the target page
176 var stash = name + ":" + arg
177 holder = honksforpage[stash]
178 if (holder) {
179 honksonpage.prepend(holder)
180 msg = servermsgs[stash]
181 if (msg) {
182 srvel.prepend(msg)
183 }
184 } else {
185 // or create one and fill it
186 honksonpage.prepend(document.createElement("div"))
187 var args = hydrargs()
188 get("/hydra?" + encode(args), function(xhr) { fillinhonks(xhr, false) })
189 }
190 refreshupdate("")
191}
192function newpagestate(name, arg) {
193 return { "name": name, "arg": arg }
194}
195function pageswitcher(name, arg) {
196 return function(evt) {
197 var topmenu = document.getElementById("topmenu")
198 topmenu.open = false
199 if (name == curpagestate.name && arg == curpagestate.arg) {
200 return false
201 }
202 switchtopage(name, arg)
203 var url = evt.srcElement.href
204 if (!url) {
205 url = evt.srcElement.parentElement.href
206 }
207 history.pushState(newpagestate(name, arg), "some title", url)
208 window.scrollTo(0, 0)
209 return false
210 }
211}
212function relinklinks() {
213 var els = document.getElementsByClassName("convoylink")
214 while (els.length) {
215 els[0].onclick = pageswitcher("convoy", els[0].text)
216 els[0].classList.remove("convoylink")
217 }
218 els = document.getElementsByClassName("combolink")
219 while (els.length) {
220 els[0].onclick = pageswitcher("combo", els[0].text)
221 els[0].classList.remove("combolink")
222 }
223 els = document.getElementsByClassName("honkerlink")
224 while (els.length) {
225 var el = els[0]
226 var xid = el.getAttribute("data-xid")
227 el.onclick = pageswitcher("honker", xid)
228 el.classList.remove("honkerlink")
229 }
230}
231(function() {
232 var el = document.getElementById("homelink")
233 el.onclick = pageswitcher("home", "")
234 el = document.getElementById("atmelink")
235 el.onclick = pageswitcher("atme", "")
236 el = document.getElementById("firstlink")
237 el.onclick = pageswitcher("first", "")
238 el = document.getElementById("savedlink")
239 el.onclick = pageswitcher("saved", "")
240 relinklinks()
241 window.onpopstate = statechanger
242 history.replaceState(curpagestate, "some title", "")
243})();
244(function() {
245 var el = document.getElementById("donkdescriptor")
246 el.style.display = "none"
247})();
248function showhonkform(elem, rid, hname) {
249 var form = lehonkform
250 form.style = "display: block"
251 if (elem) {
252 form.remove()
253 elem.parentElement.parentElement.parentElement.insertAdjacentElement('beforebegin', form)
254 } else {
255 hideelement(lehonkbutton)
256 elem = document.getElementById("honkformhost")
257 elem.insertAdjacentElement('afterend', form)
258 }
259 var ridinput = document.getElementById("ridinput")
260 var honknoise = document.getElementById("honknoise")
261 if (rid) {
262 ridinput.value = rid
263 honknoise.value = "@" + hname + " "
264 } else {
265 ridinput.value = ""
266 honknoise.value = ""
267 }
268 var updateinput = document.getElementById("updatexidinput")
269 updateinput.value = ""
270 document.getElementById("honknoise").focus()
271 return false
272}
273function cancelhonking() {
274 hideelement(lehonkform)
275 showelement(lehonkbutton)
276}
277function showelement(el) {
278 if (typeof(el) == "string")
279 el = document.getElementById(el)
280 el.style.display = "block"
281}
282function hideelement(el) {
283 if (typeof(el) == "string")
284 el = document.getElementById(el)
285 el.style.display = "none"
286}
287function updatedonker() {
288 var el = document.getElementById("donker")
289 el.children[1].textContent = el.children[0].value.slice(-20)
290 var el = document.getElementById("donkdescriptor")
291 el.style.display = ""
292 var el = document.getElementById("saveddonkxid")
293 el.value = ""
294}
295var checkinprec = 100.0
296var gpsoptions = {
297 enableHighAccuracy: false,
298 timeout: 1000,
299 maximumAge: 0
300};
301function fillcheckin() {
302 if (navigator.geolocation) {
303 navigator.geolocation.getCurrentPosition(function(pos) {
304 showelement("placedescriptor")
305 var el = document.getElementById("placelatinput")
306 el.value = Math.round(pos.coords.latitude * checkinprec) / checkinprec
307 el = document.getElementById("placelonginput")
308 el.value = Math.round(pos.coords.longitude * checkinprec) / checkinprec
309 checkinprec = 10000.0
310 gpsoptions.enableHighAccuracy = true
311 gpsoptions.timeout = 2000
312 }, function(err) {
313 showelement("placedescriptor")
314 el = document.getElementById("placenameinput")
315 el.value = err.message
316 }, gpsoptions)
317 }
318}