Merge "Align "What's this" vertically"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / dm / mw.rcfilters.dm.SavedQueriesModel.js
index 8536570..d87ef73 100644 (file)
@@ -2,6 +2,7 @@
        /**
         * View model for saved queries
         *
+        * @class
         * @mixins OO.EventEmitter
         * @mixins OO.EmitterList
         *
@@ -17,6 +18,7 @@
                OO.EmitterList.call( this );
 
                this.default = config.default;
+               this.baseState = {};
 
                // Events
                this.aggregate( { update: 'itemUpdate' } );
         *  the above structure.
         * @param {Object} [baseState] An object representing the base state
         *  so we can normalize the data
+        * @param {string[]} [ignoreFilters] Filters to ignore and remove from
+        *  the data
         * @fires initialize
         */
-       mw.rcfilters.dm.SavedQueriesModel.prototype.initialize = function ( savedQueries, baseState ) {
+       mw.rcfilters.dm.SavedQueriesModel.prototype.initialize = function ( savedQueries, baseState, ignoreFilters ) {
                var items = [],
                        defaultItem = null;
 
                savedQueries = savedQueries || {};
+               ignoreFilters = ignoreFilters || {};
 
                this.baseState = baseState;
 
                        // for existing users, who are only betalabs users at the moment.
                        normalizedData.highlights.highlight = !!Number( normalizedData.highlights.highlight );
 
+                       // Backwards-compat fix: Remove sticky parameters from the 'ignoreFilters' list
+                       ignoreFilters.forEach( function ( name ) {
+                               delete normalizedData.filters[ name ];
+                       } );
+
                        item = new mw.rcfilters.dm.SavedQueryItemModel(
                                id,
                                obj.label,
         *
         * @param {string} label Label for the new query
         * @param {Object} data Data for the new query
+        * @return {string} ID of the newly added query
         */
        mw.rcfilters.dm.SavedQueriesModel.prototype.addNewQuery = function ( label, data ) {
                var randomID = ( new Date() ).getTime(),
                                normalizedData
                        )
                ] );
+
+               return randomID;
        };
 
        /**
         * @return {mw.rcfilters.dm.SavedQueryItemModel} Matching item model
         */
        mw.rcfilters.dm.SavedQueriesModel.prototype.findMatchingQuery = function ( fullQueryComparison ) {
+               var model = this;
+
+               fullQueryComparison = this.getDifferenceFromBase( fullQueryComparison );
+
                return this.getItems().filter( function ( item ) {
+                       var comparedData = model.getDifferenceFromBase( item.getData() );
                        return OO.compare(
-                               item.getData(),
+                               comparedData,
                                fullQueryComparison
                        );
                } )[ 0 ];
        };
 
+       /**
+        * Get a minimal representation of the state for comparison
+        *
+        * @param {Object} state Given state
+        * @return {Object} Minimal state
+        */
+       mw.rcfilters.dm.SavedQueriesModel.prototype.getDifferenceFromBase = function ( state ) {
+               var result = { filters: {}, highlights: {}, invert: state.invert },
+                       baseState = this.baseState;
+
+               // XOR results
+               $.each( state.filters, function ( name, value ) {
+                       if ( baseState.filters !== undefined && baseState.filters[ name ] !== value ) {
+                               result.filters[ name ] = value;
+                       }
+               } );
+
+               $.each( state.highlights, function ( name, value ) {
+                       if ( baseState.highlights !== undefined && baseState.highlights[ name ] !== value && name !== 'highlight' ) {
+                               result.highlights[ name ] = value;
+                       }
+               } );
+
+               return result;
+       };
        /**
         * Get query by its identifier
         *