Jump to content

User:Gimmetrow/dynabars.js

From Wikipedia, the free encyclopedia
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.
// ============================================================
 // BEGIN Dynamic Navigation Bars (experimental)
 
 // set up the words in your language
 //var LocalNavigationBarHide = '[ Hide ]';
 //var LocalNavigationBarShow = '[ Show ]';
 
 // 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 LocalNavigationBarShowDefault = 1;
 
 
 // shows and hides content and picture (if available) of navigation bars
 // Parameters:
 //     indexNavigationBar: the index of navigation bar to be toggled
 function toggleLocalNavigationBar(indexNavigationBar, NavShow, NavHide)
 {
    var NavToggle = document.getElementById("LocalNavToggle" + indexNavigationBar);
    var NavFrame  = document.getElementById("LocalNavFrame"  + indexNavigationBar);
 
    if (!NavFrame || !NavToggle) {
        return false;
    }
 
    // get hide and show texts from title attributes
    //var NavHide = NavFrame.getAttribute('title');
    //var NavShow = NavToggle.getAttribute('title');
    //if (!NavShow) NavShow = LocalNavigationBarShow;
    //if (!NavHide) NavHide = LocalNavigationBarHide;
 
    // if shown now
    //if (NavToggle.firstChild.data == LocalNavigationBarHide) {
    if (NavToggle.firstChild.data == NavHide) {
        for (
                var NavChild = NavFrame.firstChild;
                NavChild != null;
                NavChild = NavChild.nextSibling
            ) {
            if (NavChild.className == 'LocalNavPic') {
                NavChild.style.display = 'none';
            }
            if (NavChild.className == 'LocalNavContent') {
                NavChild.style.display = 'none';
            }
        }
    NavToggle.firstChild.data = NavShow;
    //NavToggle.firstChild.data = LocalNavigationBarShow;
 
    // if hidden now
    //} else if (NavToggle.firstChild.data == LocalNavigationBarShow) {
    } else if (NavToggle.firstChild.data == NavShow) {
        for (
                var NavChild = NavFrame.firstChild;
                NavChild != null;
                NavChild = NavChild.nextSibling
            ) {
            if (NavChild.className == 'LocalNavPic') {
                //if (NavChild.hasAttribute('title')) NavChild.style.display = 'inline';
                //else NavChild.style.display = 'block';
                NavChild.style.display = 'inline';
            }
            if (NavChild.className == 'LocalNavContent') {
                //if (NavChild.hasAttribute('title')) NavChild.style.display = 'inline';
                //else NavChild.style.display = 'block';
                NavChild.style.display = 'inline';
            }
        }
    NavToggle.firstChild.data = NavHide;
    //NavToggle.firstChild.data = LocalNavigationBarHide;
    }
 }
 
 // adds show/hide-button to navigation bars
 function createLocalNavigationBarToggleButton()
 {
    var indexNavigationBar = 0;
    var NavHide, NavShow;
    // iterate over all < div >-elements
    for(
            var i=0; 
            NavFrame = document.getElementsByTagName("span")[i]; //div 
            i++
        ) {
        // if found a navigation bar
        if (NavFrame.className == "LocalNavFrame") {
 
            indexNavigationBar++;
 
            var toggleShow = false;
            // Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked)
            for(
              var j=0; 
              j < NavFrame.childNodes.length; 
              j++
            ) {
              if (NavFrame.childNodes[j].className == "LocalNavShow") toggleShow = true;
              if (NavFrame.childNodes[j].className == "LocalNavHead") {
  
                if (NavFrame.hasAttribute('title')) NavHide = NavFrame.getAttribute('title');
                else NavHide = NavigationBarHide;
 
                if (NavFrame.childNodes[j].hasAttribute('title')) NavShow = NavFrame.childNodes[j].getAttribute('title');
                else NavShow = NavigationBarShow;
 
              //if (NavShow) NavToggle.setAttribute('title', NavShow);
 
                var NavToggle = document.createElement("a");
                NavToggle.className = 'LocalNavToggle';
                NavToggle.setAttribute('id', 'LocalNavToggle' + indexNavigationBar);
  
                NavToggle.setAttribute('href', 'javascript:toggleLocalNavigationBar(' + indexNavigationBar + ",'" + NavShow + "','" + NavHide + "');");
                NavFrame.setAttribute('id', 'LocalNavFrame' + indexNavigationBar);
  
                var NavToggleText = document.createTextNode(NavHide);
                NavToggle.appendChild(NavToggleText);
  
                NavFrame.childNodes[j].appendChild(NavToggle);
                //NavFrame.childNodes[j].insertBefore(NavToggle, NavFrame.childNodes[j].childNodes[0]);//alt
                toggleLocalNavigationBar(indexNavigationBar, NavShow, NavHide);
              }
            }
  
            if (toggleShow) toggleLocalNavigationBar(indexNavigationBar, NavShow, NavHide);
        }
    }
    // if more Navigation Bars found than Default: hide all
    if (indexNavigationBar == 1) toggleLocalNavigationBar(1, NavShow, NavHide); // show 1 by default
  /* broken
    if (LocalNavigationBarShowDefault < indexNavigationBar) {
        for(
                var i=1; 
                i<=indexNavigationBar; 
                i++
        ) {
            toggleLocalNavigationBar(i);
        } 
    } */
 
 }
 
 addOnloadHook(createLocalNavigationBarToggleButton);
 
 // END Dynamic Navigation Bars
 // ============================================================