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 ackit(el, xid) {
57 el.innerHTML = "acked"
58 el.disabled = true
59 post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "ack", "what": xid}))
60}
61function deackit(el, xid) {
62 el.innerHTML = "deacked"
63 el.disabled = true
64 post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "deack", "what": xid}))
65}
66function fillinhonks(xhr) {
67 var doc = xhr.responseXML
68 var stash = curpagestate.name + ":" + curpagestate.arg
69 topxid[stash] = doc.children[0].children[1].children[0].innerText
70 var srvmsg = doc.children[0].children[1].children[1]
71 var honks = doc.children[0].children[1].children[2].children
72
73 var srvel = document.getElementById("srvmsg")
74 if (srvel.children[0]) {
75 srvel.children[0].remove()
76 }
77 srvel.prepend(srvmsg)
78
79 var honksonpage = document.getElementById("honksonpage")
80 var holder = honksonpage.children[0]
81 var lenhonks = honks.length
82 for (var i = honks.length; i > 0; i--) {
83 holder.prepend(honks[i-1])
84 }
85 relinklinks()
86 return lenhonks
87}
88function hydrargs() {
89 var name = curpagestate.name
90 var arg = curpagestate.arg
91 var args = { "page" : name }
92 if (name == "convoy") {
93 args["c"] = arg
94 } else if (name == "combo") {
95 args["c"] = arg
96 } else if (name == "honker") {
97 args["xid"] = arg
98 }
99 return args
100}
101function refreshhonks(btn) {
102 btn.innerHTML = "refreshing"
103 btn.disabled = true
104 var args = hydrargs()
105 var stash = curpagestate.name + ":" + curpagestate.arg
106 args["topxid"] = topxid[stash]
107 get("/hydra?" + encode(args), function(xhr) {
108 var lenhonks = fillinhonks(xhr)
109 btn.innerHTML = "refresh"
110 btn.disabled = false
111 btn.parentElement.children[1].innerHTML = " " + lenhonks + " new"
112 })
113}
114function statechanger(evt) {
115 var data = evt.state
116 if (!data) {
117 return
118 }
119 switchtopage(data.name, data.arg)
120}
121function switchtopage(name, arg) {
122 var stash = curpagestate.name + ":" + curpagestate.arg
123 var honksonpage = document.getElementById("honksonpage")
124 var holder = honksonpage.children[0]
125 holder.remove()
126 var srvel = document.getElementById("srvmsg")
127 var msg = srvel.children[0]
128 if (msg) {
129 msg.remove()
130 servermsgs[stash] = msg
131 }
132
133 honksforpage[stash] = holder
134
135 curpagestate.name = name
136 curpagestate.arg = arg
137 // get the holder for the target page
138 var stash = name + ":" + arg
139 holder = honksforpage[stash]
140 if (holder) {
141 honksonpage.prepend(holder)
142 msg = servermsgs[stash]
143 if (msg) {
144 srvel.prepend(msg)
145 }
146 } else {
147 // or create one and fill it
148 honksonpage.prepend(document.createElement("div"))
149 var args = hydrargs()
150 get("/hydra?" + encode(args), fillinhonks)
151 }
152}
153function newpagestate(name, arg) {
154 return { "name": name, "arg": arg }
155}
156function pageswitcher(name, arg) {
157 return function(evt) {
158 var topmenu = document.getElementById("topmenu")
159 topmenu.open = false
160 console.log("switching to", name +":"+arg)
161 if (name == curpagestate.name && arg == curpagestate.arg) {
162 console.log("skipping nav")
163 return false
164 }
165 switchtopage(name, arg)
166 var url = evt.srcElement.href
167 history.pushState(newpagestate(name, arg), "some title", url)
168 window.scrollTo(0, 0)
169 return false
170 }
171}
172function relinklinks() {
173 var els = document.getElementsByClassName("convoylink")
174 while (els.length) {
175 els[0].onclick = pageswitcher("convoy", els[0].text)
176 els[0].classList.remove("convoylink")
177 }
178 els = document.getElementsByClassName("combolink")
179 while (els.length) {
180 els[0].onclick = pageswitcher("combo", els[0].text)
181 els[0].classList.remove("combolink")
182 }
183 els = document.getElementsByClassName("honkerlink")
184 while (els.length) {
185 var el = els[0]
186 var xid = el.getAttribute("data-xid")
187 el.onclick = pageswitcher("honker", xid)
188 el.classList.remove("honkerlink")
189 }
190}
191(function() {
192 var el = document.getElementById("homelink")
193 el.onclick = pageswitcher("home", "")
194 el = document.getElementById("atmelink")
195 el.onclick = pageswitcher("atme", "")
196 el = document.getElementById("firstlink")
197 el.onclick = pageswitcher("first", "")
198 relinklinks()
199 window.onpopstate = statechanger
200 history.replaceState(curpagestate, "some title", "")
201})();
202(function() {
203 var el = document.getElementById("donkdescriptor")
204 el.style.display = "none"
205})();
206function showhonkform(elem, rid, hname) {
207 var form = document.getElementById("honkform")
208 form.style = "display: block"
209 if (elem) {
210 form.remove()
211 elem.parentElement.parentElement.parentElement.insertAdjacentElement('beforebegin', form)
212 } else {
213 elem = document.getElementById("honkformhost")
214 elem.insertAdjacentElement('afterend', form)
215 }
216 var ridinput = document.getElementById("ridinput")
217 var honknoise = document.getElementById("honknoise")
218 if (rid) {
219 ridinput.value = rid
220 honknoise.value = "@" + hname + " "
221 }
222 document.getElementById("honknoise").focus()
223}
224function showelement(id) {
225 var el = document.getElementById(id)
226 el.style.display = "block"
227}
228function updatedonker() {
229 var el = document.getElementById("donker")
230 el.children[1].textContent = el.children[0].value.slice(-20)
231 var el = document.getElementById("donkdescriptor")
232 el.style.display = ""
233}
234var checkinprec = 100.0
235var gpsoptions = {
236 enableHighAccuracy: false,
237 timeout: 1000,
238 maximumAge: 0
239};
240function fillcheckin() {
241 if (navigator.geolocation) {
242 navigator.geolocation.getCurrentPosition(function(pos) {
243 showelement("placedescriptor")
244 var el = document.getElementById("placelatinput")
245 el.value = Math.round(pos.coords.latitude * checkinprec) / checkinprec
246 el = document.getElementById("placelonginput")
247 el.value = Math.round(pos.coords.longitude * checkinprec) / checkinprec
248 checkinprec = 10000.0
249 gpsoptions.enableHighAccuracy = true
250 gpsoptions.timeout = 2000
251 }, function(err) {
252 showelement("placedescriptor")
253 el = document.getElementById("placenameinput")
254 el.value = err.message
255 }, gpsoptions)
256 }
257}