| 1 |
jQuery(document).ready(function () { |
| 2 |
editorialCommentReply.init(); |
| 3 |
|
| 4 |
// Check if certain hash flag set and take action |
| 5 |
if (location.hash == '#editorialcomments/add') { |
| 6 |
editorialCommentReply.open(); |
| 7 |
} else if (location.hash.search(/#editorialcomments\/reply/) > -1) { |
| 8 |
var reply_id = location.hash.substring(location.hash.lastIndexOf('/')+1); |
| 9 |
editorialCommentReply.open(reply_id); |
| 10 |
} |
| 11 |
}); |
| 12 |
|
| 13 |
/** |
| 14 |
* Blatantly stolen and modified from /wp-admin/js/edit-comment.dev.js -- yay! |
| 15 |
*/ |
| 16 |
editorialCommentReply = { |
| 17 |
|
| 18 |
init : function() { |
| 19 |
var row = jQuery('#ef-replyrow'); |
| 20 |
|
| 21 |
// Bind click events to cancel and submit buttons |
| 22 |
jQuery('a.ef-replycancel', row).click(function() { return editorialCommentReply.revert(); }); |
| 23 |
jQuery('a.ef-replysave', row).click(function() { return editorialCommentReply.send(); }); |
| 24 |
|
| 25 |
// Watch for changes to the subscribed users. |
| 26 |
$( '#ef-post_following_box' ).on( 'following_list_updated', function() { |
| 27 |
editorialCommentReply.notifiedMessage(); |
| 28 |
} ); |
| 29 |
}, |
| 30 |
|
| 31 |
revert : function() { |
| 32 |
// Fade out slowly, slowly, slowly... |
| 33 |
jQuery('#ef-replyrow').fadeOut('fast', function(){ |
| 34 |
editorialCommentReply.close(); |
| 35 |
}); |
| 36 |
return false; |
| 37 |
}, |
| 38 |
|
| 39 |
close : function() { |
| 40 |
|
| 41 |
jQuery('#ef-comment_respond').show(); |
| 42 |
|
| 43 |
// Move reply form back after the main "Respond" form |
| 44 |
jQuery('#ef-post_comment').after( jQuery('#ef-replyrow') ); |
| 45 |
|
| 46 |
// Empty out all the form values |
| 47 |
jQuery('#ef-replycontent').val(''); |
| 48 |
jQuery('#ef-comment_parent').val(''); |
| 49 |
|
| 50 |
// Hide error and waiting |
| 51 |
jQuery('#ef-replysubmit .error').html('').hide(); |
| 52 |
jQuery('#ef-comment_loading').hide(); |
| 53 |
}, |
| 54 |
|
| 55 |
/** |
| 56 |
* @id = comment id |
| 57 |
*/ |
| 58 |
open : function(id) { |
| 59 |
var parent; |
| 60 |
|
| 61 |
// Close any open reply boxes |
| 62 |
this.close(); |
| 63 |
|
| 64 |
// Check if reply or new comment |
| 65 |
if(id) { |
| 66 |
jQuery('input#ef-comment_parent').val(id); |
| 67 |
parent = '#comment-'+id; |
| 68 |
} else { |
| 69 |
parent = '#ef-comments_wrapper'; |
| 70 |
} |
| 71 |
|
| 72 |
jQuery('#ef-comment_respond').hide(); |
| 73 |
|
| 74 |
// Display who will be notified for this comment |
| 75 |
this.notifiedMessage(); |
| 76 |
|
| 77 |
// Show reply textbox |
| 78 |
jQuery('#ef-replyrow') |
| 79 |
.show() |
| 80 |
.appendTo(jQuery(parent)) |
| 81 |
; |
| 82 |
|
| 83 |
jQuery('#ef-replycontent').focus(); |
| 84 |
|
| 85 |
return false; |
| 86 |
}, |
| 87 |
|
| 88 |
/** |
| 89 |
* Sends the ajax response to save the commment |
| 90 |
* @param bool reply - indicates whether the comment is a reply or not |
| 91 |
*/ |
| 92 |
send : function(reply) { |
| 93 |
var post = {}; |
| 94 |
var containter_id = '#ef-replyrow'; |
| 95 |
|
| 96 |
jQuery('#ef-replysubmit .error').html('').hide(); |
| 97 |
|
| 98 |
// Validation: check to see if comment entered |
| 99 |
post.content = jQuery.trim(jQuery('#ef-replycontent').val()); |
| 100 |
if(!post.content) { |
| 101 |
jQuery('#ef-replyrow .error').text('Please enter a comment').show(); |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
jQuery('#ef-comment_loading').show(); |
| 106 |
|
| 107 |
// Prepare data |
| 108 |
post.action = 'editflow_ajax_insert_comment'; |
| 109 |
post.parent = (jQuery("#ef-comment_parent").val()=='') ? 0 : jQuery("#ef-comment_parent").val(); |
| 110 |
post._nonce = jQuery("#ef_comment_nonce").val(); |
| 111 |
post.post_id = jQuery("#ef-post_id").val(); |
| 112 |
post.notification = jQuery('#ef-reply-notifier').val(); |
| 113 |
|
| 114 |
// Send the request |
| 115 |
jQuery.ajax({ |
| 116 |
type : 'POST', |
| 117 |
url : (ajaxurl) ? ajaxurl : wpListL10n.url, |
| 118 |
data : post, |
| 119 |
success : function(x) { editorialCommentReply.show(x); }, |
| 120 |
error : function(r) { editorialCommentReply.error(r); } |
| 121 |
}); |
| 122 |
|
| 123 |
return false; |
| 124 |
}, |
| 125 |
|
| 126 |
/** |
| 127 |
* Display who will be notified of the new comment. |
| 128 |
*/ |
| 129 |
notifiedMessage : function() { |
| 130 |
var message_wrapper = jQuery( '#ef-reply-notifier' ); |
| 131 |
|
| 132 |
if ( ! message_wrapper[0] ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
var subscribed_users = jQuery( '.ef-post_following_list li input:checkbox:checked' ); |
| 137 |
|
| 138 |
// No users will be notified, so return early with a default message. |
| 139 |
if ( 0 === subscribed_users.length ) { |
| 140 |
message_wrapper.addClass( 'ef-none-selected' ); |
| 141 |
message_wrapper.val( __ef_localize_post_comment.none_notified ); |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
var usernames = []; |
| 146 |
subscribed_users.each( function() { |
| 147 |
// Add usernames of checked users to the list if they don't have a blocking class |
| 148 |
if ( ! $( this ).siblings().is( '.post_following_list-no_email,.post_following_list-no_access' ) && ! $( this ).hasClass( 'post_following_list-current_user' ) ) { |
| 149 |
usernames.push( $( this ).parent().siblings( '.ef-user_displayname, .ef-usergroup_name' ).text() ); |
| 150 |
} |
| 151 |
} ); |
| 152 |
|
| 153 |
// Convert array of usernames into a sentence. |
| 154 |
var message = usernames.pop(); |
| 155 |
if ( usernames.length > 0 ) { |
| 156 |
message = usernames.join( ', ' ) + ' ' + __ef_localize_post_comment.and + ' ' + message + '.'; |
| 157 |
} |
| 158 |
|
| 159 |
message_wrapper.removeClass( 'ef-none-selected' ); |
| 160 |
message_wrapper.val( message ); |
| 161 |
}, |
| 162 |
|
| 163 |
show : function(xml) { |
| 164 |
var response, comment, supplemental, id, bg; |
| 165 |
|
| 166 |
// Didn't pass validation, so let's throw an error |
| 167 |
if ( typeof(xml) == 'string' ) { |
| 168 |
this.error({'responseText': xml}); |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
// Parse the response |
| 173 |
response = wpAjax.parseAjaxResponse(xml); |
| 174 |
if ( response.errors ) { |
| 175 |
// Uh oh, errors found |
| 176 |
this.error({'responseText': wpAjax.broken}); |
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
response = response.responses[0]; |
| 181 |
comment = response.data; |
| 182 |
supplemental = response.supplemental; |
| 183 |
|
| 184 |
jQuery(comment).hide() |
| 185 |
|
| 186 |
if(response.action.indexOf('reply') == -1 || !ef_thread_comments) { |
| 187 |
// Not a reply, so add it to the bottom |
| 188 |
jQuery('#ef-comments').append(comment); |
| 189 |
} else { |
| 190 |
|
| 191 |
// This is a reply, so add it after the comment replied to |
| 192 |
|
| 193 |
if(jQuery('#ef-replyrow').parent().next().is('ul')) { |
| 194 |
// Already been replied to, so just add to the list |
| 195 |
jQuery('#ef-replyrow').parent().next().append(comment); |
| 196 |
} else { |
| 197 |
// This is a first reply, so create an unordered list to house the comment |
| 198 |
var newUL = jQuery('<ul></ul>') |
| 199 |
.addClass('children') |
| 200 |
.append(comment) |
| 201 |
; |
| 202 |
jQuery('#ef-replyrow').parent().after(newUL) |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
// Get the comment contaner's id |
| 207 |
this.o = id = '#comment-'+response.id; |
| 208 |
// Close the reply box |
| 209 |
this.revert(); |
| 210 |
|
| 211 |
// Show the new comment |
| 212 |
jQuery(id) |
| 213 |
.animate( { 'backgroundColor':'#CCEEBB' }, 600 ) |
| 214 |
.animate( { 'backgroundColor':'#fff' }, 600 ); |
| 215 |
|
| 216 |
}, |
| 217 |
|
| 218 |
error : function(r) { |
| 219 |
// Oh noes! We haz an error! |
| 220 |
jQuery('#ef-comment_loading').hide(); |
| 221 |
|
| 222 |
if ( r.responseText ) { |
| 223 |
er = r.responseText.replace( /<.[^<>]*?>/g, '' ); |
| 224 |
} |
| 225 |
|
| 226 |
if ( er ) { |
| 227 |
jQuery('#ef-replysubmit .error').html(er).show(); |
| 228 |
} |
| 229 |
|
| 230 |
} |
| 231 |
}; |