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 honks = doc.children[0].children[1].children[1].children
71 var honksonpage = document.getElementById("honksonpage")
72 var holder = honksonpage.children[0]
73 var lenhonks = honks.length
74 for (var i = honks.length; i > 0; i--) {
75 holder.prepend(honks[i-1])
76 }
77 relinklinks()
78 return lenhonks
79}
80function hydrargs() {
81 var name = curpagestate.name
82 var arg = curpagestate.arg
83 var args = { "page" : name }
84 if (name == "convoy") {
85 args["c"] = arg
86 } else if (name == "combo") {
87 console.log("loading combo " + arg)
88 args["c"] = arg
89 } else {
90 var stash = name + ":" + arg
91 args["topxid"] = topxid[stash]
92 }
93 return args
94}
95function refreshhonks(btn) {
96 btn.innerHTML = "refreshing"
97 btn.disabled = true
98 var args = hydrargs()
99 var stash = curpagestate.name + ":" + curpagestate.arg
100 args["topxid"] = topxid[stash]
101 get("/hydra?" + encode(args), function(xhr) {
102 var lenhonks = fillinhonks(xhr)
103 btn.innerHTML = "refresh"
104 btn.disabled = false
105 btn.parentElement.children[1].innerHTML = " " + lenhonks + " new"
106 })
107}
108function statechanger(evt) {
109 var data = evt.state
110 if (!data) {
111 return
112 }
113 switchtopage(data.name, data.arg)
114}
115function switchtopage(name, arg) {
116 var honksonpage = document.getElementById("honksonpage")
117 var holder = honksonpage.children[0]
118 holder.remove()
119 // if not convoy, save current page
120 if (name != "convoy") {
121 var stash = curpagestate.name + ":" + curpagestate.arg
122 honksforpage[stash] = holder
123 }
124 curpagestate.name = name
125 curpagestate.arg = arg
126 // get the holder for the target page
127 var stash = name + ":" + arg
128 holder = honksforpage[stash]
129 if (holder) {
130 honksonpage.prepend(holder)
131 } else {
132 // or create one and fill it
133 honksonpage.prepend(document.createElement("div"))
134 var args = hydrargs()
135 get("/hydra?" + encode(args), fillinhonks)
136 }
137 var topmenu = document.getElementById("topmenu")
138 topmenu.open = false
139}
140function newpagestate(name, arg) {
141 return { "name": name, "arg": arg }
142}
143function pageswitcher(name, arg) {
144 return function(evt) {
145 console.log("switching to", name +":"+arg)
146 if (name == curpagestate.name && arg == curpagestate.arg) {
147 console.log("skipping nav")
148 return false
149 }
150 switchtopage(name, arg)
151 var url = evt.srcElement.href
152 history.pushState(newpagestate(name, arg), "some title", url)
153 return false
154 }
155}
156function relinklinks() {
157 var els = document.getElementsByClassName("convoylink")
158 for (var i = 0; i < els.length; i++) {
159 els[i].onclick = pageswitcher("convoy", els[i].text)
160 }
161 els = document.getElementsByClassName("combolink")
162 for (var i = 0; i < els.length; i++) {
163 els[i].onclick = pageswitcher("combo", els[i].text)
164 }
165}
166(function() {
167 var el = document.getElementById("homelink")
168 el.onclick = pageswitcher("home", "")
169 var el = document.getElementById("atmelink")
170 el.onclick = pageswitcher("atme", "")
171 relinklinks()
172 window.onpopstate = statechanger
173 history.replaceState(curpagestate, "some title", "")
174})();
175(function() {
176 var el = document.getElementById("donkdescriptor")
177 el.style.display = "none"
178})();
179function showhonkform(elem, rid, hname) {
180 var form = document.getElementById("honkform")
181 form.style = "display: block"
182 if (elem) {
183 form.remove()
184 elem.parentElement.insertAdjacentElement('beforebegin', form)
185 } else {
186 elem = document.getElementById("honkformhost")
187 elem.insertAdjacentElement('afterend', form)
188 }
189 var ridinput = document.getElementById("ridinput")
190 var honknoise = document.getElementById("honknoise")
191 if (rid) {
192 ridinput.value = rid
193 honknoise.value = "@" + hname + " "
194 }
195 document.getElementById("honknoise").focus()
196}
197function updatedonker() {
198 var el = document.getElementById("donker")
199 el.children[1].textContent = el.children[0].value.slice(-20)
200 var el = document.getElementById("donkdescriptor")
201 el.style.display = ""
202}