Merge "Add support for 'hu-formal'"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / mw.rcfilters.Controller.js
index 79546b4..cd3f684 100644 (file)
@@ -1,4 +1,7 @@
 ( function ( mw, $ ) {
+
+       var byteLength = require( 'mediawiki.String' ).byteLength;
+
        /* eslint no-underscore-dangle: "off" */
        /**
         * Controller for the filters in Recent Changes
@@ -12,6 +15,9 @@
         * @cfg {string} savedQueriesPreferenceName Where to save the saved queries
         * @cfg {string} daysPreferenceName Preference name for the days filter
         * @cfg {string} limitPreferenceName Preference name for the limit filter
+        * @cfg {boolean} [normalizeTarget] Dictates whether or not to go through the
+        *  title normalization to separate title subpage/parts into the target= url
+        *  parameter
         */
        mw.rcfilters.Controller = function MwRcfiltersController( filtersModel, changesListModel, savedQueriesModel, config ) {
                this.filtersModel = filtersModel;
@@ -20,6 +26,7 @@
                this.savedQueriesPreferenceName = config.savedQueriesPreferenceName;
                this.daysPreferenceName = config.daysPreferenceName;
                this.limitPreferenceName = config.limitPreferenceName;
+               this.normalizeTarget = !!config.normalizeTarget;
 
                this.requestCounter = {};
                this.baseFilterState = {};
                this.filtersModel.initializeFilters( filterStructure, views );
 
                this.uriProcessor = new mw.rcfilters.UriProcessor(
-                       this.filtersModel
+                       this.filtersModel,
+                       { normalizeTarget: this.normalizeTarget }
                );
 
                if ( !mw.user.isAnon() ) {
                                info.noResultsDetails = 'NO_RESULTS_TIMEOUT';
                        } else if ( $root.find( '.mw-changeslist-notargetpage' ).length ) {
                                info.noResultsDetails = 'NO_RESULTS_NO_TARGET_PAGE';
+                       } else if ( $root.find( '.mw-changeslist-invalidtargetpage' ).length ) {
+                               info.noResultsDetails = 'NO_RESULTS_INVALID_TARGET_PAGE';
                        } else {
                                info.noResultsDetails = 'NO_RESULTS_NORMAL';
                        }
                } );
        };
 
-       /**
-        * Switch the view of the filters model
-        *
-        * @param {string} view Requested view
-        */
-       mw.rcfilters.Controller.prototype.switchView = function ( view ) {
-               this.filtersModel.switchView( view );
-       };
-
        /**
         * Reset to default filters
         */
                // Stringify state
                stringified = JSON.stringify( state );
 
-               if ( $.byteLength( stringified ) > 65535 ) {
+               if ( byteLength( stringified ) > 65535 ) {
                        // Sanity check, since the preference can only hold that.
                        return;
                }
        mw.rcfilters.Controller.prototype.updateStickyPreferences = function () {
                // Update default sticky values with selected, whether they came from
                // the initial defaults or from the URL value that is being normalized
-               this.updateDaysDefault( this.filtersModel.getGroup( 'days' ).getSelectedItems()[ 0 ].getParamName() );
-               this.updateLimitDefault( this.filtersModel.getGroup( 'limit' ).getSelectedItems()[ 0 ].getParamName() );
+               this.updateDaysDefault( this.filtersModel.getGroup( 'days' ).findSelectedItems()[ 0 ].getParamName() );
+               this.updateLimitDefault( this.filtersModel.getGroup( 'limit' ).findSelectedItems()[ 0 ].getParamName() );
 
                // TODO: Make these automatic by having the model go over sticky
                // items and update their default values automatically
                        rightNow = new Date().getTime(),
                        randomIdentifier = String( mw.user.sessionId() ) + String( rightNow ) + String( Math.random() ),
                        // Get all current filters
-                       filters = this.filtersModel.getSelectedItems().map( function ( item ) {
+                       filters = this.filtersModel.findSelectedItems().map( function ( item ) {
                                return item.getName();
                        } );
 
                        this.updateChangesList( null, 'markSeen' );
                }.bind( this ) );
        };
+
+       /**
+        * Set the current search for the system.
+        *
+        * @param {string} searchQuery Search query, including triggers
+        */
+       mw.rcfilters.Controller.prototype.setSearch = function ( searchQuery ) {
+               this.filtersModel.setSearch( searchQuery );
+       };
+
+       /**
+        * Switch the view by changing the search query trigger
+        * without changing the search term
+        *
+        * @param  {string} view View to change to
+        */
+       mw.rcfilters.Controller.prototype.switchView = function ( view ) {
+               this.setSearch(
+                       this.filtersModel.getViewTrigger( view ) +
+                       this.filtersModel.removeViewTriggers( this.filtersModel.getSearch() )
+               );
+       };
+
+       /**
+        * Reset the search for a specific view. This means we null the search query
+        * and replace it with the relevant trigger for the requested view
+        *
+        * @param  {string} [view='default'] View to change to
+        */
+       mw.rcfilters.Controller.prototype.resetSearchForView = function ( view ) {
+               view = view || 'default';
+
+               this.setSearch(
+                       this.filtersModel.getViewTrigger( view )
+               );
+       };
 }( mediaWiki, jQuery ) );