honk/views/honkpage.js

175 lines
4.6 KiB
JavaScript
Raw Normal View History

2019-09-16 21:15:32 +02:00
function encode(hash) {
var s = []
for (var key in hash) {
var val = hash[key]
s.push(escape(key) + "=" + escape(val))
}
return s.join("&")
}
function post(url, data) {
var x = new XMLHttpRequest()
x.open("POST", url)
x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
x.send(data)
}
function get(url, whendone) {
var x = new XMLHttpRequest()
x.open("GET", url)
x.responseType = "document"
x.onload = function() { whendone(x) }
x.send()
}
function bonk(el, xid) {
el.innerHTML = "bonked"
el.disabled = true
2019-09-19 04:17:50 +02:00
post("/bonk", encode({"CSRF": csrftoken, "xid": xid}))
2019-09-16 21:15:32 +02:00
}
function unbonk(el, xid) {
el.innerHTML = "unbonked"
el.disabled = true
2019-09-19 04:17:50 +02:00
post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "unbonk", "what": xid}))
2019-09-16 21:15:32 +02:00
}
function muteit(el, convoy) {
el.innerHTML = "muted"
el.disabled = true
2019-09-19 04:17:50 +02:00
post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "zonvoy", "what": convoy}))
2019-09-16 21:15:32 +02:00
var els = document.querySelectorAll('article.honk')
for (var i = 0; i < els.length; i++) {
var e = els[i]
if (e.getAttribute("data-convoy") == convoy) {
e.remove()
}
}
}
function zonkit(el, xid) {
el.innerHTML = "zonked"
el.disabled = true
2019-09-19 04:17:50 +02:00
post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "zonk", "what": xid}))
2019-09-16 21:15:32 +02:00
var p = el
while (p && p.tagName != "ARTICLE") {
p = p.parentElement
}
if (p) {
p.remove()
}
}
function ackit(el, xid) {
el.innerHTML = "acked"
el.disabled = true
2019-09-19 04:17:50 +02:00
post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "ack", "what": xid}))
2019-09-16 21:15:32 +02:00
}
function deackit(el, xid) {
el.innerHTML = "deacked"
el.disabled = true
2019-09-19 04:17:50 +02:00
post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "deack", "what": xid}))
2019-09-16 21:15:32 +02:00
}
function fillinhonks(xhr) {
var doc = xhr.responseXML
topxid[thispagename] = doc.children[0].children[1].children[0].innerText
var honks = doc.children[0].children[1].children[1].children
var honksonpage = document.getElementById("honksonpage")
var holder = honksonpage.children[0]
var lenhonks = honks.length
for (var i = honks.length; i > 0; i--) {
holder.prepend(honks[i-1])
}
relinkconvoys()
return lenhonks
}
function refreshhonks(btn) {
btn.innerHTML = "refreshing"
btn.disabled = true
var args = { "page" : thispagename }
args["topxid"] = topxid[thispagename]
get("/hydra?" + encode(args), function(xhr) {
var lenhonks = fillinhonks(xhr)
btn.innerHTML = "refresh"
btn.disabled = false
btn.parentElement.children[1].innerHTML = " " + lenhonks + " new"
})
}
2019-09-16 21:46:33 +02:00
function statechanger(evt) {
var name = evt.state
if (!name) {
return
}
switchtopage(name)
}
function switchtopage(name, evt) {
var honksonpage = document.getElementById("honksonpage")
var holder = honksonpage.children[0]
holder.remove()
if (thispagename != "convoy") {
honksforpage[thispagename] = holder
}
thispagename = name
holder = honksforpage[name]
if (holder) {
honksonpage.prepend(holder)
} else {
honksonpage.prepend(document.createElement("div"))
var args = { "page" : name }
if (name == "convoy") {
var c = evt.srcElement.text
args["c"] = c
} else {
args["topxid"] = topxid[name]
}
get("/hydra?" + encode(args), fillinhonks)
}
}
2019-09-16 21:15:32 +02:00
function pageswitcher(name) {
return function(evt) {
if (name == thispagename) {
return false
}
2019-09-16 21:46:33 +02:00
switchtopage(name, evt)
var url = evt.srcElement.href
history.pushState(name, "some title", url)
2019-09-16 21:15:32 +02:00
return false
}
}
function relinkconvoys() {
var els = document.getElementsByClassName("convoylink")
for (var i = 0; i < els.length; i++) {
els[i].onclick = pageswitcher("convoy")
}
}
(function() {
var el = document.getElementById("homelink")
el.onclick = pageswitcher("home")
var el = document.getElementById("atmelink")
el.onclick = pageswitcher("atme")
relinkconvoys()
2019-09-16 21:46:33 +02:00
window.onpopstate = statechanger
2019-09-17 04:40:37 +02:00
history.replaceState(thispagename, "some title", "")
2019-09-16 21:15:32 +02:00
})();
2019-09-18 21:19:31 +02:00
(function() {
var el = document.getElementById("donkdescriptor")
el.style.display = "none"
})();
function showhonkform(elem, rid, hname) {
var form = document.getElementById("honkform")
form.style = "display: block"
if (elem) {
form.remove()
elem.parentElement.insertAdjacentElement('beforebegin', form)
} else {
elem = document.getElementById("honkformhost")
elem.insertAdjacentElement('afterend', form)
}
var ridinput = document.getElementById("ridinput")
var honknoise = document.getElementById("honknoise")
if (rid) {
ridinput.value = rid
honknoise.value = "@" + hname + " "
}
document.getElementById("honknoise").focus()
}
function updatedonker() {
var el = document.getElementById("donker")
el.children[1].textContent = el.children[0].value.slice(-20)
var el = document.getElementById("donkdescriptor")
el.style.display = ""
}