MediaWiki:Common.js: Unterschied zwischen den Versionen

Aus HuskyWiki
KKeine Bearbeitungszusammenfassung
KKeine Bearbeitungszusammenfassung
Zeile 1: Zeile 1:
/**
* Adds show/hide-button to navigation bars.
* Collapsible tables
*
*
* @param {jQuery} $content
* Allows tables to be collapsed, showing only the header. See [[Help:Collapsing]].
*/
*
function createNavigationBarToggleButton( $content ) {
* @version 2.0.3 (2014-03-14)
var j, navChild, navToggle, navToggleText, isCollapsed,
* @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
indexNavigationBar = 0;
* @author [[User:R. Koot]]
// Iterate over all < div >-elements
* @author [[User:Krinkle]]
var $divs = $content.find( 'div.NavFrame:not(.mw-collapsible)' );
* @deprecated Since MediaWiki 1.20: Use class="mw-collapsible" instead which
$divs.each( function ( i, navFrame ) {
* is supported in MediaWiki core.
indexNavigationBar++;
*/
navToggle = document.createElement( 'a' );
navToggle.className = 'NavToggle';
navToggle.setAttribute( 'id', 'NavToggle' + indexNavigationBar );
navToggle.setAttribute( 'href', '#' );
$( navToggle ).on( 'click', $.proxy( toggleNavigationBar, null, indexNavigationBar ) );


var autoCollapse = 2;
isCollapsed = $( navFrame ).hasClass( 'collapsed' );
var collapseCaption = 'hide';
/**
var expandCaption = 'show';
* Check if any children are already hidden. This loop is here for backwards compatibility:
var tableIndex = 0;
* 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
function collapseTable( tableIndex ) {
* the content visible without JavaScript support), the new recommended way is to add the class
var Button = document.getElementById( 'collapseButton' + tableIndex );
* "collapsed" to the NavFrame itself, just like with collapsible tables.
var Table = document.getElementById( 'collapsibleTable' + tableIndex );
*/
 
for ( navChild = navFrame.firstChild; navChild !== null && !isCollapsed; navChild = navChild.nextSibling ) {
if ( !Table || !Button ) {
if ( $( navChild ).hasClass( 'NavPic' ) || $( navChild ).hasClass( 'NavContent' ) ) {
return false;
if ( navChild.style.display === 'none' ) {
}
isCollapsed = true;
 
}
var Rows = Table.rows;
}
var i;
var $row0 = $(Rows[0]);
 
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 = $row0.css( 'display' );
}
Button.firstChild.data = collapseCaption;
}
}
 
function createClickHandler( tableIndex ) {
return function ( e ) {
e.preventDefault();
collapseTable( tableIndex );
};
}
 
function createCollapseButtons( $content ) {
var NavigationBoxes = {};
var $Tables = $content.find( 'table' );
var i;
 
$Tables.each( function( i, table ) {
if ( $(table).hasClass( 'collapsible' ) ) {
 
/* only add button and increment count if there is a header row to work with */
var HeaderRow = table.getElementsByTagName( 'tr' )[0];
if ( !HeaderRow ) {
return;
}
}
var Header = table.getElementsByTagName( 'th' )[0];
if ( isCollapsed ) {
if ( !Header ) {
for ( navChild = navFrame.firstChild; navChild !== null; navChild = navChild.nextSibling ) {
return;
if ( $( navChild ).hasClass( 'NavPic' ) || $( navChild ).hasClass( 'NavContent' ) ) {
navChild.style.display = 'none';
}
}
}
}
navToggleText = document.createTextNode( isCollapsed ? navigationBarShow : navigationBarHide );
navToggle.appendChild( navToggleText );


NavigationBoxes[ tableIndex ] = table;
// Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked)
table.setAttribute( 'id', 'collapsibleTable' + tableIndex );
for ( j = 0; j < navFrame.childNodes.length; j++ ) {
 
if ( $( navFrame.childNodes[ j ] ).hasClass( 'NavHead' ) ) {
var Button    = document.createElement( 'span' );
navToggle.style.color = navFrame.childNodes[ j ].style.color;
var ButtonLink = document.createElement( 'a' );
navFrame.childNodes[ j ].appendChild( navToggle );
var ButtonText = document.createTextNode( collapseCaption );
}
// Styles are declared in [[MediaWiki:Common.css]]
}
Button.className = 'collapseButton';
navFrame.setAttribute( 'id', 'NavFrame' + indexNavigationBar );
 
} );
ButtonLink.style.color = Header.style.color;
ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
ButtonLink.setAttribute( 'href', '#' );
$( ButtonLink ).on( 'click', createClickHandler( 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' ) )
) {
collapseTable( i );
}
}
}
}


mw.hook( 'wikipage.content' ).add( createCollapseButtons );
mw.hook( 'wikipage.content' ).add( createNavigationBarToggleButton );

Version vom 25. Januar 2021, 17: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 );