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