User:Equazcion/DynaThank.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
if (mw.config.get('wgAction') == 'history'){
	
	// Append the toggle link
	$('#histlegend hr')
		.before(' <b>·</b> ' + 
			'<span style="white-space:nowrap;">' + 
				'<a class="dynathank dtoff plainlinks" style="color:#910300" href="#bzzdf">Thank a user</a>' + 
			'</span>'); 
	
	// Iterate through the History lines and hide the thank links, also remove its displayed pipe separator (|)
	$('ul#pagehistory li').each(function(){
		if ($(this).find('.mw-thanks-thank-link').length > 0){
			$(this).find('.mw-thanks-thank-link').attr('hidden','').css('color','#910300');
			$(this).contents().filter(function(){
		   		return this.nodeType != 1 && $.trim($(this).text()) != '';        
		   	}).slice(-2, -1).replaceWith('');
		}
	});
	
	// Define the animation that plays when toggling to momentarily highlight lines where thank is being displayed
	function flash(obj){
		obj.css('transition','');
		obj.css('background','#FFF2F2');
		setTimeout(function(){
			obj.css('transition','background-color 500ms ease-out'); 
			setTimeout(function(){
				obj.css('background',''); 
				setTimeout(function(){
					obj.css('transition',''); 
				},500);
			},50);
		},50);
	}
	
	// Set the toggle link click function
	$('.dynathank').click(function(){
		if ($('.dynathank').hasClass('dtoff')){
			$('.dynathank').removeClass('dtoff');
			
			// Iterate through the History lines, displaying the thank links and re-inserting their pipe separators (|)
			$('ul#pagehistory li').each(function(){
				if ($(this).find('.mw-thanks-thank-link').length > 0){
					flash($(this));
					$(this).find('.mw-thanks-thank-link').before(' | ');
					$(this).find('.mw-thanks-thank-link').fadeIn(200);
				}
			});
		} else {
			$('.dynathank').addClass('dtoff');
			
			// Iterate through the History lines, hiding the thank links and removing their pipe separators (|)
			$('ul#pagehistory li').each(function(){
				if ($(this).find('.mw-thanks-thank-link').length > 0){
					flash($(this));
					$(this).find('.mw-thanks-thank-link').fadeOut(200, function(){
						$(this).parent().contents().filter(function(){
					   		return this.nodeType != 1 && $.trim($(this).text()) != '';        
					   	}).slice(-2, -1).replaceWith('');
					});
				}
			});
		}
	});
}

/* Thank link creation code (for use with li.each), would become useful if history thank links are removed in MediaWiki:

var rev = $(this).find('input:first').attr('value');
$(this).find('span.mw-history-undo').after('<span class="dynathanks" hidden="hidden"> | <a class="mw-thanks-thank-link" data-revision-id="' + 
	rev + '" title="Send a thank you notification to this user" href="#">thank</a></span>'); 
*/