MediaWiki:Common.js: Unterschied zwischen den Versionen

Aus Splitterwiki
Zur Navigation springen Zur Suche springen
(Test)
K
 
(6 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 21: Zeile 21:
 
  // END Enable multiple onload functions
 
  // END Enable multiple onload functions
 
  // ============================================================
 
  // ============================================================
  /**
+
   
 
  // ============================================================
 
  // ============================================================
 
  // BEGIN Dynamic Navigation Bars
 
  // BEGIN Dynamic Navigation Bars
Zeile 136: Zeile 136:
 
  // END Dynamic Navigation Bars
 
  // END Dynamic Navigation Bars
 
  // ============================================================
 
  // ============================================================
*/
 
  
/**
+
 
  * Dynamic Navigation Bars (experimental)
+
// ============================================================
  *
+
  // BEGIN Collapsible Tables
  * Description: See [[Wikipedia:NavFrame]].
+
  //
  * Maintainers: UNMAINTAINED
+
  // Description: Allows tables to be collapsed, showing only the header.
*/
+
  //
 
   
 
   
/* set up the words in your language */
+
var autoCollapse = 2;
var NavigationBarHide = '[' + Ausblenden + ']';
+
var collapseCaption = 'hide';
var NavigationBarShow = '[' + Einblenden + ']';
+
var expandCaption = 'show';
 
   
 
   
/**
+
window.collapseTable = function ( tableIndex ) {
* Shows and hides content and picture (if available) of navigation bars
+
     var Button = document.getElementById( 'collapseButton' + tableIndex );
* Parameters:
+
     var Table = document.getElementById( 'collapsibleTable' + tableIndex );
*    indexNavigationBar: the index of navigation bar to be toggled
 
**/
 
window.toggleNavigationBar = function ( indexNavigationBar, event ) {
 
     var NavToggle = document.getElementById( 'NavToggle' + indexNavigationBar );
 
     var NavFrame = document.getElementById( 'NavFrame' + indexNavigationBar );
 
    var NavChild;
 
 
   
 
   
     if ( !NavFrame || !NavToggle ) {
+
     if ( !Table || !Button ) {
 
         return false;
 
         return false;
 
     }
 
     }
 
   
 
   
     /* if shown now */
+
     var Rows = Table.rows;
     if ( NavToggle.firstChild.data === NavigationBarHide ) {
+
    var i;
         for ( NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling ) {
+
             if ( $( NavChild ).hasClass( 'NavContent' ) || $( NavChild ).hasClass( 'NavPic' ) ) {
+
     if ( Button.firstChild.data === collapseCaption ) {
                NavChild.style.display = 'none';
+
         for ( i = 1; i < Rows.length; i++ ) {
            }
+
             Rows[i].style.display = 'none';
 +
        }
 +
        Button.firstChild.data = expandCaption;
 +
    } else {
 +
        for ( i = 1; i < Rows.length; i++ ) {
 +
            Rows[i].style.display = Rows[0].style.display;
 
         }
 
         }
    NavToggle.firstChild.data = NavigationBarShow;
+
        Button.firstChild.data = collapseCaption;
 +
    }
 +
};
 +
 +
function createCollapseButtons() {
 +
    var tableIndex = 0;
 +
    var NavigationBoxes = {};
 +
    var Tables = document.getElementsByTagName( 'table' );
 +
    var i;
 
   
 
   
     /* if hidden now */
+
     function handleButtonLink( index, e ) {
    } else if ( NavToggle.firstChild.data === NavigationBarShow ) {
+
         window.collapseTable( index );
         for ( NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling ) {
+
         e.preventDefault();
            if ( $( NavChild ).hasClass( 'NavContent' ) || $( NavChild ).hasClass( 'NavPic' ) ) {
 
                NavChild.style.display = 'block';
 
            }
 
        }
 
         NavToggle.firstChild.data = NavigationBarHide;
 
 
     }
 
     }
 
   
 
   
     event.preventDefault();
+
     for ( i = 0; i < Tables.length; i++ ) {
};
+
        if ( $( Tables[i] ).hasClass( 'collapsible' ) ) {
 +
 +
            /* only add button and increment count if there is a header row to work with */
 +
            var HeaderRow = Tables[i].getElementsByTagName( 'tr' )[0];
 +
            if ( !HeaderRow ) continue;
 +
            var Header = HeaderRow.getElementsByTagName( 'th' )[0];
 +
            if ( !Header ) continue;
 +
 +
            NavigationBoxes[ tableIndex ] = Tables[i];
 +
            Tables[i].setAttribute( 'id', 'collapsibleTable' + tableIndex );
 +
 +
            var Button    = document.createElement( 'span' );
 +
            var ButtonLink = document.createElement( 'a' );
 +
            var ButtonText = document.createTextNode( collapseCaption );
 +
 +
            Button.className = 'collapseButton'; /* Styles are declared in Common.css */
 
   
 
   
/* adds show/hide-button to navigation bars */
+
            ButtonLink.style.color = Header.style.color;
function createNavigationBarToggleButton() {
+
            ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
    var indexNavigationBar = 0;
+
            ButtonLink.setAttribute( 'href', '#' );
    var NavFrame;
+
            $( ButtonLink ).on( 'click', $.proxy( handleButtonLink, ButtonLink, tableIndex ) );
    var NavChild;
+
            ButtonLink.appendChild( ButtonText );
    /* iterate over all < div >-elements */
 
    var divs = document.getElementsByTagName( 'div' );
 
    for ( var i = 0; (NavFrame = divs[i]); i++ ) {
 
        /* if found a navigation bar */
 
        if ( $( NavFrame ).hasClass( 'NavFrame' ) ) {
 
 
   
 
   
             indexNavigationBar++;
+
             Button.appendChild( document.createTextNode( '[' ) );
            var NavToggle = document.createElement( 'a' );
+
             Button.appendChild( ButtonLink );
             NavToggle.className = 'NavToggle';
+
             Button.appendChild( document.createTextNode( ']' ) );
            NavToggle.setAttribute( 'id', 'NavToggle' + indexNavigationBar );
 
             NavToggle.setAttribute( 'href', '#' );
 
            $( NavToggle ).on( 'click', $.proxy( window.toggleNavigationBar, window, indexNavigationBar ) );
 
 
   
 
   
             var isCollapsed = $( NavFrame ).hasClass( 'collapsed' );
+
             Header.insertBefore( Button, Header.firstChild );
            /**
+
             tableIndex++;
            * Check if any children are already hidden. This loop is here for backwards compatibility:
+
        }
            * the old way of making NavFrames start out collapsed was to manually add style="display:none"
+
    }
            * to all the NavPic/NavContent 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 NavFrame itself, just like with collapsible tables.
 
            */
 
            for ( NavChild = NavFrame.firstChild; NavChild != null && !isCollapsed; NavChild = NavChild.nextSibling ) {
 
                if ( $( NavChild ).hasClass( 'NavPic' ) || $( NavChild ).hasClass( 'NavContent' ) ) {
 
                    if ( NavChild.style.display === 'none' ) {
 
                        isCollapsed = true;
 
                    }
 
                }
 
            }
 
             if ( isCollapsed ) {
 
                for ( NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling ) {
 
                    if ( $( NavChild ).hasClass( 'NavPic' ) || $( NavChild ).hasClass( 'NavContent' ) ) {
 
                        NavChild.style.display = 'none';
 
                    }
 
                }
 
            }
 
            var NavToggleText = document.createTextNode( isCollapsed ? NavigationBarShow : NavigationBarHide );
 
            NavToggle.appendChild( NavToggleText );
 
 
   
 
   
            /* Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked) */
+
    for ( i = 0; i < tableIndex; i++ ) {
            for( var j = 0; j < NavFrame.childNodes.length; j++ ) {
+
        if ( $( NavigationBoxes[i] ).hasClass( 'collapsed' ) || ( tableIndex >= autoCollapse && $( NavigationBoxes[i] ).hasClass( 'autocollapse' ) ) ) {
                if ( $( NavFrame.childNodes[j] ).hasClass( 'NavHead' ) ) {
+
            window.collapseTable( i );
                    NavToggle.style.color = NavFrame.childNodes[j].style.color;
+
        }
                     NavFrame.childNodes[j].appendChild( NavToggle );
+
        else if ( $( NavigationBoxes[i] ).hasClass ( 'innercollapse' ) ) {
 +
            var element = NavigationBoxes[i];
 +
            while ((element = element.parentNode)) {
 +
                if ( $( element ).hasClass( 'outercollapse' ) ) {
 +
                     window.collapseTable ( i );
 +
                    break;
 
                 }
 
                 }
 
             }
 
             }
            NavFrame.setAttribute( 'id', 'NavFrame' + indexNavigationBar );
 
 
         }
 
         }
 
     }
 
     }
 
}
 
}
 
   
 
   
mw.hook( 'wikipage.content' ).add( createNavigationBarToggleButton );
+
mw.hook( 'wikipage.content' ).add( createCollapseButtons );
 +
 
 +
// END Collapsible Tables
 +
// ============================================================

Aktuelle Version vom 26. März 2014, 21:52 Uhr

/* Das folgende JavaScript wird für alle Benutzer geladen. */

// ============================================================
 // 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
 // ============================================================
 
 // ============================================================
 // BEGIN Dynamic Navigation Bars
 // NEEDS Enable multiple onload functions 
 
 // set up the words in your language
 var NavigationBarHide = 'Ausblenden';
 var NavigationBarShow = 'Einblenden';
 
 // 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 = 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) {
        for(
                var i=1; 
                i<=indexNavigationBar; 
                i++
        ) {
            toggleNavigationBar(i);
        }
    }
 
 }
 
 aOnloadFunctions[aOnloadFunctions.length] = createNavigationBarToggleButton;
 
 // END Dynamic Navigation Bars
 // ============================================================


 // ============================================================
 // BEGIN Collapsible Tables
 //
 // Description: Allows tables to be collapsed, showing only the header.
 //
 
