Jump to content

User:XDaniX/hideSidebar.js

From Wikipedia, the free encyclopedia
This is the current revision of this page, as edited by Ladsgroup (talk | contribs) at 10:31, 6 February 2021 (Maintenance: Replacing addOnloadHook with native jQuery (mw:ResourceLoader/Migration_guide_(users)#addOnloadHook - phab:T130879)). The present address (URL) is a permanent link to this version.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
/* ======================================================================== *\
	Adds a link that will allow one to show/hide sidebar
 
	version:		1.7.3
	copyright:		(C) 2006-2009 Maciej Jaros (pl:User:Nux, en:User:EcceNux)
	licence:		GNU General Public License v2,
				http://opensource.org/licenses/gpl-license.php
\* ======================================================================== */
 
//
// Cookie hidding + default hidding management
//
// whole sidebar
if (window.hideSidebarByDefault)
{
	window.hideSidebarByDefault = (document.cookie.indexOf("js_hideSidebarByDefault=0")==-1);
}
else
{
	window.hideSidebarByDefault = (document.cookie.indexOf("js_hideSidebarByDefault=1")!=-1);
}
// left panel (vector only)
if (window.hideSidebarPanelByDefault)
{
	window.hideSidebarPanelByDefault = (document.cookie.indexOf("js_hideSidebarPanelByDefault=0")==-1);
}
else
{
	window.hideSidebarPanelByDefault = (document.cookie.indexOf("js_hideSidebarPanelByDefault=1")!=-1);
}
 
//
// CSS settings for skins...
//
/**/
var hideSidebar_css = {
	'vector' : {
		'hide' : {
			 'div#content' : 'margin-left: 0px; ; z-index: 100;'
			,'div#panel' : 'float:left; position:static; background-image:url(/skins-1.5/vector/images/border.png);background-position:left top;background-repeat:repeat-x;'
			,'div#head' : 'margin-left: 0px; ; z-index: 101;'
			,'div#head-base' : 'margin-left: 0px;'
			,'div#p-logo' : 'display: none;'
			,'div#left-navigation' : 'left: 0px;'
		}
	}
	,'monobook' : {
		'hide' : {
			 '#content' : 'margin-left: 0px; z-index: 100;'
			,'#p-cactions' : 'left: 0px;'
			,'#p-cactions a:hover' : 'position:relative !important; z-index:101 !important;'
			,'#p-logo' : 'display: none;'
			,'#p-search' : 'position:relative; z-index:0;'
		}
	}
}
// css skin chooser
switch (skin)
{
	// known
	case "vector":
	case "monobook":
		hideSidebar_skin = skin;
	break;
	// fall back CSS
	default:
		hideSidebar_skin = 'vector';
	break;
}
 
//
// Fast CSS init (faster because not run on load)
//
if (window.hideSidebarByDefault)
{
	var css_text = '';
	var myrules = hideSidebar_css[hideSidebar_skin].hide;
	for (var rule in myrules)
	{
		css_text += ' '+ rule +' {' + myrules[rule] + '}';
	}
	document.write('<style id="hideSidebar_css" type="text/css">'+css_text+'</style>');
}
 
//
// initialization function
//
function initHideSidebar()
{
	var el = document.getElementsByTagName('body')[0];
	var elNew = document.createElement('div');
	elNew.onclick = hideSidebar;
	elNew.style.position = 'absolute'; // for IE.ver<7
	elNew.id = 'hideSidebarElement';
	elNew.style.cssText= ''
		+'cursor:pointer;'
		+'color:#696; font-weight:bold; font-size:20px;'
		+'padding:2px; display:inline-block;'
		+'left:0px; bottom:0px; z-index:2000; position:fixed;'
	;
	el.appendChild(elNew);
 
	//
	// mange hidding by deafault
	if (window.hideSidebarByDefault)
	{
		elNew.innerHTML = '»';
		elNew.sbShown = false;
	}
	else
	{
		elNew.innerHTML = '«';
		elNew.sbShown = true;
	}
 
	if (window.userInitHideSidebar != null)
	{
		userInitHideSidebar();
	}
 
	// hide left panel with an additional button
	var oel_lpanel = document.getElementById('panel');
	if (oel_lpanel)
	{
		// add parent element for the panel
		var nel_lpanel = document.createElement(oel_lpanel.nodeName);
		oel_lpanel.parentNode.replaceChild(nel_lpanel, oel_lpanel);
		nel_lpanel.id = oel_lpanel.id;
		oel_lpanel.id = '';
		// move old panel element to new one
		nel_lpanel.appendChild(oel_lpanel);
 
		// show/hide button
		var close_el = document.createElement('img');
		close_el.id = 'hideSidebar_close_lpanel';
		close_el.lpanel_el = oel_lpanel;
		close_el.style.cssText = 'float:right; margin:1px; padding:1px; cursor:pointer';
		// funs for show/hide while sidebar not hidden
		close_el.funTmpShowPanel = function()
		{
			this.lpanel_el_tmp_display = this.lpanel_el.style.display;
			this.lpanel_el.style.display = 'block';
			this.style.visibility = 'hidden';
		}
		close_el.funRestoreTmpPanelState = function()
		{
			this.lpanel_el.style.display = this.lpanel_el_tmp_display;
			this.style.visibility = 'visible';
		}
		// click fun
		close_el.onclick = function()
		{
			// show
			if (this.lpanel_el.style.display=='none')
			{
				document.cookie='js_hideSidebarPanelByDefault=0; path=/';
				this.lpanel_el.style.display='block';
				this.src = 'http://commons.wikimedia.org/w/thumb.php?f=Edit%20remove.svg&width=15px';
				this.alt = '[-]';
			}
			// hide
			else
			{
				document.cookie='js_hideSidebarPanelByDefault=1; path=/';
				this.lpanel_el.style.display='none';
				this.src = 'http://commons.wikimedia.org/w/thumb.php?f=Edit%20add.svg&width=15px';
				this.alt = '[+]';
			}
		}
		// hide
		if (window.hideSidebarPanelByDefault)
		{
			close_el.lpanel_el.style.display='none';
			close_el.src = 'http://commons.wikimedia.org/w/thumb.php?f=Edit%20add.svg&width=15px';
			close_el.alt = '[+]';
		}
		// show
		else
		{
			close_el.lpanel_el.style.display='block';
			close_el.alt = '[-]';
			close_el.src = 'http://commons.wikimedia.org/w/thumb.php?f=Edit%20remove.svg&width=15px';
		}
		nel_lpanel.insertBefore(close_el, nel_lpanel.firstChild);
 
		if (!window.hideSidebarByDefault)
		{
			close_el.funTmpShowPanel();
		}
	}
}
$(initHideSidebar);
 
//
// Main show/hide function
//
var hideSidebar_css_el = null;
function hideSidebar()
{
	if (this.sbShown)
	//
	// hidding
	//
	{
		hideSidebar_css_el = (hideSidebar_css_el==null) ? document.getElementById('hideSidebar_css') : undefined;
 
		// style element not availble yet
		if (!hideSidebar_css_el)
		{
			//
			// create style element
			//
			var el = document.createElement('style');
			el.id="hideSidebar_css";
			document.getElementsByTagName('head')[0].appendChild(el);
			hideSidebar_css_el = el;
 
			//
			// add rules
			//
			//el.innerHTML = '...';	// yeah would nice right?... FF and Opera only
 
			// the DOM way...
			sheet = (el.sheet) ? el.sheet : el.styleSheet;
			if (sheet.insertRule)	// nonIE
			{
				var myrules = hideSidebar_css[hideSidebar_skin].hide;
				for (var rule in myrules)
				{
					sheet.insertRule (' '+ rule +' {' + myrules[rule] + '}', sheet.cssRules.length);	// at the end
				}
			}
			else if (sheet.addRule)	// IE (+some friends)
			{
				var myrules = hideSidebar_css[hideSidebar_skin].hide;
				for (var rule in myrules)
				{
					sheet.addRule (rule, myrules[rule]);	// at the end
				}
			}
		}
		// already created
		else
		{
			document.getElementsByTagName('head')[0].appendChild(hideSidebar_css_el);
		}
 
		this.innerHTML = '»';
		this.sbShown = false;
 
		document.cookie='js_hideSidebarByDefault=1; path=/';
 
		// restore left panel state
		var close_el = document.getElementById('hideSidebar_close_lpanel');
		if (close_el)
		{
			close_el.funRestoreTmpPanelState();
		}
	}
	//
	// showing
	//
	else
	{
		var el = document.getElementById('hideSidebar_css');
		if (el)
		{
			hideSidebar_css_el = el.parentNode.removeChild(el);
		}
 
		this.innerHTML = '«';
		this.sbShown = true;
 
		document.cookie='js_hideSidebarByDefault=0; path=/';
 
		// show left panel even if hidden
		var close_el = document.getElementById('hideSidebar_close_lpanel');
		if (close_el)
		{
			close_el.funTmpShowPanel();
		}
	}
}