Merge "RCFilters: Adjust server default variable names for limit/days"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / mw.rcfilters.Controller.js
index fbd44fd..b489f4e 100644 (file)
@@ -33,6 +33,7 @@
         */
        mw.rcfilters.Controller.prototype.initialize = function ( filterStructure, namespaceStructure, tagList ) {
                var parsedSavedQueries, limitDefault,
+                       displayConfig = mw.config.get( 'StructuredChangeFiltersDisplayConfig' ),
                        controller = this,
                        views = {},
                        items = [],
@@ -91,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 = {
                                        hidden: true,
                                        allowArbitrary: true,
                                        validate: $.isNumeric,
+                                       range: {
+                                               min: 0, // The server normalizes negative numbers to 0 results
+                                               max: 1000
+                                       },
                                        sortFunc: function ( a, b ) { return Number( a.name ) - Number( b.name ); },
                                        'default': String( limitDefault ),
                                        // Temporarily making this not sticky until we resolve the problem
                                        // we should remove all sticky behavior methods completely
                                        // See T172156
                                        // isSticky: true,
-                                       filters: [ 50, 100, 250, 500 ].map( function ( num ) {
+                                       filters: displayConfig.limitArray.map( function ( num ) {
                                                return controller._createFilterDataFromNumber( num, num );
                                        } )
                                },
                                        hidden: true,
                                        allowArbitrary: true,
                                        validate: $.isNumeric,
+                                       range: {
+                                               min: 0,
+                                               max: displayConfig.maxDays
+                                       },
                                        sortFunc: function ( a, b ) { return Number( a.name ) - Number( b.name ); },
                                        numToLabelFunc: function ( i ) {
                                                return Number( i ) < 1 ?
                                        // isSticky: true,
                                        filters: [
                                                // Hours (1, 2, 6, 12)
-                                               0.04166, 0.0833, 0.25, 0.5,
-                                               // Days
-                                               1, 3, 7, 14, 30
-                                       ].map( function ( num ) {
-                                               return controller._createFilterDataFromNumber(
-                                                       num,
-                                                       // Convert fractions of days to number of hours for the labels
-                                                       num < 1 ? Math.round( num * 24 ) : num
-                                               );
-                                       } )
+                                               0.04166, 0.0833, 0.25, 0.5
+                                       // Days
+                                       ].concat( displayConfig.daysArray )
+                                               .map( function ( num ) {
+                                                       return controller._createFilterDataFromNumber(
+                                                               num,
+                                                               // Convert fractions of days to number of hours for the labels
+                                                               num < 1 ? Math.round( num * 24 ) : num
+                                                       );
+                                               } )
+                               }
+                       ]
+               };
+
+               views.display = {
+                       groups: [
+                               {
+                                       name: 'display',
+                                       type: 'boolean',
+                                       title: '', // Because it's a hidden group, this title actually appears nowhere
+                                       hidden: true,
+                                       isSticky: true,
+                                       filters: [
+                                               {
+                                                       name: 'enhanced',
+                                                       'default': String( mw.user.options.get( 'usenewrc', 0 ) )
+                                               }
+                                       ]
                                }
                        ]
                };
         * @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 and the default value for a range
+               if ( groupData.range ) {
+                       arbitraryValues = arbitraryValues.map( function ( 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
                // We assume these are the only groups that will allow for
                // arbitrary, since it doesn't make any sense for the other
                                // but if that value isn't already in the definition
                                groupData.filters
                                        .map( function ( filterData ) {
-                                               return filterData.name;
+                                               return String( filterData.name );
                                        } )
-                                       .indexOf( val ) === -1
+                                       .indexOf( String( val ) ) === -1
                        ) {
                                // Add the filter information
                                groupData.filters.push( controller._createFilterDataFromNumber(
                // the initial defaults or from the URL value that is being normalized
                this.updateDaysDefault( this.filtersModel.getGroup( 'days' ).getSelectedItems()[ 0 ].getParamName() );
                this.updateLimitDefault( this.filtersModel.getGroup( 'limit' ).getSelectedItems()[ 0 ].getParamName() );
+
+               // TODO: Make these automatic by having the model go over sticky
+               // items and update their default values automatically
        };
 
        /**
                return;
        };
 
+       /**
+        * Update the group by page default value
+        *
+        * @param {number} newValue New value
+        */
+       mw.rcfilters.Controller.prototype.updateGroupByPageDefault = function ( newValue ) {
+               if ( !$.isNumeric( newValue ) ) {
+                       return;
+               }
+
+               newValue = Number( newValue );
+
+               if ( mw.user.options.get( 'usenewrc' ) !== newValue ) {
+                       // Save the preference
+                       new mw.Api().saveOption( 'usenewrc', newValue );
+                       // Update the preference for this session
+                       mw.user.options.set( 'usenewrc', newValue );
+               }
+       };
+
        /**
         * Synchronize the URL with the current state of the filters
         * without adding an history entry.