plone.app.discussion/plone/app/discussion/browser/javascripts/comments.js

266 lines
11 KiB
JavaScript
Raw Normal View History

/******************************************************************************
*
* jQuery functions for the plone.app.discussion comment viewlet and form.
*
******************************************************************************/
2015-02-10 23:41:00 +01:00
/* global require */
2015-02-10 23:21:17 +01:00
if(require === undefined){
2015-02-10 23:41:00 +01:00
require = function(reqs, torun){ // jshint ignore:line
2015-02-10 23:21:17 +01:00
'use strict';
return torun(window.jQuery);
2015-02-10 23:41:00 +01:00
};
2015-02-10 23:21:17 +01:00
}
2015-02-10 23:41:00 +01:00
require([ // jshint ignore:line
2015-02-10 23:21:17 +01:00
'jquery'
], function ($) {
2015-02-10 23:41:00 +01:00
'use strict';
// 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
/**************************************************************************
* 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.
**************************************************************************/
$.createReplyForm = function (comment_div) {
2015-02-10 23:41:00 +01:00
var comment_id = comment_div.attr('id');
2015-02-10 23:41:00 +01:00
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.
*/
2015-02-10 23:41:00 +01:00
var reply_div = $('#commenting').clone(true);
/* Remove the ReCaptcha JS code before appending the form. If not
* removed, this causes problems
*/
2015-02-10 23:41:00 +01:00
reply_div.find('#formfield-form-widgets-captcha')
.find('script')
.remove();
/* Insert the cloned comment form right after the reply button of the
* current comment.
*/
2015-02-10 23:41:00 +01:00
reply_div.appendTo(comment_div).css('display', 'none');
2015-02-10 23:41:00 +01:00
/* Remove id='commenting' attribute, since we use it to uniquely define
the main reply form. */
2015-02-10 23:41:00 +01:00
// Still belongs to class='reply'
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).
*/
2015-02-10 23:41:00 +01:00
$(reply_button).css('display', 'none');
/* Fetch the reply form inside the reply div */
2015-02-10 23:41:00 +01:00
var reply_form = reply_div.find('form');
2014-08-23 00:31:28 +02:00
/* Change the id of the textarea of the reply form
* To avoid conflict later between textareas with same id 'form-widgets-comment-text' while implementing a seperate instance of TinyMCE
* */
reply_form.find('#formfield-form-widgets-comment-text').attr('id', 'formfield-form-widgets-new-textarea'+comment_id );
reply_form.find('#form-widgets-comment-text').attr('id', 'form-widgets-new-textarea'+comment_id );
/* Populate the hidden 'in_reply_to' field with the correct comment
id */
2015-02-10 23:41:00 +01:00
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 */
2015-02-10 23:41:00 +01:00
var cancel_reply_button = reply_div.find('.cancelreplytocomment');
cancel_reply_button.attr('id', comment_id);
/* Show the cancel buttons. */
2015-02-10 23:41:00 +01:00
reply_form.find('input[name="form.buttons.cancel"]')
.css('display', 'inline');
/* Show the reply layer with a slide down effect */
2015-02-10 23:41:00 +01:00
reply_div.slideDown('slow');
/* Show the cancel button in the reply-to-comment form */
2015-02-10 23:41:00 +01:00
cancel_reply_button.css('display', 'inline');
};
/**************************************************************************
* Remove all error messages and field values from the form that is passed
* to the function.
**************************************************************************/
$.clearForm = function (form_div) {
2015-02-10 23:41:00 +01:00
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. */
};
//#JSCOVERAGE_IF 0
/**************************************************************************
* 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
2015-02-10 23:41:00 +01:00
* (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.
**********************************************************************/
2015-02-10 23:41:00 +01:00
var post_comment_div = $('#commenting');
var in_reply_to_field =
2015-02-10 23:41:00 +01:00
post_comment_div.find('input[name="form.widgets.in_reply_to"]');
if (in_reply_to_field.length !== 0 && 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);
}
/**********************************************************************
2015-02-10 23:41:00 +01:00
* If the user hits the 'reply' button of an existing comment, create a
* reply form right beneath this comment.
**********************************************************************/
2015-02-10 23:41:00 +01:00
$('.reply-to-comment-button').bind('click', function (e) { // jshint ignore:line
var comment_div = $(this).parents().filter('.comment');
$.createReplyForm(comment_div);
$.clearForm(comment_div);
});
/**********************************************************************
2015-02-10 23:41:00 +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.
**********************************************************************/
2015-02-10 23:41:00 +01:00
$('#commenting #form-buttons-cancel').bind('click', function (e) {
e.preventDefault();
var reply_to_comment_button = $(this).
parents().
2015-02-10 23:41:00 +01:00
filter('.comment').
find('.reply-to-comment-button');
2012-11-04 17:25:25 +01:00
/* Find the reply-to-comment form and hide and remove it again. */
2015-02-10 23:41:00 +01:00
$.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
/* Show the reply-to-comment button again. */
2015-02-10 23:41:00 +01:00
reply_to_comment_button.css('display', 'inline');
2012-11-04 17:25:25 +01:00
});
/**********************************************************************
* Publish a single comment.
**********************************************************************/
2015-02-10 23:41:00 +01:00
$('input[name="form.button.PublishComment"]').on('click', function () {
var trigger = this;
2015-02-10 23:41:00 +01:00
var form = $(this).parents('form');
var data = $(form).serialize();
2015-02-10 23:41:00 +01:00
var form_url = $(form).attr('action');
$.ajax({
2015-02-10 23:41:00 +01:00
type: 'GET',
url: form_url,
2014-02-04 10:00:06 +01:00
data: data,
context: trigger,
2015-02-10 23:41:00 +01:00
success: function (msg) { // jshint ignore:line
// remove button (trigger object can't be directly removed)
2015-02-10 23:41:00 +01:00
form.find('input[name="form.button.PublishComment"]').remove();
form.parents('.state-pending').toggleClass('state-pending').toggleClass('state-published');
},
2015-02-10 23:41:00 +01:00
error: function (msg) { // jshint ignore:line
return true;
}
});
return false;
});
2013-09-17 14:03:46 +02:00
/**********************************************************************
* Edit a comment
**********************************************************************/
2015-02-10 23:41:00 +01:00
$('form[name="edit"]').prepOverlay({
cssclass: 'overlay-edit-comment',
width: '60%',
2013-09-17 14:03:46 +02:00
subtype: 'ajax',
filter: '#content>*'
2015-02-10 23:41:00 +01:00
});
/**********************************************************************
* Delete a comment and its answers.
**********************************************************************/
2015-02-10 23:41:00 +01:00
$('input[name="form.button.DeleteComment"]').on('click', function () {
var trigger = this;
2015-02-10 23:41:00 +01:00
var form = $(this).parents('form');
var data = $(form).serialize();
2015-02-10 23:41:00 +01:00
var form_url = $(form).attr('action');
$.ajax({
type: 'POST',
url: form_url,
data: data,
2015-02-10 23:41:00 +01:00
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
2015-02-10 23:41:00 +01:00
var selector = '.replyTreeLevel' + treelevel;
for (var i = 0; i < treelevel; i++) {
2015-02-10 23:41:00 +01:00
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();
});
},
2015-02-10 23:41:00 +01:00
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.
**********************************************************************/
2015-02-10 23:41:00 +01:00
$('.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.
**********************************************************************/
2015-02-10 23:41:00 +01:00
$('.reply-to-comment-button').css('display' , 'inline');
});
//#JSCOVERAGE_ENDIF
2015-02-10 23:21:17 +01:00
});