RCFilters: refactor highlight state
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / dm / mw.rcfilters.dm.FilterGroup.js
index 57d1b41..d20e2e7 100644 (file)
@@ -93,7 +93,6 @@
         */
        mw.rcfilters.dm.FilterGroup.prototype.initializeFilters = function ( filterDefinition, groupDefault ) {
                var defaultParam,
-                       anyHighlighted,
                        supersetMap = {},
                        model = this,
                        items = [];
                }
 
                // add highlights to defaultParams
-               anyHighlighted = false;
                this.getItems().forEach( function ( filterItem ) {
                        if ( filterItem.isHighlighted() ) {
-                               anyHighlighted = true;
                                this.defaultParams[ filterItem.getName() + '_color' ] = filterItem.getHighlightColor();
                        }
                }.bind( this ) );
-               if ( anyHighlighted ) {
-                       this.defaultParams.highlight = '1';
-               }
 
                // Store default filter state based on default params
                this.defaultFilters = this.getFilterRepresentation( this.getDefaultParams() );
        mw.rcfilters.dm.FilterGroup.prototype.isExcludedFromSavedQueries = function () {
                return this.excludedFromSavedQueries;
        };
+
+       /**
+        * Normalize a value given to this group. This is mostly for correcting
+        * arbitrary values for 'single option' groups, given by the user settings
+        * or the URL that can go outside the limits that are allowed.
+        *
+        * @param  {string} value Given value
+        * @return {string} Corrected value
+        */
+       mw.rcfilters.dm.FilterGroup.prototype.normalizeArbitraryValue = function ( value ) {
+               if (
+                       this.getType() === 'single_option' &&
+                       this.isAllowArbitrary()
+               ) {
+                       if (
+                               this.getMaxValue() !== null &&
+                               value > this.getMaxValue()
+                       ) {
+                               // Change the value to the actual max value
+                               return String( this.getMaxValue() );
+                       } else if (
+                               this.getMinValue() !== null &&
+                               value < this.getMinValue()
+                       ) {
+                               // Change the value to the actual min value
+                               return String( this.getMinValue() );
+                       }
+               }
+
+               return value;
+       };
 }( mediaWiki ) );