User:Ilmari Karonen/test.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Ilmari Karonen/test. |
/** Dynamic Navigation Bars (experimental) *************************************
*
* Description: See [[Wikipedia:xNavFrame]].
* Maintainers: UNMAINTAINED
*/
// set up the words in your language
var xNavigationBarHide = '[' + collapseCaption + ']';
var xNavigationBarShow = '[' + expandCaption + ']';
// shows and hides content and picture (if available) of navigation bars
// Parameters:
// indexNavigationBar: the index of navigation bar to be toggled
function xtoggleNavigationBar(indexNavigationBar)
{
var xNavToggle = document.getElementById("xNavToggle" + indexNavigationBar);
var xNavFrame = document.getElementById("xNavFrame" + indexNavigationBar);
if (!xNavFrame || !xNavToggle) {
return false;
}
// if shown now
if (xNavToggle.firstChild.data == xNavigationBarHide) {
for (var xNavChild = xNavFrame.firstChild; xNavChild != null; xNavChild = xNavChild.nextSibling) {
if ( hasClass( xNavChild, 'xNavPic' ) ) {
xNavChild.style.display = 'none';
}
if ( hasClass( xNavChild, 'xNavContent') ) {
xNavChild.style.display = 'none';
}
}
xNavToggle.firstChild.data = xNavigationBarShow;
// if hidden now
} else if (xNavToggle.firstChild.data == xNavigationBarShow) {
for (var xNavChild = xNavFrame.firstChild; xNavChild != null; xNavChild = xNavChild.nextSibling) {
if (hasClass(xNavChild, 'xNavPic')) {
xNavChild.style.display = 'block';
}
if (hasClass(xNavChild, 'xNavContent')) {
xNavChild.style.display = 'block';
}
}
xNavToggle.firstChild.data = xNavigationBarHide;
}
}
// adds show/hide-button to navigation bars
function xcreateNavigationBarToggleButton()
{
var indexNavigationBar = 0;
// iterate over all < div >-elements
var divs = document.getElementsByTagName("div");
for (var i = 0; xNavFrame = divs[i]; i++) {
// if found a navigation bar
if (hasClass(xNavFrame, "xNavFrame")) {
indexNavigationBar++;
var xNavToggle = document.createElement("a");
xNavToggle.className = 'xNavToggle';
xNavToggle.setAttribute('id', 'xNavToggle' + indexNavigationBar);
xNavToggle.setAttribute('href', 'javascript:xtoggleNavigationBar(' + indexNavigationBar + ');');
var isCollapsed = hasClass( xNavFrame, "collapsed" );
/*
* Check if any children are already hidden. This loop is here for backwards compatibility:
* the old way of making xNavFrames start out collapsed was to manually add style="display:none"
* to all the xNavPic/xNavContent elements. Since this was bad for accessibility (no way to make
* the content visible without JavaScript support), the new recommended way is to add the class
* "collapsed" to the xNavFrame itself, just like with collapsible tables.
*/
for (var xNavChild = xNavFrame.firstChild; xNavChild != null; xNavChild = xNavChild.nextSibling) {
if ( hasClass( xNavChild, 'xNavPic' ) || hasClass( xNavChild, 'xNavContent' ) ) {
if (xNavChild.style.display == 'none') {
isCollapsed = true;
break;
}
}
}
if (isCollapsed) {
for (var xNavChild = xNavFrame.firstChild; xNavChild != null; xNavChild = xNavChild.nextSibling) {
if ( hasClass( xNavChild, 'xNavPic' ) || hasClass( xNavChild, 'xNavContent' ) ) {
xNavChild.style.display = 'none';
}
}
}
var xNavToggleText = document.createTextNode(isCollapsed ? xNavigationBarShow : xNavigationBarHide);
xNavToggle.appendChild(xNavToggleText);
// Find the xNavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked)
for(var j=0; j < xNavFrame.childNodes.length; j++) {
if (hasClass(xNavFrame.childNodes[j], "xNavHead")) {
xNavFrame.childNodes[j].appendChild(xNavToggle);
}
}
xNavFrame.setAttribute('id', 'xNavFrame' + indexNavigationBar);
}
}
}
addOnloadHook( xcreateNavigationBarToggleButton );