Merge "Add tags for undo edits"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / dm / mw.rcfilters.dm.FilterGroup.js
index 57d1b41..f4cdae3 100644 (file)
         * @cfg {string} [type='send_unselected_if_any'] Group type
         * @cfg {string} [view='default'] Name of the display group this group
         *  is a part of.
-        * @cfg {boolean} [isSticky] This group is using a 'sticky' default; meaning
-        *  that every time a value is changed, it becomes the new default
-        * @cfg {boolean} [excludedFromSavedQueries] A specific requirement to exclude
-        *  this filter from saved queries. This is always true if the filter is 'sticky'
-        *  but can be used for non-sticky filters as an additional requirement. Similarly
-        *  to 'sticky' it works for the entire group as a whole.
+        * @cfg {boolean} [sticky] This group is 'sticky'. It is synchronized
+        *  with a preference, does not participate in Saved Queries, and is
+        *  not shown in the active filters area.
         * @cfg {string} [title] Group title
         * @cfg {boolean} [hidden] This group is hidden from the regular menu views
+        *  and the active filters area.
         * @cfg {boolean} [allowArbitrary] Allows for an arbitrary value to be added to the
         *  group from the URL, even if it wasn't initially set up.
         * @cfg {number} [range] An object defining minimum and maximum values for numeric
@@ -47,8 +45,7 @@
                this.name = name;
                this.type = config.type || 'send_unselected_if_any';
                this.view = config.view || 'default';
-               this.sticky = !!config.isSticky;
-               this.excludedFromSavedQueries = this.sticky || !!config.excludedFromSavedQueries;
+               this.sticky = !!config.sticky;
                this.title = config.title || name;
                this.hidden = !!config.hidden;
                this.allowArbitrary = !!config.allowArbitrary;
@@ -93,7 +90,6 @@
         */
        mw.rcfilters.dm.FilterGroup.prototype.initializeFilters = function ( filterDefinition, groupDefault ) {
                var defaultParam,
-                       anyHighlighted,
                        supersetMap = {},
                        model = this,
                        items = [];
                                // For this group type, parameter values are direct
                                // We need to convert from a boolean to a string ('1' and '0')
                                model.defaultParams[ filter.name ] = String( Number( filter.default || 0 ) );
+                       } else if ( model.getType() === 'any_value' ) {
+                               model.defaultParams[ filter.name ] = filter.default;
                        }
                } );
 
                }
 
                // 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() );
                        if ( buildFromCurrentState ) {
                                // This means we have not been given a filter representation
                                // so we are building one based on current state
-                               filterRepresentation[ item.getName() ] = item.isSelected();
+                               filterRepresentation[ item.getName() ] = item.getValue();
                        } else if ( filterRepresentation[ item.getName() ] === undefined ) {
                                // We are given a filter representation, but we have to make
                                // sure that we fill in the missing filters if there are any
                // Build result
                if (
                        this.getType() === 'send_unselected_if_any' ||
-                       this.getType() === 'boolean'
+                       this.getType() === 'boolean' ||
+                       this.getType() === 'any_value'
                ) {
                        // First, check if any of the items are selected at all.
                        // If none is selected, we're treating it as if they are
                                        // Representation is straight-forward and direct from
                                        // the parameter value to the filter state
                                        result[ filterParamNames[ name ] ] = String( Number( !!value ) );
+                               } else if ( model.getType() === 'any_value' ) {
+                                       result[ filterParamNames[ name ] ] = value;
                                }
                        } );
                } else if ( this.getType() === 'string_options' ) {
                paramRepresentation = paramRepresentation || {};
                if (
                        this.getType() === 'send_unselected_if_any' ||
-                       this.getType() === 'boolean'
+                       this.getType() === 'boolean' ||
+                       this.getType() === 'any_value'
                ) {
                        // Go over param representation; map and check for selections
                        this.getItems().forEach( function ( filterItem ) {
                                } else if ( model.getType() === 'boolean' ) {
                                        // Straight-forward definition of state
                                        result[ filterItem.getName() ] = !!Number( paramRepresentation[ filterItem.getParamName() ] );
+                               } else if ( model.getType() === 'any_value' ) {
+                                       result[ filterItem.getName() ] = paramRepresentation[ filterItem.getParamName() ];
                                }
                        } );
                } else if ( this.getType() === 'string_options' ) {
                // If any filters are missing, they will get a falsey value
                this.getItems().forEach( function ( filterItem ) {
                        if ( result[ filterItem.getName() ] === undefined ) {
-                               result[ filterItem.getName() ] = false;
+                               result[ filterItem.getName() ] = this.getFalsyValue();
                        }
-               } );
+               }.bind( this ) );
 
                // Make sure that at least one option is selected in
                // single_option groups, no matter what path was taken
                return result;
        };
 
+       /**
+        * @return {*} The appropriate falsy value for this group type
+        */
+       mw.rcfilters.dm.FilterGroup.prototype.getFalsyValue = function () {
+               return this.getType() === 'any_value' ? '' : false;
+       };
+
        /**
         * Get current selected state of all filter items in this group
         *
                var state = {};
 
                this.getItems().forEach( function ( filterItem ) {
-                       state[ filterItem.getName() ] = filterItem.isSelected();
+                       state[ filterItem.getName() ] = filterItem.getValue();
                } );
 
                return state;
        };
 
        /**
-        * Check whether the group value is excluded from saved queries
+        * 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.
         *
-        * @return {boolean} Group value is excluded from saved queries
+        * @param  {string} value Given value
+        * @return {string} Corrected value
         */
-       mw.rcfilters.dm.FilterGroup.prototype.isExcludedFromSavedQueries = function () {
-               return this.excludedFromSavedQueries;
+       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 ) );