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