]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/edit-comments.js
WordPress 4.1.3
[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, updatePending;
6
7 setCommentsList = function() {
8         var totalInput, perPageInput, pageInput, dimAfter, delBefore, updateTotalCount, delAfter, refillTheExtraList, diff,
9                 lastConfidentTime = 0;
10
11         totalInput = $('input[name="_total"]', '#comments-form');
12         perPageInput = $('input[name="_per_page"]', '#comments-form');
13         pageInput = $('input[name="_page"]', '#comments-form');
14
15         dimAfter = function( r, settings ) {
16                 var editRow, replyID, replyButton,
17                         c = $( '#' + settings.element );
18
19                 editRow = $('#replyrow');
20                 replyID = $('#comment_ID', editRow).val();
21                 replyButton = $('#replybtn', editRow);
22
23                 if ( c.is('.unapproved') ) {
24                         if ( settings.data.id == replyID )
25                                 replyButton.text(adminCommentsL10n.replyApprove);
26
27                         c.find('div.comment_status').html('0');
28                 } else {
29                         if ( settings.data.id == replyID )
30                                 replyButton.text(adminCommentsL10n.reply);
31
32                         c.find('div.comment_status').html('1');
33                 }
34
35                 diff = $('#' + settings.element).is('.' + settings.dimClass) ? 1 : -1;
36                 updatePending( diff );
37         };
38
39         // Send current total, page, per_page and url
40         delBefore = function( settings, list ) {
41                 var note, id, el, n, h, a, author,
42                         action = false,
43                         wpListsData = $( settings.target ).attr( 'data-wp-lists' );
44
45                 settings.data._total = totalInput.val() || 0;
46                 settings.data._per_page = perPageInput.val() || 0;
47                 settings.data._page = pageInput.val() || 0;
48                 settings.data._url = document.location.href;
49                 settings.data.comment_status = $('input[name="comment_status"]', '#comments-form').val();
50
51                 if ( wpListsData.indexOf(':trash=1') != -1 )
52                         action = 'trash';
53                 else if ( wpListsData.indexOf(':spam=1') != -1 )
54                         action = 'spam';
55
56                 if ( action ) {
57                         id = wpListsData.replace(/.*?comment-([0-9]+).*/, '$1');
58                         el = $('#comment-' + id);
59                         note = $('#' + action + '-undo-holder').html();
60
61                         el.find('.check-column :checkbox').prop('checked', false); // Uncheck the row so as not to be affected by Bulk Edits.
62
63                         if ( el.siblings('#replyrow').length && commentReply.cid == id )
64                                 commentReply.close();
65
66                         if ( el.is('tr') ) {
67                                 n = el.children(':visible').length;
68                                 author = $('.author strong', el).text();
69                                 h = $('<tr id="undo-' + id + '" class="undo un' + action + '" style="display:none;"><td colspan="' + n + '">' + note + '</td></tr>');
70                         } else {
71                                 author = $('.comment-author', el).text();
72                                 h = $('<div id="undo-' + id + '" style="display:none;" class="undo un' + action + '">' + note + '</div>');
73                         }
74
75                         el.before(h);
76
77                         $('strong', '#undo-' + id).text(author);
78                         a = $('.undo a', '#undo-' + id);
79                         a.attr('href', 'comment.php?action=un' + action + 'comment&c=' + id + '&_wpnonce=' + settings.data._ajax_nonce);
80                         a.attr('data-wp-lists', 'delete:the-comment-list:comment-' + id + '::un' + action + '=1');
81                         a.attr('class', 'vim-z vim-destructive');
82                         $('.avatar', el).first().clone().prependTo('#undo-' + id + ' .' + action + '-undo-inside');
83
84                         a.click(function(){
85                                 list.wpList.del(this);
86                                 $('#undo-' + id).css( {backgroundColor:'#ceb'} ).fadeOut(350, function(){
87                                         $(this).remove();
88                                         $('#comment-' + id).css('backgroundColor', '').fadeIn(300, function(){ $(this).show(); });
89                                 });
90                                 return false;
91                         });
92                 }
93
94                 return settings;
95         };
96
97         // Updates the current total (stored in the _total input)
98         updateTotalCount = function( total, time, setConfidentTime ) {
99                 if ( time < lastConfidentTime )
100                         return;
101
102                 if ( setConfidentTime )
103                         lastConfidentTime = time;
104
105                 totalInput.val( total.toString() );
106         };
107
108         getCount = function(el) {
109                 var n = parseInt( el.html().replace(/[^0-9]+/g, ''), 10 );
110                 if ( isNaN(n) )
111                         return 0;
112                 return n;
113         };
114
115         updateCount = function(el, n) {
116                 var n1 = '';
117                 if ( isNaN(n) )
118                         return;
119                 n = n < 1 ? '0' : n.toString();
120                 if ( n.length > 3 ) {
121                         while ( n.length > 3 ) {
122                                 n1 = thousandsSeparator + n.substr(n.length - 3) + n1;
123                                 n = n.substr(0, n.length - 3);
124                         }
125                         n = n + n1;
126                 }
127                 el.html(n);
128         };
129
130         updatePending = function( diff ) {
131                 $('span.pending-count').each(function() {
132                         var a = $(this), n = getCount(a) + diff;
133                         if ( n < 1 )
134                                 n = 0;
135                         a.closest('.awaiting-mod')[ 0 === n ? 'addClass' : 'removeClass' ]('count-0');
136                         updateCount( a, n );
137                 });
138         };
139
140         // In admin-ajax.php, we send back the unix time stamp instead of 1 on success
141         delAfter = function( r, settings ) {
142                 var total_items_i18n, total, spam, trash, pending,
143                         untrash = $(settings.target).parent().is('span.untrash'),
144                         unspam = $(settings.target).parent().is('span.unspam'),
145                         unapproved = $('#' + settings.element).is('.unapproved');
146
147                 function getUpdate(s) {
148                         if ( $(settings.target).parent().is('span.' + s) )
149                                 return 1;
150                         else if ( $('#' + settings.element).is('.' + s) )
151                                 return -1;
152
153                         return 0;
154                 }
155
156                 if ( untrash )
157                         trash = -1;
158                 else
159                         trash = getUpdate('trash');
160
161                 if ( unspam )
162                         spam = -1;
163                 else
164                         spam = getUpdate('spam');
165
166                 if ( $(settings.target).parent().is('span.unapprove') || ( ( untrash || unspam ) && unapproved ) ) {
167                         // a comment was 'deleted' from another list (e.g. approved, spam, trash) and moved to pending,
168                         // or a trash/spam of a pending comment was undone
169                         pending = 1;
170                 } else if ( unapproved ) {
171                         // a pending comment was trashed/spammed/approved
172                         pending = -1;
173                 }
174
175                 if ( pending )
176                         updatePending(pending);
177
178                 $('span.spam-count').each( function() {
179                         var a = $(this), n = getCount(a) + spam;
180                         updateCount(a, n);
181                 });
182
183                 $('span.trash-count').each( function() {
184                         var a = $(this), n = getCount(a) + trash;
185                         updateCount(a, n);
186                 });
187
188                 if ( ! $('#dashboard_right_now').length ) {
189                         total = totalInput.val() ? parseInt( totalInput.val(), 10 ) : 0;
190                         if ( $(settings.target).parent().is('span.undo') )
191                                 total++;
192                         else
193                                 total--;
194
195                         if ( total < 0 )
196                                 total = 0;
197
198                         if ( ( 'object' == typeof r ) && lastConfidentTime < settings.parsed.responses[0].supplemental.time ) {
199                                 total_items_i18n = settings.parsed.responses[0].supplemental.total_items_i18n || '';
200                                 if ( total_items_i18n ) {
201                                         $('.displaying-num').text( total_items_i18n );
202                                         $('.total-pages').text( settings.parsed.responses[0].supplemental.total_pages_i18n );
203                                         $('.tablenav-pages').find('.next-page, .last-page').toggleClass('disabled', settings.parsed.responses[0].supplemental.total_pages == $('.current-page').val());
204                                 }
205                                 updateTotalCount( total, settings.parsed.responses[0].supplemental.time, true );
206                         } else {
207                                 updateTotalCount( total, r, false );
208                         }
209                 }
210
211                 if ( ! theExtraList || theExtraList.size() === 0 || theExtraList.children().size() === 0 || untrash || unspam ) {
212                         return;
213                 }
214
215                 theList.get(0).wpList.add( theExtraList.children(':eq(0)').remove().clone() );
216
217                 refillTheExtraList();
218         };
219
220         refillTheExtraList = function(ev) {
221                 var args = $.query.get(), total_pages = $('.total-pages').text(), per_page = $('input[name="_per_page"]', '#comments-form').val();
222
223                 if (! args.paged)
224                         args.paged = 1;
225
226                 if (args.paged > total_pages) {
227                         return;
228                 }
229
230                 if (ev) {
231                         theExtraList.empty();
232                         args.number = Math.min(8, per_page); // see WP_Comments_List_Table::prepare_items() @ class-wp-comments-list-table.php
233                 } else {
234                         args.number = 1;
235                         args.offset = Math.min(8, per_page) - 1; // fetch only the next item on the extra list
236                 }
237
238                 args.no_placeholder = true;
239
240                 args.paged ++;
241
242                 // $.query.get() needs some correction to be sent into an ajax request
243                 if ( true === args.comment_type )
244                         args.comment_type = '';
245
246                 args = $.extend(args, {
247                         'action': 'fetch-list',
248                         'list_args': list_args,
249                         '_ajax_fetch_list_nonce': $('#_ajax_fetch_list_nonce').val()
250                 });
251
252                 $.ajax({
253                         url: ajaxurl,
254                         global: false,
255                         dataType: 'json',
256                         data: args,
257                         success: function(response) {
258                                 theExtraList.get(0).wpList.add( response.rows );
259                         }
260                 });
261         };
262
263         theExtraList = $('#the-extra-comment-list').wpList( { alt: '', delColor: 'none', addColor: 'none' } );
264         theList = $('#the-comment-list').wpList( { alt: '', delBefore: delBefore, dimAfter: dimAfter, delAfter: delAfter, addColor: 'none' } )
265                 .bind('wpListDelEnd', function(e, s){
266                         var wpListsData = $(s.target).attr('data-wp-lists'), id = s.element.replace(/[^0-9]+/g, '');
267
268                         if ( wpListsData.indexOf(':trash=1') != -1 || wpListsData.indexOf(':spam=1') != -1 )
269                                 $('#undo-' + id).fadeIn(300, function(){ $(this).show(); });
270                 });
271 };
272
273 commentReply = {
274         cid : '',
275         act : '',
276
277         init : function() {
278                 var row = $('#replyrow');
279
280                 $('a.cancel', row).click(function() { return commentReply.revert(); });
281                 $('a.save', row).click(function() { return commentReply.send(); });
282                 $('input#author, input#author-email, input#author-url', row).keypress(function(e){
283                         if ( e.which == 13 ) {
284                                 commentReply.send();
285                                 e.preventDefault();
286                                 return false;
287                         }
288                 });
289
290                 // add events
291                 $('#the-comment-list .column-comment > p').dblclick(function(){
292                         commentReply.toggle($(this).parent());
293                 });
294
295                 $('#doaction, #doaction2, #post-query-submit').click(function(){
296                         if ( $('#the-comment-list #replyrow').length > 0 )
297                                 commentReply.close();
298                 });
299
300                 this.comments_listing = $('#comments-form > input[name="comment_status"]').val() || '';
301
302                 /* $(listTable).bind('beforeChangePage', function(){
303                         commentReply.close();
304                 }); */
305         },
306
307         addEvents : function(r) {
308                 r.each(function() {
309                         $(this).find('.column-comment > p').dblclick(function(){
310                                 commentReply.toggle($(this).parent());
311                         });
312                 });
313         },
314
315         toggle : function(el) {
316                 if ( $(el).css('display') != 'none' )
317                         $(el).find('a.vim-q').click();
318         },
319
320         revert : function() {
321
322                 if ( $('#the-comment-list #replyrow').length < 1 )
323                         return false;
324
325                 $('#replyrow').fadeOut('fast', function(){
326                         commentReply.close();
327                 });
328
329                 return false;
330         },
331
332         close : function() {
333                 var c, replyrow = $('#replyrow');
334
335                 // replyrow is not showing?
336                 if ( replyrow.parent().is('#com-reply') )
337                         return;
338
339                 if ( this.cid && this.act == 'edit-comment' ) {
340                         c = $('#comment-' + this.cid);
341                         c.fadeIn(300, function(){ c.show(); }).css('backgroundColor', '');
342                 }
343
344                 // reset the Quicktags buttons
345                 if ( typeof QTags != 'undefined' )
346                         QTags.closeAllTags('replycontent');
347
348                 $('#add-new-comment').css('display', '');
349
350                 replyrow.hide();
351                 $('#com-reply').append( replyrow );
352                 $('#replycontent').css('height', '').val('');
353                 $('#edithead input').val('');
354                 $('.error', replyrow).html('').hide();
355                 $('.spinner', replyrow).hide();
356
357                 this.cid = '';
358         },
359
360         open : function(comment_id, post_id, action) {
361                 var editRow, rowData, act, replyButton, editHeight,
362                         t = this,
363                         c = $('#comment-' + comment_id),
364                         h = c.height();
365
366                 t.close();
367                 t.cid = comment_id;
368
369                 editRow = $('#replyrow');
370                 rowData = $('#inline-'+comment_id);
371                 action = action || 'replyto';
372                 act = 'edit' == action ? 'edit' : 'replyto';
373                 act = t.act = act + '-comment';
374
375                 $('#action', editRow).val(act);
376                 $('#comment_post_ID', editRow).val(post_id);
377                 $('#comment_ID', editRow).val(comment_id);
378
379                 if ( action == 'edit' ) {
380                         $('#author', editRow).val( $('div.author', rowData).text() );
381                         $('#author-email', editRow).val( $('div.author-email', rowData).text() );
382                         $('#author-url', editRow).val( $('div.author-url', rowData).text() );
383                         $('#status', editRow).val( $('div.comment_status', rowData).text() );
384                         $('#replycontent', editRow).val( $('textarea.comment', rowData).val() );
385                         $('#edithead, #savebtn', editRow).show();
386                         $('#replyhead, #replybtn, #addhead, #addbtn', editRow).hide();
387
388                         if ( h > 120 ) {
389                                 // Limit the maximum height when editing very long comments to make it more manageable.
390                                 // The textarea is resizable in most browsers, so the user can adjust it if needed.
391                                 editHeight = h > 500 ? 500 : h;
392                                 $('#replycontent', editRow).css('height', editHeight + 'px');
393                         }
394
395                         c.after( editRow ).fadeOut('fast', function(){
396                                 $('#replyrow').fadeIn(300, function(){ $(this).show(); });
397                         });
398                 } else if ( action == 'add' ) {
399                         $('#addhead, #addbtn', editRow).show();
400                         $('#replyhead, #replybtn, #edithead, #editbtn', editRow).hide();
401                         $('#the-comment-list').prepend(editRow);
402                         $('#replyrow').fadeIn(300);
403                 } else {
404                         replyButton = $('#replybtn', editRow);
405                         $('#edithead, #savebtn, #addhead, #addbtn', editRow).hide();
406                         $('#replyhead, #replybtn', editRow).show();
407                         c.after(editRow);
408
409                         if ( c.hasClass('unapproved') ) {
410                                 replyButton.text(adminCommentsL10n.replyApprove);
411                         } else {
412                                 replyButton.text(adminCommentsL10n.reply);
413                         }
414
415                         $('#replyrow').fadeIn(300, function(){ $(this).show(); });
416                 }
417
418                 setTimeout(function() {
419                         var rtop, rbottom, scrollTop, vp, scrollBottom;
420
421                         rtop = $('#replyrow').offset().top;
422                         rbottom = rtop + $('#replyrow').height();
423                         scrollTop = window.pageYOffset || document.documentElement.scrollTop;
424                         vp = document.documentElement.clientHeight || window.innerHeight || 0;
425                         scrollBottom = scrollTop + vp;
426
427                         if ( scrollBottom - 20 < rbottom )
428                                 window.scroll(0, rbottom - vp + 35);
429                         else if ( rtop - 20 < scrollTop )
430                                 window.scroll(0, rtop - 35);
431
432                         $('#replycontent').focus().keyup(function(e){
433                                 if ( e.which == 27 )
434                                         commentReply.revert(); // close on Escape
435                         });
436                 }, 600);
437
438                 return false;
439         },
440
441         send : function() {
442                 var post = {};
443
444                 $('#replysubmit .error').hide();
445                 $('#replysubmit .spinner').show();
446
447                 $('#replyrow input').not(':button').each(function() {
448                         var t = $(this);
449                         post[ t.attr('name') ] = t.val();
450                 });
451
452                 post.content = $('#replycontent').val();
453                 post.id = post.comment_post_ID;
454                 post.comments_listing = this.comments_listing;
455                 post.p = $('[name="p"]').val();
456
457                 if ( $('#comment-' + $('#comment_ID').val()).hasClass('unapproved') )
458                         post.approve_parent = 1;
459
460                 $.ajax({
461                         type : 'POST',
462                         url : ajaxurl,
463                         data : post,
464                         success : function(x) { commentReply.show(x); },
465                         error : function(r) { commentReply.error(r); }
466                 });
467
468                 return false;
469         },
470
471         show : function(xml) {
472                 var t = this, r, c, id, bg, pid;
473
474                 if ( typeof(xml) == 'string' ) {
475                         t.error({'responseText': xml});
476                         return false;
477                 }
478
479                 r = wpAjax.parseAjaxResponse(xml);
480                 if ( r.errors ) {
481                         t.error({'responseText': wpAjax.broken});
482                         return false;
483                 }
484
485                 t.revert();
486
487                 r = r.responses[0];
488                 id = '#comment-' + r.id;
489
490                 if ( 'edit-comment' == t.act )
491                         $(id).remove();
492
493                 if ( r.supplemental.parent_approved ) {
494                         pid = $('#comment-' + r.supplemental.parent_approved);
495                         updatePending( -1 );
496
497                         if ( this.comments_listing == 'moderated' ) {
498                                 pid.animate( { 'backgroundColor':'#CCEEBB' }, 400, function(){
499                                         pid.fadeOut();
500                                 });
501                                 return;
502                         }
503                 }
504
505                 c = $.trim(r.data); // Trim leading whitespaces
506                 $(c).hide();
507                 $('#replyrow').after(c);
508
509                 id = $(id);
510                 t.addEvents(id);
511                 bg = id.hasClass('unapproved') ? '#FFFFE0' : id.closest('.widefat, .postbox').css('backgroundColor');
512
513                 id.animate( { 'backgroundColor':'#CCEEBB' }, 300 )
514                         .animate( { 'backgroundColor': bg }, 300, function() {
515                                 if ( pid && pid.length ) {
516                                         pid.animate( { 'backgroundColor':'#CCEEBB' }, 300 )
517                                                 .animate( { 'backgroundColor': bg }, 300 )
518                                                 .removeClass('unapproved').addClass('approved')
519                                                 .find('div.comment_status').html('1');
520                                 }
521                         });
522
523         },
524
525         error : function(r) {
526                 var er = r.statusText;
527
528                 $('#replysubmit .spinner').hide();
529
530                 if ( r.responseText )
531                         er = r.responseText.replace( /<.[^<>]*?>/g, '' );
532
533                 if ( er )
534                         $('#replysubmit .error').html(er).show();
535
536         },
537
538         addcomment: function(post_id) {
539                 var t = this;
540
541                 $('#add-new-comment').fadeOut(200, function(){
542                         t.open(0, post_id, 'add');
543                         $('table.comments-box').css('display', '');
544                         $('#no-comments').remove();
545                 });
546         }
547 };
548
549 $(document).ready(function(){
550         var make_hotkeys_redirect, edit_comment, toggle_all, make_bulk;
551
552         setCommentsList();
553         commentReply.init();
554         $(document).delegate('span.delete a.delete', 'click', function(){return false;});
555
556         if ( typeof $.table_hotkeys != 'undefined' ) {
557                 make_hotkeys_redirect = function(which) {
558                         return function() {
559                                 var first_last, l;
560
561                                 first_last = 'next' == which? 'first' : 'last';
562                                 l = $('.tablenav-pages .'+which+'-page:not(.disabled)');
563                                 if (l.length)
564                                         window.location = l[0].href.replace(/\&hotkeys_highlight_(first|last)=1/g, '')+'&hotkeys_highlight_'+first_last+'=1';
565                         };
566                 };
567
568                 edit_comment = function(event, current_row) {
569                         window.location = $('span.edit a', current_row).attr('href');
570                 };
571
572                 toggle_all = function() {
573                         $('#cb-select-all-1').data( 'wp-toggle', 1 ).trigger( 'click' ).removeData( 'wp-toggle' );
574                 };
575
576                 make_bulk = function(value) {
577                         return function() {
578                                 var scope = $('select[name="action"]');
579                                 $('option[value="' + value + '"]', scope).prop('selected', true);
580                                 $('#doaction').click();
581                         };
582                 };
583
584                 $.table_hotkeys(
585                         $('table.widefat'),
586                         [
587                                 'a', 'u', 's', 'd', 'r', 'q', 'z',
588                                 ['e', edit_comment],
589                                 ['shift+x', toggle_all],
590                                 ['shift+a', make_bulk('approve')],
591                                 ['shift+s', make_bulk('spam')],
592                                 ['shift+d', make_bulk('delete')],
593                                 ['shift+t', make_bulk('trash')],
594                                 ['shift+z', make_bulk('untrash')],
595                                 ['shift+u', make_bulk('unapprove')]
596                         ],
597                         {
598                                 highlight_first: adminCommentsL10n.hotkeys_highlight_first,
599                                 highlight_last: adminCommentsL10n.hotkeys_highlight_last,
600                                 prev_page_link_cb: make_hotkeys_redirect('prev'),
601                                 next_page_link_cb: make_hotkeys_redirect('next'),
602                                 hotkeys_opts: {
603                                         disableInInput: true,
604                                         type: 'keypress',
605                                         noDisable: '.check-column input[type="checkbox"]'
606                                 }
607                         }
608                 );
609         }
610
611         // Quick Edit and Reply have an inline comment editor.
612         $( '#the-comment-list' ).on( 'click', '.comment-inline', function (e) {
613                 e.preventDefault();
614                 var $el = $( this ),
615                         action = 'replyto';
616
617                 if ( 'undefined' !== typeof $el.data( 'action' ) ) {
618                         action = $el.data( 'action' );
619                 }
620
621                 commentReply.open( $el.data( 'commentId' ), $el.data( 'postId' ), action );
622         } );
623 });
624
625 })(jQuery);