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