]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/edit-comments.js
Wordpress 4.5.3-scripts
[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                                 e.stopPropagation(); // ticket #35904
290                                 list.wpList.del(this);
291                                 $('#undo-' + id).css( {backgroundColor:'#ceb'} ).fadeOut(350, function(){
292                                         $(this).remove();
293                                         $('#comment-' + id).css('backgroundColor', '').fadeIn(300, function(){ $(this).show(); });
294                                 });
295                         });
296                 }
297
298                 return settings;
299         };
300
301         // In admin-ajax.php, we send back the unix time stamp instead of 1 on success
302         delAfter = function( r, settings ) {
303                 var total_items_i18n, total, animated, animatedCallback,
304                         response = true === settings.parsed ? {} : settings.parsed.responses[0],
305                         commentStatus = true === settings.parsed ? '' : response.supplemental.status,
306                         commentPostId = true === settings.parsed ? '' : response.supplemental.postId,
307                         newTotal = true === settings.parsed ? '' : response.supplemental,
308
309                         targetParent = $( settings.target ).parent(),
310                         commentRow = $('#' + settings.element),
311
312                         spamDiff, trashDiff, pendingDiff, approvedDiff,
313
314                         approved = commentRow.hasClass( 'approved' ),
315                         unapproved = commentRow.hasClass( 'unapproved' ),
316                         spammed = commentRow.hasClass( 'spam' ),
317                         trashed = commentRow.hasClass( 'trash' ),
318                         undoing = false; // ticket #35904
319
320                 updateDashboardText( newTotal );
321
322                 // the order of these checks is important
323                 // .unspam can also have .approve or .unapprove
324                 // .untrash can also have .approve or .unapprove
325
326                 if ( targetParent.is( 'span.undo' ) ) {
327                         // the comment was spammed
328                         if ( targetParent.hasClass( 'unspam' ) ) {
329                                 spamDiff = -1;
330
331                                 if ( 'trash' === commentStatus ) {
332                                         trashDiff = 1;
333                                 } else if ( '1' === commentStatus ) {
334                                         approvedDiff = 1;
335                                 } else if ( '0' === commentStatus ) {
336                                         pendingDiff = 1;
337                                 }
338
339                         // the comment was trashed
340                         } else if ( targetParent.hasClass( 'untrash' ) ) {
341                                 trashDiff = -1;
342
343                                 if ( 'spam' === commentStatus ) {
344                                         spamDiff = 1;
345                                 } else if ( '1' === commentStatus ) {
346                                         approvedDiff = 1;
347                                 } else if ( '0' === commentStatus ) {
348                                         pendingDiff = 1;
349                                 }
350                         }
351
352                         undoing = true;
353
354                 // user clicked "Spam"
355                 } else if ( targetParent.is( 'span.spam' ) ) {
356                         // the comment is currently approved
357                         if ( approved ) {
358                                 approvedDiff = -1;
359                         // the comment is currently pending
360                         } else if ( unapproved ) {
361                                 pendingDiff = -1;
362                         // the comment was in the trash
363                         } else if ( trashed ) {
364                                 trashDiff = -1;
365                         }
366                         // you can't spam an item on the spam screen
367                         spamDiff = 1;
368
369                 // user clicked "Unspam"
370                 } else if ( targetParent.is( 'span.unspam' ) ) {
371                         if ( approved ) {
372                                 pendingDiff = 1;
373                         } else if ( unapproved ) {
374                                 approvedDiff = 1;
375                         } else if ( trashed ) {
376                                 // the comment was previously approved
377                                 if ( targetParent.hasClass( 'approve' ) ) {
378                                         approvedDiff = 1;
379                                 // the comment was previously pending
380                                 } else if ( targetParent.hasClass( 'unapprove' ) ) {
381                                         pendingDiff = 1;
382                                 }
383                         } else if ( spammed ) {
384                                 if ( targetParent.hasClass( 'approve' ) ) {
385                                         approvedDiff = 1;
386
387                                 } else if ( targetParent.hasClass( 'unapprove' ) ) {
388                                         pendingDiff = 1;
389                                 }
390                         }
391                         // you can Unspam an item on the spam screen
392                         spamDiff = -1;
393
394                 // user clicked "Trash"
395                 } else if ( targetParent.is( 'span.trash' ) ) {
396                         if ( approved ) {
397                                 approvedDiff = -1;
398                         } else if ( unapproved ) {
399                                 pendingDiff = -1;
400                         // the comment was in the spam queue
401                         } else if ( spammed ) {
402                                 spamDiff = -1;
403                         }
404                         // you can't trash an item on the trash screen
405                         trashDiff = 1;
406
407                 // user clicked "Restore"
408                 } else if ( targetParent.is( 'span.untrash' ) ) {
409                         if ( approved ) {
410                                 pendingDiff = 1;
411                         } else if ( unapproved ) {
412                                 approvedDiff = 1;
413                         } else if ( trashed ) {
414                                 if ( targetParent.hasClass( 'approve' ) ) {
415                                         approvedDiff = 1;
416                                 } else if ( targetParent.hasClass( 'unapprove' ) ) {
417                                         pendingDiff = 1;
418                                 }
419                         }
420                         // you can't go from trash to spam
421                         // you can untrash on the trash screen
422                         trashDiff = -1;
423
424                 // User clicked "Approve"
425                 } else if ( targetParent.is( 'span.approve:not(.unspam):not(.untrash)' ) ) {
426                         approvedDiff = 1;
427                         pendingDiff = -1;
428
429                 // User clicked "Unapprove"
430                 } else if ( targetParent.is( 'span.unapprove:not(.unspam):not(.untrash)' ) ) {
431                         approvedDiff = -1;
432                         pendingDiff = 1;
433
434                 // User clicked "Delete Permanently"
435                 } else if ( targetParent.is( 'span.delete' ) ) {
436                         if ( spammed ) {
437                                 spamDiff = -1;
438                         } else if ( trashed ) {
439                                 trashDiff = -1;
440                         }
441                 }
442
443                 if ( pendingDiff ) {
444                         updatePending( pendingDiff, commentPostId );
445                         updateCountText( 'span.all-count', pendingDiff );
446                 }
447
448                 if ( approvedDiff ) {
449                         updateApproved( approvedDiff, commentPostId );
450                         updateCountText( 'span.all-count', approvedDiff );
451                 }
452
453                 if ( spamDiff ) {
454                         updateCountText( 'span.spam-count', spamDiff );
455                 }
456
457                 if ( trashDiff ) {
458                         updateCountText( 'span.trash-count', trashDiff );
459                 }
460
461                 if ( ! isDashboard ) {
462                         total = totalInput.val() ? parseInt( totalInput.val(), 10 ) : 0;
463                         if ( $(settings.target).parent().is('span.undo') )
464                                 total++;
465                         else
466                                 total--;
467
468                         if ( total < 0 )
469                                 total = 0;
470
471                         if ( 'object' === typeof r ) {
472                                 if ( response.supplemental.total_items_i18n && lastConfidentTime < response.supplemental.time ) {
473                                         total_items_i18n = response.supplemental.total_items_i18n || '';
474                                         if ( total_items_i18n ) {
475                                                 $('.displaying-num').text( total_items_i18n );
476                                                 $('.total-pages').text( response.supplemental.total_pages_i18n );
477                                                 $('.tablenav-pages').find('.next-page, .last-page').toggleClass('disabled', response.supplemental.total_pages == $('.current-page').val());
478                                         }
479                                         updateTotalCount( total, response.supplemental.time, true );
480                                 } else if ( response.supplemental.time ) {
481                                         updateTotalCount( total, response.supplemental.time, false );
482                                 }
483                         } else {
484                                 updateTotalCount( total, r, false );
485                         }
486                 }
487
488                 if ( ! theExtraList || theExtraList.length === 0 || theExtraList.children().length === 0 || undoing ) {
489                         return;
490                 }
491
492                 theList.get(0).wpList.add( theExtraList.children( ':eq(0):not(.no-items)' ).remove().clone() );
493
494                 refillTheExtraList();
495
496                 animated = $( ':animated', '#the-comment-list' );
497                 animatedCallback = function () {
498                         if ( ! $( '#the-comment-list tr:visible' ).length ) {
499                                 theList.get(0).wpList.add( theExtraList.find( '.no-items' ).clone() );
500                         }
501                 };
502
503                 if ( animated.length ) {
504                         animated.promise().done( animatedCallback );
505                 } else {
506                         animatedCallback();
507                 }
508         };
509
510         refillTheExtraList = function(ev) {
511                 var args = $.query.get(), total_pages = $('.total-pages').text(), per_page = $('input[name="_per_page"]', '#comments-form').val();
512
513                 if (! args.paged)
514                         args.paged = 1;
515
516                 if (args.paged > total_pages) {
517                         return;
518                 }
519
520                 if (ev) {
521                         theExtraList.empty();
522                         args.number = Math.min(8, per_page); // see WP_Comments_List_Table::prepare_items() @ class-wp-comments-list-table.php
523                 } else {
524                         args.number = 1;
525                         args.offset = Math.min(8, per_page) - 1; // fetch only the next item on the extra list
526                 }
527
528                 args.no_placeholder = true;
529
530                 args.paged ++;
531
532                 // $.query.get() needs some correction to be sent into an ajax request
533                 if ( true === args.comment_type )
534                         args.comment_type = '';
535
536                 args = $.extend(args, {
537                         'action': 'fetch-list',
538                         'list_args': list_args,
539                         '_ajax_fetch_list_nonce': $('#_ajax_fetch_list_nonce').val()
540                 });
541
542                 $.ajax({
543                         url: ajaxurl,
544                         global: false,
545                         dataType: 'json',
546                         data: args,
547                         success: function(response) {
548                                 theExtraList.get(0).wpList.add( response.rows );
549                         }
550                 });
551         };
552
553         theExtraList = $('#the-extra-comment-list').wpList( { alt: '', delColor: 'none', addColor: 'none' } );
554         theList = $('#the-comment-list').wpList( { alt: '', delBefore: delBefore, dimAfter: dimAfter, delAfter: delAfter, addColor: 'none' } )
555                 .bind('wpListDelEnd', function(e, s){
556                         var wpListsData = $(s.target).attr('data-wp-lists'), id = s.element.replace(/[^0-9]+/g, '');
557
558                         if ( wpListsData.indexOf(':trash=1') != -1 || wpListsData.indexOf(':spam=1') != -1 )
559                                 $('#undo-' + id).fadeIn(300, function(){ $(this).show(); });
560                 });
561 };
562
563 commentReply = {
564         cid : '',
565         act : '',
566
567         init : function() {
568                 var row = $('#replyrow');
569
570                 $('a.cancel', row).click(function() { return commentReply.revert(); });
571                 $('a.save', row).click(function() { return commentReply.send(); });
572                 $( 'input#author-name, input#author-email, input#author-url', row ).keypress( function( e ) {
573                         if ( e.which == 13 ) {
574                                 commentReply.send();
575                                 e.preventDefault();
576                                 return false;
577                         }
578                 });
579
580                 // add events
581                 $('#the-comment-list .column-comment > p').dblclick(function(){
582                         commentReply.toggle($(this).parent());
583                 });
584
585                 $('#doaction, #doaction2, #post-query-submit').click(function(){
586                         if ( $('#the-comment-list #replyrow').length > 0 )
587                                 commentReply.close();
588                 });
589
590                 this.comments_listing = $('#comments-form > input[name="comment_status"]').val() || '';
591
592                 /* $(listTable).bind('beforeChangePage', function(){
593                         commentReply.close();
594                 }); */
595         },
596
597         addEvents : function(r) {
598                 r.each(function() {
599                         $(this).find('.column-comment > p').dblclick(function(){
600                                 commentReply.toggle($(this).parent());
601                         });
602                 });
603         },
604
605         toggle : function(el) {
606                 if ( 'none' !== $( el ).css( 'display' ) && ( $( '#replyrow' ).parent().is('#com-reply') || window.confirm( adminCommentsL10n.warnQuickEdit ) ) ) {
607                         $( el ).find( 'a.vim-q' ).click();
608                 }
609         },
610
611         revert : function() {
612
613                 if ( $('#the-comment-list #replyrow').length < 1 )
614                         return false;
615
616                 $('#replyrow').fadeOut('fast', function(){
617                         commentReply.close();
618                 });
619
620                 return false;
621         },
622
623         close : function() {
624                 var c, replyrow = $('#replyrow');
625
626                 // replyrow is not showing?
627                 if ( replyrow.parent().is('#com-reply') )
628                         return;
629
630                 if ( this.cid && this.act == 'edit-comment' ) {
631                         c = $('#comment-' + this.cid);
632                         c.fadeIn(300, function(){ c.show(); }).css('backgroundColor', '');
633                 }
634
635                 // reset the Quicktags buttons
636                 if ( typeof QTags != 'undefined' )
637                         QTags.closeAllTags('replycontent');
638
639                 $('#add-new-comment').css('display', '');
640
641                 replyrow.hide();
642                 $('#com-reply').append( replyrow );
643                 $('#replycontent').css('height', '').val('');
644                 $('#edithead input').val('');
645                 $('.error', replyrow).empty().hide();
646                 $( '.spinner', replyrow ).removeClass( 'is-active' );
647
648                 this.cid = '';
649         },
650
651         open : function(comment_id, post_id, action) {
652                 var editRow, rowData, act, replyButton, editHeight,
653                         t = this,
654                         c = $('#comment-' + comment_id),
655                         h = c.height(),
656                         colspanVal = 0;
657
658                 t.close();
659                 t.cid = comment_id;
660
661                 editRow = $('#replyrow');
662                 rowData = $('#inline-'+comment_id);
663                 action = action || 'replyto';
664                 act = 'edit' == action ? 'edit' : 'replyto';
665                 act = t.act = act + '-comment';
666                 colspanVal = $( '> th:visible, > td:visible', c ).length;
667
668                 // Make sure it's actually a table and there's a `colspan` value to apply.
669                 if ( editRow.hasClass( 'inline-edit-row' ) && 0 !== colspanVal ) {
670                         $( 'td', editRow ).attr( 'colspan', colspanVal );
671                 }
672
673                 $('#action', editRow).val(act);
674                 $('#comment_post_ID', editRow).val(post_id);
675                 $('#comment_ID', editRow).val(comment_id);
676
677                 if ( action == 'edit' ) {
678                         $( '#author-name', editRow ).val( $( 'div.author', rowData ).text() );
679                         $('#author-email', editRow).val( $('div.author-email', rowData).text() );
680                         $('#author-url', editRow).val( $('div.author-url', rowData).text() );
681                         $('#status', editRow).val( $('div.comment_status', rowData).text() );
682                         $('#replycontent', editRow).val( $('textarea.comment', rowData).val() );
683                         $( '#edithead, #editlegend, #savebtn', editRow ).show();
684                         $('#replyhead, #replybtn, #addhead, #addbtn', editRow).hide();
685
686                         if ( h > 120 ) {
687                                 // Limit the maximum height when editing very long comments to make it more manageable.
688                                 // The textarea is resizable in most browsers, so the user can adjust it if needed.
689                                 editHeight = h > 500 ? 500 : h;
690                                 $('#replycontent', editRow).css('height', editHeight + 'px');
691                         }
692
693                         c.after( editRow ).fadeOut('fast', function(){
694                                 $('#replyrow').fadeIn(300, function(){ $(this).show(); });
695                         });
696                 } else if ( action == 'add' ) {
697                         $('#addhead, #addbtn', editRow).show();
698                         $( '#replyhead, #replybtn, #edithead, #editlegend, #savebtn', editRow ) .hide();
699                         $('#the-comment-list').prepend(editRow);
700                         $('#replyrow').fadeIn(300);
701                 } else {
702                         replyButton = $('#replybtn', editRow);
703                         $( '#edithead, #editlegend, #savebtn, #addhead, #addbtn', editRow ).hide();
704                         $('#replyhead, #replybtn', editRow).show();
705                         c.after(editRow);
706
707                         if ( c.hasClass('unapproved') ) {
708                                 replyButton.text(adminCommentsL10n.replyApprove);
709                         } else {
710                                 replyButton.text(adminCommentsL10n.reply);
711                         }
712
713                         $('#replyrow').fadeIn(300, function(){ $(this).show(); });
714                 }
715
716                 setTimeout(function() {
717                         var rtop, rbottom, scrollTop, vp, scrollBottom;
718
719                         rtop = $('#replyrow').offset().top;
720                         rbottom = rtop + $('#replyrow').height();
721                         scrollTop = window.pageYOffset || document.documentElement.scrollTop;
722                         vp = document.documentElement.clientHeight || window.innerHeight || 0;
723                         scrollBottom = scrollTop + vp;
724
725                         if ( scrollBottom - 20 < rbottom )
726                                 window.scroll(0, rbottom - vp + 35);
727                         else if ( rtop - 20 < scrollTop )
728                                 window.scroll(0, rtop - 35);
729
730                         $('#replycontent').focus().keyup(function(e){
731                                 if ( e.which == 27 )
732                                         commentReply.revert(); // close on Escape
733                         });
734                 }, 600);
735
736                 return false;
737         },
738
739         send : function() {
740                 var post = {};
741
742                 $('#replysubmit .error').hide();
743                 $( '#replysubmit .spinner' ).addClass( 'is-active' );
744
745                 $('#replyrow input').not(':button').each(function() {
746                         var t = $(this);
747                         post[ t.attr('name') ] = t.val();
748                 });
749
750                 post.content = $('#replycontent').val();
751                 post.id = post.comment_post_ID;
752                 post.comments_listing = this.comments_listing;
753                 post.p = $('[name="p"]').val();
754
755                 if ( $('#comment-' + $('#comment_ID').val()).hasClass('unapproved') )
756                         post.approve_parent = 1;
757
758                 $.ajax({
759                         type : 'POST',
760                         url : ajaxurl,
761                         data : post,
762                         success : function(x) { commentReply.show(x); },
763                         error : function(r) { commentReply.error(r); }
764                 });
765
766                 return false;
767         },
768
769         show : function(xml) {
770                 var t = this, r, c, id, bg, pid;
771
772                 if ( typeof(xml) == 'string' ) {
773                         t.error({'responseText': xml});
774                         return false;
775                 }
776
777                 r = wpAjax.parseAjaxResponse(xml);
778                 if ( r.errors ) {
779                         t.error({'responseText': wpAjax.broken});
780                         return false;
781                 }
782
783                 t.revert();
784
785                 r = r.responses[0];
786                 id = '#comment-' + r.id;
787
788                 if ( 'edit-comment' == t.act )
789                         $(id).remove();
790
791                 if ( r.supplemental.parent_approved ) {
792                         pid = $('#comment-' + r.supplemental.parent_approved);
793                         updatePending( -1, r.supplemental.parent_post_id );
794
795                         if ( this.comments_listing == 'moderated' ) {
796                                 pid.animate( { 'backgroundColor':'#CCEEBB' }, 400, function(){
797                                         pid.fadeOut();
798                                 });
799                                 return;
800                         }
801                 }
802
803                 if ( r.supplemental.i18n_comments_text ) {
804                         if ( isDashboard ) {
805                                 updateDashboardText( r.supplemental );
806                         } else {
807                                 updateApproved( 1, r.supplemental.parent_post_id );
808                                 updateCountText( 'span.all-count', 1 );
809                         }
810                 }
811
812                 c = $.trim(r.data); // Trim leading whitespaces
813                 $(c).hide();
814                 $('#replyrow').after(c);
815
816                 id = $(id);
817                 t.addEvents(id);
818                 bg = id.hasClass('unapproved') ? '#FFFFE0' : id.closest('.widefat, .postbox').css('backgroundColor');
819
820                 id.animate( { 'backgroundColor':'#CCEEBB' }, 300 )
821                         .animate( { 'backgroundColor': bg }, 300, function() {
822                                 if ( pid && pid.length ) {
823                                         pid.animate( { 'backgroundColor':'#CCEEBB' }, 300 )
824                                                 .animate( { 'backgroundColor': bg }, 300 )
825                                                 .removeClass('unapproved').addClass('approved')
826                                                 .find('div.comment_status').html('1');
827                                 }
828                         });
829
830         },
831
832         error : function(r) {
833                 var er = r.statusText;
834
835                 $( '#replysubmit .spinner' ).removeClass( 'is-active' );
836
837                 if ( r.responseText )
838                         er = r.responseText.replace( /<.[^<>]*?>/g, '' );
839
840                 if ( er )
841                         $('#replysubmit .error').html(er).show();
842
843         },
844
845         addcomment: function(post_id) {
846                 var t = this;
847
848                 $('#add-new-comment').fadeOut(200, function(){
849                         t.open(0, post_id, 'add');
850                         $('table.comments-box').css('display', '');
851                         $('#no-comments').remove();
852                 });
853         }
854 };
855
856 $(document).ready(function(){
857         var make_hotkeys_redirect, edit_comment, toggle_all, make_bulk;
858
859         setCommentsList();
860         commentReply.init();
861
862         $(document).on( 'click', 'span.delete a.delete', function( e ) {
863                 e.preventDefault();
864         });
865
866         if ( typeof $.table_hotkeys != 'undefined' ) {
867                 make_hotkeys_redirect = function(which) {
868                         return function() {
869                                 var first_last, l;
870
871                                 first_last = 'next' == which? 'first' : 'last';
872                                 l = $('.tablenav-pages .'+which+'-page:not(.disabled)');
873                                 if (l.length)
874                                         window.location = l[0].href.replace(/\&hotkeys_highlight_(first|last)=1/g, '')+'&hotkeys_highlight_'+first_last+'=1';
875                         };
876                 };
877
878                 edit_comment = function(event, current_row) {
879                         window.location = $('span.edit a', current_row).attr('href');
880                 };
881
882                 toggle_all = function() {
883                         $('#cb-select-all-1').data( 'wp-toggle', 1 ).trigger( 'click' ).removeData( 'wp-toggle' );
884                 };
885
886                 make_bulk = function(value) {
887                         return function() {
888                                 var scope = $('select[name="action"]');
889                                 $('option[value="' + value + '"]', scope).prop('selected', true);
890                                 $('#doaction').click();
891                         };
892                 };
893
894                 $.table_hotkeys(
895                         $('table.widefat'),
896                         [
897                                 'a', 'u', 's', 'd', 'r', 'q', 'z',
898                                 ['e', edit_comment],
899                                 ['shift+x', toggle_all],
900                                 ['shift+a', make_bulk('approve')],
901                                 ['shift+s', make_bulk('spam')],
902                                 ['shift+d', make_bulk('delete')],
903                                 ['shift+t', make_bulk('trash')],
904                                 ['shift+z', make_bulk('untrash')],
905                                 ['shift+u', make_bulk('unapprove')]
906                         ],
907                         {
908                                 highlight_first: adminCommentsL10n.hotkeys_highlight_first,
909                                 highlight_last: adminCommentsL10n.hotkeys_highlight_last,
910                                 prev_page_link_cb: make_hotkeys_redirect('prev'),
911                                 next_page_link_cb: make_hotkeys_redirect('next'),
912                                 hotkeys_opts: {
913                                         disableInInput: true,
914                                         type: 'keypress',
915                                         noDisable: '.check-column input[type="checkbox"]'
916                                 },
917                                 cycle_expr: '#the-comment-list tr',
918                                 start_row_index: 0
919                         }
920                 );
921         }
922
923         // Quick Edit and Reply have an inline comment editor.
924         $( '#the-comment-list' ).on( 'click', '.comment-inline', function (e) {
925                 e.preventDefault();
926                 var $el = $( this ),
927                         action = 'replyto';
928
929                 if ( 'undefined' !== typeof $el.data( 'action' ) ) {
930                         action = $el.data( 'action' );
931                 }
932
933                 commentReply.open( $el.data( 'commentId' ), $el.data( 'postId' ), action );
934         } );
935 });
936
937 })(jQuery);