Merge "Remove space after cast"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.MenuSelectWidget.js
index d971faf..d9a1822 100644 (file)
@@ -9,7 +9,17 @@
         * @param {mw.rcfilters.dm.FiltersViewModel} model View model
         * @param {Object} [config] Configuration object
         * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
-        * @cfg {jQuery} [$footer] An optional footer for the menu
+        * @cfg {Object[]} [footers] An array of objects defining the footers for
+        *  this menu, with a definition whether they appear per specific views.
+        *  The expected structure is:
+        *  [
+        *     {
+        *        name: {string} A unique name for the footer object
+        *        $element: {jQuery} A jQuery object for the content of the footer
+        *        views: {string[]} Optional. An array stating which views this footer is
+        *               active on. Use null or omit to display this on all views.
+        *     }
+        *  ]
         */
        mw.rcfilters.ui.MenuSelectWidget = function MwRcfiltersUiMenuSelectWidget( controller, model, config ) {
                var header;
                this.model = model;
                this.currentView = '';
                this.views = {};
+               this.userSelecting = false;
 
                this.inputValue = '';
                this.$overlay = config.$overlay || this.$element;
-               this.$footer = config.$footer;
                this.$body = $( '<div>' )
                                .addClass( 'mw-rcfilters-ui-menuSelectWidget-body' );
+               this.footers = [];
 
                // Parent
                mw.rcfilters.ui.MenuSelectWidget.parent.call( this, $.extend( {
                                        .append( this.$group, this.noResults.$element )
                        );
 
-               if ( this.$footer ) {
-                       this.$element.append(
-                               this.$footer
-                                       .addClass( 'mw-rcfilters-ui-menuSelectWidget-footer' )
-                       );
-               }
+               // Append all footers; we will control their visibility
+               // based on view
+               config.footers = config.footers || [];
+               config.footers.forEach( function ( footerData ) {
+                       var isSticky = footerData.sticky === undefined ? true : !!footerData.sticky,
+                               adjustedData = {
+                                       // Wrap the element with our own footer wrapper
+                                       $element: $( '<div>' )
+                                               .addClass( 'mw-rcfilters-ui-menuSelectWidget-footer' )
+                                               .addClass( 'mw-rcfilters-ui-menuSelectWidget-footer-' + footerData.name )
+                                               .append( footerData.$element ),
+                                       views: footerData.views
+                               };
+
+                       if ( !footerData.disabled ) {
+                               this.footers.push( adjustedData );
+
+                               if ( isSticky ) {
+                                       this.$element.append( adjustedData.$element );
+                               } else {
+                                       this.$body.append( adjustedData.$element );
+                               }
+                       }
+               }.bind( this ) );
+
+               // Switch to the correct view
                this.switchView( this.model.getCurrentView() );
        };
 
 
                // Count groups per view
                $.each( groups, function ( groupName, groupModel ) {
-                       viewGroupCount[ groupModel.getView() ] = viewGroupCount[ groupModel.getView() ] || 0;
-                       viewGroupCount[ groupModel.getView() ]++;
+                       if ( !groupModel.isHidden() ) {
+                               viewGroupCount[ groupModel.getView() ] = viewGroupCount[ groupModel.getView() ] || 0;
+                               viewGroupCount[ groupModel.getView() ]++;
+                       }
                } );
 
                $.each( groups, function ( groupName, groupModel ) {
                        var currentItems = [],
                                view = groupModel.getView();
 
-                       if ( viewGroupCount[ view ] > 1 ) {
-                               // Only add a section header if there is more than
-                               // one group
-                               currentItems.push(
-                                       // Group section
-                                       new mw.rcfilters.ui.FilterMenuSectionOptionWidget(
-                                               widget.controller,
-                                               groupModel,
-                                               {
-                                                       $overlay: widget.$overlay
-                                               }
-                                       )
-                               );
-                       }
+                       if ( !groupModel.isHidden() ) {
+                               if ( viewGroupCount[ view ] > 1 ) {
+                                       // Only add a section header if there is more than
+                                       // one group
+                                       currentItems.push(
+                                               // Group section
+                                               new mw.rcfilters.ui.FilterMenuSectionOptionWidget(
+                                                       widget.controller,
+                                                       groupModel,
+                                                       {
+                                                               $overlay: widget.$overlay
+                                                       }
+                                               )
+                                       );
+                               }
 
-                       // Add items
-                       widget.model.getGroupFilters( groupName ).forEach( function ( filterItem ) {
-                               currentItems.push(
-                                       new mw.rcfilters.ui.FilterMenuOptionWidget(
-                                               widget.controller,
-                                               filterItem,
-                                               {
-                                                       $overlay: widget.$overlay
-                                               }
-                                       )
-                               );
-                       } );
-
-                       // Cache the items per view, so we can switch between them
-                       // without rebuilding the widgets each time
-                       widget.views[ view ] = widget.views[ view ] || [];
-                       widget.views[ view ] = widget.views[ view ].concat( currentItems );
+                               // Add items
+                               widget.model.getGroupFilters( groupName ).forEach( function ( filterItem ) {
+                                       currentItems.push(
+                                               new mw.rcfilters.ui.FilterMenuOptionWidget(
+                                                       widget.controller,
+                                                       filterItem,
+                                                       {
+                                                               $overlay: widget.$overlay
+                                                       }
+                                               )
+                                       );
+                               } );
+
+                               // Cache the items per view, so we can switch between them
+                               // without rebuilding the widgets each time
+                               widget.views[ view ] = widget.views[ view ] || [];
+                               widget.views[ view ] = widget.views[ view ].concat( currentItems );
+                       }
                } );
 
                this.switchView( this.model.getCurrentView() );
                if ( this.views[ viewName ] && this.currentView !== viewName ) {
                        this.clearItems();
                        this.addItems( this.views[ viewName ] );
+                       this.updateFooterVisibility( viewName );
 
                        this.$element
                                .data( 'view', viewName )
                                .addClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + viewName );
 
                        this.currentView = viewName;
+                       this.clip();
                }
        };
 
