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