2017-10-24 00:57:44 +00:00
|
|
|
function escapeRegExp(str) {
|
|
|
|
return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
|
|
|
|
}
|
2017-10-23 11:39:49 +00:00
|
|
|
jQuery(document).ready(function () {
|
2017-10-25 00:58:07 +00:00
|
|
|
var commentForm = $('#comments-form'); //$(document).find('.comments-form').first();
|
|
|
|
var commentSection = $('#comments-section'); //$(document).find('.comments').first();
|
|
|
|
var commentAlert = $('#comments-alert'); //$(document).find('.alert').first();
|
2017-10-24 00:57:44 +00:00
|
|
|
|
2017-10-23 11:39:49 +00:00
|
|
|
//hide form, show link
|
|
|
|
commentForm.hide();
|
|
|
|
$(document).find('.comment-add-new').show();
|
2017-10-24 00:57:44 +00:00
|
|
|
|
2017-10-23 11:39:49 +00:00
|
|
|
//show comment form above comments section (new comment thread)
|
|
|
|
$('body').on('click', '.comment-add-new', function (e) {
|
|
|
|
e.preventDefault();
|
2017-10-25 00:58:07 +00:00
|
|
|
if ($(this).prev().filter('#comments-form').length > 0) {
|
|
|
|
//form is already in the right place.
|
|
|
|
//just make sure it is visible.
|
|
|
|
commentForm.show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
commentForm.hide(); //hide it to make sure that it is not shown after move to make "show" transition work.
|
2017-10-23 11:39:49 +00:00
|
|
|
$(this).before(commentForm);
|
|
|
|
commentForm.show('slow');
|
2017-10-25 00:58:07 +00:00
|
|
|
commentAlert.empty().slideUp();
|
2017-10-23 11:39:49 +00:00
|
|
|
});
|
2017-10-24 00:57:44 +00:00
|
|
|
|
2017-10-23 11:39:49 +00:00
|
|
|
//show comment form below selected comment (reply to existing comment)
|
|
|
|
$('body').on('click', '.comment-add-reply', function (e) {
|
|
|
|
e.preventDefault();
|
2017-10-25 00:58:07 +00:00
|
|
|
var comment = $(this).closest('.comment');
|
|
|
|
if (comment.find('#comments-form').length > 0) {
|
|
|
|
//form is already in the right place.
|
|
|
|
//just make sure it is visible.
|
|
|
|
commentForm.show();
|
|
|
|
return;
|
|
|
|
}
|
2017-10-23 11:39:49 +00:00
|
|
|
commentForm.hide();
|
2017-10-25 00:58:07 +00:00
|
|
|
comment.find('.comment-body').last().append(commentForm);
|
2017-10-23 11:39:49 +00:00
|
|
|
commentForm.show('slow');
|
2017-10-25 00:58:07 +00:00
|
|
|
commentAlert.empty().slideUp();
|
|
|
|
});
|
|
|
|
|
|
|
|
//delete comment (authorized user only)
|
|
|
|
$('body').on('click', '.comment-delete', function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var comment = $(this).closest('.comment');
|
|
|
|
var id = parseInt(comment.attr('data-id'), 10);
|
|
|
|
var level = parseInt(comment.attr('data-level'), 10);
|
|
|
|
var nonce = commentForm.find("input[name='form-nonce']").val();
|
|
|
|
if (comment.next().filter(".comment[data-level='" + (level + 1) + "']").length > 0) {
|
|
|
|
alert('Deletion not allowed. There are replies to this comment. Please delete them first.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var url = commentForm.attr( "action" );
|
|
|
|
var posting = $.post(url, { action: 'delete', id: id, nonce: nonce}, null, 'json');
|
|
|
|
// Register events to ajax call
|
|
|
|
posting.done(function (response) {
|
|
|
|
|
|
|
|
//make sure that commentForm is definitely not within the deleted DOM part.
|
|
|
|
//hide
|
|
|
|
//temporary move it outside the comment selected for deletion. (this definitely exists, not taking any chances here)
|
|
|
|
//finally move back to start of commentSection. (preferred target)
|
|
|
|
//Hint: Don't forget commentAlert as it is not inside the form.
|
|
|
|
commentAlert.empty().hide();
|
|
|
|
commentForm.hide();
|
|
|
|
comment.before(commentForm);
|
|
|
|
comment.before(commentAlert);
|
|
|
|
commentSection.prepend(commentAlert);
|
|
|
|
commentSection.prepend(commentForm);
|
|
|
|
//remove the comment and all content from DOM.
|
|
|
|
//detach would be a soft delete but as there is no reason to reuse the deleted comment, means should not be provided.
|
|
|
|
comment.remove();
|
|
|
|
});
|
|
|
|
posting.fail(function (status, error, title) {
|
|
|
|
//alert('error');
|
|
|
|
//console.log("Response Data (fail)", JSON.parse(JSON.stringify(status)));
|
|
|
|
commentForm.after(commentAlert);
|
|
|
|
commentAlert.show();
|
|
|
|
commentAlert.empty().append("<p>Error: </p>");
|
|
|
|
commentAlert.append("<p>" + JSON.stringify(status) + "</p>");
|
|
|
|
commentAlert.append("<p>" + JSON.stringify(error) + "</p>");
|
|
|
|
commentAlert.append("<p>" + JSON.stringify(title) + "</p>");
|
|
|
|
});
|
|
|
|
posting.always(function () {
|
|
|
|
//alert("finished, be it successful or not");
|
|
|
|
});
|
2017-10-23 11:39:49 +00:00
|
|
|
});
|
2017-10-24 00:57:44 +00:00
|
|
|
|
2017-10-23 11:39:49 +00:00
|
|
|
// Attach a submit handler to the form
|
|
|
|
$(commentForm).on('submit', function (event) {
|
|
|
|
event.preventDefault();
|
2017-10-24 00:57:44 +00:00
|
|
|
// Get form data:
|
2017-10-23 11:39:49 +00:00
|
|
|
var data = $(this).serialize();
|
2017-10-24 00:57:44 +00:00
|
|
|
//console.log("Form Data (submit)", JSON.parse(JSON.stringify(data)));
|
|
|
|
var url = $(this).attr( "action" );
|
|
|
|
//var url = '/nested-comments';
|
2017-10-23 11:39:49 +00:00
|
|
|
var parentId = 0;
|
2017-10-24 00:57:44 +00:00
|
|
|
var ownLevel = 0;
|
2017-10-23 11:39:49 +00:00
|
|
|
if ($(this).parents('.comment').length > 0) {
|
2017-10-24 00:57:44 +00:00
|
|
|
parentId = $(this).closest('.comment').attr('data-id');
|
|
|
|
ownLevel = parseInt($(this).closest('.comment').attr('data-level'), 10) + 1;
|
2017-10-20 12:06:02 +00:00
|
|
|
}
|
2017-10-24 00:57:44 +00:00
|
|
|
|
2017-10-23 11:39:49 +00:00
|
|
|
// Send the data using post
|
|
|
|
//var posting = $.post(url, { parentId: parentId, data: data }, null, 'json');
|
|
|
|
var posting = $.post(url, data + '&parentID=' + parentId, null, 'json');
|
2017-10-24 00:57:44 +00:00
|
|
|
|
|
|
|
// Register events to ajax call
|
2017-10-23 11:39:49 +00:00
|
|
|
posting.done(function (response) {
|
2017-10-24 00:57:44 +00:00
|
|
|
//alert('success');
|
|
|
|
//console.log("Response Data (done)", JSON.parse(JSON.stringify(response)));
|
|
|
|
//response = JSON.parse(response); //not needed, post was done using json
|
2017-10-23 11:39:49 +00:00
|
|
|
commentForm.after(commentAlert);
|
|
|
|
if (!response.status) {
|
2017-10-24 00:57:44 +00:00
|
|
|
//should not trigger at all, if all bad requests return the right http status code
|
|
|
|
//i.e. <> 200 success => thus triggering posting.fail()
|
|
|
|
//leave this check just in case
|
|
|
|
commentAlert.stop().css('opacity', 1).text('Error: ' + response.message).fadeIn(30).fadeOut(5000);
|
|
|
|
return;
|
2017-10-23 11:39:49 +00:00
|
|
|
}
|
|
|
|
if (response.status) {
|
2017-10-24 00:57:44 +00:00
|
|
|
commentAlert.css('color', 'green').empty().append(document.createTextNode( response.message )).fadeIn(30);
|
|
|
|
var newMedia = "<div class='comment comment-level-{{comment.level|e}} comment-flag-new' data-level='{{comment.level}}' data-id='{{comment.id}}' >" +
|
|
|
|
"<div class='comment-left'>" +
|
2017-10-25 00:58:07 +00:00
|
|
|
"<img class='comment-object' src='https://www.gravatar.com/avatar/{{comment.email|trim|lower|md5}}?d=identicon' alt='user icon'>" +
|
2017-10-24 00:57:44 +00:00
|
|
|
"</div>" +
|
|
|
|
"<div class='comment-body'>" +
|
|
|
|
"<div class='comment-heading'>" +
|
|
|
|
"<div class='comment-title'><h4>{{comment.title}}</h4></div>" +
|
2017-10-25 00:58:07 +00:00
|
|
|
"<div class='comment-reply'><a class='comment-add-reply' href='#'><i class='fa fa-reply' title='{{'PLUGIN_COMMENTS.ADD_REPLY'|t}}'></i> {{'PLUGIN_COMMENTS.REPLY'|t}}</a></div>" +
|
2017-10-24 00:57:44 +00:00
|
|
|
"<div class='comment-meta'>{{'PLUGIN_COMMENTS.WRITTEN_ON'|t}} {{comment.date|e}} {{'PLUGIN_COMMENTS.BY'|t}} {{comment.author}}</div>" +
|
|
|
|
"</div>" +
|
|
|
|
"<div class='comment-text' >" +
|
|
|
|
"{{comment.text}}" +
|
|
|
|
"</div>" +
|
|
|
|
"{{nested}}" +
|
|
|
|
"</div>" +
|
|
|
|
"</div>";
|
|
|
|
newMedia = newMedia.replace(new RegExp(escapeRegExp("{{comment.id}}"), 'g'), response.data.id);
|
|
|
|
newMedia = newMedia.replace(new RegExp(escapeRegExp("{{comment.level|e}}"), 'g'), ownLevel);
|
|
|
|
newMedia = newMedia.replace(new RegExp(escapeRegExp("{{comment.level}}"), 'g'), ownLevel);
|
|
|
|
newMedia = newMedia.replace(new RegExp(escapeRegExp("{{comment.email|trim|lower|md5}}"), 'g'), response.data.hash);
|
|
|
|
newMedia = newMedia.replace(new RegExp(escapeRegExp("{{parent_id}}"), 'g'), response.data.parent_id);
|
|
|
|
newMedia = newMedia.replace(new RegExp(escapeRegExp("{{comment.title}}"), 'g'), response.data.title);
|
|
|
|
newMedia = newMedia.replace(new RegExp(escapeRegExp("{{comment.text}}"), 'g'), response.data.text);
|
|
|
|
newMedia = newMedia.replace(new RegExp(escapeRegExp("{{comment.author}}"), 'g'), response.data.name);
|
|
|
|
newMedia = newMedia.replace(new RegExp(escapeRegExp("{{comment.date|e}}"), 'g'), response.data.date);
|
|
|
|
newMedia = newMedia.replace(new RegExp(escapeRegExp("{{nested}}"), 'g'), '');
|
|
|
|
newMedia = newMedia.replace(new RegExp(escapeRegExp("{{'PLUGIN_COMMENTS.ADD_REPLY'|t}}"), 'g'), response.data.ADD_REPLY);
|
2017-10-25 00:58:07 +00:00
|
|
|
newMedia = newMedia.replace(new RegExp(escapeRegExp("{{'PLUGIN_COMMENTS.REPLY'|t}}"), 'g'), response.data.REPLY);
|
2017-10-24 00:57:44 +00:00
|
|
|
newMedia = newMedia.replace(new RegExp(escapeRegExp("{{'PLUGIN_COMMENTS.WRITTEN_ON'|t}}"), 'g'), response.data.WRITTEN_ON);
|
|
|
|
newMedia = newMedia.replace(new RegExp(escapeRegExp("{{'PLUGIN_COMMENTS.BY'|t}}"), 'g'), response.data.BY);
|
|
|
|
if ($( "div[data-id='" + response.data.parent_id + "']" ).length > 0) {
|
|
|
|
$( "div[data-id='" + response.data.parent_id + "']" ).first().after(newMedia);
|
|
|
|
} else {
|
2017-10-23 11:39:49 +00:00
|
|
|
$( "div.comments" ).last().prepend(newMedia);
|
2017-10-24 00:57:44 +00:00
|
|
|
}
|
2017-10-23 11:39:49 +00:00
|
|
|
}
|
|
|
|
setTimeout(function () {
|
2017-10-25 00:58:07 +00:00
|
|
|
commentForm.slideUp();
|
2017-10-24 00:57:44 +00:00
|
|
|
commentAlert.fadeOut(5000);
|
2017-10-23 11:39:49 +00:00
|
|
|
}, 5000);
|
|
|
|
});
|
|
|
|
posting.fail(function (status, error, title) {
|
2017-10-24 00:57:44 +00:00
|
|
|
//alert('error');
|
|
|
|
//console.log("Response Data (fail)", JSON.parse(JSON.stringify(status)));
|
2017-10-23 11:39:49 +00:00
|
|
|
commentForm.after(commentAlert);
|
2017-10-25 00:58:07 +00:00
|
|
|
commentAlert.show();
|
|
|
|
commentAlert.empty().append("<p>Error: </p>");
|
|
|
|
commentAlert.append("<p>" + JSON.stringify(status) + "</p>");
|
|
|
|
commentAlert.append("<p>" + JSON.stringify(error) + "</p>");
|
|
|
|
commentAlert.append("<p>" + JSON.stringify(title) + "</p>");
|
2017-10-23 11:39:49 +00:00
|
|
|
});
|
2017-10-24 00:57:44 +00:00
|
|
|
posting.always(function () {
|
2017-10-23 11:39:49 +00:00
|
|
|
//alert("finished, be it successful or not");
|
|
|
|
});
|
|
|
|
});
|
2017-10-20 12:06:02 +00:00
|
|
|
});
|