1 /* global adminCommentsL10n, thousandsSeparator, list_args, QTags, ajaxurl, wpAjax */
2 var setCommentsList, theList, theExtraList, commentReply;
5 var getCount, updateCount, updatePending;
7 setCommentsList = function() {
8 var totalInput, perPageInput, pageInput, dimAfter, delBefore, updateTotalCount, delAfter, refillTheExtraList, diff,
11 totalInput = $('input[name="_total"]', '#comments-form');
12 perPageInput = $('input[name="_per_page"]', '#comments-form');
13 pageInput = $('input[name="_page"]', '#comments-form');
15 dimAfter = function( r, settings ) {
16 var editRow, replyID, replyButton,
17 c = $( '#' + settings.element );
19 editRow = $('#replyrow');
20 replyID = $('#comment_ID', editRow).val();
21 replyButton = $('#replybtn', editRow);
23 if ( c.is('.unapproved') ) {
24 if ( settings.data.id == replyID )
25 replyButton.text(adminCommentsL10n.replyApprove);
27 c.find('div.comment_status').html('0');
29 if ( settings.data.id == replyID )
30 replyButton.text(adminCommentsL10n.reply);
32 c.find('div.comment_status').html('1');
35 diff = $('#' + settings.element).is('.' + settings.dimClass) ? 1 : -1;
36 updatePending( diff );
39 // Send current total, page, per_page and url
40 delBefore = function( settings, list ) {
41 var note, id, el, n, h, a, author,
43 wpListsData = $( settings.target ).attr( 'data-wp-lists' );
45 settings.data._total = totalInput.val() || 0;
46 settings.data._per_page = perPageInput.val() || 0;
47 settings.data._page = pageInput.val() || 0;
48 settings.data._url = document.location.href;
49 settings.data.comment_status = $('input[name="comment_status"]', '#comments-form').val();
51 if ( wpListsData.indexOf(':trash=1') != -1 )
53 else if ( wpListsData.indexOf(':spam=1') != -1 )
57 id = wpListsData.replace(/.*?comment-([0-9]+).*/, '$1');
58 el = $('#comment-' + id);
59 note = $('#' + action + '-undo-holder').html();
61 el.find('.check-column :checkbox').prop('checked', false); // Uncheck the row so as not to be affected by Bulk Edits.
63 if ( el.siblings('#replyrow').length && commentReply.cid == id )
67 n = el.children(':visible').length;
68 author = $('.author strong', el).text();
69 h = $('<tr id="undo-' + id + '" class="undo un' + action + '" style="display:none;"><td colspan="' + n + '">' + note + '</td></tr>');
71 author = $('.comment-author', el).text();
72 h = $('<div id="undo-' + id + '" style="display:none;" class="undo un' + action + '">' + note + '</div>');
77 $('strong', '#undo-' + id).text(author);
78 a = $('.undo a', '#undo-' + id);
79 a.attr('href', 'comment.php?action=un' + action + 'comment&c=' + id + '&_wpnonce=' + settings.data._ajax_nonce);
80 a.attr('data-wp-lists', 'delete:the-comment-list:comment-' + id + '::un' + action + '=1');
81 a.attr('class', 'vim-z vim-destructive');
82 $('.avatar', el).first().clone().prependTo('#undo-' + id + ' .' + action + '-undo-inside');
85 list.wpList.del(this);
86 $('#undo-' + id).css( {backgroundColor:'#ceb'} ).fadeOut(350, function(){
88 $('#comment-' + id).css('backgroundColor', '').fadeIn(300, function(){ $(this).show(); });
97 // Updates the current total (stored in the _total input)
98 updateTotalCount = function( total, time, setConfidentTime ) {
99 if ( time < lastConfidentTime )
102 if ( setConfidentTime )
103 lastConfidentTime = time;
105 totalInput.val( total.toString() );
108 getCount = function(el) {
109 var n = parseInt( el.html().replace(/[^0-9]+/g, ''), 10 );
115 updateCount = function(el, n) {
119 n = n < 1 ? '0' : n.toString();
120 if ( n.length > 3 ) {
121 while ( n.length > 3 ) {
122 n1 = thousandsSeparator + n.substr(n.length - 3) + n1;
123 n = n.substr(0, n.length - 3);
130 updatePending = function( diff ) {
131 $('span.pending-count').each(function() {
132 var a = $(this), n = getCount(a) + diff;
135 a.closest('.awaiting-mod')[ 0 === n ? 'addClass' : 'removeClass' ]('count-0');
140 // In admin-ajax.php, we send back the unix time stamp instead of 1 on success
141 delAfter = function( r, settings ) {
142 var total_items_i18n, total, spam, trash, pending,
143 untrash = $(settings.target).parent().is('span.untrash'),
144 unspam = $(settings.target).parent().is('span.unspam'),
145 unapproved = $('#' + settings.element).is('.unapproved');
147 function getUpdate(s) {
148 if ( $(settings.target).parent().is('span.' + s) )
150 else if ( $('#' + settings.element).is('.' + s) )
159 trash = getUpdate('trash');
164 spam = getUpdate('spam');
166 if ( $(settings.target).parent().is('span.unapprove') || ( ( untrash || unspam ) && unapproved ) ) {
167 // a comment was 'deleted' from another list (e.g. approved, spam, trash) and moved to pending,
168 // or a trash/spam of a pending comment was undone
170 } else if ( unapproved ) {
171 // a pending comment was trashed/spammed/approved
176 updatePending(pending);
178 $('span.spam-count').each( function() {
179 var a = $(this), n = getCount(a) + spam;
183 $('span.trash-count').each( function() {
184 var a = $(this), n = getCount(a) + trash;
188 if ( ! $('#dashboard_right_now').length ) {
189 total = totalInput.val() ? parseInt( totalInput.val(), 10 ) : 0;
190 if ( $(settings.target).parent().is('span.undo') )
198 if ( ( 'object' == typeof r ) && lastConfidentTime < settings.parsed.responses[0].supplemental.time ) {
199 total_items_i18n = settings.parsed.responses[0].supplemental.total_items_i18n || '';
200 if ( total_items_i18n ) {
201 $('.displaying-num').text( total_items_i18n );
202 $('.total-pages').text( settings.parsed.responses[0].supplemental.total_pages_i18n );
203 $('.tablenav-pages').find('.next-page, .last-page').toggleClass('disabled', settings.parsed.responses[0].supplemental.total_pages == $('.current-page').val());
205 updateTotalCount( total, settings.parsed.responses[0].supplemental.time, true );
207 updateTotalCount( total, r, false );
211 if ( ! theExtraList || theExtraList.size() === 0 || theExtraList.children().size() === 0 || untrash || unspam ) {
215 theList.get(0).wpList.add( theExtraList.children(':eq(0)').remove().clone() );
217 refillTheExtraList();
220 refillTheExtraList = function(ev) {
221 var args = $.query.get(), total_pages = $('.total-pages').text(), per_page = $('input[name="_per_page"]', '#comments-form').val();
226 if (args.paged > total_pages) {
231 theExtraList.empty();
232 args.number = Math.min(8, per_page); // see WP_Comments_List_Table::prepare_items() @ class-wp-comments-list-table.php
235 args.offset = Math.min(8, per_page) - 1; // fetch only the next item on the extra list
238 args.no_placeholder = true;
242 // $.query.get() needs some correction to be sent into an ajax request
243 if ( true === args.comment_type )
244 args.comment_type = '';
246 args = $.extend(args, {
247 'action': 'fetch-list',
248 'list_args': list_args,
249 '_ajax_fetch_list_nonce': $('#_ajax_fetch_list_nonce').val()
257 success: function(response) {
258 theExtraList.get(0).wpList.add( response.rows );
263 theExtraList = $('#the-extra-comment-list').wpList( { alt: '', delColor: 'none', addColor: 'none' } );
264 theList = $('#the-comment-list').wpList( { alt: '', delBefore: delBefore, dimAfter: dimAfter, delAfter: delAfter, addColor: 'none' } )
265 .bind('wpListDelEnd', function(e, s){
266 var wpListsData = $(s.target).attr('data-wp-lists'), id = s.element.replace(/[^0-9]+/g, '');
268 if ( wpListsData.indexOf(':trash=1') != -1 || wpListsData.indexOf(':spam=1') != -1 )
269 $('#undo-' + id).fadeIn(300, function(){ $(this).show(); });
278 var row = $('#replyrow');
280 $('a.cancel', row).click(function() { return commentReply.revert(); });
281 $('a.save', row).click(function() { return commentReply.send(); });
282 $('input#author, input#author-email, input#author-url', row).keypress(function(e){
283 if ( e.which == 13 ) {
291 $('#the-comment-list .column-comment > p').dblclick(function(){
292 commentReply.toggle($(this).parent());
295 $('#doaction, #doaction2, #post-query-submit').click(function(){
296 if ( $('#the-comment-list #replyrow').length > 0 )
297 commentReply.close();
300 this.comments_listing = $('#comments-form > input[name="comment_status"]').val() || '';
302 /* $(listTable).bind('beforeChangePage', function(){
303 commentReply.close();
307 addEvents : function(r) {
309 $(this).find('.column-comment > p').dblclick(function(){
310 commentReply.toggle($(this).parent());
315 toggle : function(el) {
316 if ( 'none' !== $( el ).css( 'display' ) && ( $( '#replyrow' ).parent().is('#com-reply') || window.confirm( adminCommentsL10n.warnQuickEdit ) ) ) {
317 $( el ).find( 'a.vim-q' ).click();
321 revert : function() {
323 if ( $('#the-comment-list #replyrow').length < 1 )
326 $('#replyrow').fadeOut('fast', function(){
327 commentReply.close();
334 var c, replyrow = $('#replyrow');
336 // replyrow is not showing?
337 if ( replyrow.parent().is('#com-reply') )
340 if ( this.cid && this.act == 'edit-comment' ) {
341 c = $('#comment-' + this.cid);
342 c.fadeIn(300, function(){ c.show(); }).css('backgroundColor', '');
345 // reset the Quicktags buttons
346 if ( typeof QTags != 'undefined' )
347 QTags.closeAllTags('replycontent');
349 $('#add-new-comment').css('display', '');
352 $('#com-reply').append( replyrow );
353 $('#replycontent').css('height', '').val('');
354 $('#edithead input').val('');
355 $('.error', replyrow).empty().hide();
356 $( '.spinner', replyrow ).removeClass( 'is-active' );
361 open : function(comment_id, post_id, action) {
362 var editRow, rowData, act, replyButton, editHeight,
364 c = $('#comment-' + comment_id),
371 editRow = $('#replyrow');
372 rowData = $('#inline-'+comment_id);
373 action = action || 'replyto';
374 act = 'edit' == action ? 'edit' : 'replyto';
375 act = t.act = act + '-comment';
376 colspanVal = $( 'th:visible, td:visible', c ).length;
378 // Make sure it's actually a table and there's a `colspan` value to apply.
379 if ( editRow.hasClass( 'inline-edit-row' ) && 0 !== colspanVal ) {
380 $( 'td', editRow ).attr( 'colspan', colspanVal );
383 $('#action', editRow).val(act);
384 $('#comment_post_ID', editRow).val(post_id);
385 $('#comment_ID', editRow).val(comment_id);
387 if ( action == 'edit' ) {
388 $('#author', editRow).val( $('div.author', rowData).text() );
389 $('#author-email', editRow).val( $('div.author-email', rowData).text() );
390 $('#author-url', editRow).val( $('div.author-url', rowData).text() );
391 $('#status', editRow).val( $('div.comment_status', rowData).text() );
392 $('#replycontent', editRow).val( $('textarea.comment', rowData).val() );
393 $('#edithead, #savebtn', editRow).show();
394 $('#replyhead, #replybtn, #addhead, #addbtn', editRow).hide();
397 // Limit the maximum height when editing very long comments to make it more manageable.
398 // The textarea is resizable in most browsers, so the user can adjust it if needed.
399 editHeight = h > 500 ? 500 : h;
400 $('#replycontent', editRow).css('height', editHeight + 'px');
403 c.after( editRow ).fadeOut('fast', function(){
404 $('#replyrow').fadeIn(300, function(){ $(this).show(); });
406 } else if ( action == 'add' ) {
407 $('#addhead, #addbtn', editRow).show();
408 $('#replyhead, #replybtn, #edithead, #savebtn', editRow).hide();
409 $('#the-comment-list').prepend(editRow);
410 $('#replyrow').fadeIn(300);
412 replyButton = $('#replybtn', editRow);
413 $('#edithead, #savebtn, #addhead, #addbtn', editRow).hide();
414 $('#replyhead, #replybtn', editRow).show();
417 if ( c.hasClass('unapproved') ) {
418 replyButton.text(adminCommentsL10n.replyApprove);
420 replyButton.text(adminCommentsL10n.reply);
423 $('#replyrow').fadeIn(300, function(){ $(this).show(); });
426 setTimeout(function() {
427 var rtop, rbottom, scrollTop, vp, scrollBottom;
429 rtop = $('#replyrow').offset().top;
430 rbottom = rtop + $('#replyrow').height();
431 scrollTop = window.pageYOffset || document.documentElement.scrollTop;
432 vp = document.documentElement.clientHeight || window.innerHeight || 0;
433 scrollBottom = scrollTop + vp;
435 if ( scrollBottom - 20 < rbottom )
436 window.scroll(0, rbottom - vp + 35);
437 else if ( rtop - 20 < scrollTop )
438 window.scroll(0, rtop - 35);
440 $('#replycontent').focus().keyup(function(e){
442 commentReply.revert(); // close on Escape
452 $('#replysubmit .error').hide();
453 $( '#replysubmit .spinner' ).addClass( 'is-active' );
455 $('#replyrow input').not(':button').each(function() {
457 post[ t.attr('name') ] = t.val();
460 post.content = $('#replycontent').val();
461 post.id = post.comment_post_ID;
462 post.comments_listing = this.comments_listing;
463 post.p = $('[name="p"]').val();
465 if ( $('#comment-' + $('#comment_ID').val()).hasClass('unapproved') )
466 post.approve_parent = 1;
472 success : function(x) { commentReply.show(x); },
473 error : function(r) { commentReply.error(r); }
479 show : function(xml) {
480 var t = this, r, c, id, bg, pid;
482 if ( typeof(xml) == 'string' ) {
483 t.error({'responseText': xml});
487 r = wpAjax.parseAjaxResponse(xml);
489 t.error({'responseText': wpAjax.broken});
496 id = '#comment-' + r.id;
498 if ( 'edit-comment' == t.act )
501 if ( r.supplemental.parent_approved ) {
502 pid = $('#comment-' + r.supplemental.parent_approved);
505 if ( this.comments_listing == 'moderated' ) {
506 pid.animate( { 'backgroundColor':'#CCEEBB' }, 400, function(){
513 c = $.trim(r.data); // Trim leading whitespaces
515 $('#replyrow').after(c);
519 bg = id.hasClass('unapproved') ? '#FFFFE0' : id.closest('.widefat, .postbox').css('backgroundColor');
521 id.animate( { 'backgroundColor':'#CCEEBB' }, 300 )
522 .animate( { 'backgroundColor': bg }, 300, function() {
523 if ( pid && pid.length ) {
524 pid.animate( { 'backgroundColor':'#CCEEBB' }, 300 )
525 .animate( { 'backgroundColor': bg }, 300 )
526 .removeClass('unapproved').addClass('approved')
527 .find('div.comment_status').html('1');
533 error : function(r) {
534 var er = r.statusText;
536 $( '#replysubmit .spinner' ).removeClass( 'is-active' );
538 if ( r.responseText )
539 er = r.responseText.replace( /<.[^<>]*?>/g, '' );
542 $('#replysubmit .error').html(er).show();
546 addcomment: function(post_id) {
549 $('#add-new-comment').fadeOut(200, function(){
550 t.open(0, post_id, 'add');
551 $('table.comments-box').css('display', '');
552 $('#no-comments').remove();
557 $(document).ready(function(){
558 var make_hotkeys_redirect, edit_comment, toggle_all, make_bulk;
562 $(document).delegate('span.delete a.delete', 'click', function(){return false;});
564 if ( typeof $.table_hotkeys != 'undefined' ) {
565 make_hotkeys_redirect = function(which) {
569 first_last = 'next' == which? 'first' : 'last';
570 l = $('.tablenav-pages .'+which+'-page:not(.disabled)');
572 window.location = l[0].href.replace(/\&hotkeys_highlight_(first|last)=1/g, '')+'&hotkeys_highlight_'+first_last+'=1';
576 edit_comment = function(event, current_row) {
577 window.location = $('span.edit a', current_row).attr('href');
580 toggle_all = function() {
581 $('#cb-select-all-1').data( 'wp-toggle', 1 ).trigger( 'click' ).removeData( 'wp-toggle' );
584 make_bulk = function(value) {
586 var scope = $('select[name="action"]');
587 $('option[value="' + value + '"]', scope).prop('selected', true);
588 $('#doaction').click();
595 'a', 'u', 's', 'd', 'r', 'q', 'z',
597 ['shift+x', toggle_all],
598 ['shift+a', make_bulk('approve')],
599 ['shift+s', make_bulk('spam')],
600 ['shift+d', make_bulk('delete')],
601 ['shift+t', make_bulk('trash')],
602 ['shift+z', make_bulk('untrash')],
603 ['shift+u', make_bulk('unapprove')]
606 highlight_first: adminCommentsL10n.hotkeys_highlight_first,
607 highlight_last: adminCommentsL10n.hotkeys_highlight_last,
608 prev_page_link_cb: make_hotkeys_redirect('prev'),
609 next_page_link_cb: make_hotkeys_redirect('next'),
611 disableInInput: true,
613 noDisable: '.check-column input[type="checkbox"]'
615 cycle_expr: '#the-comment-list tr',
621 // Quick Edit and Reply have an inline comment editor.
622 $( '#the-comment-list' ).on( 'click', '.comment-inline', function (e) {
627 if ( 'undefined' !== typeof $el.data( 'action' ) ) {
628 action = $el.data( 'action' );
631 commentReply.open( $el.data( 'commentId' ), $el.data( 'postId' ), action );