MediaWiki:Common.js: Unterschied zwischen den Versionen
KKeine Bearbeitungszusammenfassung |
KKeine Bearbeitungszusammenfassung |
||
Zeile 1: | Zeile 1: | ||
/** | * Adds show/hide-button to navigation bars. | ||
* | |||
* @param {jQuery} $content | |||
*/ | |||
function createNavigationBarToggleButton( $content ) { | |||
var j, navChild, navToggle, navToggleText, isCollapsed, | |||
indexNavigationBar = 0; | |||
// Iterate over all < div >-elements | |||
var $divs = $content.find( 'div.NavFrame:not(.mw-collapsible)' ); | |||
$divs.each( function ( i, navFrame ) { | |||
indexNavigationBar++; | |||
navToggle = document.createElement( 'a' ); | |||
navToggle.className = 'NavToggle'; | |||
navToggle.setAttribute( 'id', 'NavToggle' + indexNavigationBar ); | |||
navToggle.setAttribute( 'href', '#' ); | |||
$( navToggle ).on( 'click', $.proxy( toggleNavigationBar, null, indexNavigationBar ) ); | |||
isCollapsed = $( navFrame ).hasClass( 'collapsed' ); | |||
/** | |||
* 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'; | |||
} | |||
} | |||
} | } | ||
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 ( j = 0; j < navFrame.childNodes.length; j++ ) { | |||
if ( $( navFrame.childNodes[ j ] ).hasClass( 'NavHead' ) ) { | |||
navToggle.style.color = navFrame.childNodes[ j ].style.color; | |||
navFrame.childNodes[ j ].appendChild( navToggle ); | |||
} | |||
} | |||
navFrame.setAttribute( 'id', 'NavFrame' + indexNavigationBar ); | |||
} ); | |||
} | } | ||
mw.hook( 'wikipage.content' ).add( | mw.hook( 'wikipage.content' ).add( createNavigationBarToggleButton ); |
Version vom 25. Januar 2021, 16:57 Uhr
* Adds show/hide-button to navigation bars.
*
* @param {jQuery} $content
*/
function createNavigationBarToggleButton( $content ) {
var j, navChild, navToggle, navToggleText, isCollapsed,
indexNavigationBar = 0;
// Iterate over all < div >-elements
var $divs = $content.find( 'div.NavFrame:not(.mw-collapsible)' );
$divs.each( function ( i, navFrame ) {
indexNavigationBar++;
navToggle = document.createElement( 'a' );
navToggle.className = 'NavToggle';
navToggle.setAttribute( 'id', 'NavToggle' + indexNavigationBar );
navToggle.setAttribute( 'href', '#' );
$( navToggle ).on( 'click', $.proxy( toggleNavigationBar, null, indexNavigationBar ) );
isCollapsed = $( navFrame ).hasClass( 'collapsed' );
/**
* 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';
}
}
}
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 ( j = 0; j < navFrame.childNodes.length; j++ ) {
if ( $( navFrame.childNodes[ j ] ).hasClass( 'NavHead' ) ) {
navToggle.style.color = navFrame.childNodes[ j ].style.color;
navFrame.childNodes[ j ].appendChild( navToggle );
}
}
navFrame.setAttribute( 'id', 'NavFrame' + indexNavigationBar );
} );
}
mw.hook( 'wikipage.content' ).add( createNavigationBarToggleButton );