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