MediaWiki:Common.js: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
| Line 2: | Line 2: | ||
/* Any JavaScript here will be loaded for all users on every page load. */ | /* Any JavaScript here will be loaded for all users on every page load. */ | ||
/** | |||
* Hideable tables ********************************************************* | |||
* | |||
* Description: Allows tables to be hidden, showing nothing. See | |||
* [[Wikipedia:NavFrame]]. | |||
* Maintainers: [[User:Jesper Hanberg, DTU Danchip]] | |||
*/ | |||
var autoHide = 2; | |||
var hideCaption = 'hide'; | |||
var expandCaption = 'show'; | |||
window.hideTable = function ( tableIndex ) { | |||
var Button = document.getElementById( 'hideButton' + tableIndex ); | |||
var Table = document.getElementById( 'hideableTable' + tableIndex ); | |||
if ( !Table || !Button ) { | |||
return false; | |||
} | |||
var Rows = Table.rows; | |||
var i; | |||
if ( Button.firstChild.data === hideCaption ) { | |||
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 = hideCaption; | |||
} | |||
}; | |||
function createHideButtons() { | |||
var tableIndex = 0; | |||
var NavigationBoxes = {}; | |||
var Tables = document.getElementsByTagName( 'table' ); | |||
var i; | |||
function handleButtonLink( index, e ) { | |||
window.hideTable( index ); | |||
e.preventDefault(); | |||
} | |||
for ( i = 0; i < Tables.length; i++ ) { | |||
if ( $( Tables[i] ).hasClass( 'hideable' ) ) { | |||
/* 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', 'hideableTable' + tableIndex ); | |||
var Button = document.createElement( 'span' ); | |||
var ButtonLink = document.createElement( 'a' ); | |||
var ButtonText = document.createTextNode( hideCaption ); | |||
Button.className = 'hideButton'; /* Styles are declared in Common.css */ | |||
ButtonLink.style.color = Header.style.color; | |||
ButtonLink.setAttribute( 'id', 'hideButton' + 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( 'hidden' ) || ( tableIndex >= autoHide && $( NavigationBoxes[i] ).hasClass( 'autohide' ) ) ) { | |||
window.hideTable( i ); | |||
} | |||
else if ( $( NavigationBoxes[i] ).hasClass ( 'innerhide' ) ) { | |||
var element = NavigationBoxes[i]; | |||
while ((element = element.parentNode)) { | |||
if ( $( element ).hasClass( 'outerhide' ) ) { | |||
window.hideTable ( i ); | |||
break; | |||
} | |||
} | |||
} | |||
} | |||
} | |||
$( createHideButtons ); | |||
/** | /** | ||