MediaSearchWidget: Listen to "change" event to reposition
authorMoriel Schottlender <moriel@gmail.com>
Wed, 5 Apr 2017 21:55:15 +0000 (14:55 -0700)
committerBartosz Dziewoński <matma.rex@gmail.com>
Mon, 10 Apr 2017 18:50:45 +0000 (18:50 +0000)
The "add" event is emitted too early (by the OO.EmitterList, before
the items are attached to the DOM) so instead we have to go by the
GroupElement event api itself -- which uses "change" event to all
item changes.

Listening to item change means that we are certain the items are
already attached to the DOM before we manipulate and read their
positioning, etc.

Bug: T162202
Change-Id: I3ef9d9451562c725e12a66b80048fc3836280d9f

resources/src/mediawiki.widgets/MediaSearch/mw.widgets.MediaSearchWidget.js

index c6938e8..5cbd2ec 100644 (file)
@@ -65,7 +65,7 @@
                this.$results.on( 'scroll', this.onResultsScroll.bind( this ) );
                this.$query.append( this.noItemsMessage.$element );
                this.results.connect( this, {
-                       add: 'onResultsAdd',
+                       change: 'onResultsChange',
                        remove: 'onResultsRemove'
                } );
 
         *
         * @param {mw.widgets.MediaResultWidget[]} items An array of item elements
         */
-       mw.widgets.MediaSearchWidget.prototype.onResultsAdd = function ( items ) {
+       mw.widgets.MediaSearchWidget.prototype.onResultsChange = function ( items ) {
                var search = this;
 
+               if ( !items.length ) {
+                       return;
+               }
+
                // Add method to a queue; this queue will only run when the widget
                // is visible
                this.layoutQueue.push( function () {