MediaWiki:Skin/ResizeGalleries.js: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
K |
|||
Zeile 5: | Zeile 5: | ||
if (typeof doNotResize != 'undefined') return; | if (typeof doNotResize != 'undefined') return; | ||
− | this.$tables = | + | this.$tables = jQuery('table.gallery:not(.noresize)'); |
if (this.$tables.length < 1) return; | if (this.$tables.length < 1) return; | ||
− | + | jQuery(window).resize( function () { resizeGalleries.delay(); } ); | |
this.resize(); | this.resize(); | ||
Zeile 18: | Zeile 18: | ||
resize: function () { | resize: function () { | ||
this.$tables.each( function () { | this.$tables.each( function () { | ||
− | var $table = | + | var $table = jQuery(this); |
var $tds = $table.find( 'tbody:first > tr > td' ); // Do not clone; breaks other scripts! | var $tds = $table.find( 'tbody:first > tr > td' ); // Do not clone; breaks other scripts! | ||
if ( $tds.length < 1 ) return; // paranoia | if ( $tds.length < 1 ) return; // paranoia | ||
Zeile 29: | Zeile 29: | ||
if ( newShown == curShown || ( newShown > curShown && $table.find('tr').length < 2 ) ) return; | if ( newShown == curShown || ( newShown > curShown && $table.find('tr').length < 2 ) ) return; | ||
− | var $newbody = | + | var $newbody = jQuery('<tbody/>'); |
var $tr = null; | var $tr = null; | ||
var curpos = 0; | var curpos = 0; | ||
$tds.each( function () { | $tds.each( function () { | ||
if (curpos >= newShown || $tr == null) { | if (curpos >= newShown || $tr == null) { | ||
− | $newbody.append( $tr = | + | $newbody.append( $tr = jQuery('<tr/>') ); |
curpos = 0; | curpos = 0; | ||
} | } |
Aktuelle Version vom 9. Dezember 2010, 16:04 Uhr
// <source lang="JavaScript">
var resizeGalleries = {
init: function () {
if (typeof doNotResize != 'undefined') return;
this.$tables = jQuery('table.gallery:not(.noresize)');
if (this.$tables.length < 1) return;
jQuery(window).resize( function () { resizeGalleries.delay(); } );
this.resize();
},
delay: function () {
if ( typeof this.timeoutId !== 'undefined' ) window.clearTimeout( this.timeoutId );
this.timeoutId = window.setTimeout( function () { resizeGalleries.resize(); }, 10);
},
resize: function () {
this.$tables.each( function () {
var $table = jQuery(this);
var $tds = $table.find( 'tbody:first > tr > td' ); // Do not clone; breaks other scripts!
if ( $tds.length < 1 ) return; // paranoia
if ($tds.first().outerWidth() == 0) return;
// XXX: we assume that all <td>s in each gallery have the same width, and that no row is longer than the first
var curShown = $table.find( 'tbody:first > tr:first > td' ).length;
var newShown = Math.floor( $table.parent().width() / $tds.first().outerWidth() );
if ( newShown < 1 ) newShown = 1;
if ( newShown == curShown || ( newShown > curShown && $table.find('tr').length < 2 ) ) return;
var $newbody = jQuery('<tbody/>');
var $tr = null;
var curpos = 0;
$tds.each( function () {
if (curpos >= newShown || $tr == null) {
$newbody.append( $tr = jQuery('<tr/>') );
curpos = 0;
}
curpos++;
$tr.append( this );
} );
$table.children( 'tbody' ).replaceWith( $newbody );
} );
}
};
jQuery(document).ready( function () {
resizeGalleries.init();
} );
//</source>