MediaWiki:Common.js: Difference between revisions

From FSAirlinesWiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
// ============================================================
// BEGIN Enable multiple onload functions
// setup onload functions this way:
// aOnloadFunctions[aOnloadFunctions.length] = function_name; // without brackets!
if (!window.aOnloadFunctions) {
  var aOnloadFunctions = new Array();
}
window.onload = function() {
  if (window.aOnloadFunctions) {
    for (var _i=0; _i<aOnloadFunctions.length; _i++) {
      aOnloadFunctions[_i]();
    }
  }
}
// END Enable multiple onload functions
// ============================================================


// If the page name (wgPageName) contains "MediaWiki" and we're in normal view
//================================================================================
// mode, add "mainpage" class to the body element
//*** Dynamic Navigation Bars
if ( /^MediaWiki(\/.+)?$/.test( wgPageName ) && wgAction == 'view' ) {
addOnloadHook(function() {
// set up the words in your language
document.body.className += ' mainpage';
var NavigationBarHide = '[hide]';
});
var NavigationBarShow = '[show]';
}
 
// set up max count of Navigation Bars on page,
// switches for scripts
// if there are more, all will be hidden
// TODO: migrate to JSConfig
// NavigationBarShowDefault = 0; // all bars will be hidden
// var load_extratabs = true;
// NavigationBarShowDefault = 1; // on pages with more than 1 bar all bars will be hidden
var load_edittools = true;
if (typeof NavigationBarShowDefault == 'undefined' ) {
 
    var NavigationBarShowDefault = 0;
// extra drop down menu on editing for adding special characters
}
importScript( 'MediaWiki:Edittools.js' );
 
// shows and hides content and picture (if available) of navigation bars
// Editpage scripts
  // Parameters:
if( wgAction == 'edit' || wgAction == 'submit' ) {
  //    indexNavigationBar: the index of navigation bar to be toggled
importScript( 'MediaWiki:Editpage.js' );
  function toggleNavigationBar(indexNavigationBar)
}
{
 
    var NavToggle = document.getElementById("NavToggle" + indexNavigationBar);
/* End of extra pages */
    var NavFrame = document.getElementById("NavFrame" + indexNavigationBar);
 
/* Test if an element has a certain class **************************************
    if (!NavFrame || !NavToggle) {
  *
        return false;
* From English Wikipedia, 2008-09-15
    }
*
   
* Description: Uses regular expressions and caching for better performance.
    // if shown now
  * Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
    if (NavToggle.firstChild.data == NavigationBarHide) {
  */
        for (
var hasClass = (function() {
                var NavChild = NavFrame.firstChild;
var reCache = {};
                NavChild != null;
return function( element, className ) {
                NavChild = NavChild.nextSibling
return ( reCache[className] ? reCache[className] : ( reCache[className] = new RegExp( "(?:\\s|^)" + className + "(?:\\s|$)" ) ) ).test( element.className );
            ) {
};
            if (NavChild.className == 'NavPic') {
})();
                NavChild.style.display = 'none';
 
            }
/** Collapsible tables *********************************************************
            if (NavChild.className == 'NavContent') {
  *
                NavChild.style.display = 'none';
* From English Wikipedia, 2008-09-15
            }
*
            if (NavChild.className == 'NavToggle') {
*  Description: Allows tables to be collapsed, showing only the header. See
                NavChild.firstChild.data = NavigationBarShow;
*              [[Wikipedia:NavFrame]].
            }
*  Maintainers: [[User:R. Koot]]
        }
*/
var autoCollapse = 2;
    // if hidden now
var collapseCaption = 'hide';
    } else if (NavToggle.firstChild.data == NavigationBarShow) {
var expandCaption = 'show';
        for (
 
                var NavChild = NavFrame.firstChild;
function collapseTable( tableIndex ) {
                NavChild != null;
var Button = document.getElementById( 'collapseButton' + tableIndex );
                NavChild = NavChild.nextSibling
var Table = document.getElementById( 'collapsibleTable' + tableIndex );
            ) {
 
            if (NavChild.className == 'NavPic') {
if ( !Table || !Button ) {
                NavChild.style.display = 'block';
return false;
            }
}
            if (NavChild.className == 'NavContent') {
 
                NavChild.style.display = 'block';
var Rows = Table.rows;
            }
 
            if (NavChild.className == 'NavToggle') {
if ( Button.firstChild.data == collapseCaption ) {
                NavChild.firstChild.data = NavigationBarHide;
for ( var i = 1; i < Rows.length; i++ ) {
            }
Rows[i].style.display = 'none';
        }
}
    }
Button.firstChild.data = expandCaption;
}
} else {
for ( var i = 1; i < Rows.length; i++ ) {
// adds show/hide-button to navigation bars
Rows[i].style.display = Rows[0].style.display;
function createNavigationBarToggleButton()
}
{
Button.firstChild.data = collapseCaption;
    var indexNavigationBar = 0;
}
    // iterate over all < div >-elements
}
    var divs = document.getElementsByTagName("div");
 
    for (var i=0; i<divs.length; i++) {
function createCollapseButtons(){
        var NavFrame = divs[i];
var tableIndex = 0;
        // if found a navigation bar
var NavigationBoxes = new Object();
        if (NavFrame.className == "NavFrame") {
var Tables = document.getElementsByTagName( 'table' );
 
            indexNavigationBar++;
for ( var i = 0; i < Tables.length; i++ ) {
            var NavToggle = document.createElement("a");
if ( hasClass( Tables[i], 'collapsible' ) ) {
            NavToggle.className = 'NavToggle';
/* only add button and increment count if there is a header row to work with */
            NavToggle.setAttribute('id', 'NavToggle' + indexNavigationBar);
var HeaderRow = Tables[i].getElementsByTagName( 'tr' )[0];
            NavToggle.setAttribute('href', 'javascript:toggleNavigationBar(' + indexNavigationBar + ');');
if( !HeaderRow ) {
continue;
            var NavToggleText = document.createTextNode(NavigationBarHide);
}
            NavToggle.appendChild(NavToggleText);
var Header = HeaderRow.getElementsByTagName( 'th' )[0];
if( !Header ) {
            // add NavToggle-Button as first div-element
continue;
            // in < div class="NavFrame" >
}
            NavFrame.insertBefore(
 
                NavToggle,
NavigationBoxes[tableIndex] = Tables[i];
                NavFrame.firstChild
Tables[i].setAttribute( 'id', 'collapsibleTable' + tableIndex );
            );
 
            NavFrame.setAttribute('id', 'NavFrame' + indexNavigationBar);
var Button    = document.createElement( 'span' );
        }
var ButtonLink = document.createElement( 'a' );
    }
var ButtonText = document.createTextNode( collapseCaption );
    // if more Navigation Bars found than Default: hide all
 
    if (NavigationBarShowDefault < indexNavigationBar) {
Button.style.styleFloat = 'right';
        for(
Button.style.cssFloat = 'right';
                var i=1;  
Button.style.fontWeight = 'normal';
                i<=indexNavigationBar;  
Button.style.textAlign = 'right';
                i++
Button.style.width = '6em';
        ) {
 
            toggleNavigationBar(i);
ButtonLink.style.color = Header.style.color;
        }
ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
ButtonLink.setAttribute( 'href', "javascript:collapseTable(" + tableIndex + ");" );
ButtonLink.appendChild( ButtonText );
 
Button.appendChild( document.createTextNode( '[' ) );
Button.appendChild( ButtonLink );
Button.appendChild( document.createTextNode( ']' ) );
 
Header.insertBefore( Button, Header.childNodes[0] );
tableIndex++;
}
     }
     }
 
for ( var i = 0; i < tableIndex; i++ ) {
  }
if ( hasClass( NavigationBoxes[i], 'collapsed' ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], 'autocollapse' ) ) ) {
   
collapseTable( i );
  addOnloadHook(createNavigationBarToggleButton);
}
}
}
 
