Jump to content

User:Gary/expand hidden templates.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.
/*
	EXPAND HIDDEN TEMPLATES
	Description: Expands and collapses hidden templates on a page.
		Templates affected include {{hidden}}, {{hidden archive top}}, and their derivatives.
	Documentation: [[Wikipedia:Expand Hidden Templates]]
*/

if (typeof(unsafeWindow) != 'undefined')
{
	addPortletLink = mw.util.addPortletLink;
	collapseTable = unsafeWindow.collapseTable;
	expandHiddenTemplates = expandHiddenTemplates;
	toggleNavigationBar = unsafeWindow.toggleNavigationBar;
	wgAction = mw.config.get('wgAction')
}

if (typeof(expandHiddenTemplates) == 'undefined')
	expandHiddenTemplates = {};

expandHiddenTemplatesMain = function()
{
	var nodeText = document.getElementById('t-expand-hidden-templates').firstChild.firstChild.firstChild;
	if (nodeText.nodeValue == 'Expand templates')
		var action = 'expanding';
	else
		var action = 'collapsing';
	
	// .collapseButton
	var buttons = document.getElementsByClassName('collapseButton');
	for (var i = 0; i < buttons.length; i++)
	{
		if (
			(action == 'expanding' && buttons[i].childNodes[1].firstChild.nodeValue == 'show')
			|| (action == 'collapsing' && buttons[i].childNodes[1].firstChild.nodeValue == 'hide')
		)
			buttons[i].childNodes[1].click(); // IE-only?
	}
	
	// .NavToggle
	var toggles = document.getElementsByClassName('NavToggle');
	for (var i = 0; i < toggles.length; i++)
	{
		if (
			(action == 'expanding' && toggles[i].firstChild.nodeValue == '[show]')
			|| (action == 'collapsing' && toggles[i].firstChild.nodeValue == '[hide]')
		)
		toggles[i].click(); // IE-only?
	}
	
	if (buttons.length == 0 && toggles.length == 0)
		alert('There are no templates to expand on this page.');
	else
	{
		if (action == 'expanding')
			nodeText.nodeValue = 'Collapse templates';
		else
			nodeText.nodeValue = 'Expand templates';
	}
}

expandHiddenTemplatesPortletLink = function()
{
    var wgAction = mw.config.get('wgAction');
	if ( mw.util.addPortletLink)
	{
		if ((wgAction != 'view' && wgAction != 'submit' && wgAction != 'edit' && wgAction != 'purge') || navigator.appName.indexOf('Microsoft Internet Explorer') == -1)
			return;
		
		var buttons = document.getElementsByClassName('collapseButton');
		var toggles = document.getElementsByClassName('NavToggle');
		
		if (buttons.length + toggles.length == 0)
			return;
		
		if (typeof(expandHiddenTemplates) == 'undefined')
			expandHiddenTemplates = {};
		
		if (typeof(expandHiddenTemplates.addTab) == 'undefined')
			expandHiddenTemplates.addTab = false;
		
		if (typeof(expandHiddenTemplates.addToNavigation) == 'undefined')
			expandHiddenTemplates.addToNavigation = false;
		
		if (expandHiddenTemplates.addTab)
			mw.util.addPortletLink('p-cactions', 'javascript:expandHiddenTemplatesMain()', 'Expand templates', 't-expand-hidden-templates', 'Expands or hides all collapsible templates');
		if (expandHiddenTemplates.addToNavigation)
			mw.util.addPortletLink('p-navigation', 'javascript:expandHiddenTemplatesMain()', 'Expand templates', 't-expand-hidden-templates', 'Expands or hides all collapsible templates');
		if (!expandHiddenTemplates.addTab && !expandHiddenTemplates.addToNavigation)
			mw.util.addPortletLink('p-tb', 'javascript:expandHiddenTemplatesMain()', 'Expand templates', 't-expand-hidden-templates', 'Expands or hides all collapsible templates');
	}
}

if (typeof(unsafeWindow) == 'undefined')
	$(expandHiddenTemplatesPortletLink);
else
	expandHiddenTemplatesPortletLink();

if (typeof(unsafeWindow) != 'undefined')
	unsafeWindow.expandHiddenTemplatesMain = expandHiddenTemplatesMain;