User:Tyrok1/monobook.js

From Rosetta Code
Revision as of 00:13, 20 May 2010 by rosettacode>Tyrok1 (Added compatibility tests)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/*========================================
This comparison script was written by
Tyrok1, and has been tested on IE 5.5,
IE 6, IE 7, IE 8, Fx 3.6.2, and
Fe 4.0.280.
========================================*/
function CompareActivate()
{
	//check to see if we're looking at a task page
	var tocEl = document.getElementById("toc");
	if(tocEl)
	{
		//add some instructions
		var toctitleEl = document.getElementById("toctitle");
		var instructionsEl = toctitleEl.appendChild(document.createElement("p"));
		instructionsEl.appendChild(document.createTextNode("Check the boxes next to languages to compare them."));
		instructionsEl.appendChild(document.createElement("br"));
		instructionsEl.appendChild(document.createTextNode("Uncheck all boxes to show all languages."));
		
		//find all of the links to language sections
		var liEls = tocEl.getElementsByTagName("li");
		for(var l  = 0; l < liEls.length; ++l)
		{
			//add a checkbox with the language section's <a> name attribute as its value for easier lookup in the refresh function
			var checkboxEl = document.createElement("input");
			checkboxEl.setAttribute("type", "checkbox");
			checkboxEl.setAttribute("name", "CompareLanguage" + l);
			checkboxEl.setAttribute("id", "CompareLanguage" + l);
			var href = liEls[l].getElementsByTagName("a")[0].getAttribute("href");
			checkboxEl.setAttribute("value", href.substring(href.indexOf("#") + 1, href.length));
			checkboxEl.onclick = CompareRefresh;
			liEls[l].insertBefore(checkboxEl, liEls[l].firstChild);
		}
		
		//and refresh the display for good measure (sometimes browsers like to save <input> states between reloads)
		CompareRefresh();
	}
}

function CompareRefresh()
{
	//refresh the display of the language sections displayed under the TOC
	//first, check to see if we're looking at a task page
	var tocEl = document.getElementById("toc");
	var languages = new Array();
	var numChecked = 0;
	if(tocEl)
	{
		//find all of the checkboxes within the TOC
		var inputEls = tocEl.getElementsByTagName("input");
		for(var i = 0; i < inputEls.length; ++i)
		{
			//if it's one of the ones we added earlier, log the checked status to the languages array for later use
			if(inputEls[i].getAttribute("name").substring(0, ("CompareLanguage").length) == "CompareLanguage")
			{
				languages[inputEls[i].value] = inputEls[i].checked;
				numChecked += (inputEls[i].checked ? 1 : 0);
			}
		}
		//now, find all <a> tags with name attributes
		var aEls = document.getElementsByTagName("a");
		for(var a = 0; a < aEls.length; ++a)
		{
			if(aEls[a].name && typeof languages[aEls[a].name] != "undefined")
			{
				//we can assume that this is a language section
				//make a list of all of the elements between this <a> tag and the next one that has a name attribute
				var topicEls = new Array();
				var curEl = aEls[a].nextSibling;
				while(curEl && (!curEl.tagName || curEl.tagName.toLowerCase() != "a" || !curEl.name || typeof languages[curEl.name] == "undefined"))
				{
					topicEls[topicEls.length] = curEl;
					curEl = curEl.nextSibling;
				};
				
				//either show or hide them depending on the language's checked status (of if none are checked, display all)
				for(var e = 0; e < topicEls.length; ++e)
				{
					if(topicEls[e].tagName && topicEls[e].style)
					{
						topicEls[e].style.display = (numChecked < 1 || languages[aEls[a].name] ? "" : "none");
					}
				}
			}
		}
	}
}

//register the comparison script with the window's load event
if(window.addEventListener)
{
	window.addEventListener("load", CompareActivate, false);
}
else if(window.attachEvent)
{
	window.attachEvent("onload", CompareActivate);
}
else if(document.addEventListener)
{
	document.addEventListener("load", CompareActivate, false);
}
else
{
	window.onload = CompareActivate;
}