2010-09-24 16:21:26 +02:00
|
|
|
/******************************************************************************
|
2010-10-30 19:32:31 +02:00
|
|
|
*
|
2010-09-24 16:21:26 +02:00
|
|
|
* jQuery functions for the plone.app.discussion comment viewlet and form.
|
2010-10-30 19:32:31 +02:00
|
|
|
*
|
2010-09-24 16:21:26 +02:00
|
|
|
******************************************************************************/
|
|
|
|
(function ($) {
|
2011-01-20 21:04:38 +01:00
|
|
|
// This unnamed function allows us to use $ inside of a block of code
|
|
|
|
// without permanently overwriting $.
|
|
|
|
// http://docs.jquery.com/Using_jQuery_with_Other_Libraries
|
2010-08-27 23:26:37 +02:00
|
|
|
/**************************************************************************
|
2010-09-24 16:21:26 +02:00
|
|
|
* Create a reply-to-comment form right beneath the form that is passed to
|
2010-10-30 19:32:31 +02:00
|
|
|
* the function. We do this by copying the regular comment form and
|
2010-09-24 16:21:26 +02:00
|
|
|
* adding a hidden in_reply_to field to the form.
|
2010-08-27 23:26:37 +02:00
|
|
|
**************************************************************************/
|
2010-09-24 16:21:26 +02:00
|
|
|
$.createReplyForm = function (comment_div) {
|
2010-10-30 19:32:31 +02:00
|
|
|
|
2010-09-24 16:21:26 +02:00
|
|
|
var comment_id = comment_div.attr("id");
|
2010-10-30 19:32:31 +02:00
|
|
|
|
2010-09-24 16:21:26 +02:00
|
|
|
var reply_button = comment_div.find(".reply-to-comment-button");
|
2010-10-30 19:32:31 +02:00
|
|
|
|
2010-09-24 16:21:26 +02:00
|
|
|
/* Clone the reply div at the end of the page template that contains
|
|
|
|
* the regular comment form.
|
|
|
|
*/
|
|
|
|
var reply_div = $("#commenting").clone(true);
|
2010-10-30 19:32:31 +02:00
|
|
|
|
2010-09-24 16:21:26 +02:00
|
|
|
/* Remove the ReCaptcha JS code before appending the form. If not
|
|
|
|
* removed, this causes problems
|
|
|
|
*/
|
|
|
|
reply_div.find("#formfield-form-widgets-captcha")
|
|
|
|
.find("script")
|
|
|
|
.remove();
|
2010-10-30 19:32:31 +02:00
|
|
|
|
2010-09-24 16:21:26 +02:00
|
|
|
/* Insert the cloned comment form right after the reply button of the
|
|
|
|
* current comment.
|
|
|
|
*/
|
|
|
|
reply_div.appendTo(comment_div).css("display", "none");
|
2010-10-30 19:32:31 +02:00
|
|
|
|
2010-09-24 16:21:26 +02:00
|
|
|
/* Remove id="reply" attribute, since we use it to uniquely
|
|
|
|
the main reply form. */
|
|
|
|
reply_div.removeAttr("id");
|
2010-10-30 19:32:31 +02:00
|
|
|
|
2010-09-24 16:21:26 +02:00
|
|
|
/* Hide the reply button (only hide, because we may want to show it
|
|
|
|
* again if the user hits the cancel button).
|
|
|
|
*/
|
|
|
|
$(reply_button).css("display", "none");
|
2010-10-30 19:32:31 +02:00
|
|
|
|
2010-09-24 16:21:26 +02:00
|
|
|
/* Fetch the reply form inside the reply div */
|
|
|
|
var reply_form = reply_div.find("form");
|
2010-10-30 19:32:31 +02:00
|
|
|
|
|
|
|
/* Populate the hidden 'in_reply_to' field with the correct comment
|
2010-09-24 16:21:26 +02:00
|
|
|
id */
|
|
|
|
reply_form.find("input[name='form.widgets.in_reply_to']")
|
|
|
|
.val(comment_id);
|
2010-10-30 19:32:31 +02:00
|
|
|
|
|
|
|
/* Add a remove-reply-to-comment Javascript function to remove the
|
2010-09-24 16:21:26 +02:00
|
|
|
form */
|
|
|
|
var cancel_reply_button = reply_div.find(".cancelreplytocomment");
|
|
|
|
cancel_reply_button.attr("id", comment_id);
|
2010-10-30 19:32:31 +02:00
|
|
|
|
2010-09-24 16:21:26 +02:00
|
|
|
/* Show the cancel buttons. */
|
|
|
|
reply_form.find("input[name='form.buttons.cancel']")
|
|
|
|
.css("display", "inline");
|
2010-10-30 19:32:31 +02:00
|
|
|
|
2010-09-24 16:21:26 +02:00
|
|
|
/* Show the reply layer with a slide down effect */
|
|
|
|
reply_div.slideDown("slow");
|
2010-10-30 19:32:31 +02:00
|
|
|
|
2010-09-24 16:21:26 +02:00
|
|
|
/* Show the cancel button in the reply-to-comment form */
|
|
|
|
cancel_reply_button.css("display", "inline");
|
2010-10-30 19:32:31 +02:00
|
|
|
};
|
2009-08-15 18:36:49 +02:00
|
|
|
|
2010-06-04 11:25:47 +02:00
|
|
|
|
|
|
|
/**************************************************************************
|
2010-09-24 16:21:26 +02:00
|
|
|
* Remove all error messages and field values from the form that is passed
|
|
|
|
* to the function.
|
2010-06-04 11:25:47 +02:00
|
|
|
**************************************************************************/
|
2010-09-24 16:21:26 +02:00
|
|
|
$.clearForm = function (form_div) {
|
|
|
|
form_div.find(".error").removeClass("error");
|
|
|
|
form_div.find(".fieldErrorBox").remove();
|
|
|
|
form_div.find("input[type='text']").attr("value", "");
|
|
|
|
form_div.find("textarea").attr("value", "");
|
|
|
|
/* XXX: Clean all additional form extender fields. */
|
2010-10-30 19:32:31 +02:00
|
|
|
};
|
2009-07-06 19:38:18 +02:00
|
|
|
|
2010-09-24 16:21:26 +02:00
|
|
|
//#JSCOVERAGE_IF 0
|
2009-07-06 19:38:18 +02:00
|
|
|
|
2010-09-24 16:21:26 +02:00
|
|
|
/**************************************************************************
|
2010-10-30 19:32:31 +02:00
|
|
|
* Window Load Function: Executes when complete page is fully loaded,
|
2010-09-24 16:21:26 +02:00
|
|
|
* including all frames,
|
2010-10-30 19:32:31 +02:00
|
|
|
**************************************************************************/
|
2010-09-24 16:21:26 +02:00
|
|
|
$(window).load(function () {
|
2010-10-30 19:32:31 +02:00
|
|
|
|
2011-08-26 13:13:19 +02:00
|
|
|
|
2011-01-20 21:04:38 +01:00
|
|
|
/**********************************************************************
|
|
|
|
* 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
|
|
|
|
* request), create a reply-to-comment form right under this comment.
|
|
|
|
**********************************************************************/
|
|
|
|
var post_comment_div = $("#commenting");
|
|
|
|
var in_reply_to_field =
|
|
|
|
post_comment_div.find("input[name='form.widgets.in_reply_to']");
|
|
|
|
if (in_reply_to_field.val() !== "") {
|
|
|
|
var current_reply_id = "#" + in_reply_to_field.val();
|
|
|
|
var current_reply_to_div = $(".discussion").find(current_reply_id);
|
|
|
|
$.createReplyForm(current_reply_to_div);
|
|
|
|
$.clearForm(post_comment_div);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-26 13:13:19 +02:00
|
|
|
/**********************************************************************
|
2011-01-20 21:04:38 +01:00
|
|
|
* If the user hits the "reply" button of an existing comment, create a
|
|
|
|
* reply form right beneath this comment.
|
|
|
|
**********************************************************************/
|
|
|
|
$(".reply-to-comment-button").bind("click", function (e) {
|
|
|
|
var comment_div = $(this).parents().filter(".comment");
|
|
|
|
$.createReplyForm(comment_div);
|
|
|
|
$.clearForm(comment_div);
|
|
|
|
});
|
2010-10-30 19:32:31 +02:00
|
|
|
|
|
|
|
|
2011-01-20 21:04:38 +01:00
|
|
|
/**********************************************************************
|
|
|
|
* If the user hits the "clear" button of an open reply-to-comment form,
|
|
|
|
* remove the form and show the "reply" button again.
|
|
|
|
**********************************************************************/
|
|
|
|
$("#commenting #form-buttons-cancel").bind("click", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var reply_to_comment_button = $(this).
|
|
|
|
parents().
|
|
|
|
filter(".comment").
|
|
|
|
find(".reply-to-comment-button");
|
2012-11-04 17:25:25 +01:00
|
|
|
|
2011-01-20 21:04:38 +01:00
|
|
|
/* Find the reply-to-comment form and hide and remove it again. */
|
|
|
|
$.reply_to_comment_form = $(this).parents().filter(".reply");
|
|
|
|
$.reply_to_comment_form.slideUp("slow", function () {
|
|
|
|
$(this).remove();
|
|
|
|
});
|
2012-11-04 17:25:25 +01:00
|
|
|
|
2011-01-20 21:04:38 +01:00
|
|
|
/* Show the reply-to-comment button again. */
|
|
|
|
reply_to_comment_button.css("display", "inline");
|
2012-11-04 17:25:25 +01:00
|
|
|
|
2011-01-20 21:04:38 +01:00
|
|
|
});
|
2010-10-28 11:39:00 +02:00
|
|
|
|
|
|
|
|
2010-10-30 19:32:31 +02:00
|
|
|
/**********************************************************************
|
|
|
|
* 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,
|
2014-02-04 10:00:06 +01:00
|
|
|
data: data,
|
2010-10-30 19:32:31 +02:00
|
|
|
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;
|
|
|
|
});
|
2011-08-26 13:13:19 +02:00
|
|
|
|
|
|
|
|
2010-10-30 19:32:31 +02:00
|
|
|
/**********************************************************************
|
|
|
|
* Delete a comment and its answers.
|
|
|
|
**********************************************************************/
|
2011-01-20 21:04:38 +01:00
|
|
|
$("input[name='form.button.DeleteComment']").live('click', function () {
|
2010-10-30 19:32:31 +02:00
|
|
|
var trigger = this;
|
|
|
|
var form = $(this).parents("form");
|
|
|
|
var data = $(form).serialize();
|
|
|
|
var form_url = $(form).attr("action");
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: form_url,
|
2013-08-21 11:07:48 +02:00
|
|
|
data: data,
|
2010-10-30 19:32:31 +02:00
|
|
|
context: $(trigger).parents(".comment"),
|
|
|
|
success: function (data) {
|
2012-05-07 11:11:19 +02:00
|
|
|
var comment = $(this);
|
|
|
|
var clss = comment.attr('class');
|
|
|
|
// remove replies
|
|
|
|
var treelevel = parseInt(clss[clss.indexOf('replyTreeLevel') + 'replyTreeLevel'.length], 10);
|
|
|
|
// selector for all the following elements of lower level
|
|
|
|
var selector = ".replyTreeLevel" + treelevel;
|
|
|
|
for (var i = 0; i < treelevel; i++) {
|
|
|
|
selector += ", .replyTreeLevel" + i;
|
2010-10-30 19:32:31 +02:00
|
|
|
}
|
2012-05-07 11:11:19 +02:00
|
|
|
comment.nextUntil(selector).each(function () {
|
2010-10-30 19:32:31 +02:00
|
|
|
$(this).fadeOut('fast', function () {
|
|
|
|
$(this).remove();
|
|
|
|
});
|
2012-05-07 11:11:19 +02:00
|
|
|
});
|
|
|
|
// remove comment
|
|
|
|
$(this).fadeOut('fast', function () {
|
|
|
|
$(this).remove();
|
|
|
|
});
|
2010-10-30 19:32:31 +02:00
|
|
|
},
|
|
|
|
error: function (req, error) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return false;
|
2011-01-20 20:38:26 +01:00
|
|
|
});
|
2010-10-30 19:32:31 +02:00
|
|
|
|
2011-08-26 13:13:19 +02:00
|
|
|
|
2011-01-20 21:04:38 +01:00
|
|
|
/**********************************************************************
|
|
|
|
* By default, hide the reply and the cancel button for the regular add
|
|
|
|
* comment form.
|
|
|
|
**********************************************************************/
|
|
|
|
$(".reply").find("input[name='form.buttons.reply']")
|
|
|
|
.css("display", "none");
|
|
|
|
$(".reply").find("input[name='form.buttons.cancel']")
|
|
|
|
.css("display", "none");
|
2010-10-30 19:32:31 +02:00
|
|
|
|
|
|
|
|
2011-01-20 21:04:38 +01:00
|
|
|
/**********************************************************************
|
|
|
|
* By default, show the reply button only when Javascript is enabled.
|
|
|
|
* Otherwise hide it, since the reply functions only work with JS
|
|
|
|
* enabled.
|
|
|
|
**********************************************************************/
|
|
|
|
$(".reply-to-comment-button").css("display" , "inline");
|
2010-10-30 19:32:31 +02:00
|
|
|
|
2011-01-20 21:04:38 +01:00
|
|
|
});
|
2010-09-24 16:21:26 +02:00
|
|
|
|
2010-10-30 19:32:31 +02:00
|
|
|
|
2010-09-24 16:21:26 +02:00
|
|
|
//#JSCOVERAGE_ENDIF
|
|
|
|
|
|
|
|
}(jQuery));
|