1 /* global adminCommentsL10n, thousandsSeparator, list_args, QTags, ajaxurl, wpAjax */
2 var setCommentsList, theList, theExtraList, commentReply,
3 toggleWithKeyboard = false;
6 var getCount, updateCount, updatePending, dashboardTotals;
8 setCommentsList = function() {
9 var totalInput, perPageInput, pageInput, dimAfter, delBefore, updateTotalCount, delAfter, refillTheExtraList, diff,
10 lastConfidentTime = 0;
12 totalInput = $('input[name="_total"]', '#comments-form');
13 perPageInput = $('input[name="_per_page"]', '#comments-form');
14 pageInput = $('input[name="_page"]', '#comments-form');
16 dimAfter = function( r, settings ) {
17 var editRow, replyID, replyButton,
18 c = $( '#' + settings.element );
20 editRow = $('#replyrow');
21 replyID = $('#comment_ID', editRow).val();
22 replyButton = $('#replybtn', editRow);
24 if ( c.is('.unapproved') ) {
25 if ( settings.data.id == replyID )
26 replyButton.text(adminCommentsL10n.replyApprove);
28 c.find('div.comment_status').html('0');
30 if ( settings.data.id == replyID )
31 replyButton.text(adminCommentsL10n.reply);
33 c.find('div.comment_status').html('1');
36 diff = $('#' + settings.element).is('.' + settings.dimClass) ? 1 : -1;
37 updatePending( diff );
40 // Send current total, page, per_page and url
41 delBefore = function( settings, list ) {
42 var note, id, el, n, h, a, author,
44 wpListsData = $( settings.target ).attr( 'data-wp-lists' );
46 settings.data._total = totalInput.val() || 0;
47 settings.data._per_page = perPageInput.val() || 0;
48 settings.data._page = pageInput.val() || 0;
49 settings.data._url = document.location.href;
50 settings.data.comment_status = $('input[name="comment_status"]', '#comments-form').val();
52 if ( wpListsData.indexOf(':trash=1') != -1 )
54 else if ( wpListsData.indexOf(':spam=1') != -1 )
58 id = wpListsData.replace(/.*?comment-([0-9]+).*/, '$1');
59 el = $('#comment-' + id);
60 note = $('#' + action + '-undo-holder').html();
62 el.find('.check-column :checkbox').prop('checked', false); // Uncheck the row so as not to be affected by Bulk Edits.
64 if ( el.siblings('#replyrow').length && commentReply.cid == id )
68 n = el.children(':visible').length;
69 author = $('.author strong', el).text();
70 h = $('<tr id="undo-' + id + '" class="undo un' + action + '" style="display:none;"><td colspan="' + n + '">' + note + '</td></tr>');
72 author = $('.comment-author', el).text();
73 h = $('<div id="undo-' + id + '" style="display:none;" class="undo un' + action + '">' + note + '</div>');
78 $('strong', '#undo-' + id).text(author);
79 a = $('.undo a', '#undo-' + id);
80 a.attr('href', 'comment.php?action=un' + action + 'comment&c=' + id + '&_wpnonce=' + settings.data._ajax_nonce);
81 a.attr('data-wp-lists', 'delete:the-comment-list:comment-' + id + '::un' + action + '=1');
82 a.attr('class', 'vim-z vim-destructive');
83 $('.avatar', el).clone().prependTo('#undo-' + id + ' .' + action + '-undo-inside');
86 list.wpList.del(this);
87 $('#undo-' + id).css( {backgroundColor:'#ceb'} ).fadeOut(350, function(){
89 $('#comment-' + id).css('backgroundColor', '').fadeIn(300, function(){ $(this).show(); });
98 // Updates the current total (stored in the _total input)
99 updateTotalCount = function( total, time, setConfidentTime ) {
100 if ( time < lastConfidentTime )
103 if ( setConfidentTime )
104 lastConfidentTime = time;
106 totalInput.val( total.toString() );
109 dashboardTotals = function(n) {
110 var total, appr, totalN, apprN,
111 dash = $('#dashboard_right_now');
114 if ( isNaN(n) || !dash.length )
117 total = $('span.total-count', dash);
118 appr = $('span.approved-count', dash);
119 totalN = getCount(total);
122 apprN = totalN - getCount( $('span.pending-count', dash) ) - getCount( $('span.spam-count', dash) );
123 updateCount(total, totalN);
124 updateCount(appr, apprN);
127 getCount = function(el) {
128 var n = parseInt( el.html().replace(/[^0-9]+/g, ''), 10 );
134 updateCount = function(el, n) {
138 n = n < 1 ? '0' : n.toString();
139 if ( n.length > 3 ) {
140 while ( n.length > 3 ) {
141 n1 = thousandsSeparator + n.substr(n.length - 3) + n1;
142 n = n.substr(0, n.length - 3);
149 updatePending = function( diff ) {
150 $('span.pending-count').each(function() {
151 var a = $(this), n = getCount(a) + diff;
154 a.closest('.awaiting-mod')[ 0 === n ? 'addClass' : 'removeClass' ]('count-0');
161 // In admin-ajax.php, we send back the unix time stamp instead of 1 on success
162 delAfter = function( r, settings ) {
163 var total_items_i18n, total, N, spam, trash, pending,
164 untrash = $(settings.target).parent().is('span.untrash'),
165 unspam = $(settings.target).parent().is('span.unspam'),
166 unapproved = $('#' + settings.element).is('.unapproved');
168 function getUpdate(s) {
169 if ( $(settings.target).parent().is('span.' + s) )
171 else if ( $('#' + settings.element).is('.' + s) )
180 trash = getUpdate('trash');
185 spam = getUpdate('spam');
187 if ( $(settings.target).parent().is('span.unapprove') || ( ( untrash || unspam ) && unapproved ) ) {
188 // a comment was 'deleted' from another list (e.g. approved, spam, trash) and moved to pending,
189 // or a trash/spam of a pending comment was undone
191 } else if ( unapproved ) {
192 // a pending comment was trashed/spammed/approved
197 updatePending(pending);
199 $('span.spam-count').each( function() {
200 var a = $(this), n = getCount(a) + spam;
204 $('span.trash-count').each( function() {
205 var a = $(this), n = getCount(a) + trash;
209 if ( $('#dashboard_right_now').length ) {
210 N = trash ? -1 * trash : 0;
213 total = totalInput.val() ? parseInt( totalInput.val(), 10 ) : 0;
214 if ( $(settings.target).parent().is('span.undo') )
222 if ( ( 'object' == typeof r ) && lastConfidentTime < settings.parsed.responses[0].supplemental.time ) {
223 total_items_i18n = settings.parsed.responses[0].supplemental.total_items_i18n || '';
224 if ( total_items_i18n ) {
225 $('.displaying-num').text( total_items_i18n );
226 $('.total-pages').text( settings.parsed.responses[0].supplemental.total_pages_i18n );
227 $('.tablenav-pages').find('.next-page, .last-page').toggleClass('disabled', settings.parsed.responses[0].supplemental.total_pages == $('.current-page').val());
229 updateTotalCount( total, settings.parsed.responses[0].supplemental.time, true );
231 updateTotalCount( total, r, false );
235 if ( ! theExtraList || theExtraList.size() === 0 || theExtraList.children().size() === 0 || untrash || unspam ) {
239 theList.get(0).wpList.add( theExtraList.children(':eq(0)').remove().clone() );
241 refillTheExtraList();
244 refillTheExtraList = function(ev) {
245 var args = $.query.get(), total_pages = $('.total-pages').text(), per_page = $('input[name="_per_page"]', '#comments-form').val();
250 if (args.paged > total_pages) {
255 theExtraList.empty();
256 args.number = Math.min(8, per_page); // see WP_Comments_List_Table::prepare_items() @ class-wp-comments-list-table.php
259 args.offset = Math.min(8, per_page) - 1; // fetch only the next item on the extra list
262 args.no_placeholder = true;
266 // $.query.get() needs some correction to be sent into an ajax request
267 if ( true === args.comment_type )
268 args.comment_type = '';
270 args = $.extend(args, {
271 'action': 'fetch-list',
272 'list_args': list_args,
273 '_ajax_fetch_list_nonce': $('#_ajax_fetch_list_nonce').val()
281 success: function(response) {
282 theExtraList.get(0).wpList.add( response.rows );
287 theExtraList = $('#the-extra-comment-list').wpList( { alt: '', delColor: 'none', addColor: 'none' } );
288 theList = $('#the-comment-list').wpList( { alt: '', delBefore: delBefore, dimAfter: dimAfter, delAfter: delAfter, addColor: 'none' } )
289 .bind('wpListDelEnd', function(e, s){
290 var wpListsData = $(s.target).attr('data-wp-lists'), id = s.element.replace(/[^0-9]+/g, '');
292 if ( wpListsData.indexOf(':trash=1') != -1 || wpListsData.indexOf(':spam=1') != -1 )
293 $('#undo-' + id).fadeIn(300, function(){ $(this).show(); });
302 var row = $('#replyrow');
304 $('a.cancel', row).click(function() { return commentReply.revert(); });
305 $('a.save', row).click(function() { return commentReply.send(); });
306 $('input#author, input#author-email, input#author-url', row).keypress(function(e){
307 if ( e.which == 13 ) {
315 $('#the-comment-list .column-comment > p').dblclick(function(){
316 commentReply.toggle($(this).parent());
319 $('#doaction, #doaction2, #post-query-submit').click(function(){
320 if ( $('#the-comment-list #replyrow').length > 0 )
321 commentReply.close();
324 this.comments_listing = $('#comments-form > input[name="comment_status"]').val() || '';
326 /* $(listTable).bind('beforeChangePage', function(){
327 commentReply.close();
331 addEvents : function(r) {
333 $(this).find('.column-comment > p').dblclick(function(){
334 commentReply.toggle($(this).parent());
339 toggle : function(el) {
340 if ( $(el).css('display') != 'none' )
341 $(el).find('a.vim-q').click();
344 revert : function() {
346 if ( $('#the-comment-list #replyrow').length < 1 )
349 $('#replyrow').fadeOut('fast', function(){
350 commentReply.close();
357 var c, replyrow = $('#replyrow');
359 // replyrow is not showing?
360 if ( replyrow.parent().is('#com-reply') )
363 if ( this.cid && this.act == 'edit-comment' ) {
364 c = $('#comment-' + this.cid);
365 c.fadeIn(300, function(){ c.show(); }).css('backgroundColor', '');
368 // reset the Quicktags buttons
369 if ( typeof QTags != 'undefined' )
370 QTags.closeAllTags('replycontent');
372 $('#add-new-comment').css('display', '');
375 $('#com-reply').append( replyrow );
376 $('#replycontent').css('height', '').val('');
377 $('#edithead input').val('');
378 $('.error', replyrow).html('').hide();
379 $('.spinner', replyrow).hide();
384 open : function(comment_id, post_id, action) {
385 var editRow, rowData, act, replyButton, editHeight,
387 c = $('#comment-' + comment_id),
393 editRow = $('#replyrow');
394 rowData = $('#inline-'+comment_id);
395 action = action || 'replyto';
396 act = 'edit' == action ? 'edit' : 'replyto';
397 act = t.act = act + '-comment';
399 $('#action', editRow).val(act);
400 $('#comment_post_ID', editRow).val(post_id);
401 $('#comment_ID', editRow).val(comment_id);
403 if ( action == 'edit' ) {
404 $('#author', editRow).val( $('div.author', rowData).text() );
405 $('#author-email', editRow).val( $('div.author-email', rowData).text() );
406 $('#author-url', editRow).val( $('div.author-url', rowData).text() );
407 $('#status', editRow).val( $('div.comment_status', rowData).text() );
408 $('#replycontent', editRow).val( $('textarea.comment', rowData).val() );
409 $('#edithead, #savebtn', editRow).show();
410 $('#replyhead, #replybtn, #addhead, #addbtn', editRow).hide();
413 // Limit the maximum height when editing very long comments to make it more manageable.
414 // The textarea is resizable in most browsers, so the user can adjust it if needed.
415 editHeight = h > 500 ? 500 : h;
416 $('#replycontent', editRow).css('height', editHeight + 'px');
419 c.after( editRow ).fadeOut('fast', function(){
420 $('#replyrow').fadeIn(300, function(){ $(this).show(); });
422 } else if ( action == 'add' ) {
423 $('#addhead, #addbtn', editRow).show();
424 $('#replyhead, #replybtn, #edithead, #editbtn', editRow).hide();
425 $('#the-comment-list').prepend(editRow);
426 $('#replyrow').fadeIn(300);
428 replyButton = $('#replybtn', editRow);
429 $('#edithead, #savebtn, #addhead, #addbtn', editRow).hide();
430 $('#replyhead, #replybtn', editRow).show();
433 if ( c.hasClass('unapproved') ) {
434 replyButton.text(adminCommentsL10n.replyApprove);
436 replyButton.text(adminCommentsL10n.reply);
439 $('#replyrow').fadeIn(300, function(){ $(this).show(); });
442 setTimeout(function() {
443 var rtop, rbottom, scrollTop, vp, scrollBottom;
445 rtop = $('#replyrow').offset().top;
446 rbottom = rtop + $('#replyrow').height();
447 scrollTop = window.pageYOffset || document.documentElement.scrollTop;
448 vp = document.documentElement.clientHeight || window.innerHeight || 0;
449 scrollBottom = scrollTop + vp;
451 if ( scrollBottom - 20 < rbottom )
452 window.scroll(0, rbottom - vp + 35);
453 else if ( rtop - 20 < scrollTop )
454 window.scroll(0, rtop - 35);
456 $('#replycontent').focus().keyup(function(e){
458 commentReply.revert(); // close on Escape
468 $('#replysubmit .error').hide();
469 $('#replysubmit .spinner').show();
471 $('#replyrow input').not(':button').each(function() {
473 post[ t.attr('name') ] = t.val();
476 post.content = $('#replycontent').val();
477 post.id = post.comment_post_ID;
478 post.comments_listing = this.comments_listing;
479 post.p = $('[name="p"]').val();
481 if ( $('#comment-' + $('#comment_ID').val()).hasClass('unapproved') )
482 post.approve_parent = 1;
488 success : function(x) { commentReply.show(x); },
489 error : function(r) { commentReply.error(r); }
495 show : function(xml) {
496 var t = this, r, c, id, bg, pid;
498 if ( typeof(xml) == 'string' ) {
499 t.error({'responseText': xml});
503 r = wpAjax.parseAjaxResponse(xml);
505 t.error({'responseText': wpAjax.broken});
512 id = '#comment-' + r.id;
514 if ( 'edit-comment' == t.act )
517 if ( r.supplemental.parent_approved ) {
518 pid = $('#comment-' + r.supplemental.parent_approved);
521 if ( this.comments_listing == 'moderated' ) {
522 pid.animate( { 'backgroundColor':'#CCEEBB' }, 400, function(){
529 c = $.trim(r.data); // Trim leading whitespaces
531 $('#replyrow').after(c);
535 bg = id.hasClass('unapproved') ? '#FFFFE0' : id.closest('.widefat, .postbox').css('backgroundColor');
537 id.animate( { 'backgroundColor':'#CCEEBB' }, 300 )
538 .animate( { 'backgroundColor': bg }, 300, function() {
539 if ( pid && pid.length ) {
540 pid.animate( { 'backgroundColor':'#CCEEBB' }, 300 )
541 .animate( { 'backgroundColor': bg }, 300 )
542 .removeClass('unapproved').addClass('approved')
543 .find('div.comment_status').html('1');
549 error : function(r) {
550 var er = r.statusText;
552 $('#replysubmit .spinner').hide();
554 if ( r.responseText )
555 er = r.responseText.replace( /<.[^<>]*?>/g, '' );
558 $('#replysubmit .error').html(er).show();
562 addcomment: function(post_id) {
565 $('#add-new-comment').fadeOut(200, function(){
566 t.open(0, post_id, 'add');
567 $('table.comments-box').css('display', '');
568 $('#no-comments').remove();
573 $(document).ready(function(){
574 var make_hotkeys_redirect, edit_comment, toggle_all, make_bulk;
578 $(document).delegate('span.delete a.delete', 'click', function(){return false;});
580 if ( typeof $.table_hotkeys != 'undefined' ) {
581 make_hotkeys_redirect = function(which) {
585 first_last = 'next' == which? 'first' : 'last';
586 l = $('.tablenav-pages .'+which+'-page:not(.disabled)');
588 window.location = l[0].href.replace(/\&hotkeys_highlight_(first|last)=1/g, '')+'&hotkeys_highlight_'+first_last+'=1';
592 edit_comment = function(event, current_row) {
593 window.location = $('span.edit a', current_row).attr('href');
596 toggle_all = function() {
597 toggleWithKeyboard = true;
598 $('input:checkbox', '#cb').click().prop('checked', false);
599 toggleWithKeyboard = false;
602 make_bulk = function(value) {
604 var scope = $('select[name="action"]');
605 $('option[value="' + value + '"]', scope).prop('selected', true);
606 $('#doaction').click();
612 ['a', 'u', 's', 'd', 'r', 'q', 'z', ['e', edit_comment], ['shift+x', toggle_all],
613 ['shift+a', make_bulk('approve')], ['shift+s', make_bulk('spam')],
614 ['shift+d', make_bulk('delete')], ['shift+t', make_bulk('trash')],
615 ['shift+z', make_bulk('untrash')], ['shift+u', make_bulk('unapprove')]],
616 { highlight_first: adminCommentsL10n.hotkeys_highlight_first, highlight_last: adminCommentsL10n.hotkeys_highlight_last,
617 prev_page_link_cb: make_hotkeys_redirect('prev'), next_page_link_cb: make_hotkeys_redirect('next') }