MediaWiki:Common.js: Unterschied zwischen den Versionen

Aus HuskyWiki
KKeine Bearbeitungszusammenfassung
KKeine Bearbeitungszusammenfassung
Zeile 1: Zeile 1:
//================================================================================
/**
//*** Dynamic Navigation Bars
* Dynamic Navigation Bars. See [[Wikipedia:NavFrame]]
*
* Based on script from en.wikipedia.org, 2008-09-15.
*
* @source www.mediawiki.org/wiki/MediaWiki:Gadget-NavFrame.js
* @maintainer Helder.wiki, 2012–2013
* @maintainer Krinkle, 2013
*/
( function () {


// set up max count of Navigation Bars on page,
// Set up the words in your language
// if there are more, all will be hidden
var autoCollapse = 0;
// mw.user.options.set( 'NavigationBarShowDefault', 0 ); // all bars will be hidden
var collapseCaption = 'Einklappen';
// mw.user.options.set( 'NavigationBarShowDefault', 1 ); // on pages with more than 1 bar all bars will be hidden
var expandCaption = 'Ausklappen';


// adds show/hide-button to navigation bars
var navigationBarHide = '[' + collapseCaption + ']';
// using 'jquery.makeCollapsible': for messages
var navigationBarShow = '[' + expandCaption + ']';
// using 'user', 'mediawiki.user', 'user.options': wait for overrides in user.js
mw.loader.using( [ 'jquery.makeCollapsible', 'user', 'mediawiki.user', 'user.options' ], function() { mw.hook( 'wikipage.content' ).add( function( $content ) {
// allow setting NavigationBarShowDefault
var showDefaultCount = mw.user.options.get( 'NavigationBarShowDefault',
typeof NavigationBarShowDefault !== 'undefined' ? NavigationBarShowDefault : 1 );
// allow user overrides for b/c
var textHide = typeof NavigationBarHide === 'string' ? NavigationBarHide : mw.msg( 'collapsible-collapse' );
var textShow = typeof NavigationBarShow === 'string' ? NavigationBarShow : mw.msg( 'collapsible-expand' );


// shows and hides content and picture (if available) of navigation bars
/**
// Parameters:
* Shows and hides content and picture (if available) of navigation bars.
//    indexNavigationBar: the index of navigation bar to be toggled
*
function toggleNavigationBar(NavToggle, NavFrame)
* @param {number} indexNavigationBar The index of navigation bar to be toggled
{
* @param {jQuery.Event} e Event object
if (!NavFrame || !NavToggle) {
*/
return false;
function toggleNavigationBar( indexNavigationBar, e ) {
}
var navChild,
navToggle = document.getElementById( 'NavToggle' + indexNavigationBar ),
navFrame = document.getElementById( 'NavFrame' + indexNavigationBar );
 
// Prevent browser from jumping to href "#"
e.preventDefault();
 
if ( !navFrame || !navToggle ) {
return false;
}


// if shown now
// If shown now
if (NavToggle.firstChild.data === textHide) {
if ( navToggle.firstChild.data === navigationBarHide ) {
for (
for ( navChild = navFrame.firstChild; navChild !== null; navChild = navChild.nextSibling ) {
var NavChild = NavFrame.firstChild;
if ( $( navChild ).hasClass( 'NavContent' ) || $( navChild ).hasClass( 'NavPic' ) ) {
NavChild !== null;
navChild.style.display = 'none';
NavChild = NavChild.nextSibling
) {
if (NavChild.className === 'NavPic' || NavChild.className === 'NavContent') {
NavChild.style.display = 'none';
}
}
}
NavToggle.firstChild.data = textShow;
}
navToggle.firstChild.data = navigationBarShow;


// if hidden now
// If hidden now
} else if (NavToggle.firstChild.data === textShow) {
} else if ( navToggle.firstChild.data === navigationBarShow ) {
for (
for ( navChild = navFrame.firstChild; navChild !== null; navChild = navChild.nextSibling ) {
var NavChild = NavFrame.firstChild;
if ( $( navChild ).hasClass( 'NavContent' ) || $( navChild ).hasClass( 'NavPic' ) ) {
NavChild !== null;
navChild.style.display = 'block';
NavChild = NavChild.nextSibling
) {
if (NavChild.className === 'NavPic' || NavChild.className === 'NavContent') {
NavChild.style.display = 'block';
}
}
}
NavToggle.firstChild.data = textHide;
}
}
navToggle.firstChild.data = navigationBarHide;
}
}
}


function toggleNavigationBarFunction(NavToggle, NavFrame) {
/**
return function() {
* Adds show/hide-button to navigation bars.
toggleNavigationBar(NavToggle, NavFrame);
*
return false;
* @param {jQuery} $content
};
*/
}
function createNavigationBarToggleButton( $content ) {
// iterate over all NavFrames
var i, j, navChild, navToggle, navToggleText, isCollapsed,
var NavFrames = $content.find( 'div.NavFrame' );
indexNavigationBar = 0;
// iterate over all < div >-elements
var $divs = $content.find( 'div.NavFrame' );
$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 ) );


// if more Navigation Bars found and not template namespace than Default: hide all
isCollapsed = $( navFrame ).hasClass( 'collapsed' );
var initiallyToggle = showDefaultCount < NavFrames.length && mw.config.get( 'wgNamespaceNumber' ) !== 10;
// backwards compatibility for old technique where the collapsed class was not yet used
for (var i=0; i<NavFrames.length; i++) {
for ( navChild = navFrame.firstChild; navChild !== null && !isCollapsed; navChild = navChild.nextSibling ) {
var NavFrame = NavFrames[i];
if ( $( navChild ).hasClass( 'NavPic' ) || $( navChild ).hasClass( 'NavContent' ) ) {
var NavToggle = document.createElement("a");
if ( navChild.style.display === 'none' ) {
NavToggle.className = 'NavToggle';
isCollapsed = true;
NavToggle.setAttribute('href', '#');
}
}
}
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 );


var NavToggleText = document.createTextNode(textHide);
// Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked)
NavToggle.appendChild(NavToggleText);
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 );
} );
}


