RCFilters: Read base value of limit/days from preference
authorMoriel Schottlender <moriel@gmail.com>
Thu, 27 Jul 2017 18:20:27 +0000 (11:20 -0700)
committerRoan Kattouw <roan.kattouw@gmail.com>
Thu, 27 Jul 2017 20:37:46 +0000 (13:37 -0700)
Bug: T171368
Change-Id: I0cdbae5bf6b9d00efe351c551d1f8a52459559c4

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

index b6eda0f..7849cc2 100644 (file)
         */
        mw.rcfilters.dm.FilterGroup.prototype.selectItemByParamName = function ( paramName ) {
                this.getItems().forEach( function ( item ) {
-                       item.toggleSelected( item.getParamName() === paramName );
+                       item.toggleSelected( item.getParamName() === String( paramName ) );
                } );
        };
 
         */
        mw.rcfilters.dm.FilterGroup.prototype.getItemByParamName = function ( paramName ) {
                return this.getItems().filter( function ( item ) {
-                       return item.getParamName() === paramName;
+                       return item.getParamName() === String( paramName );
                } )[ 0 ];
        };
 
index 3b8ebbd..45e3046 100644 (file)
         */
        mw.rcfilters.Controller.prototype.initialize = function ( filterStructure, namespaceStructure, tagList ) {
                var parsedSavedQueries,
+                       controller = this,
                        views = {},
                        items = [],
                        uri = new mw.Uri(),
                        $changesList = $( '.mw-changeslist' ).first().contents(),
-                       experimentalViews = mw.config.get( 'wgStructuredChangeFiltersEnableExperimentalViews' ),
-                       createFilterDataFromNumber = function ( num, convertedNumForLabel ) {
-                               return {
-                                       name: String( num ),
-                                       label: mw.language.convertNumber( convertedNumForLabel )
-                               };
-                       };
+                       experimentalViews = mw.config.get( 'wgStructuredChangeFiltersEnableExperimentalViews' );
 
                // Prepare views
                if ( namespaceStructure && experimentalViews ) {
                                        allowArbitrary: true,
                                        validate: $.isNumeric,
                                        sortFunc: function ( a, b ) { return Number( a.name ) - Number( b.name ); },
-                                       'default': '50',
+                                       'default': mw.user.options.get( 'rclimit' ),
                                        filters: [ 50, 100, 250, 500 ].map( function ( num ) {
-                                               return createFilterDataFromNumber( num, num );
+                                               return controller._createFilterDataFromNumber( num, num );
                                        } )
                                },
                                {
                                        allowArbitrary: true,
                                        validate: $.isNumeric,
                                        sortFunc: function ( a, b ) { return Number( a.name ) - Number( b.name ); },
-                                       'default': '7',
+                                       numToLabelFunc: function ( i ) {
+                                               return Number( i ) < 1 ?
+                                                       ( Number( i ) * 24 ).toFixed( 2 ) :
+                                                       Number( i );
+                                       },
+                                       'default': mw.user.options.get( 'rcdays' ),
                                        filters: [
                                                // Hours (1, 2, 6, 12)
                                                0.04166, 0.0833, 0.25, 0.5,
                                                // Days
                                                1, 3, 7, 14, 30
                                        ].map( function ( num ) {
-                                               return createFilterDataFromNumber(
+                                               return controller._createFilterDataFromNumber(
                                                        num,
                                                        // Convert fractions of days to number of hours for the labels
                                                        num < 1 ? Math.round( num * 24 ) : num
                        ]
                };
 
-               // Before we do anything, we need to see if we require another item in the
+               // Before we do anything, we need to see if we require additional items in the
                // groups that have 'AllowArbitrary'. For the moment, those are only single_option
                // groups; if we ever expand it, this might need further generalization:
                $.each( views, function ( viewName, viewData ) {
                        viewData.groups.forEach( function ( groupData ) {
-                               // This is only true for single_option and string_options
-                               // We assume these are the only groups that will allow for
-                               // arbitrary, since it doesn't make any sense for the other
-                               // groups.
-                               var uriValue = uri.query[ groupData.name ];
-
-                               if (
-                                       // If the group allows for arbitrary data
-                                       groupData.allowArbitrary &&
-                                       // and it is single_option (or string_options, but we
-                                       // don't have cases of those yet, nor do we plan to)
-                                       groupData.type === 'single_option' &&
-                                       // and if there is a valid value in the URI already
-                                       uri.query[ groupData.name ] !== undefined &&
-                                       // and, if there is a validate method and it passes on
-                                       // the data
-                                       ( !groupData.validate || groupData.validate( uri.query[ groupData.name ] ) ) &&
-                                       // but if that value isn't already in the definition
-                                       groupData.filters
-                                               .map( function ( filterData ) {
-                                                       return filterData.name;
-                                               } )
-                                               .indexOf( uri.query[ groupData.name ] ) === -1
-                               ) {
-                                       // Add the filter information
-                                       if ( groupData.name === 'days' ) {
-                                               // Specific fix for hours/days which go by the same param
-                                               groupData.filters.push( createFilterDataFromNumber(
-                                                       uriValue,
-                                                       // In this case we don't want to round because it can be arbitrary
-                                                       // weird numbers but we want to round to 2 decimal digits
-                                                       Number( uriValue ) < 1 ?
-                                                               ( Number( uriValue ) * 24 ).toFixed( 2 ) :
-                                                               Number( uriValue )
-                                               ) );
-                                       } else {
-                                               groupData.filters.push( createFilterDataFromNumber( uriValue, uriValue ) );
+                               var extraValues = [];
+                               if ( groupData.allowArbitrary ) {
+                                       // If the value in the URI isn't in the group, add it
+                                       if ( uri.query[ groupData.name ] !== undefined ) {
+                                               extraValues.push( uri.query[ groupData.name ] );
                                        }
-
-                                       // If there's a sort function set up, re-sort the values
-                                       if ( groupData.sortFunc ) {
-                                               groupData.filters.sort( groupData.sortFunc );
+                                       // If the default value isn't in the group, add it
+                                       if ( groupData.default !== undefined ) {
+                                               extraValues.push( groupData.default );
                                        }
+                                       controller.addNumberValuesToGroup( groupData, extraValues );
                                }
                        } );
                } );
                this.switchView( 'default' );
        };
 
+       /**
+        * Create filter data from a number, for the filters that are numerical value
+        *
+        * @param {Number} num Number
+        * @param {Number} numForDisplay Number for the label
+        * @return {Object} Filter data
+        */
+       mw.rcfilters.Controller.prototype._createFilterDataFromNumber = function ( num, numForDisplay ) {
+               return {
+                       name: String( num ),
+                       label: mw.language.convertNumber( numForDisplay )
+               };
+       };
+
+       /**
+        * Add an arbitrary values to groups that allow arbitrary values
+        *
+        * @param {Object} groupData Group data
+        * @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;
+
+               arbitraryValues = Array.isArray( arbitraryValues ) ? arbitraryValues : [ arbitraryValues ];
+
+               // This is only true for single_option group
+               // We assume these are the only groups that will allow for
+               // arbitrary, since it doesn't make any sense for the other
+               // groups.
+               arbitraryValues.forEach( function ( val ) {
+                       if (
+                               // If the group allows for arbitrary data
+                               groupData.allowArbitrary &&
+                               // and it is single_option (or string_options, but we
+                               // don't have cases of those yet, nor do we plan to)
+                               groupData.type === 'single_option' &&
+                               // and, if there is a validate method and it passes on
+                               // the data
+                               ( !groupData.validate || groupData.validate( val ) ) &&
+                               // but if that value isn't already in the definition
+                               groupData.filters
+                                       .map( function ( filterData ) {
+                                               return filterData.name;
+                                       } )
+                                       .indexOf( val ) === -1
+                       ) {
+                               // Add the filter information
+                               groupData.filters.push( controller._createFilterDataFromNumber(
+                                       val,
+                                       groupData.numToLabelFunc ?
+                                               groupData.numToLabelFunc( val ) :
+                                               val
+                               ) );
+
+                               // If there's a sort function set up, re-sort the values
+                               if ( groupData.sortFunc ) {
+                                       groupData.filters.sort( groupData.sortFunc );
+                               }
+                       }
+               } );
+       };
+
        /**
         * Switch the view of the filters model
         *