1 /******************************************************************************
3 * jQuery functions for the plone.app.discussion comment viewlet and form.
5 ******************************************************************************/
7 // This unnamed function allows us to use $ inside of a block of code
8 // without permanently overwriting $.
9 // http://docs.jquery.com/Using_jQuery_with_Other_Libraries
10 /**************************************************************************
11 * Create a reply-to-comment form right beneath the form that is passed to
12 * the function. We do this by copying the regular comment form and
13 * adding a hidden in_reply_to field to the form.
14 **************************************************************************/
15 $.createReplyForm = function (comment_div
) {
17 var comment_id
= comment_div
.attr("id");
19 var reply_button
= comment_div
.find(".reply-to-comment-button");
21 /* Clone the reply div at the end of the page template that contains
22 * the regular comment form.
24 var reply_div
= $("#commenting").clone(true);
26 /* Remove the ReCaptcha JS code before appending the form. If not
27 * removed, this causes problems
29 reply_div
.find("#formfield-form-widgets-captcha")
33 /* Insert the cloned comment form right after the reply button of the
36 reply_div
.appendTo(comment_div
).css("display", "none");
38 /* Remove id="reply" attribute, since we use it to uniquely
39 the main reply form. */
40 reply_div
.removeAttr("id");
42 /* Hide the reply button (only hide, because we may want to show it
43 * again if the user hits the cancel button).
45 $(reply_button
).css("display", "none");
47 /* Fetch the reply form inside the reply div */
48 var reply_form
= reply_div
.find("form");
50 /* Populate the hidden 'in_reply_to' field with the correct comment
52 reply_form
.find("input[name='form.widgets.in_reply_to']")
55 /* Add a remove-reply-to-comment Javascript function to remove the
57 var cancel_reply_button
= reply_div
.find(".cancelreplytocomment");
58 cancel_reply_button
.attr("id", comment_id
);
60 /* Show the cancel buttons. */
61 reply_form
.find("input[name='form.buttons.cancel']")
62 .css("display", "inline");
64 /* Show the reply layer with a slide down effect */
65 reply_div
.slideDown("slow");
67 /* Show the cancel button in the reply-to-comment form */
68 cancel_reply_button
.css("display", "inline");
72 /**************************************************************************
73 * Remove all error messages and field values from the form that is passed
75 **************************************************************************/
76 $.clearForm = function (form_div
) {
77 form_div
.find(".error").removeClass("error");
78 form_div
.find(".fieldErrorBox").remove();
79 form_div
.find("input[type='text']").attr("value", "");
80 form_div
.find("textarea").attr("value", "");
81 /* XXX: Clean all additional form extender fields. */
86 /**************************************************************************
87 * Window Load Function: Executes when complete page is fully loaded,
88 * including all frames,
89 **************************************************************************/
90 $(window
).load(function () {
92 /**********************************************************************
93 * If the user has hit the reply button of a reply-to-comment form
94 * (form was submitted with a value for the "in_reply_to" field in the
95 * request), create a reply-to-comment form right under this comment.
96 **********************************************************************/
97 var post_comment_div
= $("#commenting");
98 var in_reply_to_field
=
99 post_comment_div
.find("input[name='form.widgets.in_reply_to']");
100 if (in_reply_to_field
.val() !== "") {
101 var current_reply_id
= "#" + in_reply_to_field
.val();
102 var current_reply_to_div
= $(".discussion").find(current_reply_id
);
103 $.createReplyForm(current_reply_to_div
);
104 $.clearForm(post_comment_div
);
108 /**********************************************************************
109 * If the user hits the "reply" button of an existing comment, create a
110 * reply form right beneath this comment.
111 **********************************************************************/
112 $(".reply-to-comment-button").bind("click", function (e
) {
113 var comment_div
= $(this).parents().filter(".comment");
114 $.createReplyForm(comment_div
);
115 $.clearForm(comment_div
);
119 /**********************************************************************
120 * If the user hits the "clear" button of an open reply-to-comment form,
121 * remove the form and show the "reply" button again.
122 **********************************************************************/
123 $("#commenting #form-buttons-cancel").bind("click", function (e
) {
125 var reply_to_comment_button
= $(this).
128 find(".reply-to-comment-button");
130 /* Find the reply-to-comment form and hide and remove it again. */
131 $.reply_to_comment_form
= $(this).parents().filter(".reply");
132 $.reply_to_comment_form
.slideUp("slow", function () {
136 /* Show the reply-to-comment button again. */
137 reply_to_comment_button
.css("display", "inline");
142 /**********************************************************************
143 * Publish a single comment.
144 **********************************************************************/
145 $("input[name='form.button.PublishComment']").live('click', function () {
147 var form
= $(this).parents("form");
148 var data
= $(form
).serialize();
149 var form_url
= $(form
).attr("action");
153 data
: "workflow_action=publish",
155 success: function (msg
) {
156 // remove button (trigger object can't be directly removed)
157 form
.find("input[name='form.button.PublishComment']").remove();
158 form
.parents(".state-pending").toggleClass('state-pending').toggleClass('state-published');
160 error: function (msg
) {
168 /**********************************************************************
169 * Delete a comment and its answers.
170 **********************************************************************/
172 $("input[name='form.button.DeleteComment']").live('click', function () {
174 var form = $(this).parents("form");
175 var data = $(form).serialize();
176 var form_url = $(form).attr("action");
180 context: $(trigger).parents(".comment"),
181 success: function (data) {
182 if ($(".discussion .replyTreeLevel0").length === 1) {
183 $(".discussion").fadeOut('fast', function () {
184 $(".discussion").remove();
188 var comment = $(this);
189 var clss = comment.attr('class');
191 var treelevel = parseInt(clss[clss.indexOf('replyTreeLevel') + 'replyTreeLevel'.length], 10);
192 // selector for all the following elements of lower level
193 var selector = ".replyTreeLevel" + treelevel;
194 for (var i = 0; i < treelevel; i++) {
195 selector += ", .replyTreeLevel" + i;
197 comment.nextUntil(selector).each(function () {
198 $(this).fadeOut('fast', function () {
203 $(this).fadeOut('fast', function () {
208 error: function (req, error) {
215 /**********************************************************************
216 * By default, hide the reply and the cancel button for the regular add
218 **********************************************************************/
219 $(".reply").find("input[name='form.buttons.reply']")
220 .css("display", "none");
221 $(".reply").find("input[name='form.buttons.cancel']")
222 .css("display", "none");
225 /**********************************************************************
226 * By default, show the reply button only when Javascript is enabled.
227 * Otherwise hide it, since the reply functions only work with JS
229 **********************************************************************/
230 $(".reply-to-comment-button").css("display" , "inline");