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