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