diff --git a/CHANGES.txt b/CHANGES.txt index 322a6a5..4d8f289 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -4,6 +4,11 @@ Changelog 1.0b8 (unreleased) ------------------ +- Use "(function($) { /* some code that uses $ */ })(jQuery)" instead of + "$(document).ready(function(){ /* some code that uses $ */ });" to invoke + jQuery code. + [timo] + - Finnish translation added. [saffe] diff --git a/plone/app/discussion/browser/javascripts/comments.js b/plone/app/discussion/browser/javascripts/comments.js index 6d891b3..90caa4f 100644 --- a/plone/app/discussion/browser/javascripts/comments.js +++ b/plone/app/discussion/browser/javascripts/comments.js @@ -1,144 +1,169 @@ -/************************************************************************** - * Remove all error messages and field values from the form that is passed - * to the function. - **************************************************************************/ -function clearForm(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. */ -} +/****************************************************************************** + * + * jQuery functions for the plone.app.discussion comment viewlet and form. + * + ******************************************************************************/ - -/************************************************************************** - * 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 - * adding a hidden in_reply_to field to the form. - **************************************************************************/ -function createReplyForm(comment_div) { - - var comment_id = comment_div.attr("id"); - - var reply_button = comment_div.find(".reply-to-comment-button"); - - /* Clone the reply div at the end of the page template that contains - * the regular comment form. - */ - var reply_div = $("#commenting").clone(true); - - /* 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(); - - /* Insert the cloned comment form right after the reply button of the - * current comment. - */ - reply_div.appendTo(comment_div).css("display", "none"); - - /* Remove id="reply" attribute, since we use it to uniquely - the main reply form. */ - reply_div.removeAttr("id"); - - /* 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"); - - /* Fetch the reply form inside the reply div */ - var reply_form = reply_div.find("form"); - - /* Populate the hidden 'in_reply_to' field with the correct comment id */ - reply_form.find("input[name='form.widgets.in_reply_to']") - .val(comment_id); - - /* Add a remove-reply-to-comment Javascript function to remove the form */ - var cancel_reply_button = reply_div.find(".cancelreplytocomment"); - cancel_reply_button.attr("id", comment_id); - - /* Show the cancel buttons. */ - reply_form.find("input[name='form.buttons.cancel']") - .css("display", "inline"); - - /* Show the reply layer with a slide down effect */ - reply_div.slideDown("slow"); - - /* Show the cancel button in the reply-to-comment form */ - cancel_reply_button.css("display", "inline"); -} - - -$(document).ready(function () { + +(function ($) { + // 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 /************************************************************************** - * By default, hide the reply and the cancel button for the regular add - * comment form. + * 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 + * adding a hidden in_reply_to field to the form. **************************************************************************/ - $(".reply").find("input[name='form.buttons.reply']") - .css("display", "none"); - $(".reply").find("input[name='form.buttons.cancel']") - .css("display", "none"); - - - /************************************************************************** - * 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"); - + $.createReplyForm = function (comment_div) { + + var comment_id = comment_div.attr("id"); - /************************************************************************** - * 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); + var reply_button = comment_div.find(".reply-to-comment-button"); + + /* Clone the reply div at the end of the page template that contains + * the regular comment form. + */ + var reply_div = $("#commenting").clone(true); + + /* 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(); + + /* Insert the cloned comment form right after the reply button of the + * current comment. + */ + reply_div.appendTo(comment_div).css("display", "none"); + + /* Remove id="reply" attribute, since we use it to uniquely + the main reply form. */ + reply_div.removeAttr("id"); + + /* 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"); + + /* Fetch the reply form inside the reply div */ + var reply_form = reply_div.find("form"); + + /* Populate the hidden 'in_reply_to' field with the correct comment + id */ + reply_form.find("input[name='form.widgets.in_reply_to']") + .val(comment_id); + + /* Add a remove-reply-to-comment Javascript function to remove the + form */ + var cancel_reply_button = reply_div.find(".cancelreplytocomment"); + cancel_reply_button.attr("id", comment_id); + + /* Show the cancel buttons. */ + reply_form.find("input[name='form.buttons.cancel']") + .css("display", "inline"); + + /* Show the reply layer with a slide down effect */ + reply_div.slideDown("slow"); + + /* Show the cancel button in the reply-to-comment form */ + cancel_reply_button.css("display", "inline"); } - - - /************************************************************************** - * 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); - }); /************************************************************************** - * If the user hits the "clear" button of an open reply-to-comment form, - * remove the form and show the "reply" button again. + * Remove all error messages and field values from the form that is passed + * to the function. **************************************************************************/ - $("#form-buttons-cancel").bind("click", function (e) { - e.preventDefault(); - var reply_to_comment_button = $(this). - parents(). - filter(".comment"). - find(".reply-to-comment-button"); + $.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. */ + } - /* 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(); - }); + //#JSCOVERAGE_IF 0 - /* Show the reply-to-comment button again. */ - reply_to_comment_button.css("display", "inline"); + /************************************************************************** + * Window Load Function: Executes when complete page is fully loaded, + * including all frames, + **************************************************************************/ + $(window).load(function () { + + /********************************************************************** + * 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); + } + + + /********************************************************************** + * 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); + }); + + + /********************************************************************** + * If the user hits the "clear" button of an open reply-to-comment form, + * remove the form and show the "reply" button again. + **********************************************************************/ + $("#form-buttons-cancel").bind("click", function (e) { + e.preventDefault(); + var reply_to_comment_button = $(this). + parents(). + filter(".comment"). + find(".reply-to-comment-button"); + + /* 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(); + }); + + /* Show the reply-to-comment button again. */ + reply_to_comment_button.css("display", "inline"); + + }); + - }); -}); + /********************************************************************** + * 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"); + + + /********************************************************************** + * 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"); + + }); + + //#JSCOVERAGE_ENDIF + +}(jQuery)); \ No newline at end of file diff --git a/plone/app/discussion/browser/javascripts/moderation.js b/plone/app/discussion/browser/javascripts/moderation.js index 87b3add..7e95ed7 100644 --- a/plone/app/discussion/browser/javascripts/moderation.js +++ b/plone/app/discussion/browser/javascripts/moderation.js @@ -1,25 +1,24 @@ -jq(document).ready(function () { - +(function ($) { /************************************************************************** * Delete a single comment. **************************************************************************/ - jq("input[name='form.button.Delete']").click(function (e) { + $("input[name='form.button.Delete']").click(function (e) { e.preventDefault(); - var button = jq(this); - var row = jq(this).parent().parent(); - var form = jq(row).parents("form"); - var path = jq(row).find("input:checkbox").attr("value"); + var button = $(this); + var row = $(this).parent().parent(); + var form = $(row).parents("form"); + var path = $(row).find("input:checkbox").attr("value"); var target = path + "/@@moderate-delete-comment"; - var comment_id = jq(this).attr("id"); - jq.ajax({ + var comment_id = $(this).attr("id"); + $.ajax({ type: "GET", url: target, success: function (msg) { // fade out row - jq(row).fadeOut("normal", function () { - jq(this).remove(); + $(row).fadeOut("normal", function () { + $(this).remove(); }); }, error: function (msg) { @@ -32,21 +31,21 @@ jq(document).ready(function () { /************************************************************************** * Publish a single comment. **************************************************************************/ - jq("input[name='form.button.Publish']").click(function (e) { + $("input[name='form.button.Publish']").click(function (e) { e.preventDefault(); - var button = jq(this); - var row = jq(this).parent().parent(); - var form = jq(row).parents("form"); - var path = jq(row).find("input:checkbox").attr("value"); + var button = $(this); + var row = $(this).parent().parent(); + var form = $(row).parents("form"); + var path = $(row).find("input:checkbox").attr("value"); var target = path + "/@@moderate-publish-comment"; - jq.ajax({ + $.ajax({ type: "GET", url: target, data: "workflow_action=publish", success: function (msg) { // fade out row - jq(row).fadeOut("normal", function () { - jq(this).remove(); + $(row).fadeOut("normal", function () { + $(this).remove(); }); }, error: function (msg) { @@ -59,24 +58,25 @@ jq(document).ready(function () { /************************************************************************** * Bulk actions for comments (delete, publish) **************************************************************************/ - jq("input[name='form.button.BulkAction']").click(function (e) { + $("input[name='form.button.BulkAction']").click(function (e) { e.preventDefault(); - var form = jq(this).parents("form"); - var target = jq(form).attr('action'); - var params = jq(form).serialize(); - var valArray = jq('input:checkbox:checked'); - var selectField = jq(form).find("[name='form.select.BulkAction']"); + var form = $(this).parents("form"); + var target = $(form).attr('action'); + var params = $(form).serialize(); + var valArray = $('input:checkbox:checked'); + var selectField = $(form).find("[name='form.select.BulkAction']"); if (selectField.val() === '-1') { // XXX: translate message alert("You haven't selected a bulk action. Please select one."); } else if (valArray.length === 0) { // XXX: translate message - alert("You haven't selected any comment for this bulk action. Please select at least one comment."); + alert("You haven't selected any comment for this bulk action." + + "Please select at least one comment."); } else { - jq.post(target, params, function (data) { + $.post(target, params, function (data) { valArray.each(function () { /* Remove all selected lines. */ - var row = jq(this).parent().parent(); + var row = $(this).parent().parent(); row.fadeOut("normal", function () { row.remove(); }); @@ -91,17 +91,17 @@ jq(document).ready(function () { /************************************************************************** * Check or uncheck all checkboxes from the batch moderation page. **************************************************************************/ - jq("input[name='check_all']").click(function () { - if (jq(this).val() === 0) { - jq(this).parents("table") + $("input[name='check_all']").click(function () { + if ($(this).val() === 0) { + $(this).parents("table") .find("input:checkbox") .attr("checked", "checked"); - jq(this).val("1"); + $(this).val("1"); } else { - jq(this).parents("table") + $(this).parents("table") .find("input:checkbox") .attr("checked", ""); - jq(this).val("0"); + $(this).val("0"); } }); @@ -109,11 +109,11 @@ jq(document).ready(function () { /************************************************************************** * Show full text of a comment in the batch moderation page. **************************************************************************/ - jq(".show-full-comment-text").click(function (e) { + $(".show-full-comment-text").click(function (e) { e.preventDefault(); - var target = jq(this).attr("href"); - var td = jq(this).parent(); - jq.ajax({ + var target = $(this).attr("href"); + var td = $(this).parent(); + $.ajax({ type: "GET", url: target, data: "", @@ -127,4 +127,5 @@ jq(document).ready(function () { }); }); -}); +}(jQuery)); + diff --git a/plone/app/discussion/tests/javascripts/test_comments.html b/plone/app/discussion/tests/javascripts/test_comments.html index 7ffb87b..be846e0 100644 --- a/plone/app/discussion/tests/javascripts/test_comments.html +++ b/plone/app/discussion/tests/javascripts/test_comments.html @@ -5,23 +5,21 @@ dir="ltr" id="html"> plone.app.discussion comments Test Suite - - - - - - - - + + + + + + - + diff --git a/plone/app/discussion/tests/javascripts/test_comments.js b/plone/app/discussion/tests/javascripts/test_comments.js index c51395a..8bca11f 100644 --- a/plone/app/discussion/tests/javascripts/test_comments.js +++ b/plone/app/discussion/tests/javascripts/test_comments.js @@ -115,26 +115,24 @@ $(document).ready(function () { test("Create a comment reply form.", function() { expect(2); var comment_div = $("#1282720906349675"); - var reply_button = comment_div.find(".reply-to-comment-button"); - // Hit the reply button - reply_button.trigger("click"); - createReplyForm(comment_div); - var reply_form = comment_div.find(".reply"); - ok(reply_form, "Reply form has been copied"); + var reply_button = comment_div.children(".reply-to-comment-button"); + $.createReplyForm(comment_div); + var reply_form = comment_div.children(".reply"); + ok(reply_form.find("input[name='form.widgets.in_reply_to']"), "Reply form has been copied"); same(reply_form.find("input[name='form.widgets.in_reply_to']").val(), "1282720906349675", "The reply for should have the id of the comment in the in_reply_to field"); }); test("Clear all form values from a form.", function() { // Create a reply form with some values var comment_div = $("#1282720906349675"); - createReplyForm(comment_div); + $.createReplyForm(comment_div); var reply_form = comment_div.find(".reply"); var author = reply_form.find("input[name='form.widgets.author']"); var text = comment_div.find("input[name='form.widgets.text']"); author.val("my author"); text.val("my text"); // Call the clearForm function to clear the form - clearForm(comment_div); + $.clearForm(comment_div); // Check if all form fields have been cleared var author = comment_div.find("input[name='form.widgets.author']"); var text = comment_div.find("input[name='form.widgets.text']");