Merge "Change 'editfont' default preference to 'monospace'"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / dm / mw.rcfilters.dm.SavedQueriesModel.js
index 3ffc12e..a7f3d23 100644 (file)
@@ -2,6 +2,7 @@
        /**
         * View model for saved queries
         *
+        * @class
         * @mixins OO.EventEmitter
         * @mixins OO.EmitterList
         *
         *  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 ) {
-               var items = [];
+       mw.rcfilters.dm.SavedQueriesModel.prototype.initialize = function ( savedQueries, baseState, ignoreFilters ) {
+               var items = [],
+                       defaultItem = null;
 
                savedQueries = savedQueries || {};
+               ignoreFilters = ignoreFilters || {};
 
                this.baseState = baseState;
 
                this.clearItems();
                $.each( savedQueries.queries || {}, function ( id, obj ) {
-                       var normalizedData = $.extend( true, {}, baseState, obj.data );
+                       var item,
+                               normalizedData = $.extend( true, {}, baseState, obj.data ),
+                               isDefault = String( savedQueries.default ) === String( id );
 
                        // Backwards-compat fix: We stored the 'highlight' state with
                        // "1" and "0" instead of true/false; for already-stored states,
                        // for existing users, who are only betalabs users at the moment.
                        normalizedData.highlights.highlight = !!Number( normalizedData.highlights.highlight );
 
-                       items.push(
-                               new mw.rcfilters.dm.SavedQueryItemModel(
-                                       id,
-                                       obj.label,
-                                       normalizedData,
-                                       { 'default': savedQueries.default === id }
-                               )
+                       // 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,
+                               normalizedData,
+                               { 'default': isDefault }
                        );
+
+                       if ( isDefault ) {
+                               defaultItem = item;
+                       }
+
+                       items.push( item );
                } );
 
-               this.default = savedQueries.default;
+               if ( defaultItem ) {
+                       this.default = defaultItem.getID();
+               }
 
                this.addItems( items );
 
         *
         * @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;
+       };
+
+       /**
+        * Remove query from model
+        *
+        * @param {string} queryID Query ID
+        */
+       mw.rcfilters.dm.SavedQueriesModel.prototype.removeQuery = function ( queryID ) {
+               var query = this.getItemByID( queryID );
+
+               if ( query ) {
+                       // Check if this item was the default
+                       if ( String( this.getDefault() ) === String( queryID ) ) {
+                               // Nulify the default
+                               this.setDefault( null );
+                       }
+
+                       this.removeItems( [ query ] );
+               }
        };
 
        /**