adding all weblabels from weblabels.fsf.org
[weblabels.fsf.org.git] / www.fsf.org / 20131028 / files / static.fsf.org / plone2012 / comments2.js
CommitLineData
5a920362 1/******************************************************************************
2 *
3 * jQuery functions for the plone.app.discussion comment viewlet and form.
4 *
5 ******************************************************************************/
6(function ($) {
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) {
16
17 var comment_id = comment_div.attr("id");
18
19 var reply_button = comment_div.find(".reply-to-comment-button");
20
21 /* Clone the reply div at the end of the page template that contains
22 * the regular comment form.
23 */
24 var reply_div = $("#commenting").clone(true);
25
26 /* Remove the ReCaptcha JS code before appending the form. If not
27 * removed, this causes problems
28 */
29 reply_div.find("#formfield-form-widgets-captcha")
30 .find("script")
31 .remove();
32
33 /* Insert the cloned comment form right after the reply button of the
34 * current comment.
35 */
36 reply_div.appendTo(comment_div).css("display", "none");
37
38 /* Remove id="reply" attribute, since we use it to uniquely
39 the main reply form. */
40 reply_div.removeAttr("id");
41
42 /* Hide the reply button (only hide, because we may want to show it
43 * again if the user hits the cancel button).
44 */
45 $(reply_button).css("display", "none");
46
47 /* Fetch the reply form inside the reply div */
48 var reply_form = reply_div.find("form");
49
50 /* Populate the hidden 'in_reply_to' field with the correct comment
51 id */
52 reply_form.find("input[name='form.widgets.in_reply_to']")
53 .val(comment_id);
54
55 /* Add a remove-reply-to-comment Javascript function to remove the
56 form */
57 var cancel_reply_button = reply_div.find(".cancelreplytocomment");
58 cancel_reply_button.attr("id", comment_id);
59
60 /* Show the cancel buttons. */
61 reply_form.find("input[name='form.buttons.cancel']")
62 .css("display", "inline");
63
64 /* Show the reply layer with a slide down effect */
65 reply_div.slideDown("slow");
66
67 /* Show the cancel button in the reply-to-comment form */
68 cancel_reply_button.css("display", "inline");
69 };
70
71
72 /**************************************************************************
73 * Remove all error messages and field values from the form that is passed
74 * to the function.
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. */
82 };
83
84 //#JSCOVERAGE_IF 0
85
86 /**************************************************************************
87 * Window Load Function: Executes when complete page is fully loaded,
88 * including all frames,
89 **************************************************************************/
90 $(window).load(function () {
91
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);
105 }
106
107
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);
116 });
117
118
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) {
124 e.preventDefault();
125 var reply_to_comment_button = $(this).
126 parents().
127 filter(".comment").
128 find(".reply-to-comment-button");
129
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 () {
133 $(this).remove();
134 });
135
136 /* Show the reply-to-comment button again. */
137 reply_to_comment_button.css("display", "inline");
138
139 });
140
141
142 /**********************************************************************
143 * Publish a single comment.
144 **********************************************************************/
145 $("input[name='form.button.PublishComment']").live('click', function () {
146 var trigger = this;
147 var form = $(this).parents("form");
148 var data = $(form).serialize();
149 var form_url = $(form).attr("action");
150 $.ajax({
151 type: "GET",
152 url: form_url,
153 data: "workflow_action=publish",
154 context: trigger,
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');
159 },
160 error: function (msg) {
161 return true;
162 }
163 });
164 return false;
165 });
166
167
168 /**********************************************************************
169 * Delete a comment and its answers.
170 **********************************************************************/
171 /*
172 $("input[name='form.button.DeleteComment']").live('click', function () {
173 var trigger = this;
174 var form = $(this).parents("form");
175 var data = $(form).serialize();
176 var form_url = $(form).attr("action");
177 $.ajax({
178 type: 'POST',
179 url: form_url,
180 context: $(trigger).parents(".comment"),
181 success: function (data) {
182 if ($(".discussion .replyTreeLevel0").length === 1) {
183 $(".discussion").fadeOut('fast', function () {
184 $(".discussion").remove();
185 });
186 }
187 else {
188 var comment = $(this);
189 var clss = comment.attr('class');
190 // remove replies
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;
196 }
197 comment.nextUntil(selector).each(function () {
198 $(this).fadeOut('fast', function () {
199 $(this).remove();
200 });
201 });
202 // remove comment
203 $(this).fadeOut('fast', function () {
204 $(this).remove();
205 });
206 }
207 },
208 error: function (req, error) {
209 return true;
210 }
211 });
212 return false;
213 });*/
214
215 /**********************************************************************
216 * By default, hide the reply and the cancel button for the regular add
217 * comment form.
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");
223
224
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
228 * enabled.
229 **********************************************************************/
230 $(".reply-to-comment-button").css("display" , "inline");
231
232 });
233
234
235 //#JSCOVERAGE_ENDIF
236
237}(jQuery));