Changes

Jump to navigation Jump to search

MediaWiki:Common.js

4,757 bytes added, 11:25, 17 October 2015
Added support for collapsible tables (from Wikipedia MediaWiki:Common.js).
/**
* Dynamic Navigation Bars. See [[Wikipedia:NavFrame]] * * Based on script from en.wikipedia.org, 2008-09-15.Collapsible tables
*
* Allows tables to be collapsed, showing only the header. See [[Wikipedia:NavFrame]]. * * @version 2.0.3 (2014-03-14) * @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-NavFramecollapsibleTables.js * @maintainer Helderauthor [[User:R.wiki, 2012–2013Koot]] * @maintainer author [[User:Krinkle, 2013]] * @deprecated Since MediaWiki 1.20: Use class="mw-collapsible" instead which * is supported in MediaWiki core.
*/
( function () {
// Set up the words in your languagevar autoCollapse = 2;
var collapseCaption = 'hide';
var expandCaption = 'show';
var tableIndex = 0;
 
function collapseTable( 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 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 navigationBarHide HeaderRow = table.getElementsByTagName( 'tr' )[' + collapseCaption + '0]'; if ( !HeaderRow ) { return; } var navigationBarShow Header = table.getElementsByTagName( 'th' )[' + expandCaption + '0]'; if ( !Header ) { return; }
/** NavigationBoxes[ tableIndex ] = table; * Shows and hides content and picture table.setAttribute(if available'id', 'collapsibleTable' + tableIndex ) of navigation bars.; * * @param {number} indexNavigationBar The index of navigation bar to be toggled * @param {jQuery var Button = document.Event} e Event object */function toggleNavigationBarcreateElement( indexNavigationBar, e 'span' ) {; var navChild, navToggle ButtonLink = document.getElementByIdcreateElement( 'NavTogglea' + indexNavigationBar ),; navFrame var ButtonText = document.getElementByIdcreateTextNode( collapseCaption ); // Styles are declared in [[MediaWiki:Common.css]] Button.className = 'NavFramecollapseButton' + indexNavigationBar );
// Prevent browser from jumping to ButtonLink.style.color = Header.style.color; ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex ); ButtonLink.setAttribute( 'href "', '#"' ); $( ButtonLink ).on( 'click', createClickHandler( tableIndex ) ); e ButtonLink.preventDefaultappendChild(ButtonText );
if Button.appendChild( !navFrame || !navToggle document.createTextNode( '[' ) {); return false Button.appendChild( ButtonLink ); } Button.appendChild( document.createTextNode( ']' ) );
// If shown now if ( navToggle Header.firstChild.data == navigationBarHide ) { for insertBefore( navChild = navFrameButton, Header.firstChild; navChild != null; navChild = navChild.nextSibling ) { if ( hasClass( navChild, 'NavPic' ) ) { navChild.style.display = 'none'; } if ( hasClass( navChild, 'NavContent' ) ) { navChild.style.display = 'none' tableIndex++; } } navToggle.firstChild.data = navigationBarShow);
// If hidden now } else if ( navToggle.firstChild.data == navigationBarShow ) { for ( navChild i = navFrame.firstChild0; navChild != null i < tableIndex; navChild = navChild.nextSibling i++ ) { if ( $( navChild NavigationBoxes[i] ).hasClass( 'NavPiccollapsed' ) || ( tableIndex >= autoCollapse && $( navChild NavigationBoxes[i] ).hasClass( 'NavContentautocollapse' ) ) ) { navChild collapseTable( i ); } else if ( $( NavigationBoxes[i] ).stylehasClass ( 'innercollapse' ) ) { var element = NavigationBoxes[i]; while ((element = element.parentNode)) { if ( $( element ).display = hasClass( 'blockoutercollapse') ) { collapseTable ( i ); break; } } navToggle.firstChild.data = navigationBarHide; } }
}
 
mw.hook( 'wikipage.content' ).add( createCollapseButtons );
/**
* Adds show/hide-button to navigation bars.Dynamic Navigation Bars (experimental)
*
* @param {jQuery} $contentDescription: See [[Wikipedia:NavFrame]]. * Maintainers: UNMAINTAINED
*/
 
/* set up the words in your language */
var NavigationBarHide = '[' + collapseCaption + ']';
var NavigationBarShow = '[' + expandCaption + ']';
var indexNavigationBar = 0;
 
/**
* Shows and hides content and picture (if available) of navigation bars
* Parameters:
* 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 ) {
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;
}
 
event.preventDefault();
};
 
/* adds show/hide-button to navigation bars */
function createNavigationBarToggleButton( $content ) {
var i, j, navFrame, navToggle, navToggleText, navChild,NavChild; indexNavigationBar = 0, /* iterate over all < div >-elements */ navFrames var $divs = $content.find( 'div' ); $divs.each( function ( i, NavFrame ) { /* if found a navigation bar */ if ( $( NavFrame' ).toArrayhasClass('NavFrame' ) );{
// Iterate over all (new) nav frames for ( i = 0; i < navFrames.length; i++ ) { navFrame = navFrames[i]; // If found a navigation bar indexNavigationBar++; navToggle var NavToggle = document.createElement( 'a' ); navToggle NavToggle.className = 'NavToggle'; navToggle NavToggle.setAttribute( 'id', 'NavToggle' + indexNavigationBar ); navToggle NavToggle.setAttribute( 'href', '#' ); $( navToggle NavToggle ).on( 'click', $.proxy( window.toggleNavigationBar, nullwindow, indexNavigationBar ) );
navToggleText var isCollapsed = document$( NavFrame ).createTextNodehasClass( navigationBarHide '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 NavChild = navFrameNavFrame.firstChild; navChild NavChild != null&& !isCollapsed; navChild NavChild = navChildNavChild.nextSibling ) { if ( $( navChild NavChild ).hasClass( 'NavPic' ) || $( navChild NavChild ).hasClass( 'NavContent' ) ) { if ( navChildNavChild.style.display === 'none' ) { navToggleText isCollapsed = documenttrue; } } } if ( isCollapsed ) { for ( NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling ) { if ( $( NavChild ).createTextNodehasClass( 'NavPic' ) || $( navigationBarShow NavChild );.hasClass( 'NavContent' ) ) { break NavChild.style.display = 'none'; } } } var NavToggleText = document.createTextNode( isCollapsed ? NavigationBarShow : NavigationBarHide ); NavToggle.appendChild( NavToggleText );
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 < navFrameNavFrame.childNodes.length; j++ ) { if ( $( navFrameNavFrame.childNodes[j] ).hasClass( 'NavHead' ) ) { navFrame NavToggle.style.color = NavFrame.childNodes[j].style.color; NavFrame.childNodes[j].appendChild( navToggle NavToggle ); } } navFrame NavFrame.setAttribute( 'id', 'NavFrame' + indexNavigationBar ); } } );
}
mw.hook( 'wikipage.content' ).add( createNavigationBarToggleButton );
 
}());

Navigation menu