From 61545c6ef667bf75d07c437ad528bc50ee0a4d11 Mon Sep 17 00:00:00 2001 From: Katja Suess Date: Tue, 21 Jan 2020 12:02:09 +0100 Subject: [PATCH] Moderation on commented page also with ajax like on moderation view --- .../browser/javascripts/comments.js | 201 +++++++++--------- .../browser/javascripts/moderation.js | 14 +- plone/app/discussion/browser/moderation.py | 10 +- 3 files changed, 113 insertions(+), 112 deletions(-) diff --git a/plone/app/discussion/browser/javascripts/comments.js b/plone/app/discussion/browser/javascripts/comments.js index be53bd6..89ce56a 100644 --- a/plone/app/discussion/browser/javascripts/comments.js +++ b/plone/app/discussion/browser/javascripts/comments.js @@ -106,13 +106,109 @@ require([ /* XXX: Clean all additional form extender fields. */ }; - //#JSCOVERAGE_IF 0 + function init_comment_eventhandler () { + /********************************************************************** + * Transmit a single comment. + **********************************************************************/ + $('input[name="form.button.TransmitComment"]').on("click", function(e) { + e.preventDefault(); + var trigger = this; + var form = $(this).parents("form"); + var data = $(form).serialize(); + var form_url = $(form).attr("action"); + var comment_id = $(this).parents(".comment").attr("id"); + $.ajax({ + type: "GET", + url: form_url, + data: data, + context: trigger, + success: function(msg) { + let url = location.href; + $(this).parents(".comment").load( + // loading child nodes is not enough, + // class attributes are needed for visualization of workflow_state + url + " #" + comment_id + ".comment", + function() { + $(this).find(".comment").unwrap(); + init_comment_eventhandler(); + $(".pat-plone-modal").patPloneModal(); + } + ); + }, + error: function(msg) { + return true; + } + }); + return false; + }); + + /********************************************************************** + * Edit a comment + **********************************************************************/ + if ($.fn.prepOverlay) { + $('form[name="edit"]').prepOverlay({ + cssclass: "overlay-edit-comment", + width: "60%", + subtype: "ajax", + filter: "#content>*" + }); + } + + /********************************************************************** + * Delete a comment and its answers. + **********************************************************************/ + $('input[name="form.button.DeleteComment"]').on("click", function(e) { + e.preventDefault(); + var trigger = this; + var form = $(this).parents("form"); + var data = $(form).serialize(); + var form_url = $(form).attr("action"); + $.ajax({ + type: "POST", + url: form_url, + data: data, + context: $(trigger).parents(".comment"), + success: function(data) { + // jshint ignore:line + 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; + } + comment.nextUntil(selector).each(function() { + $(this).fadeOut("fast", function() { + $(this).remove(); + }); + }); + // Add delete button to the parent + var parent = comment.prev( + '[class*="replyTreeLevel' + (treelevel - 1) + '"]' + ); + parent.find('form[name="delete"]').css("display", "inline"); + // remove comment + $(this).fadeOut("fast", function() { + $(this).remove(); + }); + }, + error: function(req, error) { + // jshint ignore:line + return true; + } + }); + return false; + }); + }; + + $(document).ready(function() { + init_comment_eventhandler(); - /************************************************************************** - * 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 @@ -165,98 +261,6 @@ require([ reply_to_comment_button.css("display", "inline"); }); - /********************************************************************** - * Transmit a single comment. - **********************************************************************/ - $('input[name="form.button.TransmitComment"]').on("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: data, - context: trigger, - success: function(msg) { - // jshint ignore:line - // remove button (trigger object can't be directly removed) - form.find('input[name="form.button.TransmitComment"]').remove(); - form - .parents(".state-pending") - .toggleClass("state-pending") - .toggleClass("state-published"); - }, - error: function(msg) { - // jshint ignore:line - return true; - } - }); - return false; - }); - - /********************************************************************** - * Edit a comment - **********************************************************************/ - if ($.fn.prepOverlay) { - $('form[name="edit"]').prepOverlay({ - cssclass: "overlay-edit-comment", - width: "60%", - subtype: "ajax", - filter: "#content>*" - }); - } - - /********************************************************************** - * Delete a comment and its answers. - **********************************************************************/ - $('input[name="form.button.DeleteComment"]').on("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, - data: data, - context: $(trigger).parents(".comment"), - success: function(data) { - // jshint ignore:line - 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; - } - comment.nextUntil(selector).each(function() { - $(this).fadeOut("fast", function() { - $(this).remove(); - }); - }); - // Add delete button to the parent - var parent = comment.prev( - '[class*="replyTreeLevel' + (treelevel - 1) + '"]' - ); - parent.find('form[name="delete"]').css("display", "inline"); - // remove comment - $(this).fadeOut("fast", function() { - $(this).remove(); - }); - }, - error: function(req, error) { - // jshint ignore:line - return true; - } - }); - return false; - }); - /********************************************************************** * By default, hide the reply and the cancel button for the regular add * comment form. @@ -276,5 +280,4 @@ require([ $(".reply-to-comment-button").removeClass("hide"); }); - //#JSCOVERAGE_ENDIF }); diff --git a/plone/app/discussion/browser/javascripts/moderation.js b/plone/app/discussion/browser/javascripts/moderation.js index 5275b45..f74a2d6 100644 --- a/plone/app/discussion/browser/javascripts/moderation.js +++ b/plone/app/discussion/browser/javascripts/moderation.js @@ -15,10 +15,10 @@ require(["jquery", "pat-registry"], function($, registry) { "use strict"; $(document).ready(function() { - init(); + init_moderation_eventhandler(); }); - function init() { + function init_moderation_eventhandler() { /********************************************************************** * Delete a single comment. **********************************************************************/ @@ -83,7 +83,7 @@ require(["jquery", "pat-registry"], function($, registry) { $("#review-comments").load( url + " #review-comments > *", function() { - init(); + init_moderation_eventhandler(); $(".pat-plone-modal").patPloneModal(); } ); @@ -129,7 +129,7 @@ require(["jquery", "pat-registry"], function($, registry) { $("#review-comments").load( window.location + " #review-comments", function() { - init(); + init_moderation_eventhandler(); $(".pat-plone-modal").patPloneModal(); } ); @@ -174,8 +174,8 @@ require(["jquery", "pat-registry"], function($, registry) { $("#fieldset-moderate-comments") .parent() - .load(url + " #fieldset-moderate-comments", function() { - init(); + .load(url + " #content form > *", function() { + init_moderation_eventhandler(); $(".pat-plone-modal").patPloneModal(); let stateObj = { review_state: review_state }; history.pushState(stateObj, "moderate comments", url); @@ -224,5 +224,5 @@ require(["jquery", "pat-registry"], function($, registry) { } }); }); - } // end init + } // end init_moderation_eventhandler }); diff --git a/plone/app/discussion/browser/moderation.py b/plone/app/discussion/browser/moderation.py index fa42c7b..05c0236 100644 --- a/plone/app/discussion/browser/moderation.py +++ b/plone/app/discussion/browser/moderation.py @@ -3,6 +3,7 @@ from AccessControl import getSecurityManager from AccessControl import Unauthorized from Acquisition import aq_inner from Acquisition import aq_parent +from plone import api from plone.app.discussion.events import CommentPublishedEvent from plone.app.discussion.events import CommentTransitionEvent from plone.app.discussion.events import CommentDeletedEvent @@ -260,16 +261,14 @@ class CommentTransition(BrowserView): """Call CommentTransition.""" comment = aq_inner(self.context) content_object = aq_parent(aq_parent(comment)) - workflowTool = getToolByName(comment, 'portal_workflow', None) workflow_action = self.request.form.get('workflow_action', 'publish') - review_state = workflowTool.getInfoFor(comment, 'review_state', '') - workflowTool.doActionFor(comment, workflow_action) + api.content.transition(comment, transition=workflow_action) comment.reindexObject() content_object.reindexObject(idxs=['total_comments']) notify(CommentPublishedEvent(self.context, comment)) # for complexer workflows: notify(CommentTransitionEvent(self.context, comment)) - review_state_new = workflowTool.getInfoFor(comment, 'review_state', '') + review_state_new = api.content.get_state(comment, '') comment_state_translated = self.context.restrictedTraverse("translationhelper").translate_comment_review_state(review_state_new) @@ -278,8 +277,7 @@ class CommentTransition(BrowserView): default='Comment ${comment_state_translated}.', mapping={"comment_state_translated": comment_state_translated}) translated = self.context.translate(msgid) - IStatusMessage(self.context.REQUEST).addStatusMessage( - translated, type='info') + api.portal.show_message(translated, self.context.REQUEST) came_from = self.context.REQUEST.HTTP_REFERER # if the referrer already has a came_from in it, don't redirect back