MediaWiki:Vector.js

Fra Wikipedia, den frie encyklopædi
/* Any JavaScript here will be loaded for users using the Vector skin */
/*
<pre>
*/

// ============================================================
// BEGIN Dynamic Navigation Bars
// NEEDS Enable multiple onload functions 
 
// set up the words in your language
var NavigationBarHide = 'Skjul';
var NavigationBarShow = 'Vis';
 
// set up max count of Navigation Bars on page,
// if there are more, all will be hidden
// NavigationBarShowDefault = 0; // all bars will be hidden
// NavigationBarShowDefault = 1; // on pages with more than 1 bar all bars will be hidden
var NavigationBarShowDefault = 1;
 
 
// shows and hides content and picture (if available) of navigation bars
// Parameters:
//     indexNavigationBar: the index of navigation bar to be toggled
function toggleNavigationBar(indexNavigationBar)
{
    var NavToggle = document.getElementById("NavToggle" + indexNavigationBar);
    var NavFrame = document.getElementById("NavFrame" + indexNavigationBar);
 
    if (!NavFrame || !NavToggle) {
        return false;
    }
 
    // if shown now
    if (NavToggle.firstChild.data == NavigationBarHide) {
        for (
                var NavChild = NavFrame.firstChild;
                NavChild != null;
                NavChild = NavChild.nextSibling
            ) {
            if (NavChild.className == 'NavPic') {
                NavChild.style.display = 'none';
            }
            if (NavChild.className == 'NavContent') {
                NavChild.style.display = 'none';
            }
            if (NavChild.className == 'NavToggle') {
                NavChild.firstChild.data = NavigationBarShow;
            }
        }
 
    // if hidden now
    } else if (NavToggle.firstChild.data == NavigationBarShow) {
        for (
                var NavChild = NavFrame.firstChild;
                NavChild != null;
                NavChild = NavChild.nextSibling
            ) {
            if (NavChild.className == 'NavPic') {
                NavChild.style.display = 'block';
            }
            if (NavChild.className == 'NavContent') {
                NavChild.style.display = 'block';
            }
            if (NavChild.className == 'NavToggle') {
                NavChild.firstChild.data = NavigationBarHide;
            }
        }
    }
}
 
// adds show/hide-button to navigation bars
function createNavigationBarToggleButton()
{
    var indexNavigationBar = 0;
    // iterate over all < div >-elements
    for(
            var i=0; 
            NavFrame = document.getElementsByTagName("div")[i]; 
            i++
        ) {
        // if found a navigation bar
        if (NavFrame.className == "NavFrame") {
 
            indexNavigationBar++;
            var NavToggle = document.createElement("a");
            NavToggle.className = 'NavToggle';
            NavToggle.setAttribute('id', 'NavToggle' + indexNavigationBar);
            NavToggle.setAttribute('href', 'javascript:toggleNavigationBar(' + indexNavigationBar + ');');
 
            var NavToggleText = document.createTextNode(NavigationBarHide);
            NavToggle.appendChild(NavToggleText);
 
            // add NavToggle-Button as first div-element 
            // in < div class="NavFrame" >
            NavFrame.insertBefore(
                NavToggle,
                NavFrame.firstChild
            );
            NavFrame.setAttribute('id', 'NavFrame' + indexNavigationBar);
        }
    }
    // if more Navigation Bars found than Default: hide all
    if (NavigationBarShowDefault < indexNavigationBar && !document.getElementById("NavFrameOpenAll")) {
        for(
                var i=1; 
                i<=indexNavigationBar; 
                i++
        ) {
            toggleNavigationBar(i);
        }
    }
 
}

$(createNavigationBarToggleButton);
 
// END Dynamic Navigation Bars
// ============================================================

// ------------------------------------
// BEGIN JOIN NAVIGATION BARS
function joinFrames(nodes) {
   // helper-function
   // make new NavGroup-div
   var e = document.createElement("div");
   // just making sure class is set in all browsers
   e.className = "NavGroup";
   e.setAttribute("class", "NavGroup");
   e.setAttribute("className", "NavGroup");
   e.setAttribute("id", "NavGroup");
   // append clones
   var node;
   for (i = 0; i < nodes.length; i++) {
   	 node = nodes[i];
   	 if(node) {
	     e.appendChild(node.cloneNode(true));
   	 }
   }
   //alert("after append, e ("+e.className+") has childNodes: "+e.hasChildNodes()+", count: "+e.childNodes.length);
   // insert this before next element of last navframe:
   var last_node = nodes[nodes.length-1];
   if(last_node && last_node.parentNode) {
		var parent_node = last_node.parentNode;
		var next_node = last_node.nextSibling;
	   // remove old nodes
	   for (i = 0; i < nodes.length; i++) {
	     parent_node.removeChild(nodes[i]);
	   }
	 // insert clones
	 if(next_node)  {
		parent_node.insertBefore(e, next_node);
	 }
  }
}
function joinNavigationBars() {
   var nodes = new Array();
   var e = function(x) { return document.getElementById("NavFrame" + x); };
   for(
     var i=1, NavFrame = e(i), NextNavFrame = e(i+1);
     NavFrame;
     i++, NavFrame = NextNavFrame, NextNavFrame = e(i+1)
   ) {
     // continue if already grouped (or no parentNode)
     if (!NavFrame.parentNode || NavFrame.parentNode.className == "NavGroup") {
       continue;
     }
     // if nextframe is adjacent or maybe separated by whitespace (nodeType 3),
     // add next to nodes-array
     if (NavFrame.nextSibling == NextNavFrame ||
         (NavFrame.nextSibling &&
          NavFrame.nextSibling.nodeType == 3 && 
          NavFrame.nextSibling.nextSibling && 
          NavFrame.nextSibling.nextSibling == NextNavFrame
         )
        ) {
       // if no nodes, add NavFrame
       if (!nodes || nodes.length == 0) nodes = [NavFrame];
       nodes.push(NextNavFrame);
     } else if (nodes.length > 1) {
       joinFrames(nodes);
       nodes = new Array();
     }
   }
   if (nodes.length > 1) {
     joinFrames(nodes);
   }
}
$(joinNavigationBars);
// 
// END JOIN NAVIGATION BARS
// ---------------------------------------

/*
</pre>
*/