Diferencia entre revisiones de «MediaWiki:Common.js»

Bth0 (discusión | contribs.)
Sin resumen de edición
Bth0 (discusión | contribs.)
Sin resumen de edición
Línea 3: Línea 3:


/**
/**
  * Instead of cluttering up the global scope with
  * Dynamic Navigation Bars. See [[Wikipedia:NavFrame]]
  * variables, they should instead be set as a
  *  
  * property of this global variable
  * Based on script from en.wikipedia.org, 2008-09-15.
  *
  *
  * E.g: Instead of
  * @source www.mediawiki.org/wiki/MediaWiki:Gadget-NavFrame.js
  *   myVar = 'blah';
  * @maintainer Helder.wiki, 2012–2013
  * use
  * @maintainer Krinkle, 2013
*  mcw.myVar = 'blah';
  */
  */
window.mcw = {};
( function () {
 
 
// Set up the words in your language
/* Legacy support */
var collapseCaption = 'hide';
mcw.baseURL = '/';
var expandCaption = 'show';
mcw.wikiURL = '/';
 
var navigationBarHide = '[' + collapseCaption + ']';
 
var navigationBarShow = '[' + expandCaption + ']';
/* Variables for interface text used throughout the script, for ease of translating */
mcw.i18n = {
// Collapsible tables and page loader
hideText: 'hide',
showText: 'show',
// Page loader
loadErrorTitle: 'An error occurred loading the content',
// File upload
defaultLicense: 'License'
};
 
/* Add extra buttons to the classic toolbar */
if ( mw.user.options.get( 'showtoolbar' ) && !mw.user.options.get( 'usebetatoolbar' ) ) {
importScript( 'MediaWiki:Toolbar.js' );
}
 
 
/* Wait for DOMContentLoaded */
$( function() {
 
/**
/**
  * Collapsible tables
  * Shows and hides content and picture (if available) of navigation bars.
  *
  *
  * Based on http://www.mediawiki.org/wiki/Manual:Collapsible_tables#Common.js_script_.28before_1.18.29
  * @param {number} indexNavigationBar The index of navigation bar to be toggled
* @param {jQuery.Event} e Event object
  */
  */
mcw.makeCollapsible = function( $content ) {
function toggleNavigationBar( indexNavigationBar, e ) {
if ( $content === undefined ) {
var navChild,
$content = $( 'table.collapsible' );
navToggle = document.getElementById( 'NavToggle' + indexNavigationBar ),
} else {
navFrame = document.getElementById( 'NavFrame' + indexNavigationBar );
$content = $content.find( 'table.collapsible' );
}
// Prevent browser from jumping to href "#"
if ( !$content.length ) {
e.preventDefault();
if ( !navFrame || !navToggle ) {
return false;
return false;
}
}
var buttonText = ' <span class="collapsible-button">[<span class="jslink">' + mcw.i18n.hideText + '</span>]</span> ';
// If shown now
if ( navToggle.firstChild.data == navigationBarHide ) {
$content.each( function() {
for ( navChild = navFrame.firstChild; navChild != null; navChild = navChild.nextSibling ) {
var $table = $( this ), $header, $collapseButton, firstWidth, secondWidth;
if ( hasClass( navChild, 'NavPic' ) ) {
navChild.style.display = 'none';
// This table is already collapsible
}
if ( $table.data( 'collapsible' ) ) {
if ( hasClass( navChild, 'NavContent' ) ) {
return true;
navChild.style.display = 'none';
}
}
}
navToggle.firstChild.data = navigationBarShow;
// Use the collapse-button if specified otherwise the first header cell of the first row
$header = $table.find( 'tr:first .collapse-button' );
// If hidden now
if ( !$header.length ) {
} else if ( navToggle.firstChild.data == navigationBarShow ) {
$header = $table.find( 'tr:first > th:first' );
for ( navChild = navFrame.firstChild; navChild != null; navChild = navChild.nextSibling ) {
if ( $( navChild ).hasClass( 'NavPic' ) || $( navChild ).hasClass( 'NavContent' ) ) {
navChild.style.display = 'block';
}
}
}
navToggle.firstChild.data = navigationBarHide;
// No header or the table body is empty
}
if ( !$header.length || !$table.find( 'tr:not(tr:first)' ).text().replace( /\n/g, '' ).length ) {
}
return true;
/**
* Adds show/hide-button to navigation bars.
*
* @param {jQuery} $content
*/
function createNavigationBarToggleButton( $content ) {
var i, j, navFrame, navToggle, navToggleText, navChild,
indexNavigationBar = 0,
navFrames = $content.find( 'div.NavFrame' ).toArray();
// Iterate over all (new) nav frames
for ( i = 0; i < navFrames.length; i++ ) {
navFrame = navFrames[i];
// If found a navigation bar
indexNavigationBar++;
navToggle = document.createElement( 'a' );
navToggle.className = 'NavToggle';
navToggle.setAttribute( 'id', 'NavToggle' + indexNavigationBar );
navToggle.setAttribute( 'href', '#' );
$( navToggle ).on( 'click', $.proxy( toggleNavigationBar, null, indexNavigationBar ) );
navToggleText = document.createTextNode( navigationBarHide );
for ( navChild = navFrame.firstChild; navChild != null; navChild = navChild.nextSibling ) {
if ( $( navChild ).hasClass( 'NavPic' ) || $( navChild ).hasClass( 'NavContent' ) ) {
if ( navChild.style.display == 'none' ) {
navToggleText = document.createTextNode( navigationBarShow );
break;
}
}
}
}
// For the button to float properly, it has to be /before/ the cell text
navToggle.appendChild( navToggleText );
if ( $table.hasClass( 'collapse-button-none' ) ) {
// Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked)
$header.append( buttonText );
for ( j = 0; j < navFrame.childNodes.length; j++ ) {
} else {
if ( $( navFrame.childNodes[j] ).hasClass( 'NavHead' ) ) {
$header.prepend( buttonText );
navFrame.childNodes[j].appendChild( navToggle );
}
// Find max button size, and set its min-width to it
$collapseButton = $table.find( '.collapsible-button' );
firstWidth = $collapseButton.width();
$collapseButton.find( '> .jslink' ).text( mcw.i18n.showText );
secondWidth = $collapseButton.width();
if ( firstWidth != secondWidth ) {
if ( firstWidth < secondWidth ) {
$collapseButton.css( 'min-width', secondWidth );
} else {
$collapseButton.css( 'min-width', firstWidth );
}
}
}
}
navFrame.setAttribute( 'id', 'NavFrame' + indexNavigationBar );
// Set the text back to hide if it's not collapsed to begin with
if ( !$table.hasClass( 'collapsed' ) ) {
$collapseButton.find( '> .jslink' ).text( mcw.i18n.hideText );
}
$table.data( 'collapsible', true );
} );
};
 
$( '#mw-content-text' ).on( 'click', 'table.collapsible .collapsible-button .jslink', function( e ) {
var $table = $( this ).closest( 'table.collapsible' );
// Stop table sorting activating when clicking the link
e.stopPropagation();
if ( $table.hasClass( 'collapsed' ) ) {
$table.removeClass( 'collapsed' ).addClass( 'expanded' );
$( this ).text( mcw.i18n.hideText );
} else {
$table.removeClass( 'expanded' ).addClass( 'collapsed' );
$( this ).text( mcw.i18n.showText );
}
}
} );
}
mcw.makeCollapsible();
mw.hook( 'wikipage.content' ).add( createNavigationBarToggleButton );
}());