RCFilters UI: Compare states instead of params when switching URL
authorMoriel Schottlender <moriel@gmail.com>
Fri, 26 May 2017 20:45:09 +0000 (23:45 +0300)
committerMoriel Schottlender <moriel@gmail.com>
Sun, 28 May 2017 19:24:11 +0000 (22:24 +0300)
For empty url (initial load) and for cases where the URL is
minimized (upcoming) the method should check filter states
to recognize whether there's a difference between current and
requested URL.

Bug: T166347
Change-Id: I9dcc82ce7dbc0ad7c6cf3169cee7269234c95298

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

index aec2922..e1694f6 100644 (file)
@@ -14,6 +14,7 @@
                this.requestCounter = 0;
                this.baseFilterState = {};
                this.emptyParameterState = {};
+               this.initializing = false;
        };
 
        /* Initialization */
@@ -72,6 +73,7 @@
                        // There are parameters in the url, update model state
                        this.updateStateBasedOnUrl();
                } else {
+                       this.initializing = true;
                        // No valid parameters are given, load defaults
                        this._updateModelState(
                                $.extend(
@@ -86,6 +88,7 @@
                                )
                        );
                        this.updateChangesList();
+                       this.initializing = false;
                }
 
                // Update the changes list with the existing data
         * @param {Object} [params] Extra parameters to add to the API call
         */
        mw.rcfilters.Controller.prototype._updateURL = function ( params ) {
-               var updatedUri,
+               var currentFilterState, updatedFilterState, updatedUri,
                        notEquivalent = function ( obj1, obj2 ) {
                                var keys = Object.keys( obj1 ).concat( Object.keys( obj2 ) );
                                return keys.some( function ( key ) {
                updatedUri = this._getUpdatedUri();
                updatedUri.extend( params );
 
-               if ( notEquivalent( updatedUri.query, new mw.Uri().query ) ) {
-                       window.history.pushState( { tag: 'rcfilters' }, document.title, updatedUri.toString() );
+               // Compare states instead of parameters
+               // This will allow us to always have a proper check of whether
+               // the requested new url is one to change or not, regardless of
+               // actual parameter visibility/representation in the URL
+               currentFilterState = this.filtersModel.getFiltersFromParameters( new mw.Uri().query );
+               updatedFilterState = this.filtersModel.getFiltersFromParameters( updatedUri.query );
+
+               if ( notEquivalent( currentFilterState, updatedFilterState ) ) {
+                       if ( this.initializing ) {
+                               // Initially, when we just build the first page load
+                               // out of defaults, we want to replace the history
+                               window.history.replaceState( { tag: 'rcfilters' }, document.title, updatedUri.toString() );
+                       } else {
+                               window.history.pushState( { tag: 'rcfilters' }, document.title, updatedUri.toString() );
+                       }
                }
        };