Adjust images in packed gallery on window resize
authorm4tx <m4tx@m4tx.pl>
Sun, 4 Jan 2015 07:32:52 +0000 (08:32 +0100)
committerBartosz Dziewoński <matma.rex@gmail.com>
Mon, 5 Jan 2015 14:59:18 +0000 (15:59 +0100)
Original element sizes are stored in data(), and on window resize,
they are restored, so elements can be justified as on just loaded
page.

Bug: T55664
Change-Id: I286e0a4c8230c11619ca30f8f3b66778de835a33

resources/Resources.php
resources/src/mediawiki.page/mediawiki.page.gallery.js

index ccb842d..bc4790b 100644 (file)
@@ -1220,6 +1220,9 @@ return array(
 
        'mediawiki.page.gallery' => array(
                'scripts' => 'resources/src/mediawiki.page/mediawiki.page.gallery.js',
+               'dependencies' => array(
+                       'jquery.throttle-debounce',
+               )
        ),
        'mediawiki.page.ready' => array(
                'scripts' => 'resources/src/mediawiki.page/mediawiki.page.ready.js',
index 1892967..e3ffbda 100644 (file)
                }
 
                // Now on to justification.
-               // We may still get ragged edges if someone resizes their window. Could bind to
-               // that event, otoh do we really want to constantly be resizing galleries?
-               $( galleries ).each( function () {
+               function justify() {
                        var lastTop,
                                $img,
                                imgWidth,
                                imgHeight,
+                               captionWidth,
                                rows = [],
                                $gallery = $( this );
 
                                        imgHeight = 0;
                                }
 
+                               captionWidth = $this.children().children( 'div.gallerytextwrapper' ).width();
                                rows[rows.length - 1][rows[rows.length - 1].length] = {
                                        $elm: $this,
                                        width: $this.outerWidth(),
                                        imgWidth: imgWidth,
                                        // XXX: can divide by 0 ever happen?
                                        aspect: imgWidth / imgHeight,
-                                       captionWidth: $this.children().children( 'div.gallerytextwrapper' ).width(),
+                                       captionWidth: captionWidth,
                                        height: imgHeight
                                };
+
+                               // Save all boundaries so we can restore them on window resize
+                               $this.data( 'imgWidth', imgWidth );
+                               $this.data( 'imgHeight', imgHeight );
+                               $this.data( 'width', $this.outerWidth() );
+                               $this.data( 'captionWidth', captionWidth );
                        } );
 
                        ( function () {
                                        }
                                }
                        }() );
-               } );
+               }
+
+               $( galleries ).each( justify );
+               $( window ).resize( $.debounce( 300, true, function () {
+                       $( galleries ).children( 'li' ).each( function () {
+                               var imgWidth = $( this ).data( 'imgWidth' ),
+                                       imgHeight = $( this ).data( 'imgHeight' ),
+                                       width = $( this ).data( 'width' ),
+                                       captionWidth = $( this ).data( 'captionWidth' ),
+                                       $imageElm, imageElm;
+
+                               // Restore original sizes so we can arrange the elements as on freshly loaded page
+                               $( this ).width( width );
+                               $( this ).children( 'div' ).first().width( width );
+                               $( this ).children( 'div' ).first().children( 'div.thumb' ).width( imgWidth );
+                               $( this ).find( 'div.gallerytextwrapper' ).width( captionWidth );
+
+                               $imageElm = $( this ).find( 'img' ).first();
+                               imageElm = $imageElm.length ? $imageElm[0] : null;
+                               if ( imageElm ) {
+                                       imageElm.width = imgWidth;
+                                       imageElm.height = imgHeight;
+                               }
+                       } );
+               } ) );
+               $( window ).resize( $.debounce( 300, function () {
+                       $( galleries ).each( justify );
+               } ) );
        } );
 }( jQuery ) );