var autoCollapse = 2;
var collapseCaption = 'hide';
var expandCaption = 'show';
 
window.collapseTable = function ( tableIndex ) {
    var Button = document.getElementById( 'collapseButton' + tableIndex );
    var Table = document.getElementById( 'collapsibleTable' + tableIndex );
 
    if ( !Table || !Button ) {
        return false;
    }
 
    var Rows = Table.rows;
    var i;
 
    if ( Button.firstChild.data === collapseCaption ) {
        for ( i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = 'none';
        }
        Button.firstChild.data = expandCaption;
    } else {
        for ( i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = Rows[0].style.display;
        }
        Button.firstChild.data = collapseCaption;
    }
};
 
function createCollapseButtons() {
    var tableIndex = 0;
    var NavigationBoxes = {};
    var Tables = document.getElementsByTagName( 'table' );
    var i;
 
    function handleButtonLink( index, e ) {
        window.collapseTable( index );
        e.preventDefault();
    }
 
    for ( i = 0; i < Tables.length; i++ ) {
        if ( $( Tables[i] ).hasClass( 'collapsible' ) ) {
 
            /* only add button and increment count if there is a header row to work with */
            var HeaderRow = Tables[i].getElementsByTagName( 'tr' )[0];
            if ( !HeaderRow ) continue;
            var Header = HeaderRow.getElementsByTagName( 'th' )[0];
            if ( !Header ) continue;
 
            NavigationBoxes[ tableIndex ] = Tables[i];
            Tables[i].setAttribute( 'id', 'collapsibleTable' + tableIndex );
 
            var Button     = document.createElement( 'span' );
            var ButtonLink = document.createElement( 'a' );
            var ButtonText = document.createTextNode( collapseCaption );
 
            Button.className = 'collapseButton';  /* Styles are declared in Common.css */
 
            ButtonLink.style.color = Header.style.color;
            ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
            ButtonLink.setAttribute( 'href', '#' );
            $( ButtonLink ).on( 'click', $.proxy( handleButtonLink, ButtonLink, tableIndex ) );
            ButtonLink.appendChild( ButtonText );
 
            Button.appendChild( document.createTextNode( '[' ) );
            Button.appendChild( ButtonLink );
            Button.appendChild( document.createTextNode( ']' ) );
 
            Header.insertBefore( Button, Header.firstChild );
            tableIndex++;
        }
    }
 
    for ( i = 0;  i < tableIndex; i++ ) {
        if ( $( NavigationBoxes[i] ).hasClass( 'collapsed' ) || ( tableIndex >= autoCollapse && $( NavigationBoxes[i] ).hasClass( 'autocollapse' ) ) ) {
            window.collapseTable( i );
        } 
        else if ( $( NavigationBoxes[i] ).hasClass ( 'innercollapse' ) ) {
            var element = NavigationBoxes[i];
            while ((element = element.parentNode)) {
                if ( $( element ).hasClass( 'outercollapse' ) ) {
                    window.collapseTable ( i );
                    break;
                }
            }
        }
    }
}
 
mw.hook( 'wikipage.content' ).add( createCollapseButtons );

 // END Collapsible Tables
 // ============================================================