addOnloadHook( createCollapseButtons );
 
/** Dynamic Navigation Bars (experimental) *************************************
*
* From English Wikipedia, 2008-09-15
*
*  Description: See [[Wikipedia:NavFrame]].
* Maintainers: UNMAINTAINED
  */
 
// set up the words in your language
var NavigationBarHide = '[' + collapseCaption + ']';
var NavigationBarShow = '[' + expandCaption + ']';
 
// 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 ( hasClass( NavChild, 'NavPic' ) ) {
NavChild.style.display = 'none';
}
if ( hasClass( NavChild, 'NavContent' ) ) {
NavChild.style.display = 'none';
}
}
NavToggle.firstChild.data = NavigationBarShow;
 
// if hidden now
} else if( NavToggle.firstChild.data == NavigationBarShow ) {
for( var NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling ) {
if( hasClass( NavChild, 'NavPic' ) ) {
NavChild.style.display = 'block';
}
if( hasClass( NavChild, 'NavContent' ) ) {
NavChild.style.display = 'block';
}
}
NavToggle.firstChild.data = NavigationBarHide;
}
}
 
// adds show/hide-button to navigation bars
function createNavigationBarToggleButton() {
var indexNavigationBar = 0;
// iterate over all < div >-elements
var divs = document.getElementsByTagName( 'div' );
for( var i = 0; NavFrame = divs[i]; i++ ) {
// if found a navigation bar
if( hasClass( NavFrame, '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 );
for( var NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling ) {
if ( hasClass( NavChild, 'NavPic' ) || hasClass( NavChild, 'NavContent' ) ) {
if( NavChild.style.display == 'none' ) {
NavToggleText = document.createTextNode( NavigationBarShow );
break;
}
}
}
 
NavToggle.appendChild(NavToggleText);
// 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( hasClass( NavFrame.childNodes[j], 'NavHead' ) ) {
NavFrame.childNodes[j].appendChild( NavToggle );
}
}
NavFrame.setAttribute( 'id', 'NavFrame' + indexNavigationBar );
}
}
}
addOnloadHook( createNavigationBarToggleButton );

Latest revision as of 21:20, 8 September 2010

// ============================================================
 // BEGIN Enable multiple onload functions
 
 // setup onload functions this way:
 // aOnloadFunctions[aOnloadFunctions.length] = function_name; // without brackets!
 
 if (!window.aOnloadFunctions) {
   var aOnloadFunctions = new Array();
 }
 
 window.onload = function() {
   if (window.aOnloadFunctions) {
     for (var _i=0; _i<aOnloadFunctions.length; _i++) {
       aOnloadFunctions[_i]();
     }
   }
 }
 
 // END Enable multiple onload functions
 // ============================================================

 //================================================================================
 //*** Dynamic Navigation Bars
 
 // set up the words in your language
 var NavigationBarHide = '[hide]';
 var NavigationBarShow = '[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
 if (typeof NavigationBarShowDefault == 'undefined' ) {
     var NavigationBarShowDefault = 0;
 }
 
 // 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
    var divs = document.getElementsByTagName("div");
    for (var i=0;  i<divs.length; i++) {
        var NavFrame = divs[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) {
        for(
                var i=1; 
                i<=indexNavigationBar; 
                i++
        ) {
            toggleNavigationBar(i);
        }
    }
 
 }
 
 addOnloadHook(createNavigationBarToggleButton);