Merge "Live Preview: Cope with the edit summary being an OOjs UI widget"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / mw.rcfilters.Controller.js
index f3ea248..31effe1 100644 (file)
                this.initializing = false;
 
                this.prevLoggedItems = [];
+
+               this.FILTER_CHANGE = 'filterChange';
+               this.SHOW_NEW_CHANGES = 'showNewChanges';
+               this.LIVE_UPDATE = 'liveUpdate';
        };
 
        /* Initialization */
                                        allowArbitrary: true,
                                        validate: $.isNumeric,
                                        range: {
-                                               min: 1,
+                                               min: 0, // The server normalizes negative numbers to 0 results
                                                max: 1000
                                        },
                                        sortFunc: function ( a, b ) { return Number( a.name ) - Number( b.name ); },
                                        // we should remove all sticky behavior methods completely
                                        // See T172156
                                        // isSticky: true,
-                                       filters: displayConfig.arrayLimit.map( function ( num ) {
+                                       excludedFromSavedQueries: true,
+                                       filters: displayConfig.limitArray.map( function ( num ) {
                                                return controller._createFilterDataFromNumber( num, num );
                                        } )
                                },
                                        validate: $.isNumeric,
                                        range: {
                                                min: 0,
-                                               max: displayConfig.maxLimit
+                                               max: displayConfig.maxDays
                                        },
                                        sortFunc: function ( a, b ) { return Number( a.name ) - Number( b.name ); },
                                        numToLabelFunc: function ( i ) {
                                        'default': mw.user.options.get( 'rcdays', '30' ),
                                        // Temporarily making this not sticky while limit is not sticky, see above
                                        // isSticky: true,
+                                       excludedFromSavedQueries: true,
                                        filters: [
                                                // Hours (1, 2, 6, 12)
                                                0.04166, 0.0833, 0.25, 0.5
                                        // Days
-                                       ].concat( displayConfig.arrayDays )
+                                       ].concat( displayConfig.daysArray )
                                                .map( function ( num ) {
                                                        return controller._createFilterDataFromNumber(
                                                                num,
                this.savedQueriesModel.initialize(
                        parsedSavedQueries,
                        this._getBaseFilterState(),
-                       // This is for backwards compatibility - delete all sticky filter states
-                       Object.keys( this.filtersModel.getStickyFiltersState() )
+                       // This is for backwards compatibility - delete all excluded filter states
+                       Object.keys( this.filtersModel.getExcludedFiltersState() )
                );
 
                // Check whether we need to load defaults.
        mw.rcfilters.Controller.prototype.toggleLiveUpdate = function ( enable ) {
                this.changesListModel.toggleLiveUpdate( enable );
                if ( this.changesListModel.getLiveUpdate() && this.changesListModel.getNewChangesExist() ) {
-                       this.showNewChanges();
+                       this.updateChangesList( null, this.LIVE_UPDATE );
                }
        };
 
 
                                if ( data.changes !== 'NO_RESULTS' ) {
                                        if ( this.changesListModel.getLiveUpdate() ) {
-                                               return this.updateChangesList( false, null, true, false );
+                                               return this.updateChangesList( null, this.LIVE_UPDATE );
                                        } else {
                                                this.changesListModel.setNewChangesExist( true );
                                        }
         * @private
         */
        mw.rcfilters.Controller.prototype._shouldCheckForNewChanges = function () {
-               var liveUpdateFeatureFlag = mw.config.get( 'wgStructuredChangeFiltersEnableLiveUpdate' ) ||
-                       new mw.Uri().query.liveupdate;
-
                return !document.hidden &&
                        !this.filtersModel.hasConflict() &&
                        !this.changesListModel.getNewChangesExist() &&
                        !this.updatingChangesList &&
-                       liveUpdateFeatureFlag;
+                       mw.rcfilters.featureFlags.liveUpdate;
        };
 
        /**
         * fetching and showing the new changes
         */
        mw.rcfilters.Controller.prototype.showNewChanges = function () {
-               return this.updateChangesList( false, null, true, true );
+               return this.updateChangesList( null, this.SHOW_NEW_CHANGES );
        };
 
        /**
                // These are filter states; highlight is stored as boolean
                highlightedItems.highlight = this.filtersModel.isHighlightEnabled();
 
-               // Delete all sticky filters
-               this._deleteStickyValuesFromFilterState( selectedState );
+               // Delete all excluded filters
+               this._deleteExcludedValuesFromFilterState( selectedState );
 
                // Add item
                queryID = this.savedQueriesModel.addNewQuery(
 
                        // Update model state from filters
                        this.filtersModel.toggleFiltersSelected(
-                               // Merge filters with sticky values
-                               $.extend( true, {}, data.filters, this.filtersModel.getStickyFiltersState() )
+                               // Merge filters with excluded values
+                               $.extend( true, {}, data.filters, this.filtersModel.getExcludedFiltersState() )
                        );
 
                        // Update namespace inverted property
                } );
                highlightedItems.highlight = this.filtersModel.isHighlightEnabled();
 
-               // Remove sticky filters
-               this._deleteStickyValuesFromFilterState( selectedState );
+               // Remove anything that should be excluded from the saved query
+               // this includes sticky filters and filters marked with 'excludedFromSavedQueries'
+               this._deleteExcludedValuesFromFilterState( selectedState );
 
                return this.savedQueriesModel.findMatchingQuery(
                        {
         *
         * @param {Object} filterState Filter state
         */
-       mw.rcfilters.Controller.prototype._deleteStickyValuesFromFilterState = function ( filterState ) {
-               // Remove sticky filters
-               $.each( this.filtersModel.getStickyFiltersState(), function ( filterName ) {
+       mw.rcfilters.Controller.prototype._deleteExcludedValuesFromFilterState = function ( filterState ) {
+               // Remove excluded filters
+               $.each( this.filtersModel.getExcludedFiltersState(), function ( filterName ) {
                        delete filterState[ filterName ];
                } );
        };
        /**
         * Update the list of changes and notify the model
         *
-        * @param {boolean} [updateUrl=true] Whether the URL should be updated with the current state of the filters
         * @param {Object} [params] Extra parameters to add to the API call
-        * @param {boolean} [isLiveUpdate=false] The purpose of this update is to show new results for the same filters
-        * @param {boolean} [invalidateCurrentChanges=true] Invalidate current changes by default (show spinner)
+        * @param {string} [updateMode='filterChange'] One of 'filterChange', 'liveUpdate', 'showNewChanges'
         * @return {jQuery.Promise} Promise that is resolved when the update is complete
         */
-       mw.rcfilters.Controller.prototype.updateChangesList = function ( updateUrl, params, isLiveUpdate, invalidateCurrentChanges ) {
-               updateUrl = updateUrl === undefined ? true : updateUrl;
-               invalidateCurrentChanges = invalidateCurrentChanges === undefined ? true : invalidateCurrentChanges;
-               if ( updateUrl ) {
+       mw.rcfilters.Controller.prototype.updateChangesList = function ( params, updateMode ) {
+               updateMode = updateMode === undefined ? this.FILTER_CHANGE : updateMode;
+
+               if ( updateMode === this.FILTER_CHANGE ) {
                        this._updateURL( params );
                }
-               if ( invalidateCurrentChanges ) {
+               if ( updateMode === this.FILTER_CHANGE || updateMode === this.SHOW_NEW_CHANGES ) {
                        this.changesListModel.invalidate();
                }
                this.changesListModel.setNewChangesExist( false );
                                function ( pieces ) {
                                        var $changesListContent = pieces.changes,
                                                $fieldset = pieces.fieldset;
-                                       this.changesListModel.update( $changesListContent, $fieldset, false, isLiveUpdate );
+                                       this.changesListModel.update(
+                                               $changesListContent,
+                                               $fieldset,
+                                               false,
+                                               // separator between old and new changes
+                                               updateMode === this.SHOW_NEW_CHANGES || updateMode === this.LIVE_UPDATE
+                                       );
                                }.bind( this )
                                // Do nothing for failure
                        )