MediaWiki:Gadget-ResizeGalleries.js

Note : après avoir publié vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

  • Firefox / Safari : maintenez la touche Maj (Shift) en cliquant sur le bouton Actualiser ou appuyez sur Ctrl + F5 ou Ctrl + R (⌘ + R sur un Mac).
  • Google Chrome : appuyez sur Ctrl + Maj + R (⌘ + Shift + R sur un Mac).
  • Internet Explorer / Edge : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl + F5.
  • Opera : appuyez sur Ctrl + F5.
/* [[Catégorie:MediaWiki:Fonction Monobook en JavaScript|ResizeGalleries]] */
//<pre><nowiki>

$( resize_galleries ) ;

function resize_galleries () {
  var bc = document.getElementById ( "bodyContent" ) ;
  var width = bc.offsetWidth ; // Visible width
  var padding = 20 ; // padding per TD
  var tables = getElementsByClassName ( document , "table" , "gallery" ) ;

  if ( tables.length == 0 ) return ;
  window.onresize = resize_galleries;

  for ( var tablecount = 0 ; tablecount < tables.length ; tablecount++ ) {
    var t = tables[tablecount] ;

    // Get DIVs and their parent nodes (TDs)
    var divs = getElementsByClassName ( t , "div" , "gallerybox" ) ;
    var tds = new Array () ;
    for ( var i = 0 ; i < divs.length ; i++ ) tds.push ( divs[i].parentNode ) ;

    // Remove Table Rows
    for ( var i = 0 ; i < t.rows.length ; i++ ) t.rows[i].parentNode.removeChild ( t.rows[i] ) ;

    // Remove Table guts ( old tbodies, anything with type 1 should go )
    for ( var i = 0 ; i < t.childNodes.length ; i++ ) 
     if( t.childNodes[i].nodeType == 1 && t.childNodes[i].tagName == "TR" ) t.removeChild ( t.childNodes[i] ) ;

    // Reorder
    var tb = document.createElement("tbody");
    var tr = null ;
    var curpos = 0 ;
    for ( var i = 0 ; i < tds.length ; i++ ) {
      var nextwidth = parseInt ( divs[i].style.width ) + padding ;
      if ( curpos + nextwidth > width || tr == null ) {
        tr = document.createElement ( "tr" ) ;
        tb.appendChild ( tr ) ;
        curpos = 0 ;
      }
      curpos += nextwidth ;
      tr.appendChild ( tds[i] ) ;
    }
    t.appendChild(tb);
  }
}

//</nowiki></pre>