Ajaxify comment deletion and approval. thanks thomasdesvenain!

svn path=/plone.app.discussion/trunk/; revision=40984
This commit is contained in:
Timo Stollenwerk 2010-10-30 17:32:31 +00:00
parent 18d7aadd0e
commit cc96acb50a
2 changed files with 119 additions and 48 deletions

View File

@ -4,6 +4,9 @@ Changelog
1.0b11 (unreleased) 1.0b11 (unreleased)
------------------- -------------------
- Ajaxify comment deletion and approval.
[thomasdesvenain]
- New feature: As a logged-in user, I can enable/disable email notification of - New feature: As a logged-in user, I can enable/disable email notification of
additional comments on this content object. additional comments on this content object.
[timo] [timo]

View File

@ -1,75 +1,72 @@
/****************************************************************************** /******************************************************************************
* *
* jQuery functions for the plone.app.discussion comment viewlet and form. * jQuery functions for the plone.app.discussion comment viewlet and form.
* *
******************************************************************************/ ******************************************************************************/
(function ($) { (function ($) {
// This unnamed function allows us to use $ inside of a block of code // This unnamed function allows us to use $ inside of a block of code
// without permanently overwriting $. // without permanently overwriting $.
// http://docs.jquery.com/Using_jQuery_with_Other_Libraries // http://docs.jquery.com/Using_jQuery_with_Other_Libraries
/************************************************************************** /**************************************************************************
* Create a reply-to-comment form right beneath the form that is passed to * Create a reply-to-comment form right beneath the form that is passed to
* the function. We do this by copying the regular comment form and * the function. We do this by copying the regular comment form and
* adding a hidden in_reply_to field to the form. * adding a hidden in_reply_to field to the form.
**************************************************************************/ **************************************************************************/
$.createReplyForm = function (comment_div) { $.createReplyForm = function (comment_div) {
var comment_id = comment_div.attr("id"); var comment_id = comment_div.attr("id");
var reply_button = comment_div.find(".reply-to-comment-button"); var reply_button = comment_div.find(".reply-to-comment-button");
/* Clone the reply div at the end of the page template that contains /* Clone the reply div at the end of the page template that contains
* the regular comment form. * the regular comment form.
*/ */
var reply_div = $("#commenting").clone(true); var reply_div = $("#commenting").clone(true);
/* Remove the ReCaptcha JS code before appending the form. If not /* Remove the ReCaptcha JS code before appending the form. If not
* removed, this causes problems * removed, this causes problems
*/ */
reply_div.find("#formfield-form-widgets-captcha") reply_div.find("#formfield-form-widgets-captcha")
.find("script") .find("script")
.remove(); .remove();
/* Insert the cloned comment form right after the reply button of the /* Insert the cloned comment form right after the reply button of the
* current comment. * current comment.
*/ */
reply_div.appendTo(comment_div).css("display", "none"); reply_div.appendTo(comment_div).css("display", "none");
/* Remove id="reply" attribute, since we use it to uniquely /* Remove id="reply" attribute, since we use it to uniquely
the main reply form. */ the main reply form. */
reply_div.removeAttr("id"); reply_div.removeAttr("id");
/* Hide the reply button (only hide, because we may want to show it /* Hide the reply button (only hide, because we may want to show it
* again if the user hits the cancel button). * again if the user hits the cancel button).
*/ */
$(reply_button).css("display", "none"); $(reply_button).css("display", "none");
/* Fetch the reply form inside the reply div */ /* Fetch the reply form inside the reply div */
var reply_form = reply_div.find("form"); var reply_form = reply_div.find("form");
/* Populate the hidden 'in_reply_to' field with the correct comment /* Populate the hidden 'in_reply_to' field with the correct comment
id */ id */
reply_form.find("input[name='form.widgets.in_reply_to']") reply_form.find("input[name='form.widgets.in_reply_to']")
.val(comment_id); .val(comment_id);
/* Add a remove-reply-to-comment Javascript function to remove the /* Add a remove-reply-to-comment Javascript function to remove the
form */ form */
var cancel_reply_button = reply_div.find(".cancelreplytocomment"); var cancel_reply_button = reply_div.find(".cancelreplytocomment");
cancel_reply_button.attr("id", comment_id); cancel_reply_button.attr("id", comment_id);
/* Show the cancel buttons. */ /* Show the cancel buttons. */
reply_form.find("input[name='form.buttons.cancel']") reply_form.find("input[name='form.buttons.cancel']")
.css("display", "inline"); .css("display", "inline");
/* Show the reply layer with a slide down effect */ /* Show the reply layer with a slide down effect */
reply_div.slideDown("slow"); reply_div.slideDown("slow");
/* Show the cancel button in the reply-to-comment form */ /* Show the cancel button in the reply-to-comment form */
cancel_reply_button.css("display", "inline"); cancel_reply_button.css("display", "inline");
} };
/************************************************************************** /**************************************************************************
@ -82,23 +79,23 @@
form_div.find("input[type='text']").attr("value", ""); form_div.find("input[type='text']").attr("value", "");
form_div.find("textarea").attr("value", ""); form_div.find("textarea").attr("value", "");
/* XXX: Clean all additional form extender fields. */ /* XXX: Clean all additional form extender fields. */
} };
//#JSCOVERAGE_IF 0 //#JSCOVERAGE_IF 0
/************************************************************************** /**************************************************************************
* Window Load Function: Executes when complete page is fully loaded, * Window Load Function: Executes when complete page is fully loaded,
* including all frames, * including all frames,
**************************************************************************/ **************************************************************************/
$(window).load(function () { $(window).load(function () {
/********************************************************************** /**********************************************************************
* If the user has hit the reply button of a reply-to-comment form * If the user has hit the reply button of a reply-to-comment form
* (form was submitted with a value for the "in_reply_to" field in the * (form was submitted with a value for the "in_reply_to" field in the
* request), create a reply-to-comment form right under this comment. * request), create a reply-to-comment form right under this comment.
**********************************************************************/ **********************************************************************/
var post_comment_div = $("#commenting"); var post_comment_div = $("#commenting");
var in_reply_to_field = var in_reply_to_field =
post_comment_div.find("input[name='form.widgets.in_reply_to']"); post_comment_div.find("input[name='form.widgets.in_reply_to']");
if (in_reply_to_field.val() !== "") { if (in_reply_to_field.val() !== "") {
var current_reply_id = "#" + in_reply_to_field.val(); var current_reply_id = "#" + in_reply_to_field.val();
@ -106,10 +103,10 @@
$.createReplyForm(current_reply_to_div); $.createReplyForm(current_reply_to_div);
$.clearForm(post_comment_div); $.clearForm(post_comment_div);
} }
/********************************************************************** /**********************************************************************
* If the user hits the "reply" button of an existing comment, create a * If the user hits the "reply" button of an existing comment, create a
* reply form right beneath this comment. * reply form right beneath this comment.
**********************************************************************/ **********************************************************************/
$(".reply-to-comment-button").bind("click", function (e) { $(".reply-to-comment-button").bind("click", function (e) {
@ -117,8 +114,8 @@
$.createReplyForm(comment_div); $.createReplyForm(comment_div);
$.clearForm(comment_div); $.clearForm(comment_div);
}); });
/********************************************************************** /**********************************************************************
* If the user hits the "clear" button of an open reply-to-comment form, * If the user hits the "clear" button of an open reply-to-comment form,
* remove the form and show the "reply" button again. * remove the form and show the "reply" button again.
@ -129,19 +126,90 @@
parents(). parents().
filter(".comment"). filter(".comment").
find(".reply-to-comment-button"); find(".reply-to-comment-button");
/* Find the reply-to-comment form and hide and remove it again. */ /* Find the reply-to-comment form and hide and remove it again. */
reply_to_comment_form = $(this).parents().filter(".reply"); $.reply_to_comment_form = $(this).parents().filter(".reply");
reply_to_comment_form.slideUp("slow", function () { $.reply_to_comment_form.slideUp("slow", function () {
$(this).remove(); $(this).remove();
}); });
/* Show the reply-to-comment button again. */ /* Show the reply-to-comment button again. */
reply_to_comment_button.css("display", "inline"); reply_to_comment_button.css("display", "inline");
});
});
/**********************************************************************
* Publish a single comment.
**********************************************************************/
$("input[name='form.button.PublishComment']").live('click', function () {
var trigger = this;
var form = $(this).parents("form");
var data = $(form).serialize();
var form_url = $(form).attr("action");
$.ajax({
type: "GET",
url: form_url,
data: "workflow_action=publish",
context: trigger,
success: function (msg) {
// remove button (trigger object can't be directly removed)
form.find("input[name='form.button.PublishComment']").remove();
form.parents(".state-pending").toggleClass('state-pending').toggleClass('state-published');
},
error: function (msg) {
return true;
}
});
return false;
});
/**********************************************************************
* Delete a comment and its answers.
**********************************************************************/
$("input[name='form.button.DeleteComment']").live('click', function () {
var trigger = this;
var form = $(this).parents("form");
var data = $(form).serialize();
var form_url = $(form).attr("action");
$.ajax({
type: 'POST',
url: form_url,
context: $(trigger).parents(".comment"),
success: function (data) {
if ($(".discussion .comment .replyTreeLevel0").length === 1) {
$(".discussion").fadeOut('fast', function () {
$(".discussion").remove();
});
}
else {
var comment = $(this);
var clss = comment.attr('class');
// remove replies
var treelevel = parseInt(clss[clss.indexOf('replyTreeLevel') + 'replyTreeLevel'.length]);
// selector for all the following elements of lower level
var selector = ".replyTreeLevel" + treelevel;
for (var i = 0; i < treelevel; i++) {
selector += ", .replyTreeLevel" + i;
}
comment.nextUntil(selector).each(function () {
$(this).fadeOut('fast', function () {
$(this).remove();
});
});
// remove comment
$(this).fadeOut('fast', function () {
$(this).remove();
});
}
},
error: function (req, error) {
return true;
}
});
return false;
});
/********************************************************************** /**********************************************************************
* By default, hide the reply and the cancel button for the regular add * By default, hide the reply and the cancel button for the regular add
@ -151,18 +219,18 @@
.css("display", "none"); .css("display", "none");
$(".reply").find("input[name='form.buttons.cancel']") $(".reply").find("input[name='form.buttons.cancel']")
.css("display", "none"); .css("display", "none");
/********************************************************************** /**********************************************************************
* By default, show the reply button only when Javascript is enabled. * By default, show the reply button only when Javascript is enabled.
* Otherwise hide it, since the reply functions only work with JS * Otherwise hide it, since the reply functions only work with JS
* enabled. * enabled.
**********************************************************************/ **********************************************************************/
$(".reply-to-comment-button").css("display" , "inline"); $(".reply-to-comment-button").css("display" , "inline");
}); });
//#JSCOVERAGE_ENDIF //#JSCOVERAGE_ENDIF
}(jQuery)); }(jQuery));