User:Tyrok1/monobook.js: Difference between revisions
Content added Content deleted
(Created page with 'function CompareActivate() { var tocEl = document.getElementById("toc"); if(tocEl) { var toctitleEl = document.getElementById("toctitle"); var instructionsEl = toctitleEl.…') |
(Commented the code) |
||
Line 1:
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"));
Line 9 ⟶ 11:
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");
Line 21 ⟶ 26:
liEls[l].insertBefore(checkboxEl, liEls[l].firstChild);
}
//and refresh the display for good measure (sometimes browsers like to save <input> states between reloads)
CompareRefresh();
}
Line 27 ⟶ 34:
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();
Line 32 ⟶ 41:
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")
{
Line 41 ⟶ 52:
}
}
//now, find all <a> tags with name attributes
if(aEls[a].name && typeof languages[aEls[a].name] != "undefined")▼
{
▲ if(aEls[a].name && typeof languages[aEls[a].name] != "undefined")
var topicEls = new Array();▼
var curEl = aEls[a].nextSibling;▼
while(curEl && (!curEl.tagName || curEl.tagName.toLowerCase() != "a" || !curEl.name || typeof languages[curEl.name] == "undefined"))▼
{
//we can assume that this is a language section
topicEls[topicEls.length] = curEl;▼
//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();
};▼
▲ while(curEl && (!curEl.tagName || curEl.tagName.toLowerCase() != "a" || !curEl.name || typeof languages[curEl.name] == "undefined"))
▲ {
{
▲ topicEls[topicEls.length] = curEl;
▲ };
//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].
{
topicEls[e].style.display = (numChecked < 1 || languages[aEls[a].name] ? "" : "none");
}
}
}
Line 65 ⟶ 81:
}
//register the comparison script with the window's load event
if(window.addEventListener)
{
|
Revision as of 00:11, 20 May 2010
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;
}