RCFilters: Normalize user-generated default values
authorMoriel Schottlender <moriel@gmail.com>
Wed, 2 Aug 2017 20:44:11 +0000 (13:44 -0700)
committerMoriel Schottlender <moriel@gmail.com>
Wed, 2 Aug 2017 21:10:32 +0000 (14:10 -0700)
Bug: T172026
Change-Id: Id75116cf22a31f8b762801fc0a6aee554a9ca6b2

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

index c4cce8d..f7021e2 100644 (file)
                        this.getType() === 'single_option' &&
                        !oneWasSelected
                ) {
+                       item = this.getItems()[ 0 ];
                        if ( defaultParams[ this.getName() ] ) {
                                item = this.getItemByParamName( defaultParams[ this.getName() ] );
-                       } else {
-                               item = this.getItems()[ 0 ];
                        }
+
                        result[ item.getName() ] = true;
                }
 
index f07d3f1..16822d8 100644 (file)
@@ -92,7 +92,7 @@
                // Convert the default from the old preference
                // since the limit preference actually affects more
                // than just the RecentChanges page
-               limitDefault = Number( mw.user.options.get( 'rcfilters-rclimit', mw.user.options.get( 'rclimit', '50' ) ) );
+               limitDefault = Number( mw.user.options.get( 'rclimit', '50' ) );
 
                // Add parameter range operations
                views.range = {
         * @param {string|string[]} arbitraryValues An array of arbitrary values to add to the group
         */
        mw.rcfilters.Controller.prototype.addNumberValuesToGroup = function ( groupData, arbitraryValues ) {
-               var controller = this;
+               var controller = this,
+                       normalizeWithinRange = function ( range, val ) {
+                               if ( val < range.min ) {
+                                       return range.min; // Min
+                               } else if ( val >= range.max ) {
+                                       return range.max; // Max
+                               }
+                               return val;
+                       };
 
                arbitraryValues = Array.isArray( arbitraryValues ) ? arbitraryValues : [ arbitraryValues ];
 
-               // Normalize the arbitrary values
+               // Normalize the arbitrary values and the default value for a range
                if ( groupData.range ) {
                        arbitraryValues = arbitraryValues.map( function ( val ) {
-                               if ( val < 0 ) {
-                                       return groupData.range.min; // Min
-                               } else if ( val >= groupData.range.max ) {
-                                       return groupData.range.max; // Max
-                               }
-                               return val;
+                               return normalizeWithinRange( groupData.range, val );
                        } );
+
+                       // Normalize the default, since that's user defined
+                       if ( groupData.default !== undefined ) {
+                               groupData.default = String( normalizeWithinRange( groupData.range, groupData.default ) );
+                       }
                }
 
                // This is only true for single_option group