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