Archived
1
0
This repository has been archived on 2023-08-12. You can view files and clone it, but cannot push or open issues or pull requests.
nntpchan/contrib/static/mod.js

122 lines
2.8 KiB
JavaScript
Raw Normal View History

2015-08-04 19:03:44 +05:00
/*
* mod.js, moderator page js stuff
*/
// TODO: implement mod panel all the way
document.onload = function(ev) {
// populate the mod page with stuff
}
function get_longhash(str) {
var idx = str.indexOf("#") + 1;
if ( idx > 0 ) {
str = str.substr(idx);
}
console.log(str);
return str;
}
2015-08-07 19:59:57 +05:00
// handle ban command
function nntpchan_ban() {
nntpchan_mod({
name: "ban",
handle: function(j) {
if (j.banned) {
return document.createTextNode(j.banned);
}
}
});
}
2015-08-04 19:03:44 +05:00
// handle delete command
function nntpchan_delete() {
2015-08-07 19:59:57 +05:00
nntpchan_mod({
name: "del",
handle: function(j) {
var elem = document.createElement("div");
if (j.deleted) {
for ( var idx = 0 ; idx < j.deleted.length ; idx ++ ) {
var msg = "deleted: " + j.deleted[idx];
var e = document.createTextNode(msg);
var el = document.createElement("div");
el.appendChild(e);
elem.appendChild(el);
}
}
if (j.notdeleted) {
for ( var idx = 0 ; idx < j.notdeleted.length ; idx ++ ) {
var msg = "not deleted: " + j.notdeleted[idx];
var e = document.createTextNode(msg);
var el = document.createElement("div");
el.appendChild(e);
elem.appendChild(el);
}
}
return elem;
}
});
}
function nntpchan_mod(mod_action) {
2015-08-04 19:03:44 +05:00
// get the element
2015-08-07 19:59:57 +05:00
var input = document.getElementById("nntpchan_mod_target");
2015-08-04 19:03:44 +05:00
// get the long hash
var longhash = get_longhash(input.value);
var elem = document.getElementById("nntpchan_mod_result");
// clear old results
while( elem.firstChild ) {
elem.removeChild(elem.firstChild);
}
2015-08-07 19:59:57 +05:00
2015-08-04 19:03:44 +05:00
// fire off ajax
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if (ajax.readyState == XMLHttpRequest.DONE) {
var status = ajax.status;
// we gud?
if (status == 200) {
// yah
var txt = ajax.responseText;
var j = JSON.parse(txt);
if (j.error) {
var e = document.createTextNode(j.error);
elem.appendChild(e);
} else {
2015-08-07 19:59:57 +05:00
if (mod_action.handle) {
var result = mod_action.handle(j);
if (result) {
elem.appendChild(result);
} else {
// fail
alert("mod action failed, handler returned nothing");
2015-08-04 19:03:44 +05:00
}
2015-08-07 19:59:57 +05:00
} else {
// fail
alert("mod action has no handler");
2015-08-04 19:03:44 +05:00
}
}
2015-08-07 20:11:24 +05:00
} else if (status) {
2015-08-07 20:06:04 +05:00
// nah
// http error
elem.innerHTML = "error: HTTP "+status;
2015-08-04 19:03:44 +05:00
}
2015-08-07 20:06:04 +05:00
// clear input
input.value = "";
2015-08-04 19:03:44 +05:00
}
2015-08-07 19:59:57 +05:00
}
if (mod_action.name) {
var url = mod_action.name + "/" + longhash;
ajax.open("GET", url);
ajax.send();
} else {
alert("mod action has no name");
2015-08-04 19:03:44 +05:00
}
}