Gazelle/static/functions/comments.js

353 lines
12 KiB
JavaScript
Raw Normal View History

2011-03-28 14:21:28 +00:00
var username;
var postid;
2013-01-22 08:00:16 +00:00
function QuoteJump(event, post) {
var button = event.button;
2012-08-15 08:00:15 +00:00
var hash = "#post" + post;
2013-04-18 08:00:54 +00:00
if (button == 0) {
if ($(hash).raw() != null) {
2013-01-22 08:00:16 +00:00
window.location.hash = hash;
2013-04-18 08:00:54 +00:00
} else {
2013-01-22 08:00:16 +00:00
window.open("forums.php?action=viewthread&postid="+post, '_self');
}
2013-04-18 08:00:54 +00:00
} else if (button == 1) {
2013-01-22 08:00:16 +00:00
window.open("forums.php?action=viewthread&postid="+post, '_window');
2012-08-15 08:00:15 +00:00
}
}
2011-03-28 14:21:28 +00:00
function Quote(post, user) {
2013-02-22 08:00:24 +00:00
Quote(post, user, false)
2012-08-12 08:00:16 +00:00
}
2013-07-14 08:00:49 +00:00
var original_post;
2012-08-12 08:00:16 +00:00
function Quote(post, user, link) {
2011-03-28 14:21:28 +00:00
username = user;
postid = post;
2013-07-14 08:00:49 +00:00
if(!$('#reply_box').length) {
if ($("#quote_" + postid).text() == "Quote") {
original_post = $("#content" + postid).html();
$("#quote_" + postid).text("Unquote");
$.ajax({
type : "POST",
url: "ajax.php?action=raw_bbcode",
dataType: "json",
data : {
"postid" : postid
}
}).done(function(response) {
$("#content" + postid).html(response['response']['body']);
select_all($("#content" + postid).get(0));
});
} else {
document.getSelection().removeAllRanges();
$("#content" + postid).html(original_post);
$("#quote_" + postid).text("Quote");
2011-03-28 14:21:28 +00:00
}
2013-07-14 08:00:49 +00:00
} else {
ajax.get("?action=get_post&post=" + postid, function(response) {
if ($('#quickpost').raw().value !== '') {
$('#quickpost').raw().value = $('#quickpost').raw().value + "\n\n";
}
$('#quickpost').raw().value = $('#quickpost').raw().value + "[quote=" + username + (link == true ? "|" + post : "") + "]" +
//response.replace(/(img|aud)(\]|=)/ig,'url$2').replace(/\[url\=(https?:\/\/[^\s\[\]<>"\'()]+?)\]\[url\](.+?)\[\/url\]\[\/url\]/gi, "[url]$1[/url]")
html_entity_decode(response)
+ "[/quote]";
resize('quickpost');
});
}
2011-03-28 14:21:28 +00:00
}
function Edit_Form(post,key) {
2013-04-18 08:00:54 +00:00
postid = post;
//If no edit is already going underway or a previous edit was finished, make the necessary dom changes.
2013-06-27 08:01:06 +00:00
if (!$('#editbox' + postid).results() || $('#editbox' + postid + '.hidden').results()) {
2013-06-17 08:01:02 +00:00
$('#reply_box').ghide();
2013-04-18 08:00:54 +00:00
if (location.href.match(/torrents\.php/) ||
location.href.match(/artist\.php/)) {
2013-06-18 08:00:48 +00:00
boxWidth = "50";
2013-04-18 08:00:54 +00:00
} else {
2013-06-18 08:00:48 +00:00
boxWidth = "80";
2013-04-18 08:00:54 +00:00
}
2013-06-27 08:01:06 +00:00
postuserid = $('#post' + postid + ' strong a').attr('href').split('=')[1];
2013-04-18 08:00:54 +00:00
if (postuserid != userid) {
2013-06-18 08:00:48 +00:00
pmbox = '<span id="pmbox' + postid + '"><label>PM user on edit? <input type="checkbox" name="pm" value="1" /></label></span>';
2013-04-18 08:00:54 +00:00
} else {
pmbox = '';
};
$('#bar' + postid).raw().cancel = $('#content' + postid).raw().innerHTML;
$('#bar' + postid).raw().oldbar = $('#bar' + postid).raw().innerHTML;
2013-06-18 08:00:48 +00:00
$('#content' + postid).raw().innerHTML = "<div id=\"preview" + postid + "\"></div><form id=\"form" + postid + "\" method=\"post\" action=\"\">" + pmbox + "<input type=\"hidden\" name=\"auth\" value=\"" + authkey + "\" /><input type=\"hidden\" name=\"key\" value=\"" + key + "\" /><input type=\"hidden\" name=\"post\" value=\"" + postid + "\" /><textarea id=\"editbox" + postid + "\" onkeyup=\"resize('editbox" + postid + "');\" name=\"body\" cols=\"" + boxWidth + "\" rows=\"10\"></textarea></form>";
2013-04-18 08:00:54 +00:00
$('#bar' + postid).raw().innerHTML = '<input type="button" value="Preview" onclick="Preview_Edit(' + postid + ');" /><input type="button" value="Post" onclick="Save_Edit(' + postid + ')" /><input type="button" value="Cancel" onclick="Cancel_Edit(' + postid + ');" />';
2011-03-28 14:21:28 +00:00
}
2013-04-18 08:00:54 +00:00
/* If it's the initial edit, fetch the post content to be edited.
* If editing is already underway and edit is pressed again, reset the post
* (keeps current functionality, move into brackets to stop from happening).
*/
2013-05-04 08:00:48 +00:00
ajax.get("?action=get_post&post=" + postid, function(response) {
2011-03-28 14:21:28 +00:00
$('#editbox' + postid).raw().value = html_entity_decode(response);
resize('editbox' + postid);
});
}
function Cancel_Edit(postid) {
2013-02-24 08:00:18 +00:00
var answer = confirm("Are you sure you want to cancel?");
if (answer) {
2013-06-17 08:01:02 +00:00
$('#reply_box').gshow();
2013-02-24 08:00:18 +00:00
$('#bar' + postid).raw().innerHTML = $('#bar' + postid).raw().oldbar;
$('#content' + postid).raw().innerHTML = $('#bar' + postid).raw().cancel;
}
2011-03-28 14:21:28 +00:00
}
function Preview_Edit(postid) {
$('#bar' + postid).raw().innerHTML = "<input type=\"button\" value=\"Editor\" onclick=\"Cancel_Preview(" + postid + ");\" /><input type=\"button\" value=\"Post\" onclick=\"Save_Edit(" + postid + ")\" /><input type=\"button\" value=\"Cancel\" onclick=\"Cancel_Edit(" + postid + ");\" />";
2013-05-04 08:00:48 +00:00
ajax.post("ajax.php?action=preview","form" + postid, function(response) {
2011-03-28 14:21:28 +00:00
$('#preview' + postid).raw().innerHTML = response;
2013-06-17 08:01:02 +00:00
$('#editbox' + postid).ghide();
2011-03-28 14:21:28 +00:00
});
}
function Cancel_Preview(postid) {
$('#bar' + postid).raw().innerHTML = "<input type=\"button\" value=\"Preview\" onclick=\"Preview_Edit(" + postid + ");\" /><input type=\"button\" value=\"Post\" onclick=\"Save_Edit(" + postid + ")\" /><input type=\"button\" value=\"Cancel\" onclick=\"Cancel_Edit(" + postid + ");\" />";
$('#preview' + postid).raw().innerHTML = "";
2013-06-17 08:01:02 +00:00
$('#editbox' + postid).gshow();
2011-03-28 14:21:28 +00:00
}
function Save_Edit(postid) {
2013-06-17 08:01:02 +00:00
$('#reply_box').gshow();
2011-03-28 14:21:28 +00:00
if (location.href.match(/forums\.php/)) {
ajax.post("forums.php?action=takeedit","form" + postid, function (response) {
2013-02-09 08:01:01 +00:00
$('#bar' + postid).raw().innerHTML = "<a href=\"reports.php?action=report&amp;type=post&amp;id="+postid+"\" class=\"brackets\">Report</a>&nbsp;<a href=\"#\">&uarr;</a>";
2011-03-28 14:21:28 +00:00
$('#preview' + postid).raw().innerHTML = response;
2013-06-17 08:01:02 +00:00
$('#editbox' + postid).ghide();
$('#pmbox' + postid).ghide();
2011-03-28 14:21:28 +00:00
});
} else if (location.href.match(/collages?\.php/)) {
ajax.post("collages.php?action=takeedit_comment","form" + postid, function (response) {
$('#bar' + postid).raw().innerHTML = "";
$('#preview' + postid).raw().innerHTML = response;
2013-06-17 08:01:02 +00:00
$('#editbox' + postid).ghide();
2011-03-28 14:21:28 +00:00
});
} else if (location.href.match(/requests\.php/)) {
ajax.post("requests.php?action=takeedit_comment","form" + postid, function (response) {
$('#bar' + postid).raw().innerHTML = "";
$('#preview' + postid).raw().innerHTML = response;
2013-06-17 08:01:02 +00:00
$('#editbox' + postid).ghide();
2011-03-28 14:21:28 +00:00
});
2012-10-27 08:00:09 +00:00
} else if (location.href.match(/artist\.php/)) {
ajax.post("artist.php?action=takeedit_post","form" + postid, function (response) {
$('#bar' + postid).raw().innerHTML = "";
$('#preview' + postid).raw().innerHTML = response;
2013-06-17 08:01:02 +00:00
$('#editbox' + postid).ghide();
2012-10-27 08:00:09 +00:00
});
2011-03-28 14:21:28 +00:00
} else {
ajax.post("torrents.php?action=takeedit_post","form" + postid, function (response) {
$('#bar' + postid).raw().innerHTML = "";
$('#preview' + postid).raw().innerHTML = response;
2013-06-17 08:01:02 +00:00
$('#editbox' + postid).ghide();
2011-03-28 14:21:28 +00:00
});
}
}
function Delete(post) {
postid = post;
if (confirm('Are you sure you wish to delete this post?') == true) {
if (location.href.match(/forums\.php/)) {
ajax.get("forums.php?action=delete&auth=" + authkey + "&postid=" + postid, function () {
2013-06-17 08:01:02 +00:00
$('#post' + postid).ghide();
2011-03-28 14:21:28 +00:00
});
2012-05-18 13:35:17 +00:00
} else if (location.href.match(/collages?\.php/)) {
ajax.get("collages.php?action=delete_comment&auth=" + authkey + "&postid=" + postid, function () {
2013-06-17 08:01:02 +00:00
$('#post' + postid).ghide();
2011-03-28 14:21:28 +00:00
});
} else if (location.href.match(/requests\.php/)) {
ajax.get("requests.php?action=delete_comment&auth=" + authkey + "&postid=" + postid, function () {
2013-06-17 08:01:02 +00:00
$('#post' + postid).ghide();
2011-03-28 14:21:28 +00:00
});
2013-04-18 08:00:54 +00:00
} else if (location.href.match(/artist\.php/)) {
2013-05-04 08:00:48 +00:00
ajax.get("artist.php?action=delete_comment&auth="+authkey+ "&postid=" + postid, function () {
2013-06-17 08:01:02 +00:00
$('#post' + postid).ghide();
2013-04-18 08:00:54 +00:00
});
2011-03-28 14:21:28 +00:00
} else {
ajax.get("torrents.php?action=delete_post&auth=" + authkey + "&postid=" + postid, function () {
2013-06-17 08:01:02 +00:00
$('#post' + postid).ghide();
2011-03-28 14:21:28 +00:00
});
}
}
}
function Quick_Preview() {
var quickreplybuttons;
$('#post_preview').raw().value = "Make changes";
$('#post_preview').raw().preview = true;
2013-06-18 08:00:48 +00:00
ajax.post("ajax.php?action=preview", "quickpostform", function(response) {
2013-06-17 08:01:02 +00:00
$('#quickreplypreview').gshow();
2011-03-28 14:21:28 +00:00
$('#contentpreview').raw().innerHTML = response;
2013-06-17 08:01:02 +00:00
$('#quickreplytext').ghide();
2011-03-28 14:21:28 +00:00
});
}
function Quick_Edit() {
var quickreplybuttons;
$('#post_preview').raw().value = "Preview";
$('#post_preview').raw().preview = false;
2013-06-17 08:01:02 +00:00
$('#quickreplypreview').ghide();
$('#quickreplytext').gshow();
2011-03-28 14:21:28 +00:00
}
function Newthread_Preview(mode) {
2013-06-17 08:01:02 +00:00
$('#newthreadpreviewbutton').gtoggle();
$('#newthreadeditbutton').gtoggle();
2013-04-18 08:00:54 +00:00
if (mode) { // Preview
2013-06-18 08:00:48 +00:00
ajax.post("ajax.php?action=preview", "newthreadform", function(response) {
2011-03-28 14:21:28 +00:00
$('#contentpreview').raw().innerHTML = response;
});
$('#newthreadtitle').raw().innerHTML = $('#title').raw().value;
var pollanswers = $('#answer_block').raw();
2013-04-18 08:00:54 +00:00
if (pollanswers && pollanswers.children.length > 4) {
2011-03-28 14:21:28 +00:00
pollanswers = pollanswers.children;
$('#pollquestion').raw().innerHTML = $('#pollquestionfield').raw().value;
2013-05-06 08:00:32 +00:00
for (var i = 0; i < pollanswers.length; i += 2) {
2013-04-18 08:00:54 +00:00
if (!pollanswers[i].value) {
continue;
}
2011-03-28 14:21:28 +00:00
var el = document.createElement('input');
2013-05-06 08:00:32 +00:00
el.id = 'answer_' + (i + 1);
2011-03-28 14:21:28 +00:00
el.type = 'radio';
el.name = 'vote';
$('#pollanswers').raw().appendChild(el);
$('#pollanswers').raw().appendChild(document.createTextNode(' '));
el = document.createElement('label');
2013-05-06 08:00:32 +00:00
el.htmlFor = 'answer_' + (i + 1);
2011-03-28 14:21:28 +00:00
el.innerHTML = pollanswers[i].value;
$('#pollanswers').raw().appendChild(el);
$('#pollanswers').raw().appendChild(document.createElement('br'));
}
2013-04-18 08:00:54 +00:00
if ($('#pollanswers').raw().children.length > 4) {
2013-06-17 08:01:02 +00:00
$('#pollpreview').gshow();
2011-03-28 14:21:28 +00:00
}
}
} else { // Back to editor
2013-06-17 08:01:02 +00:00
$('#pollpreview').ghide();
2011-03-28 14:21:28 +00:00
$('#newthreadtitle').raw().innerHTML = 'New Topic';
var pollanswers = $('#pollanswers').raw();
2013-04-18 08:00:54 +00:00
if (pollanswers) {
2011-03-28 14:21:28 +00:00
var el = document.createElement('div');
el.id = 'pollanswers';
pollanswers.parentNode.replaceChild(el, pollanswers);
}
}
2013-06-17 08:01:02 +00:00
$('#newthreadtext').gtoggle();
$('#newthreadpreview').gtoggle();
$('#subscribediv').gtoggle();
2011-03-28 14:21:28 +00:00
}
function LoadEdit(type, post, depth) {
ajax.get("forums.php?action=ajax_get_edit&postid=" + post + "&depth=" + depth + "&type=" + type, function(response) {
$('#content' + post).raw().innerHTML = response;
}
);
}
function AddPollOption(id) {
var list = $('#poll_options').raw();
var item = document.createElement("li");
var form = document.createElement("form");
form.method = "POST";
var auth = document.createElement("input");
auth.type = "hidden";
auth.name = "auth";
auth.value = authkey;
form.appendChild(auth);
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
var action = document.createElement("input");
action.type = "hidden";
action.name = "action";
action.value = "add_poll_option";
form.appendChild(action);
var threadid = document.createElement("input");
threadid.type = "hidden";
threadid.name = "threadid";
threadid.value = id;
form.appendChild(threadid);
var input = document.createElement("input");
input.type = "text";
input.name = "new_option";
input.size = "50";
form.appendChild(input);
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
var submit = document.createElement("input");
submit.type = "submit";
submit.id = "new_submit";
submit.value = "Add";
form.appendChild(submit);
item.appendChild(form);
list.appendChild(item);
}
2012-10-27 08:00:09 +00:00
/**
* HTML5-compatible storage system
* Tries to use 'oninput' event to detect text changes and sessionStorage to save it.
*
2012-10-28 08:00:19 +00:00
* new StoreText('some_textarea_id', 'some_form_id', 'some_topic_id')
2012-10-27 08:00:09 +00:00
* The form is required to remove the stored text once it is submitted.
2012-10-28 08:00:19 +00:00
*
* Topic ID is required to retrieve the right text on the right topic
2012-10-27 08:00:09 +00:00
**/
2012-10-28 08:00:19 +00:00
function StoreText (field, form, topic) {
2012-10-27 08:00:09 +00:00
this.field = document.getElementById(field);
this.form = document.getElementById(form);
this.key = 'auto_save_temp';
2012-10-28 08:00:19 +00:00
this.keyID = 'auto_save_temp_id';
this.topic = +topic;
2012-10-27 08:00:09 +00:00
this.load();
}
StoreText.prototype = {
constructor : StoreText,
load : function () {
2012-10-28 08:00:19 +00:00
if (this.enabled() && this.valid()) {
2012-10-27 08:00:09 +00:00
this.retrieve();
this.autosave();
this.clearForm();
}
},
2012-10-28 08:00:19 +00:00
valid : function () {
return this.field && this.form && !isNaN(this.topic);
},
2012-10-27 08:00:09 +00:00
enabled : function () {
return window.sessionStorage && typeof window.sessionStorage === 'object';
},
retrieve : function () {
var r = sessionStorage.getItem(this.key);
2012-10-28 08:00:19 +00:00
if (this.topic === +sessionStorage.getItem(this.keyID) && r) {
this.field.value = r;
2012-10-27 08:00:09 +00:00
}
},
remove : function () {
2012-10-28 08:00:19 +00:00
sessionStorage.removeItem(this.keyID);
2012-10-27 08:00:09 +00:00
sessionStorage.removeItem(this.key);
},
save : function () {
2012-10-28 08:00:19 +00:00
sessionStorage.setItem(this.keyID, this.topic);
2012-10-27 08:00:09 +00:00
sessionStorage.setItem(this.key, this.field.value);
},
autosave : function () {
2013-06-27 08:01:06 +00:00
$(this.field).on(this.getInputEvent(), $.proxy(this.save, this));
2012-10-27 08:00:09 +00:00
},
getInputEvent : function () {
var e;
if ('oninput' in this.field) {
e = 'input';
} else if (document.body.addEventListener) {
e = 'change keyup paste cut';
} else {
e = 'propertychange';
}
return e;
},
clearForm : function () {
2013-06-27 08:01:06 +00:00
$(this.form).submit($.proxy(this.remove, this));
2012-10-27 08:00:09 +00:00
}
};