MediaWiki:Common.js: Difference between revisions

Till Kraemer (talk | contribs)
No edit summary
Till Kraemer (talk | contribs)
No edit summary
Line 55: Line 55:
/* End of extra pages */
/* End of extra pages */


/** Collapsible tables *********************************************************
/**
* Collapsible tables
  *
  *
  * Description: Allows tables to be collapsed, showing only the header. See
  * Allows tables to be collapsed, showing only the header. See [[Help:Collapsing]].
  *                         http://www.mediawiki.org/wiki/Manual:Collapsible_tables.
  *
  * Maintainers: [[en:User:R. Koot]]
* @version 2.0.3 (2014-03-14)
* @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
  * @author [[User:R. Koot]]
* @author [[User:Krinkle]]
* @deprecated Since MediaWiki 1.20: Use class="mw-collapsible" instead which
* is supported in MediaWiki core.
  */
  */
 
var autoCollapse = 2;
var autoCollapse = 2;
var collapseCaption = 'Hide';
var collapseCaption = 'Hide';
var expandCaption = 'Show';
var expandCaption = 'Show';
var tableIndex = 0;
 
function collapseTable( tableIndex ) {
function collapseTable( tableIndex ) {
     var Button = document.getElementById( 'collapseButton' + tableIndex );
     var Button = document.getElementById( 'collapseButton' + tableIndex );
     var Table = document.getElementById( 'collapsibleTable' + tableIndex );
     var Table = document.getElementById( 'collapsibleTable' + tableIndex );
 
     if ( !Table || !Button ) {
     if ( !Table || !Button ) {
         return false;
         return false;
     }
     }
 
     var Rows = Table.rows;
     var Rows = Table.rows;
     var i;
     var i;
    var $row0 = $(Rows[0]);
 
     if ( Button.firstChild.data === collapseCaption ) {
     if ( Button.firstChild.data === collapseCaption ) {
         for ( i = 1; i < Rows.length; i++ ) {
         for ( i = 1; i < Rows.length; i++ ) {
Line 84: Line 92:
     } else {
     } else {
         for ( i = 1; i < Rows.length; i++ ) {
         for ( i = 1; i < Rows.length; i++ ) {
             Rows[i].style.display = Rows[0].style.display;
             Rows[i].style.display = $row0.css( 'display' );
         }
         }
         Button.firstChild.data = collapseCaption;
         Button.firstChild.data = collapseCaption;
     }
     }
}
}
 
function createClickHandler( tableIndex ) {
function createClickHandler( tableIndex ) {
     return function ( e ) {
     return function ( e ) {
Line 96: Line 104:
     };
     };
}
}
 
function createCollapseButtons() {
function createCollapseButtons( $content ) {
    var tableIndex = 0;
     var NavigationBoxes = {};
     var NavigationBoxes = {};
     var Tables = document.getElementsByTagName( 'table' );
     var $Tables = $content.find( 'table' );
     var i;
     var i;
 
     for ( i = 0; i < Tables.length; i++ ) {
     $Tables.each( function( i, table ) {
         if ( $( Tables[i] ).hasClass( 'collapsible' ) ) {
         if ( $(table).hasClass( 'collapsible' ) ) {
 
             /* only add button and increment count if there is a header row to work with */
             /* only add button and increment count if there is a header row to work with */
             var HeaderRow = Tables[i].getElementsByTagName( 'tr' )[0];
             var HeaderRow = table.getElementsByTagName( 'tr' )[0];
             if ( !HeaderRow ) {
             if ( !HeaderRow ) {
                 continue;
                 return;
             }
             }
             var Header = HeaderRow.getElementsByTagName( 'th' )[0];
             var Header = table.getElementsByTagName( 'th' )[0];
             if ( !Header ) {
             if ( !Header ) {
                 continue;
                 return;
             }
             }
 
             NavigationBoxes[ tableIndex ] = Tables[i];
             NavigationBoxes[ tableIndex ] = table;
             Tables[i].setAttribute( 'id', 'collapsibleTable' + tableIndex );
             table.setAttribute( 'id', 'collapsibleTable' + tableIndex );
 
             var Button    = document.createElement( 'span' );
             var Button    = document.createElement( 'span' );
             var ButtonLink = document.createElement( 'a' );
             var ButtonLink = document.createElement( 'a' );
Line 124: Line 131:
             // Styles are declared in [[MediaWiki:Common.css]]
             // Styles are declared in [[MediaWiki:Common.css]]
             Button.className = 'collapseButton';
             Button.className = 'collapseButton';
 
             ButtonLink.style.color = Header.style.color;
             ButtonLink.style.color = Header.style.color;
             ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
             ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
Line 130: Line 137:
             $( ButtonLink ).on( 'click', createClickHandler( tableIndex ) );
             $( ButtonLink ).on( 'click', createClickHandler( tableIndex ) );
             ButtonLink.appendChild( ButtonText );
             ButtonLink.appendChild( ButtonText );
 
             Button.appendChild( document.createTextNode( '[' ) );
             Button.appendChild( document.createTextNode( '[' ) );
             Button.appendChild( ButtonLink );
             Button.appendChild( ButtonLink );
             Button.appendChild( document.createTextNode( ']' ) );
             Button.appendChild( document.createTextNode( ']' ) );
 
             Header.insertBefore( Button, Header.firstChild );
             Header.insertBefore( Button, Header.firstChild );
             tableIndex++;
             tableIndex++;
         }
         }
     }
     } );
 
     for ( i = 0;  i < tableIndex; i++ ) {
     for ( i = 0;  i < tableIndex; i++ ) {
         if ( $( NavigationBoxes[i] ).hasClass( 'collapsed' ) ||
         if ( $( NavigationBoxes[i] ).hasClass( 'collapsed' ) ||
Line 145: Line 152:
         ) {
         ) {
             collapseTable( i );
             collapseTable( i );
         }  
         }
         else if ( $( NavigationBoxes[i] ).hasClass ( 'innercollapse' ) ) {
         else if ( $( NavigationBoxes[i] ).hasClass ( 'innercollapse' ) ) {
             var element = NavigationBoxes[i];
             var element = NavigationBoxes[i];
Line 157: Line 164:
     }
     }
}
}
 
mw.hook( 'wikipage.content' ).add( createCollapseButtons );
mw.hook( 'wikipage.content' ).add( createCollapseButtons );
 
/**
/**
  * Dynamic Navigation Bars (experimental)
  * Dynamic Navigation Bars (experimental)
Line 166: Line 173:
  * Maintainers: UNMAINTAINED
  * Maintainers: UNMAINTAINED
  */
  */
 
/* set up the words in your language */
/* set up the words in your language */
var NavigationBarHide = '[' + collapseCaption + ']';
var NavigationBarHide = '[' + collapseCaption + ']';
var NavigationBarShow = '[' + expandCaption + ']';
var NavigationBarShow = '[' + expandCaption + ']';
var indexNavigationBar = 0;
 
/**
/**
  * Shows and hides content and picture (if available) of navigation bars
  * Shows and hides content and picture (if available) of navigation bars
Line 180: Line 188:
     var NavFrame = document.getElementById( 'NavFrame' + indexNavigationBar );
     var NavFrame = document.getElementById( 'NavFrame' + indexNavigationBar );
     var NavChild;
     var NavChild;
 
     if ( !NavFrame || !NavToggle ) {
     if ( !NavFrame || !NavToggle ) {
         return false;
         return false;
     }
     }
 
     /* if shown now */
     /* if shown now */
     if ( NavToggle.firstChild.data === NavigationBarHide ) {
     if ( NavToggle.firstChild.data === NavigationBarHide ) {
Line 193: Line 201:
         }
         }
     NavToggle.firstChild.data = NavigationBarShow;
     NavToggle.firstChild.data = NavigationBarShow;
 
     /* if hidden now */
     /* if hidden now */
     } else if ( NavToggle.firstChild.data === NavigationBarShow ) {
     } else if ( NavToggle.firstChild.data === NavigationBarShow ) {
Line 203: Line 211:
         NavToggle.firstChild.data = NavigationBarHide;
         NavToggle.firstChild.data = NavigationBarHide;
     }
     }
 
     event.preventDefault();
     event.preventDefault();
};
};
 