// add NavToggle-Button as first div-element
mw.hook( 'wikipage.content' ).add( createNavigationBarToggleButton );
// in < div class="NavFrame" >
NavFrame.insertBefore(NavToggle, NavFrame.firstChild);


NavToggle.onclick = toggleNavigationBarFunction(NavToggle, NavFrame);
}());
if (initiallyToggle) {
toggleNavigationBar(NavToggle, NavFrame);
} else { // make sure that 'display' is always set for every NavFrame
for (
var NavChild = NavFrame.firstChild;
NavChild !== null;
NavChild = NavChild.nextSibling
) {
if (NavChild.className === 'NavPic' || NavChild.className === 'NavContent') {
NavChild.style.display = 'block';
}
}
}
}
})});

Version vom 25. Januar 2021, 16:50 Uhr

/**
 * Dynamic Navigation Bars. See [[Wikipedia:NavFrame]]
 * 
 * Based on script from en.wikipedia.org, 2008-09-15.
 *
 * @source www.mediawiki.org/wiki/MediaWiki:Gadget-NavFrame.js
 * @maintainer Helder.wiki, 2012–2013
 * @maintainer Krinkle, 2013
 */
( function () {

// Set up the words in your language
var autoCollapse = 0;
var collapseCaption = 'Einklappen';
var expandCaption = 'Ausklappen';

var navigationBarHide = '[' + collapseCaption + ']';
var navigationBarShow = '[' + expandCaption + ']';

/**
 * Shows and hides content and picture (if available) of navigation bars.
 *
 * @param {number} indexNavigationBar The index of navigation bar to be toggled
 * @param {jQuery.Event} e Event object
 */
function toggleNavigationBar( indexNavigationBar, e ) {
	var navChild,
		navToggle = document.getElementById( 'NavToggle' + indexNavigationBar ),
		navFrame = document.getElementById( 'NavFrame' + indexNavigationBar );

	// Prevent browser from jumping to href "#"
	e.preventDefault();

	if ( !navFrame || !navToggle ) {
		return false;
	}

	// If shown now
	if ( navToggle.firstChild.data === navigationBarHide ) {
		for ( navChild = navFrame.firstChild; navChild !== null; navChild = navChild.nextSibling ) {
			if ( $( navChild ).hasClass( 'NavContent' ) || $( navChild ).hasClass( 'NavPic' ) ) {
				navChild.style.display = 'none';
			}
		}
		navToggle.firstChild.data = navigationBarShow;

	// If hidden now
	} else if ( navToggle.firstChild.data === navigationBarShow ) {
		for ( navChild = navFrame.firstChild; navChild !== null; navChild = navChild.nextSibling ) {
			if ( $( navChild ).hasClass( 'NavContent' ) || $( navChild ).hasClass( 'NavPic' ) ) {
				navChild.style.display = 'block';
			}
		}
		navToggle.firstChild.data = navigationBarHide;
	}
}

/**
 * Adds show/hide-button to navigation bars.
 *
 * @param {jQuery} $content
 */
function createNavigationBarToggleButton( $content ) {
	var i, j, navChild, navToggle, navToggleText, isCollapsed,
		indexNavigationBar = 0;
	// iterate over all < div >-elements
	var $divs = $content.find( 'div.NavFrame' );
	$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' );
		// backwards compatibility for old technique where the collapsed class was not yet used
		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 );

}());