ENHANCEMENT: Addition of the ability to handle threaded comments. Events now thrown in order for other JavaScript files to bind to those events and manipulate the HTML content

This commit is contained in:
Gordon Anderson 2015-02-07 12:18:19 +07:00
parent fef43c56d9
commit e7c3fbfe9f

View File

@ -12,6 +12,7 @@
form = $('form', container),
previewEl = form.find('#PreviewComment');
if (container.length > 0) {
/**
* Init
*/
@ -68,9 +69,103 @@
}
});
function applyAjaxButtonClick() {
form = $('form', container);
form.submit( function (e) {
var formbutton = $('#Form_CommentsForm_action_doPostComment');
formbutton.prop('disabled', true);
$.event.trigger({
type: "onBeforeValidateNewCommentForm",
});
// trigger validation
if(!form.validate().valid()) return false;
if(!form.validate().valid()) {
formbutton.prop('disabled', false);
return false;
}
formbutton.html("Posting...");
$.event.trigger({
type: "onBeforeSubmitNewCommentForm",
});
$.ajax({
type: form.attr('method'),
url: form.attr('action'),
data: form.serialize(),
// accepts:"application/json",
dataType: 'json',
success: function (dataj) {
if (typeof(dataj.success) === 'undefined') {
$.event.trigger({
type: "onAfterServerValidationFailedNewCommentForm",
commentid: dataj.commentid,
parentcommentid: dataj.parentcommentid,
success: dataj.success,
message: dataj.message,
formhtml: dataj.html,
requiresmoderation: dataj.requiresmoderation,
depth: dataj.depth
});
var message = dataj[0].message;
var fieldName = dataj[0].fieldName;
var errormsg='<span for="'+fieldName+'" class="error message">'+message+'</span>';
var field=$('#'+fieldName);
$(errormsg).insertAfter(field);
var formbutton = $('#Form_CommentsForm_action_doPostComment');
formbutton.html("Post");
formbutton.prop('disabled', false);
} else {
var data = dataj;
var wrapper = $('#commentFormInnerWrapper');
wrapper.html(data.html);
var msg = $('#commentFormWrapper').find('p.message').first();
var commentbox = $('#comment-'+data.parentcommentid);
msg.insertAfter(commentbox);//.parent().parent());
var formbutton = $('#Form_CommentsForm_action_doPostComment');
formbutton.html("Post");
formbutton.removeAttr('disabled');
// click the canel button to reset the comment form
commentbox.find('li.cancelReplyButton').find('button').click();
// remove submitted comment
$('#Form_CommentsForm_Comment').val('');
applyAjaxButtonClick();
$.event.trigger({
type: "onAfterSuccessfulNewComment",
commentid: data.commentid,
parentcommentid: data.parentcommentid,
success: data.success,
message: data.message,
formhtml: data.html,
requiresmoderation: data.requiresmoderation,
depth: data.depth
});
}
}
});
e.preventDefault();
});
}
applyAjaxButtonClick();
$.event.trigger({
type: "commentFormInitliased",
});
/**
@ -115,6 +210,10 @@
var link = $(this);
var comment = link.parents('.comment:first');
$.event.trigger({
type: "onBeforeModerationClick",
});
$.ajax({
url: $(this).attr('href'),
cache: false,
@ -141,6 +240,11 @@
else if(link.hasClass('spam')) {
comment.html(html).addClass('spam').hide().fadeIn();
}
$.event.trigger({
type: "onAfterModerationClick",
commentNode: comment
});
},
failure: function(html) {
alert(html)
@ -150,12 +254,56 @@
e.preventDefault();
});
/*
Deal with reply to buttons
*/
$(document).on("click",".replyButton button",function(e){
// get the root node of the comment
var commentbox = $(this).closest('.comment');
commentbox.append($('#CommentsFormContainer'));//.parent().parent());
$(this).parent().addClass('hidden'); // hide reply button
$(this).parent().parent().find('.cancelReplyButton').removeClass('hidden'); // show cancel reply button
$('.whenReplying').removeClass('hidden');
$('.newComment').addClass('hidden');
var parentCommentID = $(this).attr('data-comment-to-reply-to-id');
$('#Form_CommentsForm_ParentCommentID').val(parentCommentID);
$.event.trigger({
type: "replyToCommentButtonClicked",
commentid: commentbox.attr('data-comment-id')
});
});
$(document).on("click",".cancelReplyButton button",function(e){
$($('#CommentsFormContainer')).insertAfter($('#postYourCommentHeader'));
$(this).parent().parent().find('.replyButton').removeClass('hidden');
$(this).parent().addClass('hidden');
$('.newComment').removeClass('hidden');
$('.whenReplying').addClass('hidden');
var commentid = $('#Form_CommentsForm_ParentCommentID').val();
$('#Form_CommentsForm_ParentCommentID').val(0);
e.preventDefault();
$.event.trigger({
type: "cancelReplyToCommentButtonClicked",
commentid: commentid
});
return false;
});
/**
* Ajax pagination
*/
pagination.find('a').on('click', function(){
pagination.find('a').on('click', function(e){
commentsList.addClass('loading');
$.event.trigger({
type: "onBeforeLoadNewPageOfComments",
page: $(this).html()
});
$.ajax({
url: $(this).attr('href'),
cache: false,
@ -167,6 +315,10 @@
$('html, body').animate({
scrollTop: commentsList.offset().top - 30
}, 200);
$.event.trigger({
type: "onAfterLoadNewPageOfComments",
page: $(e.target).html()
});
},
failure: function(html) {
alert('Error loading comments');
@ -174,5 +326,8 @@
});
return false;
});
}
});
})(jQuery);