/* adds show/hide-button to navigation bars */
/* adds show/hide-button to navigation bars */
function createNavigationBarToggleButton() {
function createNavigationBarToggleButton( $content ) {
    var indexNavigationBar = 0;
    var NavFrame;
     var NavChild;
     var NavChild;
     /* iterate over all < div >-elements */
     /* iterate over all < div >-elements */
     var divs = document.getElementsByTagName( 'div' );
     var $divs = $content.find( 'div' );
     for ( var i = 0; (NavFrame = divs[i]); i++ ) {
     $divs.each( function ( i, NavFrame ) {
         /* if found a navigation bar */
         /* if found a navigation bar */
         if ( $( NavFrame ).hasClass( 'NavFrame' ) ) {
         if ( $( NavFrame ).hasClass( 'NavFrame' ) ) {
 
             indexNavigationBar++;
             indexNavigationBar++;
             var NavToggle = document.createElement( 'a' );
             var NavToggle = document.createElement( 'a' );
Line 224: Line 230:
             NavToggle.setAttribute( 'href', '#' );
             NavToggle.setAttribute( 'href', '#' );
             $( NavToggle ).on( 'click', $.proxy( window.toggleNavigationBar, window, indexNavigationBar ) );
             $( NavToggle ).on( 'click', $.proxy( window.toggleNavigationBar, window, indexNavigationBar ) );
 
             var isCollapsed = $( NavFrame ).hasClass( 'collapsed' );
             var isCollapsed = $( NavFrame ).hasClass( 'collapsed' );
             /**
             /**
Line 249: Line 255:
             var NavToggleText = document.createTextNode( isCollapsed ? NavigationBarShow : NavigationBarHide );
             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) */
             /* Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked) */
             for( var j = 0; j < NavFrame.childNodes.length; j++ ) {
             for( var j = 0; j < NavFrame.childNodes.length; j++ ) {
Line 259: Line 265:
             NavFrame.setAttribute( 'id', 'NavFrame' + indexNavigationBar );
             NavFrame.setAttribute( 'id', 'NavFrame' + indexNavigationBar );
         }
         }
     }
     } );
}
}
 
mw.hook( 'wikipage.content' ).add( createNavigationBarToggleButton );
mw.hook( 'wikipage.content' ).add( createNavigationBarToggleButton );