Archived
1
0
This commit is contained in:
Jeff Becker 2017-01-16 11:47:53 -05:00
parent ad3089728a
commit a97c3d7d48

View File

@ -10,259 +10,231 @@ function getReplyTo() {
} }
function table_insert_row(table, header, items) { function table_insert_row(table, header, items) {
var tr = document.createElement("tr"); var tr = document.createElement("tr");
// insert header element // insert header element
var th = document.createElement("th"); var th = document.createElement("th");
th.appendChild(header); th.appendChild(header);
tr.appendChild(th); tr.appendChild(th);
// insert the rest of the elements // insert the rest of the elements
for (var idx = 0; idx < items.length; idx ++ ) { for (var idx = 0; idx < items.length; idx ++ ) {
var elem = document.createElement("td"); var elem = document.createElement("td");
elem.appendChild(items[idx]); elem.appendChild(items[idx]);
tr.appendChild(elem); tr.appendChild(elem);
} }
table.appendChild(tr); table.appendChild(tr);
} }
/** /**
build dynamic reply box build dynamic reply box
*/ */
function DynReply() { function DynReply(rootElem) {
var elem = document.createElement("div"); var elem = document.createElement("div");
elem.setAttribute("id", "postform_container"); this.elem = elem;
this.elem = elem; // reference
// build post form elem = document.createElement("input");
this.form = document.createElement("form"); elem.name = "reference";
this.form.enctype = "multipart/form-data"; elem.type = "hidden";
this.form.name = "post"; this.elem.appendChild(elem);
this.form.method = "post";
// reference
elem = document.createElement("input");
elem.setAttribute("id", "postform_reference");
elem.name = "reference";
elem.type = "hidden";
this.form.appendChild(elem);
var table = document.createElement("table"); var table = document.createElement("table");
table.setAttribute("class", "postform"); table.setAttribute("class", "postform");
var tbody = document.createElement("tbody"); var tbody = document.createElement("tbody");
var span = document.createElement("span"); var span = document.createElement("span");
// name // name
elem = document.createElement("input"); elem = document.createElement("input");
elem.setAttribute("name", "name"); elem.setAttribute("name", "name");
elem.setAttribute("value", "Anonymous"); elem.setAttribute("value", "Anonymous");
elem.setAttribute("id", "postform_name"); elem.setAttribute("id", "postform_name");
span.appendChild(elem); span.appendChild(elem);
// error message // error message
var err_elem = document.createElement("span"); var err_elem = document.createElement("span");
err_elem.setAttribute("id", "postform_msg"); err_elem.setAttribute("id", "postform_msg");
span.appendChild(err_elem); span.appendChild(err_elem);
this._error = err_elem; this._error = err_elem;
table_insert_row(tbody, document.createTextNode("Name"), [span]); table_insert_row(tbody, document.createTextNode("Name"), [span]);
// subject // subject
elem = document.createElement("input"); elem = document.createElement("input");
elem.setAttribute("name", "subject"); elem.setAttribute("name", "subject");
elem.setAttribute("value", ""); elem.setAttribute("value", "");
elem.setAttribute("id", "postform_subject"); elem.setAttribute("id", "postform_subject");
// submit // submit
var submit = document.createElement("input"); var submit = document.createElement("input");
submit.setAttribute("value", "reply"); submit.setAttribute("value", "reply");
submit.setAttribute("class", "button"); submit.setAttribute("class", "button");
submit.setAttribute("type", "submit"); submit.setAttribute("type", "submit");
submit.setAttribute("id", "postform_submit"); submit.setAttribute("id", "postform_submit");
table_insert_row(tbody, document.createTextNode("Subject"), [elem, submit]); table_insert_row(tbody, document.createTextNode("Subject"), [elem, submit]);
// Comment // Comment
elem = document.createElement("textarea"); elem = document.createElement("textarea");
elem.setAttribute("id", "postform_message"); elem.setAttribute("id", "postform_message");
elem.setAttribute("name", "message"); elem.setAttribute("name", "message");
elem.setAttribute("cols", "40"); elem.setAttribute("cols", "40");
elem.setAttribute("rows", "5"); elem.setAttribute("rows", "5");
table_insert_row(tbody, document.createTextNode("Comment"), [elem]); table_insert_row(tbody, document.createTextNode("Comment"), [elem]);
// file // file
elem = document.createElement("input"); elem = document.createElement("input");
elem.setAttribute("class", "postform_attachment"); elem.setAttribute("class", "postform_attachment");
elem.setAttribute("id", "postform_attachments"); elem.setAttribute("type", "file");
elem.setAttribute("type", "file"); elem.setAttribute("name", "attachment_uploaded");
elem.setAttribute("name", "attachment_uploaded"); elem.setAttribute("multiple", "multiple");
elem.setAttribute("multiple", "multiple"); table_insert_row(tbody, document.createTextNode("Files"), [elem]);
table_insert_row(tbody, document.createTextNode("Files"), [elem]);
// dubs // dubs
elem = document.createElement("input"); elem = document.createElement("input");
elem.setAttribute("type", "checkbox"); elem.setAttribute("type", "checkbox");
elem.setAttribute("name", "dubs"); elem.setAttribute("name", "dubs");
table_insert_row(tbody, document.createTextNode("Get Dubs"), [elem]); table_insert_row(tbody, document.createTextNode("Get Dubs"), [elem]);
// captcha // captcha
elem = document.createElement("img"); elem = document.createElement("img");
elem.setAttribute("id", "captcha_img"); elem.alt = "captcha";
elem.alt = "captcha"; table_insert_row(tbody, document.createTextNode("Captcha"), [elem]);
table_insert_row(tbody, document.createTextNode("Captcha"), [elem]);
// captcha solution // captcha solution
elem = document.createElement("input"); elem = document.createElement("input");
elem.name = "captcha"; elem.name = "captcha";
elem.autocomplete = "off"; elem.autocomplete = "off";
elem.setAttribute("id", "captcha_solution");
table_insert_row(tbody, document.createTextNode("Solution"), [elem]); table_insert_row(tbody, document.createTextNode("Solution"), [elem]);
table.appendChild(tbody); table.appendChild(tbody);
this.form.appendChild(table); this.elem.appendChild(table);
this.elem.appendChild(this.form); this.board = null;
document.body.appendChild(this.elem); this.rootmsg = null;
this.board = null; this.prefix = null;
this.rootmsg = null; this.url = null;
this.prefix = null; this.x = 1;
this.url = null; this.y = 1;
this.x = 1; rootElem.appendChild(this.elem);
this.y = 1;
}
DynReply.prototype.update = function() {
if (this.prefix) {
// update captcha
this.updateCaptcha();
if (this.board) {
// update post form
var ref = document.getElementById("postform_reference");
if (this.rootmsg) {
ref.setAttribute("value", this.rootmsg);
} else {
ref.setAttribute("value", "");
}
this.url = this.prefix + "post/" + this.board + "/json";
}
}
} }
DynReply.prototype.show = function() { DynReply.prototype.show = function() {
console.log("show dynreply"); console.log("show dynreply");
this.update(); this.update();
this.elem.style.display = 'inline'; this.elem.style.display = 'inline';
} }
DynReply.prototype.hide = function() { DynReply.prototype.hide = function() {
console.log("hide dynreply"); console.log("hide dynreply");
this.elem.style.display = "none"; this.elem.style.display = "none";
} }
// clear all fields // clear all fields
DynReply.prototype.clear = function() { DynReply.prototype.clear = function() {
this.clearSolution(); this.clearSolution();
this.clearPostbox(); this.clearPostbox();
} }
// clear captcha solution // clear captcha solution
DynReply.prototype.clearSolution = function() { DynReply.prototype.clearSolution = function() {
var e = document.getElementById("captcha_solution"); var e = document.getElementById("captcha_solution");
// reset value // reset value
e.value = ""; e.value = "";
} }
// clear postform elements // clear postform elements
DynReply.prototype.clearPostbox = function() { DynReply.prototype.clearPostbox = function() {
var e = document.getElementById("postform_subject"); var e = document.getElementById("postform_subject");
e.value = ""; e.value = "";
e = document.getElementById("postform_message"); e = document.getElementById("postform_message");
e.value = ""; e.value = "";
e = document.getElementById("postform_attachments"); e = document.getElementById("postform_attachments");
e.value = null; e.value = null;
} }
DynReply.prototype.post = function(cb, err_cb) { DynReply.prototype.post = function(cb, err_cb) {
if (this.url && this.form) { if (this.url && this.form) {
var data = new FormData(this.form); var data = new FormData(this.form);
var ajax = new XMLHttpRequest(); var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function(ev) { ajax.onreadystatechange = function(ev) {
if (ajax.readyState == XMLHttpRequest.DONE) { if (ajax.readyState == XMLHttpRequest.DONE) {
var j = null; var j = null;
try { try {
j = JSON.parse(ajax.responseText); j = JSON.parse(ajax.responseText);
cb(j); cb(j);
} catch (e) { } catch (e) {
if(err_cb) { if(err_cb) {
err_cb(e); err_cb(e);
} }
}
}
} }
} ajax.open("POST", this.url);
ajax.send(data);
} }
ajax.open("POST", this.url);
ajax.send(data);
}
} }
DynReply.prototype.updateCaptcha = function() { DynReply.prototype.updateCaptcha = function() {
if (this.prefix) { if (this.prefix) {
var captcha_img = document.getElementById("captcha_img"); var captcha_img = document.getElementById("captcha_img");
captcha_img.src = this.prefix + "captcha/img"; captcha_img.src = this.prefix + "captcha/img";
} }
this.clearSolution(); this.clearSolution();
} }
DynReply.prototype.setPrefix = function(prefix) { DynReply.prototype.setPrefix = function(prefix) {
this.prefix = prefix; this.prefix = prefix;
} }
DynReply.prototype.hide = function() { DynReply.prototype.hide = function() {
this.elem.style.display = 'none'; this.elem.style.display = 'none';
} }
DynReply.prototype.setBoard = function(boardname) { DynReply.prototype.setBoard = function(boardname) {
if (boardname) { if (boardname) {
this.board = boardname; this.board = boardname;
} }
} }
DynReply.prototype.setRoot = function(rootmsg) { DynReply.prototype.setRoot = function(rootmsg) {
if (rootmsg) { if (rootmsg) {
this.rootmsg = rootmsg; this.rootmsg = rootmsg;
} }
} }
DynReply.prototype.showError = function(msg) { DynReply.prototype.showError = function(msg) {
console.log("error in dynreply: "+msg); console.log("error in dynreply: "+msg);
this._error.setAttribute("class", "error message"); this._error.setAttribute("class", "error message");
this._error.appendChild(document.createTextNode(msg)); this._error.appendChild(document.createTextNode(msg));
this.updateCaptcha(); this.updateCaptcha();
} }
DynReply.prototype.showMessage = function(msg) { DynReply.prototype.showMessage = function(msg) {
this._error.setAttribute("class", "message"); this._error.setAttribute("class", "message");
this._error.innerHTML = ""; this._error.innerHTML = "";
this._error.appendChild(document.createTextNode(msg)); this._error.appendChild(document.createTextNode(msg));
var e = this._error; var e = this._error;
setTimeout(function() { setTimeout(function() {
// clear it // clear it
e.innerHTML = ""; e.innerHTML = "";
}, 2000); }, 2000);
} }
// reply box function // reply box function
function nntpchan_reply(parent, shorthash) { function nntpchan_reply(parent, shorthash) {
if (parent && document.dynreply) { if (parent && document.dynreply) {
var boardname = parent.dataset.newsgroup; var boardname = parent.dataset.newsgroup;
var rootmsg = parent.dataset.rootmsgid; var rootmsg = parent.dataset.rootmsgid;
var replyto = getReplyTo(); var replyto = getReplyTo();
// set target // set target
replyto.setBoard(boardname); replyto.setBoard(boardname);
replyto.setRoot(rootmsg); replyto.setRoot(rootmsg);
// show it // show it
replyto.show(); replyto.show();
} }
var elem = document.getElementById("postform_message"); var elem = document.getElementById("postform_message");
if ( elem ) if ( elem )
{ {
elem.value += ">>" + shorthash.substr(0,10) + "\n"; elem.value += ">>" + shorthash.substr(0,10) + "\n";
} }
} }
@ -280,44 +252,44 @@ function init(prefix) {
var $dragging = null; var $dragging = null;
$(rpl.elem).on("mousemove", function(ev) { $(rpl.elem).on("mousemove", function(ev) {
if ($dragging) { if ($dragging) {
var x = ev.pageX - $(this).width() / 2, var x = ev.pageX - $(this).width() / 2,
y = ev.pageY - $(this).height() / 2; y = ev.pageY - $(this).height() / 2;
$dragging.offset({ $dragging.offset({
top: y - 50, top: y - 50,
left: x - 50 left: x - 50
}); });
} }
}); });
$(rpl.elem).on("mousedown", e, function (ev) { $(rpl.elem).on("mousedown", e, function (ev) {
$dragging = $(rpl.elem); $dragging = $(rpl.elem);
}); });
$(rpl.elem).on("mouseup", function (e) { $(rpl.elem).on("mouseup", function (e) {
$dragging = null; $dragging = null;
}); });
// add replyto post handlers // add replyto post handlers
var postit = function() { var postit = function() {
// do ajax request to post data // do ajax request to post data
var r = getReplyTo(); var r = getReplyTo();
r.showMessage("posting... "); r.showMessage("posting... ");
r.post(function(j) { r.post(function(j) {
if(j.error) { if(j.error) {
// an error happened // an error happened
r.showError(j.error); r.showError(j.error);
} else { } else {
// we're good // we're good
r.showMessage("posted :^)"); r.showMessage("posted :^)");
r.updateCaptcha(); r.updateCaptcha();
r.clear(); r.clear();
} }
}, function(err) { }, function(err) {
r.showError(err); r.showError(err);
r.clearSolution(); r.clearSolution();
}); });
} }
} }