RCFilters: Actually recognize a saved query default and delete when removed
authorMoriel Schottlender <moriel@gmail.com>
Thu, 22 Jun 2017 01:21:56 +0000 (18:21 -0700)
committerMoriel Schottlender <moriel@gmail.com>
Thu, 22 Jun 2017 18:30:08 +0000 (11:30 -0700)
Compare ID to saved default even if those were saved in different
formats (number vs string) when setting default on initialization
and when deleting the default when deleting the item.

Change-Id: Ia211a7166b3ff514c0e2a34bb370cf2d50057745

resources/src/mediawiki.rcfilters/dm/mw.rcfilters.dm.SavedQueriesModel.js
resources/src/mediawiki.rcfilters/mw.rcfilters.Controller.js

index 3ffc12e..efccc11 100644 (file)
@@ -68,7 +68,8 @@
         * @fires initialize
         */
        mw.rcfilters.dm.SavedQueriesModel.prototype.initialize = function ( savedQueries, baseState ) {
         * @fires initialize
         */
        mw.rcfilters.dm.SavedQueriesModel.prototype.initialize = function ( savedQueries, baseState ) {
-               var items = [];
+               var items = [],
+                       defaultItem = null;
 
                savedQueries = savedQueries || {};
 
 
                savedQueries = savedQueries || {};
 
@@ -76,7 +77,9 @@
 
                this.clearItems();
                $.each( savedQueries.queries || {}, function ( id, obj ) {
 
                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,
 
                        // 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 );
 
                        // 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 }
-                               )
+                       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 );
 
 
                this.addItems( items );
 
                ] );
        };
 
                ] );
        };
 
+       /**
+        * 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.savedQueriesModel.setDefault( null );
+                       }
+
+                       this.savedQueriesModel.removeItems( [ query ] );
+               }
+       };
+
        /**
         * Get an item that matches the requested query
         *
        /**
         * Get an item that matches the requested query
         *
index 95e11d5..ebf2201 100644 (file)
         * @param {string} queryID Query id
         */
        mw.rcfilters.Controller.prototype.removeSavedQuery = function ( queryID ) {
         * @param {string} queryID Query id
         */
        mw.rcfilters.Controller.prototype.removeSavedQuery = function ( queryID ) {
-               var query = this.savedQueriesModel.getItemByID( queryID );
+               this.savedQueriesModel.removeQuery( queryID );
 
 
-               this.savedQueriesModel.removeItems( [ query ] );
-
-               // Check if this item was the default
-               if ( this.savedQueriesModel.getDefault() === queryID ) {
-                       // Nulify the default
-                       this.savedQueriesModel.setDefault( null );
-               }
                this._saveSavedQueries();
        };
 
                this._saveSavedQueries();
        };