]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/edit-comments.js
WordPress 4.4.2
[autoinstalls/wordpress.git] / wp-admin / js / edit-comments.js
1 /* global adminCommentsL10n, thousandsSeparator, list_args, QTags, ajaxurl, wpAjax */
2 var setCommentsList, theList, theExtraList, commentReply;
3
4 (function($) {
5 var getCount, updateCount, updateCountText, updatePending, updateApproved,
6         updateHtmlTitle, updateDashboardText, adminTitle = document.title,
7         isDashboard = $('#dashboard_right_now').length,
8         titleDiv, titleRegEx;
9
10         getCount = function(el) {
11                 var n = parseInt( el.html().replace(/[^0-9]+/g, ''), 10 );
12                 if ( isNaN(n) ) {
13                         return 0;
14                 }
15                 return n;
16         };
17
18         updateCount = function(el, n) {
19                 var n1 = '';
20                 if ( isNaN(n) ) {
21                         return;
22                 }
23                 n = n < 1 ? '0' : n.toString();
24                 if ( n.length > 3 ) {
25                         while ( n.length > 3 ) {
26                                 n1 = thousandsSeparator + n.substr(n.length - 3) + n1;
27                                 n = n.substr(0, n.length - 3);
28                         }
29                         n = n + n1;
30                 }
31                 el.html(n);
32         };
33
34         updateApproved = function( diff, commentPostId ) {
35                 var postSelector = '.post-com-count-' + commentPostId,
36                         noClass = 'comment-count-no-comments',
37                         approvedClass = 'comment-count-approved',
38                         approved,
39                         noComments;
40
41                 updateCountText( 'span.approved-count', diff );
42
43                 if ( ! commentPostId ) {
44                         return;
45                 }
46
47                 // cache selectors to not get dupes
48                 approved = $( 'span.' + approvedClass, postSelector );
49                 noComments = $( 'span.' + noClass, postSelector );
50
51                 approved.each(function() {
52                         var a = $(this), n = getCount(a) + diff;
53                         if ( n < 1 )
54                                 n = 0;
55
56                         if ( 0 === n ) {
57                                 a.removeClass( approvedClass ).addClass( noClass );
58                         } else {
59                                 a.addClass( approvedClass ).removeClass( noClass );
60                         }
61                         updateCount( a, n );
62                 });
63
64                 noComments.each(function() {
65                         var a = $(this);
66                         if ( diff > 0 ) {
67                                 a.removeClass( noClass ).addClass( approvedClass );
68                         } else {
69                                 a.addClass( noClass ).removeClass( approvedClass );
70                         }
71                         updateCount( a, diff );
72                 });
73         };
74
75         updateCountText = function( selector, diff ) {
76                 $( selector ).each(function() {
77                         var a = $(this), n = getCount(a) + diff;
78                         if ( n < 1 ) {
79                                 n = 0;
80                         }
81                         updateCount( a, n );
82                 });
83         };
84
85         updateDashboardText = function ( response ) {
86                 if ( ! isDashboard || ! response || ! response.i18n_comments_text ) {
87                         return;
88                 }
89
90                 var rightNow = $( '#dashboard_right_now' );
91
92                 $( '.comment-count a', rightNow ).text( response.i18n_comments_text );
93                 $( '.comment-mod-count a', rightNow ).text( response.i18n_moderation_text )
94                         .parent()
95                         [ response.in_moderation > 0 ? 'removeClass' : 'addClass' ]( 'hidden' );
96         };
97
98         updateHtmlTitle = function ( diff ) {
99                 var newTitle, regExMatch, titleCount, commentFrag;
100
101                 titleRegEx = titleRegEx || new RegExp( adminCommentsL10n.docTitleCommentsCount.replace( '%s', '\\([0-9' + thousandsSeparator + ']+\\)' ) + '?' );
102                 // count funcs operate on a $'d element
103                 titleDiv = titleDiv || $( '<div />' );
104                 newTitle = adminTitle;
105
106                 commentFrag = titleRegEx.exec( document.title );
107                 if ( commentFrag ) {
108                         commentFrag = commentFrag[0];
109                         titleDiv.html( commentFrag );
110                         titleCount = getCount( titleDiv ) + diff;
111                 } else {
112                         titleDiv.html( 0 );
113                         titleCount = diff;
114                 }
115
116                 if ( titleCount >= 1 ) {
117                         updateCount( titleDiv, titleCount );
118                         regExMatch = titleRegEx.exec( document.title );
119                         if ( regExMatch ) {
120                                 newTitle = document.title.replace( regExMatch[0], adminCommentsL10n.docTitleCommentsCount.replace( '%s', titleDiv.text() ) + ' ' );
121                         }
122                 } else {
123                         regExMatch = titleRegEx.exec( newTitle );
124                         if ( regExMatch ) {
125                                 newTitle = newTitle.replace( regExMatch[0], adminCommentsL10n.docTitleComments );
126                         }
127                 }
128                 document.title = newTitle;
129         };
130
131         updatePending = function( diff, commentPostId ) {
132                 var postSelector = '.post-com-count-' + commentPostId,
133                         noClass = 'comment-count-no-pending',
134                         noParentClass = 'post-com-count-no-pending',
135                         pendingClass = 'comment-count-pending',
136                         pending,
137                         noPending;
138
139                 if ( ! isDashboard ) {
140                         updateHtmlTitle( diff );
141                 }
142
143                 $( 'span.pending-count' ).each(function() {
144                         var a = $(this), n = getCount(a) + diff;
145                         if ( n < 1 )
146                                 n = 0;
147                         a.closest('.awaiting-mod')[ 0 === n ? 'addClass' : 'removeClass' ]('count-0');
148                         updateCount( a, n );
149                 });
150
151                 if ( ! commentPostId ) {
152                         return;
153                 }
154
155                 // cache selectors to not get dupes
156                 pending = $( 'span.' + pendingClass, postSelector );
157                 noPending = $( 'span.' + noClass, postSelector );
158
159                 pending.each(function() {
160                         var a = $(this), n = getCount(a) + diff;
161                         if ( n < 1 )
162                                 n = 0;
163
164                         if ( 0 === n ) {
165                                 a.parent().addClass( noParentClass );
166                                 a.removeClass( pendingClass ).addClass( noClass );
167                         } else {
168                                 a.parent().removeClass( noParentClass );
169                                 a.addClass( pendingClass ).removeClass( noClass );
170                         }
171                         updateCount( a, n );
172                 });
173
174                 noPending.each(function() {
175                         var a = $(this);
176                         if ( diff > 0 ) {
177                                 a.parent().removeClass( noParentClass );
178                                 a.removeClass( noClass ).addClass( pendingClass );
179                         } else {
180                                 a.parent().addClass( noParentClass );
181                                 a.addClass( noClass ).removeClass( pendingClass );
182                         }
183                         updateCount( a, diff );
184                 });
185         };
186
187 setCommentsList = function() {
188         var totalInput, perPageInput, pageInput, dimAfter, delBefore, updateTotalCount, delAfter, refillTheExtraList, diff,
189                 lastConfidentTime = 0;
190
191         totalInput = $('input[name="_total"]', '#comments-form');
192         perPageInput = $('input[name="_per_page"]', '#comments-form');
193         pageInput = $('input[name="_page"]', '#comments-form');
194
195         // Updates the current total (stored in the _total input)
196         updateTotalCount = function( total, time, setConfidentTime ) {
197                 if ( time < lastConfidentTime )
198                         return;
199
200                 if ( setConfidentTime )
201                         lastConfidentTime = time;
202
203                 totalInput.val( total.toString() );
204         };
205
206         // this fires when viewing "All"
207         dimAfter = function( r, settings ) {
208                 var editRow, replyID, replyButton, response,
209                         c = $( '#' + settings.element );
210
211                 if ( true !== settings.parsed ) {
212                         response = settings.parsed.responses[0];
213                 }
214
215                 editRow = $('#replyrow');
216                 replyID = $('#comment_ID', editRow).val();
217                 replyButton = $('#replybtn', editRow);
218
219                 if ( c.is('.unapproved') ) {
220                         if ( settings.data.id == replyID )
221                                 replyButton.text(adminCommentsL10n.replyApprove);
222
223                         c.find('div.comment_status').html('0');
224                 } else {
225                         if ( settings.data.id == replyID )
226                                 replyButton.text(adminCommentsL10n.reply);
227
228                         c.find('div.comment_status').html('1');
229                 }
230
231                 diff = $('#' + settings.element).is('.' + settings.dimClass) ? 1 : -1;
232                 if ( response ) {
233                         updateDashboardText( response.supplemental );
234                         updatePending( diff, response.supplemental.postId );
235                         updateApproved( -1 * diff, response.supplemental.postId );
236                 } else {
237                         updatePending( diff );
238                         updateApproved( -1 * diff  );
239                 }
240         };
241
242         // Send current total, page, per_page and url
243         delBefore = function( settings, list ) {
244                 var note, id, el, n, h, a, author,
245                         action = false,
246                         wpListsData = $( settings.target ).attr( 'data-wp-lists' );
247
248                 settings.data._total = totalInput.val() || 0;
249                 settings.data._per_page = perPageInput.val() || 0;
250                 settings.data._page = pageInput.val() || 0;
251                 settings.data._url = document.location.href;
252                 settings.data.comment_status = $('input[name="comment_status"]', '#comments-form').val();
253
254                 if ( wpListsData.indexOf(':trash=1') != -1 )
255                         action = 'trash';
256                 else if ( wpListsData.indexOf(':spam=1') != -1 )
257                         action = 'spam';
258
259                 if ( action ) {
260                         id = wpListsData.replace(/.*?comment-([0-9]+).*/, '$1');
261                         el = $('#comment-' + id);
262                         note = $('#' + action + '-undo-holder').html();
263
264                         el.find('.check-column :checkbox').prop('checked', false); // Uncheck the row so as not to be affected by Bulk Edits.
265
266                         if ( el.siblings('#replyrow').length && commentReply.cid == id )
267                                 commentReply.close();
268
269                         if ( el.is('tr') ) {
270                                 n = el.children(':visible').length;
271                                 author = $('.author strong', el).text();
272                                 h = $('<tr id="undo-' + id + '" class="undo un' + action + '" style="display:none;"><td colspan="' + n + '">' + note + '</td></tr>');
273                         } else {
274                                 author = $('.comment-author', el).text();
275                                 h = $('<div id="undo-' + id + '" style="display:none;" class="undo un' + action + '">' + note + '</div>');
276                         }
277
278                         el.before(h);
279
280                         $('strong', '#undo-' + id).text(author);
281                         a = $('.undo a', '#undo-' + id);
282                         a.attr('href', 'comment.php?action=un' + action + 'comment&c=' + id + '&_wpnonce=' + settings.data._ajax_nonce);
283                         a.attr('data-wp-lists', 'delete:the-comment-list:comment-' + id + '::un' + action + '=1');
284                         a.attr('class', 'vim-z vim-destructive');
285                         $('.avatar', el).first().clone().prependTo('#undo-' + id + ' .' + action + '-undo-inside');
286
287                         a.click(function( e ){
288                                 e.preventDefault();
289                                 list.wpList.del(this);
290                                 $('#undo-' + id).css( {backgroundColor:'#ceb'} ).fadeOut(350, function(){
291                                         $(this).remove();
292                                         $('#comment-' + id).css('backgroundColor', '').fadeIn(300, function(){ $(this).show(); });
293                                 });
294                         });
295                 }
296
297                 return settings;
298         };
299
300         // In admin-ajax.php, we send back the unix time stamp instead of 1 on success
301         delAfter = function( r, settings ) {
302                 var total_items_i18n, total, animated, animatedCallback,
303                         response = true === settings.parsed ? {} : settings.parsed.responses[0],
304                         commentStatus = true === settings.parsed ? '' : response.supplemental.status,
305                         commentPostId = true === settings.parsed ? '' : response.supplemental.postId,
306                         newTotal = true === settings.parsed ? '' : response.supplemental,
307
308                         targetParent = $( settings.target ).parent(),
309                         commentRow = $('#' + settings.element),
310
311                         spamDiff, trashDiff, pendingDiff, approvedDiff,
312
313                         approved = commentRow.hasClass( 'approved' ),
314                         unapproved = commentRow.hasClass( 'unapproved' ),
315                         spammed = commentRow.hasClass( 'spam' ),
316                         trashed = commentRow.hasClass( 'trash' );
317
318                 updateDashboardText( newTotal );
319
320                 // the order of these checks is important
321                 // .unspam can also have .approve or .unapprove
322                 // .untrash can also have .approve or .unapprove
323
324                 if ( targetParent.is( 'span.undo' ) ) {
325                         // the comment was spammed
326                         if ( targetParent.hasClass( 'unspam' ) ) {
327                                 spamDiff = -1;
328
329                                 if ( 'trash' === commentStatus ) {
330                                         trashDiff = 1;
331                                 } else if ( '1' === commentStatus ) {
332                                         approvedDiff = 1;
333                                 } else if ( '0' === commentStatus ) {
334                                         pendingDiff = 1;
335                                 }
336
337                         // the comment was trashed
338                         } else if ( targetParent.hasClass( 'untrash' ) ) {
339                                 trashDiff = -1;
340
341                                 if ( 'spam' === commentStatus ) {
342                                         spamDiff = 1;
343                                 } else if ( '1' === commentStatus ) {
344                                         approvedDiff = 1;
345                                 } else if ( '0' === commentStatus ) {
346                                         pendingDiff = 1;
347                                 }
348                         }
349
350                 // user clicked "Spam"
351                 } else if ( targetParent.is( 'span.spam' ) ) {
352                         // the comment is currently approved
353                         if ( approved ) {
354                                 approvedDiff = -1;
355                         // the comment is currently pending
356                         } else if ( unapproved ) {
357                                 pendingDiff = -1;
358                         // the comment was in the trash
359                         } else if ( trashed ) {
360                                 trashDiff = -1;
361                         }
362                         // you can't spam an item on the spam screen
363                         spamDiff = 1;
364
365                 // user clicked "Unspam"
366                 } else if ( targetParent.is( 'span.unspam' ) ) {
367                         if ( approved ) {
368                                 pendingDiff = 1;
369                         } else if ( unapproved ) {
370                                 approvedDiff = 1;
371                         } else if ( trashed ) {
372                                 // the comment was previously approved
373                                 if ( targetParent.hasClass( 'approve' ) ) {
374                                         approvedDiff = 1;
375                                 // the comment was previously pending
376                                 } else if ( targetParent.hasClass( 'unapprove' ) ) {
377                                         pendingDiff = 1;
378                                 }
379                         } else if ( spammed ) {
380                                 if ( targetParent.hasClass( 'approve' ) ) {
381                                         approvedDiff = 1;
382
383                                 } else if ( targetParent.hasClass( 'unapprove' ) ) {
384                                         pendingDiff = 1;
385                                 }
386                         }
387                         // you can Unspam an item on the spam screen
388                         spamDiff = -1;
389
390                 // user clicked "Trash"
391                 } else if ( targetParent.is( 'span.trash' ) ) {
392                         if ( approved ) {
393                                 approvedDiff = -1;
394                         } else if ( unapproved ) {
395                                 pendingDiff = -1;
396                         // the comment was in the spam queue
397                         } else if ( spammed ) {
398                                 spamDiff = -1;
399                         }
400                         // you can't trash an item on the trash screen
401                         trashDiff = 1;
402
403                 // user clicked "Restore"
404                 } else if ( targetParent.is( 'span.untrash' ) ) {
405                         if ( approved ) {
406                                 pendingDiff = 1;
407                         } else if ( unapproved ) {
408                                 approvedDiff = 1;
409                         } else if ( trashed ) {
410                                 if ( targetParent.hasClass( 'approve' ) ) {
411                                         approvedDiff = 1;
412                                 } else if ( targetParent.hasClass( 'unapprove' ) ) {
413                                         pendingDiff = 1;
414                                 }
415                         }
416                         // you can't go from trash to spam
417                         // you can untrash on the trash screen
418                         trashDiff = -1;
419
420                 // User clicked "Approve"
421                 } else if ( targetParent.is( 'span.approve:not(.unspam):not(.untrash)' ) ) {
422                         approvedDiff = 1;
423                         pendingDiff = -1;
424
425                 // User clicked "Unapprove"
426                 } else if ( targetParent.is( 'span.unapprove:not(.unspam):not(.untrash)' ) ) {
427                         approvedDiff = -1;
428                         pendingDiff = 1;
429
430                 // User clicked "Delete Permanently"
431                 } else if ( targetParent.is( 'span.delete' ) ) {
432                         if ( spammed ) {
433                                 spamDiff = -1;
434                         } else if ( trashed ) {
435                                 trashDiff = -1;
436                         }
437                 }
438
439                 if ( pendingDiff ) {
440                         updatePending( pendingDiff, commentPostId );
441                         updateCountText( 'span.all-count', pendingDiff );
442                 }
443
444                 if ( approvedDiff ) {
445                         updateApproved( approvedDiff, commentPostId );
446                         updateCountText( 'span.all-count', approvedDiff );
447                 }
448
449                 if ( spamDiff ) {
450                         updateCountText( 'span.spam-count', spamDiff );
451                 }
452
453                 if ( trashDiff ) {
454                         updateCountText( 'span.trash-count', trashDiff );
455                 }
456
457                 if ( ! isDashboard ) {
458                         total = totalInput.val() ? parseInt( totalInput.val(), 10 ) : 0;
459                         if ( $(settings.target).parent().is('span.undo') )
460                                 total++;
461                         else
462                                 total--;
463
464                         if ( total < 0 )
465                                 total = 0;
466
467                         if ( 'object' === typeof r ) {
468                                 if ( response.supplemental.total_items_i18n && lastConfidentTime < response.supplemental.time ) {
469                                         total_items_i18n = response.supplemental.total_items_i18n || '';
470                                         if ( total_items_i18n ) {
471                                                 $('.displaying-num').text( total_items_i18n );
472                                                 $('.total-pages').text( response.supplemental.total_pages_i18n );
473                                                 $('.tablenav-pages').find('.next-page, .last-page').toggleClass('disabled', response.supplemental.total_pages == $('.current-page').val());
474                                         }
475                                         updateTotalCount( total, response.supplemental.time, true );
476                                 } else if ( response.supplemental.time ) {
477                                         updateTotalCount( total, response.supplemental.time, false );
478                                 }
479                         } else {
480                                 updateTotalCount( total, r, false );
481                         }
482                 }
483
484                 if ( ! theExtraList || theExtraList.size() === 0 || theExtraList.children().size() === 0 ) {
485                         return;
486                 }
487
488                 theList.get(0).wpList.add( theExtraList.children( ':eq(0):not(.no-items)' ).remove().clone() );
489
490                 refillTheExtraList();
491
492                 animated = $( ':animated', '#the-comment-list' );
493                 animatedCallback = function () {
494                         if ( ! $( '#the-comment-list tr:visible' ).length ) {
495                                 theList.get(0).wpList.add( theExtraList.find( '.no-items' ).clone() );
496                         }
497                 };
498
499                 if ( animated.length ) {
500                         animated.promise().done( animatedCallback );
501                 } else {
502                         animatedCallback();
503                 }
504         };
505
506         refillTheExtraList = function(ev) {
507                 var args = $.query.get(), total_pages = $('.total-pages').text(), per_page = $('input[name="_per_page"]', '#comments-form').val();
508
509                 if (! args.paged)
510                         args.paged = 1;
511
512                 if (args.paged > total_pages) {
513                         return;
514                 }
515
516                 if (ev) {
517                         theExtraList.empty();
518                         args.number = Math.min(8, per_page); // see WP_Comments_List_Table::prepare_items() @ class-wp-comments-list-table.php
519                 } else {
520                         args.number = 1;
521                         args.offset = Math.min(8, per_page) - 1; // fetch only the next item on the extra list
522                 }
523
524                 args.no_placeholder = true;
525
526                 args.paged ++;
527
528                 // $.query.get() needs some correction to be sent into an ajax request
529                 if ( true === args.comment_type )
530                         args.comment_type = '';
531
532                 args = $.extend(args, {
533                         'action': 'fetch-list',
534                         'list_args': list_args,
535                         '_ajax_fetch_list_nonce': $('#_ajax_fetch_list_nonce').val()
536                 });
537
538                 $.ajax({
539                         url: ajaxurl,
540                         global: false,
541                         dataType: 'json',
542                         data: args,
543                         success: function(response) {
544                                 theExtraList.get(0).wpList.add( response.rows );
545                         }
546                 });
547         };
548
549         theExtraList = $('#the-extra-comment-list').wpList( { alt: '', delColor: 'none', addColor: 'none' } );
550         theList = $('#the-comment-list').wpList( { alt: '', delBefore: delBefore, dimAfter: dimAfter, delAfter: delAfter, addColor: 'none' } )
551                 .bind('wpListDelEnd', function(e, s){
552                         var wpListsData = $(s.target).attr('data-wp-lists'), id = s.element.replace(/[^0-9]+/g, '');
553
554                         if ( wpListsData.indexOf(':trash=1') != -1 || wpListsData.indexOf(':spam=1') != -1 )
555                                 $('#undo-' + id).fadeIn(300, function(){ $(this).show(); });
556                 });
557 };
558
559 commentReply = {
560         cid : '',
561         act : '',
562
563         init : function() {
564                 var row = $('#replyrow');
565
566                 $('a.cancel', row).click(function() { return commentReply.revert(); });
567                 $('a.save', row).click(function() { return commentReply.send(); });
568                 $( 'input#author-name, input#author-email, input#author-url', row ).keypress( function( e ) {
569                         if ( e.which == 13 ) {
570                                 commentReply.send();
571                                 e.preventDefault();
572                                 return false;
573                         }
574                 });
575
576                 // add events
577                 $('#the-comment-list .column-comment > p').dblclick(function(){
578                         commentReply.toggle($(this).parent());
579                 });
580
581                 $('#doaction, #doaction2, #post-query-submit').click(function(){
582                         if ( $('#the-comment-list #replyrow').length > 0 )
583                                 commentReply.close();
584                 });
585
586                 this.comments_listing = $('#comments-form > input[name="comment_status"]').val() || '';
587
588                 /* $(listTable).bind('beforeChangePage', function(){
589                         commentReply.close();
590                 }); */
591         },
592
593         addEvents : function(r) {
594                 r.each(function() {
595                         $(this).find('.column-comment > p').dblclick(function(){
596                                 commentReply.toggle($(this).parent());
597                         });
598                 });
599         },
600
601         toggle : function(el) {
602                 if ( 'none' !== $( el ).css( 'display' ) && ( $( '#replyrow' ).parent().is('#com-reply') || window.confirm( adminCommentsL10n.warnQuickEdit ) ) ) {
603                         $( el ).find( 'a.vim-q' ).click();
604                 }
605         },
606
607         revert : function() {
608
609                 if ( $('#the-comment-list #replyrow').length < 1 )
610                         return false;
611
612                 $('#replyrow').fadeOut('fast', function(){
613                         commentReply.close();
614                 });
615
616                 return false;
617         },
618
619         close : function() {
620                 var c, replyrow = $('#replyrow');
621
622                 // replyrow is not showing?
623                 if ( replyrow.parent().is('#com-reply') )
624                         return;
625
626                 if ( this.cid && this.act == 'edit-comment' ) {
627                         c = $('#comment-' + this.cid);
628                         c.fadeIn(300, function(){ c.show(); }).css('backgroundColor', '');
629                 }
630
631                 // reset the Quicktags buttons
632                 if ( typeof QTags != 'undefined' )
633                         QTags.closeAllTags('replycontent');
634
635                 $('#add-new-comment').css('display', '');
636
637                 replyrow.hide();
638                 $('#com-reply').append( replyrow );
639                 $('#replycontent').css('height', '').val('');
640                 $('#edithead input').val('');
641                 $('.error', replyrow).empty().hide();
642                 $( '.spinner', replyrow ).removeClass( 'is-active' );
643
644                 this.cid = '';
645         },
646
647         open : function(comment_id, post_id, action) {
648                 var editRow, rowData, act, replyButton, editHeight,
649                         t = this,
650                         c = $('#comment-' + comment_id),
651                         h = c.height(),
652                         colspanVal = 0;
653
654                 t.close();
655                 t.cid = comment_id;
656
657                 editRow = $('#replyrow');
658                 rowData = $('#inline-'+comment_id);
659                 action = action || 'replyto';
660                 act = 'edit' == action ? 'edit' : 'replyto';
661                 act = t.act = act + '-comment';
662                 colspanVal = $( '> th:visible, > td:visible', c ).length;
663
664                 // Make sure it's actually a table and there's a `colspan` value to apply.
665                 if ( editRow.hasClass( 'inline-edit-row' ) && 0 !== colspanVal ) {
666                         $( 'td', editRow ).attr( 'colspan', colspanVal );
667                 }
668
669                 $('#action', editRow).val(act);
670                 $('#comment_post_ID', editRow).val(post_id);
671                 $('#comment_ID', editRow).val(comment_id);
672
673                 if ( action == 'edit' ) {
674                         $( '#author-name', editRow ).val( $( 'div.author', rowData ).text() );
675                         $('#author-email', editRow).val( $('div.author-email', rowData).text() );
676                         $('#author-url', editRow).val( $('div.author-url', rowData).text() );
677                         $('#status', editRow).val( $('div.comment_status', rowData).text() );
678                         $('#replycontent', editRow).val( $('textarea.comment', rowData).val() );
679                         $( '#edithead, #editlegend, #savebtn', editRow ).show();
680                         $('#replyhead, #replybtn, #addhead, #addbtn', editRow).hide();
681
682                         if ( h > 120 ) {
683                                 // Limit the maximum height when editing very long comments to make it more manageable.
684                                 // The textarea is resizable in most browsers, so the user can adjust it if needed.
685                                 editHeight = h > 500 ? 500 : h;
686                                 $('#replycontent', editRow).css('height', editHeight + 'px');
687                         }
688
689                         c.after( editRow ).fadeOut('fast', function(){
690                                 $('#replyrow').fadeIn(300, function(){ $(this).show(); });
691                         });
692                 } else if ( action == 'add' ) {
693                         $('#addhead, #addbtn', editRow).show();
694                         $( '#replyhead, #replybtn, #edithead, #editlegend, #savebtn', editRow ) .hide();
695                         $('#the-comment-list').prepend(editRow);
696                         $('#replyrow').fadeIn(300);
697                 } else {
698                         replyButton = $('#replybtn', editRow);
699                         $( '#edithead, #editlegend, #savebtn, #addhead, #addbtn', editRow ).hide();
700                         $('#replyhead, #replybtn', editRow).show();
701                         c.after(editRow);
702
703                         if ( c.hasClass('unapproved') ) {
704                                 replyButton.text(adminCommentsL10n.replyApprove);
705                         } else {
706                                 replyButton.text(adminCommentsL10n.reply);
707                         }
708
709                         $('#replyrow').fadeIn(300, function(){ $(this).show(); });
710                 }
711
712                 setTimeout(function() {
713                         var rtop, rbottom, scrollTop, vp, scrollBottom;
714
715                         rtop = $('#replyrow').offset().top;
716                         rbottom = rtop + $('#replyrow').height();
717                         scrollTop = window.pageYOffset || document.documentElement.scrollTop;
718                         vp = document.documentElement.clientHeight || window.innerHeight || 0;
719                         scrollBottom = scrollTop + vp;
720
721                         if ( scrollBottom - 20 < rbottom )
722                                 window.scroll(0, rbottom - vp + 35);
723                         else if ( rtop - 20 < scrollTop )
724                                 window.scroll(0, rtop - 35);
725
726                         $('#replycontent').focus().keyup(function(e){
727                                 if ( e.which == 27 )
728                                         commentReply.revert(); // close on Escape
729                         });
730                 }, 600);
731
732                 return false;
733         },
734
735         send : function() {
736                 var post = {};
737
738                 $('#replysubmit .error').hide();
739                 $( '#replysubmit .spinner' ).addClass( 'is-active' );
740
741                 $('#replyrow input').not(':button').each(function() {
742                         var t = $(this);
743                         post[ t.attr('name') ] = t.val();
744                 });
745
746                 post.content = $('#replycontent').val();
747                 post.id = post.comment_post_ID;
748                 post.comments_listing = this.comments_listing;
749                 post.p = $('[name="p"]').val();
750
751                 if ( $('#comment-' + $('#comment_ID').val()).hasClass('unapproved') )
752                         post.approve_parent = 1;
753
754                 $.ajax({
755                         type : 'POST',
756                         url : ajaxurl,
757                         data : post,
758                         success : function(x) { commentReply.show(x); },
759                         error : function(r) { commentReply.error(r); }
760                 });
761
762                 return false;
763         },
764
765         show : function(xml) {
766                 var t = this, r, c, id, bg, pid;
767
768                 if ( typeof(xml) == 'string' ) {
769                         t.error({'responseText': xml});
770                         return false;
771                 }
772
773                 r = wpAjax.parseAjaxResponse(xml);
774                 if ( r.errors ) {
775                         t.error({'responseText': wpAjax.broken});
776                         return false;
777                 }
778
779                 t.revert();
780
781                 r = r.responses[0];
782                 id = '#comment-' + r.id;
783
784                 if ( 'edit-comment' == t.act )
785                         $(id).remove();
786
787                 if ( r.supplemental.parent_approved ) {
788                         pid = $('#comment-' + r.supplemental.parent_approved);
789                         updatePending( -1, r.supplemental.parent_post_id );
790
791                         if ( this.comments_listing == 'moderated' ) {
792                                 pid.animate( { 'backgroundColor':'#CCEEBB' }, 400, function(){
793                                         pid.fadeOut();
794                                 });
795                                 return;
796                         }
797                 }
798
799                 if ( r.supplemental.i18n_comments_text ) {
800                         if ( isDashboard ) {
801                                 updateDashboardText( r.supplemental );
802                         } else {
803                                 updateApproved( 1, r.supplemental.parent_post_id );
804                                 updateCountText( 'span.all-count', 1 );
805                         }
806                 }
807
808                 c = $.trim(r.data); // Trim leading whitespaces
809                 $(c).hide();
810                 $('#replyrow').after(c);
811
812                 id = $(id);
813                 t.addEvents(id);
814                 bg = id.hasClass('unapproved') ? '#FFFFE0' : id.closest('.widefat, .postbox').css('backgroundColor');
815
816                 id.animate( { 'backgroundColor':'#CCEEBB' }, 300 )
817                         .animate( { 'backgroundColor': bg }, 300, function() {
818                                 if ( pid && pid.length ) {
819                                         pid.animate( { 'backgroundColor':'#CCEEBB' }, 300 )
820                                                 .animate( { 'backgroundColor': bg }, 300 )
821                                                 .removeClass('unapproved').addClass('approved')
822                                                 .find('div.comment_status').html('1');
823                                 }
824                         });
825
826         },
827
828         error : function(r) {
829                 var er = r.statusText;
830
831                 $( '#replysubmit .spinner' ).removeClass( 'is-active' );
832
833                 if ( r.responseText )
834                         er = r.responseText.replace( /<.[^<>]*?>/g, '' );
835
836                 if ( er )
837                         $('#replysubmit .error').html(er).show();
838
839         },
840
841         addcomment: function(post_id) {
842                 var t = this;
843
844                 $('#add-new-comment').fadeOut(200, function(){
845                         t.open(0, post_id, 'add');
846                         $('table.comments-box').css('display', '');
847                         $('#no-comments').remove();
848                 });
849         }
850 };
851
852 $(document).ready(function(){
853         var make_hotkeys_redirect, edit_comment, toggle_all, make_bulk;
854
855         setCommentsList();
856         commentReply.init();
857
858         $(document).on( 'click', 'span.delete a.delete', function( e ) {
859                 e.preventDefault();
860         });
861
862         if ( typeof $.table_hotkeys != 'undefined' ) {
863                 make_hotkeys_redirect = function(which) {
864                         return function() {
865                                 var first_last, l;
866
867                                 first_last = 'next' == which? 'first' : 'last';
868                                 l = $('.tablenav-pages .'+which+'-page:not(.disabled)');
869                                 if (l.length)
870                                         window.location = l[0].href.replace(/\&hotkeys_highlight_(first|last)=1/g, '')+'&hotkeys_highlight_'+first_last+'=1';
871                         };
872                 };
873
874                 edit_comment = function(event, current_row) {
875                         window.location = $('span.edit a', current_row).attr('href');
876                 };
877
878                 toggle_all = function() {
879                         $('#cb-select-all-1').data( 'wp-toggle', 1 ).trigger( 'click' ).removeData( 'wp-toggle' );
880                 };
881
882                 make_bulk = function(value) {
883                         return function() {
884                                 var scope = $('select[name="action"]');
885                                 $('option[value="' + value + '"]', scope).prop('selected', true);
886                                 $('#doaction').click();
887                         };
888                 };
889
890                 $.table_hotkeys(
891                         $('table.widefat'),
892                         [
893                                 'a', 'u', 's', 'd', 'r', 'q', 'z',
894                                 ['e', edit_comment],
895                                 ['shift+x', toggle_all],
896                                 ['shift+a', make_bulk('approve')],
897                                 ['shift+s', make_bulk('spam')],
898                                 ['shift+d', make_bulk('delete')],
899                                 ['shift+t', make_bulk('trash')],
900                                 ['shift+z', make_bulk('untrash')],
901                                 ['shift+u', make_bulk('unapprove')]
902                         ],
903                         {
904                                 highlight_first: adminCommentsL10n.hotkeys_highlight_first,
905                                 highlight_last: adminCommentsL10n.hotkeys_highlight_last,
906                                 prev_page_link_cb: make_hotkeys_redirect('prev'),
907                                 next_page_link_cb: make_hotkeys_redirect('next'),
908                                 hotkeys_opts: {
909                                         disableInInput: true,
910                                         type: 'keypress',
911                                         noDisable: '.check-column input[type="checkbox"]'
912                                 },
913                                 cycle_expr: '#the-comment-list tr',
914                                 start_row_index: 0
915                         }
916                 );
917         }
918
919         // Quick Edit and Reply have an inline comment editor.
920         $( '#the-comment-list' ).on( 'click', '.comment-inline', function (e) {
921                 e.preventDefault();
922                 var $el = $( this ),
923                         action = 'replyto';
924
925                 if ( 'undefined' !== typeof $el.data( 'action' ) ) {
926                         action = $el.data( 'action' );
927                 }
928
929                 commentReply.open( $el.data( 'commentId' ), $el.data( 'postId' ), action );
930         } );
931 });
932
933 })(jQuery);