mediawiki.page.gallery: Various clean up and minor optimisations
authorTimo Tijhof <krinklemail@gmail.com>
Fri, 13 Jul 2018 05:22:31 +0000 (22:22 -0700)
committerTimo Tijhof <krinklemail@gmail.com>
Fri, 13 Jul 2018 05:24:29 +0000 (22:24 -0700)
* Moved some variables to a lower scope more appropiate,
  to make it easier to understand what they are used for,
  and to reduce chances of mistakes in the future.

* Use push() instead of .length assignment.

* Use the same property for checks as for access ([0] vs length),
  also potentially faster, but nothing worth mentioning.

* Avoid duplicate $this.outerWidth(), expensive computation.

Bug: T88654
Change-Id: Ib03d05a5c6422afdec11c56360bd568bdb2633fb

resources/src/mediawiki.page.gallery.js

index 892f044..ff27c58 100644 (file)
         */
        function justify() {
                var lastTop,
-                       $img,
-                       imgWidth,
-                       imgHeight,
-                       captionWidth,
                        rows = [],
                        $gallery = $( this );
 
                $gallery.children( 'li.gallerybox' ).each( function () {
-                       // Math.floor to be paranoid if things are off by 0.00000000001
-                       var top = Math.floor( $( this ).position().top ),
+                       var $img, imgWidth, imgHeight, outerWidth, captionWidth,
+                               // Math.floor, to be paranoid if things are off by 0.00000000001
+                               top = Math.floor( $( this ).position().top ),
                                $this = $( this );
 
                        if ( top !== lastTop ) {
-                               rows[ rows.length ] = [];
+                               rows.push( [] );
                                lastTop = top;
                        }
 
                        }
 
                        captionWidth = $this.children().children( 'div.gallerytextwrapper' ).width();
-                       rows[ rows.length - 1 ][ rows[ rows.length - 1 ].length ] = {
+                       outerWidth = $this.outerWidth();
+                       rows[ rows.length - 1 ].push( {
                                $elm: $this,
-                               width: $this.outerWidth(),
+                               width: outerWidth,
                                imgWidth: imgWidth,
-                               // XXX: can divide by 0 ever happen?
+                               // FIXME: Deal with devision by 0.
                                aspect: imgWidth / imgHeight,
                                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( 'width', outerWidth );
                        $this.data( 'captionWidth', captionWidth );
                } );
 
                                        $innerDiv = $outerDiv.children( 'div' ).first();
                                        $imageDiv = $innerDiv.children( 'div.thumb' );
                                        $imageElm = $imageDiv.find( 'img' ).first();
-                                       imageElm = $imageElm.length ? $imageElm[ 0 ] : null;
                                        $caption = $outerDiv.find( 'div.gallerytextwrapper' );
 
                                        // Since we are going to re-adjust the height, the vertical
                                                $caption.width( curRow[ j ].captionWidth + ( newWidth - curRow[ j ].imgWidth ) );
                                        }
 
-                                       if ( imageElm ) {
-                                               // We don't always have an img, e.g. in the case of an invalid file.
+                                       // We don't always have an img, e.g. in the case of an invalid file.
+                                       if ( $imageElm[ 0 ] ) {
+                                               imageElm = $imageElm[ 0 ];
                                                imageElm.width = newWidth;
                                                imageElm.height = preferredHeight;
                                        } else {
                        $( this ).find( 'div.gallerytextwrapper' ).width( captionWidth );
 
                        $imageElm = $( this ).find( 'img' ).first();
-                       imageElm = $imageElm.length ? $imageElm[ 0 ] : null;
-                       if ( imageElm ) {
+                       if ( $imageElm[ 0 ] ) {
+                               imageElm = $imageElm[ 0 ];
                                imageElm.width = imgWidth;
                                imageElm.height = imgHeight;
                        } else {