2d7ddbe867516a97a940b392b291d795a48f1a87
[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 = $('#comments-form .tablenav :input[name="_total"]');
8         perPageInput = $('#comments-form .tablenav :input[name="_per_page"]');
9         pageInput = $('#comments-form .tablenav :input[name="_page"]');
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;
21                         n = a.html().replace(/[ ,.]+/g, '');
22                         n = parseInt(n,10);
23                         if ( isNaN(n) ) return;
24                         n = n + ( $('#' + settings.element).is('.' + settings.dimClass) ? 1 : -1 );
25                         if ( n < 0 ) { n = 0; }
26                         $('#awaiting-mod')[ 0 == n ? 'addClass' : 'removeClass' ]('count-0');
27                         n = n.toString();
28                         if ( n.length > 3 )
29                                 n = n.substr(0, n.length-3)+' '+n.substr(-3);
30                         a.html(n);
31                 });
32         };
33
34         // Send current total, page, per_page and url
35         delBefore = function( settings ) {
36                 settings.data._total = totalInput.val();
37                 settings.data._per_page = perPageInput.val();
38                 settings.data._page = pageInput.val();
39                 settings.data._url = document.location.href;
40
41                 if ( 'undefined' != showNotice && settings.data.action && settings.data.action == 'delete-comment' && !settings.data.spam )
42                         return showNotice.warn() ? settings : false;
43
44                 return settings;
45         };
46
47         /* Updates the current total (as displayed visibly)
48         */
49         updateTotalCount = function( total, time, setConfidentTime ) {
50                 if ( time < lastConfidentTime ) {
51                         return;
52                 }
53                 totalInput.val( total.toString() );
54                 if ( setConfidentTime ) {
55                         lastConfidentTime = time;
56                 }
57                 $('span.total-type-count').each( function() {
58                         var a = $(this), n;
59                         n = totalInput.val().toString();
60                         if ( n.length > 3 )
61                                 n = n.substr(0, n.length-3)+' '+n.substr(-3);
62                         a.html(n);
63                 });
64
65         };
66
67         // In admin-ajax.php, we send back the unix time stamp instead of 1 on success
68         delAfter = function( r, settings ) {
69                 $('span.pending-count').each( function() {
70                         var a = $(this), n;
71                         n = a.html().replace(/[ ,.]+/g, '');
72                         n = parseInt(n,10);
73                         if ( isNaN(n) ) return;
74                         if ( $('#' + settings.element).is('.unapproved') ) { // we deleted a formerly unapproved comment
75                                 n = n - 1;
76                         } else if ( $(settings.target).parents( 'span.unapprove' ).size() ) { // we "deleted" an approved comment from the approved list by clicking "Unapprove"
77                                 n = n + 1;
78                         }
79                         if ( n < 0 ) { n = 0; }
80                         $('#awaiting-mod')[ 0 == n ? 'addClass' : 'removeClass' ]('count-0');
81                         n = n.toString();
82                         if ( n.length > 3 )
83                                 n = n.substr(0, n.length-3)+' '+n.substr(-3);
84                         a.html(n);
85                 });
86
87                 $('span.spam-count').each( function() {
88                         var a = $(this), n;
89                         n = a.html().replace(/[ ,.]+/g, '');
90                         n = parseInt(n,10);
91                         if ( isNaN(n) ) return;
92                         if ( $(settings.target).parents( 'span.spam' ).size() ) { // we marked a comment as spam
93                                 n = n + 1;
94                         } else if ( $('#' + settings.element).is('.spam') ) { // we approved or deleted a comment marked as spam
95                                 n = n - 1;
96                         }
97                         if ( n < 0 ) { n = 0; }
98                         n = n.toString();
99                         if ( n.length > 3 )
100                                 n = n.substr(0, n.length-3)+' '+n.substr(-3);
101                         a.html(n);
102                 });
103
104
105                 // XML response
106                 if ( ( 'object' == typeof r ) && lastConfidentTime < settings.parsed.responses[0].supplemental.time ) {
107                         // Set the total to the known good value (even if this value is a little old, newer values should only be a few less, and so shouldn't mess up the page links)
108                         updateTotalCount( settings.parsed.responses[0].supplemental.total, settings.parsed.responses[0].supplemental.time, true );
109                         if ( $.trim( settings.parsed.responses[0].supplemental.pageLinks ) ) {
110                                 $('.tablenav-pages').find( '.page-numbers' ).remove().end().append( $( settings.parsed.responses[0].supplemental.pageLinks ) );
111                         } else if ( 'undefined' != typeof settings.parsed.responses[0].supplemental.pageLinks ) {
112                                 $('.tablenav-pages').find( '.page-numbers' ).remove();
113                         }
114                 } else {
115                         // Decrement the total
116                         var total = parseInt( totalInput.val(), 10 );
117                         if ( total-- < 0 )
118                                 total = 0;
119                         updateTotalCount( total, r, false );
120                 }
121
122                 if ( theExtraList.size() == 0 || theExtraList.children().size() == 0 ) {
123                         return;
124                 }
125
126                 theList.get(0).wpList.add( theExtraList.children(':eq(0)').remove().clone() );
127                 $('#get-extra-comments').submit();
128         };
129
130         theExtraList = $('#the-extra-comment-list').wpList( { alt: '', delColor: 'none', addColor: 'none' } );
131         theList = $('#the-comment-list').wpList( { alt: '', delBefore: delBefore, dimAfter: dimAfter, delAfter: delAfter, addColor: 'none' } );
132
133 };
134
135 commentReply = {
136
137         init : function() {
138                 var row = $('#replyrow');
139
140                 $('a.cancel', row).click(function() { return commentReply.revert(); });
141                 $('a.save', row).click(function() { return commentReply.send(); });
142                 $('input#author, input#author-email, input#author-url', row).keypress(function(e){
143                         if ( e.which == 13 ) {
144                                 commentReply.send();
145                                 e.preventDefault();
146                                 return false;
147                         }
148                 });
149
150                 // add events
151                 $('#the-comment-list .column-comment > p').dblclick(function(){
152                         commentReply.toggle($(this).parent());
153                 });
154
155                 $('#doaction, #doaction2, #post-query-submit').click(function(e){
156                         if ( $('#the-comment-list #replyrow').length > 0 )
157                                 commentReply.close();
158                 });
159
160                 this.comments_listing = $('#comments-form > input[name="comment_status"]').val() || '';
161
162         },
163
164         addEvents : function(r) {
165                 r.each(function() {
166                         $(this).find('.column-comment > p').dblclick(function(){
167                                 commentReply.toggle($(this).parent());
168                         });
169                 });
170         },
171
172         toggle : function(el) {
173                 if ( $(el).css('display') != 'none' )
174                         $(el).find('a.vim-q').click();
175         },
176
177         revert : function() {
178
179                 if ( $('#the-comment-list #replyrow').length < 1 )
180                         return false;
181
182                 $('#replyrow').fadeOut('fast', function(){
183                         commentReply.close();
184                 });
185
186                 return false;
187         },
188
189         close : function() {
190                 $(this.o).fadeIn('fast').css('backgroundColor', '');
191                 $('#com-reply').append( $('#replyrow') );
192                 $('#replycontent').val('');
193                 $('#edithead input').val('');
194                 $('#replysubmit .error').html('').hide();
195                 $('#replysubmit .waiting').hide();
196                 if ( $.browser.msie )
197                         $('#replycontainer, #replycontent').css('height', '120px');
198                 else
199                         $('#replycontainer').resizable('destroy').css('height', '120px');
200         },
201
202         open : function(id, p, a) {
203                 var t = this, editRow, act, h;
204                 t.close();
205                 t.o = '#comment-'+id;
206
207                 $('#replyrow td').attr('colspan', $('.widefat thead th:visible').length);
208                 editRow = $('#replyrow'), rowData = $('#inline-'+id);
209                 act = t.act = (a == 'edit') ? 'edit-comment' : 'replyto-comment';
210
211                 $('#action', editRow).val(act);
212                 $('#comment_post_ID', editRow).val(p);
213                 $('#comment_ID', editRow).val(id);
214
215                 if ( a == 'edit' ) {
216                         $('#author', editRow).val( $('div.author', rowData).text() );
217                         $('#author-email', editRow).val( $('div.author-email', rowData).text() );
218                         $('#author-url', editRow).val( $('div.author-url', rowData).text() );
219                         $('#status', editRow).val( $('div.comment_status', rowData).text() );
220                         $('#replycontent', editRow).val( $('textarea.comment', rowData).val() );
221                         $('#edithead, #savebtn', editRow).show();
222                         $('#replyhead, #replybtn', editRow).hide();
223
224                         h = $(t.o).height();
225                         if ( h > 220 )
226                                 if ( $.browser.msie )
227                                         $('#replycontainer, #replycontent', editRow).height(h-105);
228                                 else
229                                         $('#replycontainer', editRow).height(h-105);
230
231                         $(t.o).after(editRow.hide()).fadeOut('fast', function(){
232                                 $('#replyrow').fadeIn('fast');
233                         });
234                 } else {
235                         $('#edithead, #savebtn', editRow).hide();
236                         $('#replyhead, #replybtn', editRow).show();
237                         $(t.o).after(editRow);
238                         $('#replyrow').hide().fadeIn('fast');
239                 }
240
241                 if ( ! $.browser.msie )
242                         $('#replycontainer').resizable({
243                                 handles : 's',
244                                 axis : 'y',
245                                 minHeight : 80,
246                                 stop : function() {
247                                         $('#replycontainer').width('auto');
248                                 }
249                         });
250
251                 setTimeout(function() {
252                         var rtop, rbottom, scrollTop, vp, scrollBottom;
253
254                         rtop = $('#replyrow').offset().top;
255                         rbottom = rtop + $('#replyrow').height();
256                         scrollTop = window.pageYOffset || document.documentElement.scrollTop;
257                         vp = document.documentElement.clientHeight || self.innerHeight || 0;
258                         scrollBottom = scrollTop + vp;
259
260                         if ( scrollBottom - 20 < rbottom )
261                                 window.scroll(0, rbottom - vp + 35);
262                         else if ( rtop - 20 < scrollTop )
263                                 window.scroll(0, rtop - 35);
264
265                         $('#replycontent').focus().keyup(function(e){
266                                 if (e.which == 27) commentReply.revert(); // close on Escape
267                         });
268                 }, 600);
269
270                 return false;
271         },
272
273         send : function() {
274                 var post = {};
275
276                 $('#replysubmit .waiting').show();
277
278                 $('#replyrow input').each(function() {
279                         post[ $(this).attr('name') ] = $(this).val();
280                 });
281
282                 post.content = $('#replycontent').val();
283                 post.id = post.comment_post_ID;
284                 post.comments_listing = this.comments_listing;
285
286                 $.ajax({
287                         type : 'POST',
288                         url : wpListL10n.url,
289                         data : post,
290                         success : function(x) { commentReply.show(x); },
291                         error : function(r) { commentReply.error(r); }
292                 });
293
294                 return false;
295         },
296
297         show : function(xml) {
298                 var r, c, id, bg;
299
300                 if ( typeof(xml) == 'string' ) {
301                         this.error({'responseText': xml});
302                         return false;
303                 }
304
305                 r = wpAjax.parseAjaxResponse(xml);
306                 if ( r.errors ) {
307                         this.error({'responseText': wpAjax.broken});
308                         return false;
309                 }
310
311                 if ( 'edit-comment' == this.act )
312                         $(this.o).remove();
313
314                 r = r.responses[0];
315                 c = r.data;
316
317                 $(c).hide()
318                 $('#replyrow').after(c);
319
320                 this.o = id = '#comment-'+r.id;
321                 this.revert();
322                 this.addEvents($(id));
323                 bg = $(id).hasClass('unapproved') ? '#ffffe0' : '#fff';
324
325                 $(id)
326                         .animate( { 'backgroundColor':'#CCEEBB' }, 600 )
327                         .animate( { 'backgroundColor': bg }, 600 );
328
329                 $.fn.wpList.process($(id))
330         },
331
332         error : function(r) {
333                 var er = r.statusText;
334
335                 $('#replysubmit .waiting').hide();
336
337                 if ( r.responseText )
338                         er = r.responseText.replace( /<.[^<>]*?>/g, '' );
339
340                 if ( er )
341                         $('#replysubmit .error').html(er).show();
342
343         }
344 };
345
346 $(document).ready(function(){
347         var make_hotkeys_redirect, edit_comment, toggle_all, make_bulk;
348
349         setCommentsList();
350         commentReply.init();
351         $('span.delete a.delete').click(function(){return false;});
352
353         if ( typeof QTags != 'undefined' )
354                 ed_reply = new QTags('ed_reply', 'replycontent', 'replycontainer', 'more');
355
356         if ( typeof $.table_hotkeys != 'undefined' ) {
357                 make_hotkeys_redirect = function(which) {
358                         return function() {
359                                 var first_last, l;
360
361                                 first_last = 'next' == which? 'first' : 'last';
362                                 l = $('.'+which+'.page-numbers');
363                                 if (l.length)
364                                         window.location = l[0].href.replace(/\&hotkeys_highlight_(first|last)=1/g, '')+'&hotkeys_highlight_'+first_last+'=1';
365                         }
366                 };
367                 edit_comment = function(event, current_row) {
368                         window.location = $('span.edit a', current_row).attr('href');
369                 };
370                 toggle_all = function() {
371                         toggleWithKeyboard = true;
372                         $('#comments-form thead #cb input:checkbox').click().attr('checked', '');
373                         toggleWithKeyboard = false;
374                 }
375                 make_bulk = function(value) {
376                         return function(event, _) {
377                                 $('option[value='+value+']').attr('selected', 'selected');
378                                 $('form#comments-form')[0].submit();
379                         }
380                 };
381                 $.table_hotkeys($('table.widefat'),['a', 'u', 's', 'd', 'r', 'q', ['e', edit_comment],
382                                 ['shift+a', make_bulk('approve')], ['shift+s', make_bulk('markspam')],
383                                 ['shift+d', make_bulk('delete')], ['shift+x', toggle_all],
384                                 ['shift+u', make_bulk('unapprove')]],
385                                 {highlight_first: adminCommentsL10n.hotkeys_highlight_first, highlight_last: adminCommentsL10n.hotkeys_highlight_last,
386                                 prev_page_link_cb: make_hotkeys_redirect('prev'), next_page_link_cb: make_hotkeys_redirect('next')}
387                 );
388         }
389 });
390
391 })(jQuery);