views/honkpage.js (view raw)
1{{ $BonkCSRF := .HonkCSRF }}
2function encode(hash) {
3 var s = []
4 for (var key in hash) {
5 var val = hash[key]
6 s.push(escape(key) + "=" + escape(val))
7 }
8 return s.join("&")
9}
10function post(url, data) {
11 var x = new XMLHttpRequest()
12 x.open("POST", url)
13 x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
14 x.send(data)
15}
16function get(url, whendone) {
17 var x = new XMLHttpRequest()
18 x.open("GET", url)
19 x.responseType = "document"
20 x.onload = function() { whendone(x) }
21 x.send()
22}
23function bonk(el, xid) {
24 el.innerHTML = "bonked"
25 el.disabled = true
26 post("/bonk", "CSRF={{ $BonkCSRF }}&xid=" + escape(xid))
27}
28function unbonk(el, xid) {
29 el.innerHTML = "unbonked"
30 el.disabled = true
31 post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=unbonk&what=" + escape(xid))
32}
33function muteit(el, convoy) {
34 el.innerHTML = "muted"
35 el.disabled = true
36 post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=zonvoy&what=" + escape(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", "CSRF={{ $BonkCSRF }}&wherefore=zonk&what=" + escape(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 ackit(el, xid) {
58 el.innerHTML = "acked"
59 el.disabled = true
60 post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=ack&what=" + escape(xid))
61}
62function deackit(el, xid) {
63 el.innerHTML = "deacked"
64 el.disabled = true
65 post("/zonkit", "CSRF={{ $BonkCSRF }}&wherefore=deack&what=" + escape(xid))
66}
67var topxid = { "{{ .PageName }}" : "{{ .TopXID }}" }
68var honksforpage = { }
69var thispagename = "{{ .PageName }}"
70function fillinhonks(xhr) {
71 var doc = xhr.responseXML
72 topxid[thispagename] = doc.children[0].children[1].children[0].innerText
73 var honks = doc.children[0].children[1].children[1].children
74 var honksonpage = document.getElementById("honksonpage")
75 var holder = honksonpage.children[0]
76 var lenhonks = honks.length
77 for (var i = honks.length; i > 0; i--) {
78 holder.prepend(honks[i-1])
79 }
80 relinkconvoys()
81 return lenhonks
82}
83function refreshhonks(btn) {
84 btn.innerHTML = "refreshing"
85 btn.disabled = true
86 var args = { "page" : thispagename }
87 args["topxid"] = topxid[thispagename]
88 get("/hydra?" + encode(args), function(xhr) {
89 var lenhonks = fillinhonks(xhr)
90 btn.innerHTML = "refresh"
91 btn.disabled = false
92 btn.parentElement.children[1].innerHTML = " " + lenhonks + " new"
93 })
94}
95function pageswitcher(name) {
96 return function(evt) {
97 if (name == thispagename) {
98 return false
99 }
100 var honksonpage = document.getElementById("honksonpage")
101 var holder = honksonpage.children[0]
102 holder.remove()
103 if (thispagename != "convoy") {
104 honksforpage[thispagename] = holder
105 }
106
107 thispagename = name
108 holder = honksforpage[name]
109 if (holder) {
110 honksonpage.prepend(holder)
111 } else {
112 honksonpage.prepend(document.createElement("div"))
113 var args = { "page" : name }
114 if (name == "convoy") {
115 console.log("convoy page")
116 var c = evt.srcElement.text
117 args["c"] = c
118 } else {
119 args["topxid"] = topxid[name]
120 }
121 get("/hydra?" + encode(args), fillinhonks)
122 }
123 return false
124 }
125}
126function relinkconvoys() {
127 var els = document.getElementsByClassName("convoylink")
128 for (var i = 0; i < els.length; i++) {
129 els[i].onclick = pageswitcher("convoy")
130 }
131}
132(function() {
133 console.log("ok")
134 var el = document.getElementById("homelink")
135 el.onclick = pageswitcher("home")
136 var el = document.getElementById("atmelink")
137 el.onclick = pageswitcher("atme")
138 relinkconvoys()
139})();