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