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