RCFilters: Make sure single_option always has 1 item selected
authorMoriel Schottlender <moriel@gmail.com>
Fri, 21 Jul 2017 00:02:55 +0000 (17:02 -0700)
committerMoriel Schottlender <moriel@gmail.com>
Fri, 21 Jul 2017 00:02:55 +0000 (17:02 -0700)
Bug: T171081
Change-Id: I82b6cf9660dfd5ee006197504557be366e148244

resources/src/mediawiki.rcfilters/dm/mw.rcfilters.dm.FilterGroup.js

index 9a85291..f4836aa 100644 (file)
         */
        mw.rcfilters.dm.FilterGroup.prototype.onFilterItemUpdate = function ( item ) {
                // Update state
-               var active = this.areAnySelected();
+               var changed = false,
+                       active = this.areAnySelected();
 
                if (
                        item.isSelected() &&
                        this.currSelected.toggleSelected( false );
                }
 
+               // For 'single_option' groups, check if we just unselected all
+               // items. This should never be the result. If we did unselect
+               // all (like resetting all filters to false) then this group
+               // must choose its default item or the first item in the group
                if (
+                       this.getType() === 'single_option' &&
+                       !this.getItems().some( function ( filterItem ) {
+                               return filterItem.isSelected();
+                       } )
+               ) {
+                       // Single option means there must be a single option
+                       // selected, so we have to either select the default
+                       // or select the first option
+                       this.currSelected = this.getItemByParamName( this.defaultParams[ this.getName() ] );
+                       this.currSelected.toggleSelected( true );
+                       changed = true;
+               }
+
+               if (
+                       changed ||
                        this.active !== active ||
                        this.currSelected !== item
                ) {