honk/views/honkpage.js

276 lines
7.3 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()
}
}
2019-10-11 22:03:17 +02:00
function flogit(el, how, xid) {
var s = how
if (s[s.length-1] != "e") { s += "e" }
s += "d"
el.innerHTML = s
2019-09-16 21:15:32 +02:00
el.disabled = true
2019-10-11 22:03:17 +02:00
post("/zonkit", encode({"CSRF": csrftoken, "wherefore": how, "what": xid}))
2019-09-16 21:15:32 +02:00
}
function fillinhonks(xhr) {
var doc = xhr.responseXML
var stash = curpagestate.name + ":" + curpagestate.arg
topxid[stash] = doc.children[0].children[1].children[0].innerText
2019-10-07 18:34:36 +02:00
var srvmsg = doc.children[0].children[1].children[1]
var honks = doc.children[0].children[1].children[2].children
var srvel = document.getElementById("srvmsg")
2019-10-07 19:18:30 +02:00
if (srvel.children[0]) {
srvel.children[0].remove()
}
2019-10-07 18:34:36 +02:00
srvel.prepend(srvmsg)
var frontload = true
if (curpagestate.name == "convoy") {
frontload = false
}
2019-09-16 21:15:32 +02:00
var honksonpage = document.getElementById("honksonpage")
var holder = honksonpage.children[0]
var lenhonks = honks.length
for (var i = honks.length; i > 0; i--) {
if (frontload) {
holder.prepend(honks[i-1])
} else {
holder.append(honks[i-1])
}
2019-09-16 21:15:32 +02:00
}
relinklinks()
2019-09-16 21:15:32 +02:00
return lenhonks
}
function hydrargs() {
var name = curpagestate.name
var arg = curpagestate.arg
var args = { "page" : name }
if (name == "convoy") {
args["c"] = arg
} else if (name == "combo") {
args["c"] = arg
2019-10-02 01:45:17 +02:00
} else if (name == "honker") {
args["xid"] = arg
}
return args
}
2019-10-11 07:01:15 +02:00
function refreshupdate(msg) {
var el = document.querySelector("#refreshbox p span")
if (el) {
el.innerHTML = msg
}
}
2019-09-16 21:15:32 +02:00
function refreshhonks(btn) {
btn.innerHTML = "refreshing"
btn.disabled = true
var args = hydrargs()
var stash = curpagestate.name + ":" + curpagestate.arg
args["topxid"] = topxid[stash]
2019-09-16 21:15:32 +02:00
get("/hydra?" + encode(args), function(xhr) {
var lenhonks = fillinhonks(xhr)
btn.innerHTML = "refresh"
btn.disabled = false
2019-10-11 07:01:15 +02:00
refreshupdate(" " + lenhonks + " new")
2019-09-16 21:15:32 +02:00
})
}
2019-09-16 21:46:33 +02:00
function statechanger(evt) {
var data = evt.state
if (!data) {
2019-09-16 21:46:33 +02:00
return
}
switchtopage(data.name, data.arg)
2019-09-16 21:46:33 +02:00
}
function switchtopage(name, arg) {
2019-10-07 19:18:30 +02:00
var stash = curpagestate.name + ":" + curpagestate.arg
2019-09-16 21:46:33 +02:00
var honksonpage = document.getElementById("honksonpage")
var holder = honksonpage.children[0]
holder.remove()
2019-10-07 19:18:30 +02:00
var srvel = document.getElementById("srvmsg")
var msg = srvel.children[0]
if (msg) {
msg.remove()
servermsgs[stash] = msg
2019-09-16 21:46:33 +02:00
}
2019-10-07 19:18:30 +02:00
honksforpage[stash] = holder
curpagestate.name = name
curpagestate.arg = arg
// get the holder for the target page
var stash = name + ":" + arg
holder = honksforpage[stash]
2019-09-16 21:46:33 +02:00
if (holder) {
honksonpage.prepend(holder)
2019-10-07 19:18:30 +02:00
msg = servermsgs[stash]
if (msg) {
srvel.prepend(msg)
}
2019-09-16 21:46:33 +02:00
} else {
// or create one and fill it
2019-09-16 21:46:33 +02:00
honksonpage.prepend(document.createElement("div"))
var args = hydrargs()
2019-09-16 21:46:33 +02:00
get("/hydra?" + encode(args), fillinhonks)
}
2019-10-11 07:01:15 +02:00
refreshupdate("")
2019-09-16 21:46:33 +02:00
}
function newpagestate(name, arg) {
return { "name": name, "arg": arg }
}
function pageswitcher(name, arg) {
2019-09-16 21:15:32 +02:00
return function(evt) {
2019-10-03 22:08:31 +02:00
var topmenu = document.getElementById("topmenu")
topmenu.open = false
if (name == curpagestate.name && arg == curpagestate.arg) {
2019-09-16 21:15:32 +02:00
return false
}
switchtopage(name, arg)
2019-09-16 21:46:33 +02:00
var url = evt.srcElement.href
2019-10-07 22:33:31 +02:00
if (!url) {
url = evt.srcElement.parentElement.href
}
history.pushState(newpagestate(name, arg), "some title", url)
2019-10-01 23:02:53 +02:00
window.scrollTo(0, 0)
2019-09-16 21:15:32 +02:00
return false
}
}
function relinklinks() {
2019-09-16 21:15:32 +02:00
var els = document.getElementsByClassName("convoylink")
2019-10-02 01:45:17 +02:00
while (els.length) {
els[0].onclick = pageswitcher("convoy", els[0].text)
els[0].classList.remove("convoylink")
}
els = document.getElementsByClassName("combolink")
2019-10-02 01:45:17 +02:00
while (els.length) {
els[0].onclick = pageswitcher("combo", els[0].text)
els[0].classList.remove("combolink")
}
els = document.getElementsByClassName("honkerlink")
while (els.length) {
var el = els[0]
var xid = el.getAttribute("data-xid")
el.onclick = pageswitcher("honker", xid)
el.classList.remove("honkerlink")
2019-09-16 21:15:32 +02:00
}
}
(function() {
var el = document.getElementById("homelink")
el.onclick = pageswitcher("home", "")
2019-09-26 17:28:05 +02:00
el = document.getElementById("atmelink")
el.onclick = pageswitcher("atme", "")
2019-09-26 17:28:05 +02:00
el = document.getElementById("firstlink")
el.onclick = pageswitcher("first", "")
2019-10-11 22:03:17 +02:00
el = document.getElementById("savedlink")
el.onclick = pageswitcher("saved", "")
relinklinks()
2019-09-16 21:46:33 +02:00
window.onpopstate = statechanger
history.replaceState(curpagestate, "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.parentElement.parentElement.insertAdjacentElement('beforebegin', form)
2019-09-18 21:19:31 +02:00
} 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 showelement(id) {
var el = document.getElementById(id)
el.style.display = "block"
}
2019-09-18 21:19:31 +02:00
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 = ""
}
2019-10-05 22:41:17 +02:00
var checkinprec = 100.0
2019-10-05 19:11:24 +02:00
var gpsoptions = {
enableHighAccuracy: false,
timeout: 1000,
maximumAge: 0
};
2019-09-28 06:12:50 +02:00
function fillcheckin() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(pos) {
showelement("placedescriptor")
var el = document.getElementById("placelatinput")
2019-09-28 22:24:55 +02:00
el.value = Math.round(pos.coords.latitude * checkinprec) / checkinprec
2019-09-28 06:12:50 +02:00
el = document.getElementById("placelonginput")
2019-09-28 22:24:55 +02:00
el.value = Math.round(pos.coords.longitude * checkinprec) / checkinprec
checkinprec = 10000.0
2019-10-05 19:11:24 +02:00
gpsoptions.enableHighAccuracy = true
gpsoptions.timeout = 2000
2019-09-28 06:12:50 +02:00
}, function(err) {
showelement("placedescriptor")
2019-09-28 06:16:43 +02:00
el = document.getElementById("placenameinput")
el.value = err.message
2019-10-05 19:11:24 +02:00
}, gpsoptions)
2019-09-28 06:12:50 +02:00
}
}