+       /**
+        * Go over the available footers and decide which should be visible
+        * for this view
+        *
+        * @param {string} [currentView] Current view
+        */
+       mw.rcfilters.ui.MenuSelectWidget.prototype.updateFooterVisibility = function ( currentView ) {
+               currentView = currentView || this.model.getCurrentView();
+
+               this.footers.forEach( function ( data ) {
+                       data.$element.toggle(
+                               // This footer should only be shown if it is configured
+                               // for all views or for this specific view
+                               !data.views || data.views.length === 0 || data.views.indexOf( currentView ) > -1
+                       );
+               } );
+       };
+
        /**
         * @fires itemVisibilityChange
         * @inheritdoc
 
                // Since the method hides/shows items, we don't want to
                // call it unless the input actually changed
-               if ( this.inputValue !== inputVal ) {
+               if (
+                       !this.userSelecting &&
+                       this.inputValue !== inputVal
+               ) {
                        // Parent method
                        mw.rcfilters.ui.MenuSelectWidget.parent.prototype.updateItemVisibility.call( this );
 
        mw.rcfilters.ui.MenuSelectWidget.prototype.scrollToTop = function () {
                this.$body.scrollTop( 0 );
        };
+
+       /**
+        * Set whether the user is currently selecting an item.
+        * This is important when the user selects an item that is in between
+        * different views, and makes sure we do not re-select a different
+        * item (like the item on top) when this is happening.
+        *
+        * @param {boolean} isSelecting User is selecting
+        */
+       mw.rcfilters.ui.MenuSelectWidget.prototype.setUserSelecting = function ( isSelecting ) {
+               this.userSelecting = !!isSelecting;
+       };
 }( mediaWiki ) );