X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=resources%2Fsrc%2Fmediawiki.rcfilters%2Fmw.rcfilters.Controller.js;h=79546b46d4101cfd820944f1cbec751b712b7979;hp=5386291f7ca6350bb7ef7f98fe4ab220f8c7d388;hb=46fa949b8b5e10b1843d4ea2930679a0d3999025;hpb=69f4cbfee84c005583aaa0f73e8b7de1e768549e diff --git a/resources/src/mediawiki.rcfilters/mw.rcfilters.Controller.js b/resources/src/mediawiki.rcfilters/mw.rcfilters.Controller.js index 5386291f7c..79546b46d4 100644 --- a/resources/src/mediawiki.rcfilters/mw.rcfilters.Controller.js +++ b/resources/src/mediawiki.rcfilters/mw.rcfilters.Controller.js @@ -10,12 +10,16 @@ * @param {mw.rcfilters.dm.SavedQueriesModel} savedQueriesModel Saved queries model * @param {Object} config Additional configuration * @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 */ mw.rcfilters.Controller = function MwRcfiltersController( filtersModel, changesListModel, savedQueriesModel, config ) { this.filtersModel = filtersModel; this.changesListModel = changesListModel; this.savedQueriesModel = savedQueriesModel; this.savedQueriesPreferenceName = config.savedQueriesPreferenceName; + this.daysPreferenceName = config.daysPreferenceName; + this.limitPreferenceName = config.limitPreferenceName; this.requestCounter = {}; this.baseFilterState = {}; @@ -39,13 +43,14 @@ * @param {Array} filterStructure Filter definition and structure for the model * @param {Object} [namespaceStructure] Namespace definition * @param {Object} [tagList] Tag definition + * @param {Object} [conditionalViews] Conditional view definition */ - mw.rcfilters.Controller.prototype.initialize = function ( filterStructure, namespaceStructure, tagList ) { + mw.rcfilters.Controller.prototype.initialize = function ( filterStructure, namespaceStructure, tagList, conditionalViews ) { var parsedSavedQueries, pieces, displayConfig = mw.config.get( 'StructuredChangeFiltersDisplayConfig' ), defaultSavedQueryExists = mw.config.get( 'wgStructuredChangeFiltersDefaultSavedQueryExists' ), controller = this, - views = {}, + views = $.extend( true, {}, conditionalViews ), items = [], uri = new mw.Uri(); @@ -78,17 +83,20 @@ separator: ';', fullCoverage: true, filters: items - }, - { - name: 'invertGroup', - type: 'boolean', - hidden: true, - filters: [ { - name: 'invert', - 'default': '0' - } ] } ] }; + views.invert = { + groups: [ + { + name: 'invertGroup', + type: 'boolean', + hidden: true, + filters: [ { + name: 'invert', + 'default': '0' + } ] + } ] + }; } if ( tagList ) { views.tags = { @@ -122,13 +130,8 @@ max: 1000 }, sortFunc: function ( a, b ) { return Number( a.name ) - Number( b.name ); }, - 'default': displayConfig.limitDefault, - // Temporarily making this not sticky until we resolve the problem - // with the misleading preference. Note that if this is to be permanent - // we should remove all sticky behavior methods completely - // See T172156 - // isSticky: true, - excludedFromSavedQueries: true, + 'default': mw.user.options.get( this.limitPreferenceName, displayConfig.limitDefault ), + sticky: true, filters: displayConfig.limitArray.map( function ( num ) { return controller._createFilterDataFromNumber( num, num ); } ) @@ -150,10 +153,8 @@ ( Number( i ) * 24 ).toFixed( 2 ) : Number( i ); }, - 'default': displayConfig.daysDefault, - // Temporarily making this not sticky while limit is not sticky, see above - // isSticky: true, - excludedFromSavedQueries: true, + 'default': mw.user.options.get( this.daysPreferenceName, displayConfig.daysDefault ), + sticky: true, filters: [ // Hours (1, 2, 6, 12) 0.04166, 0.0833, 0.25, 0.5 @@ -177,7 +178,7 @@ type: 'boolean', title: '', // Because it's a hidden group, this title actually appears nowhere hidden: true, - isSticky: true, + sticky: true, filters: [ { name: 'enhanced', @@ -280,6 +281,7 @@ * Extracts information from the changes list DOM * * @param {jQuery} $root Root DOM to find children from + * @param {boolean} [statusCode] Server response status code * @return {Object} Information about changes list * @return {Object|string} return.changes Changes list, or 'NO_RESULTS' if there are no results * (either normally or as an error) @@ -287,10 +289,21 @@ * 'NO_RESULTS_TIMEOUT' for no results due to a timeout, or omitted for more than 0 results * @return {jQuery} return.fieldset Fieldset */ - mw.rcfilters.Controller.prototype._extractChangesListInfo = function ( $root ) { - var info, isTimeout, + mw.rcfilters.Controller.prototype._extractChangesListInfo = function ( $root, statusCode ) { + var info, $changesListContents = $root.find( '.mw-changeslist' ).first().contents(), - areResults = !!$changesListContents.length; + areResults = !!$changesListContents.length, + checkForLogout = !areResults && statusCode === 200; + + // We check if user logged out on different tab/browser or the session has expired. + // 205 status code returned from the server, which indicates that we need to reload the page + // is not usable on WL page, because we get redirected to login page, which gives 200 OK + // status code (if everything else goes well). + // Bug: T177717 + if ( checkForLogout && !!$root.find( '#wpName1' ).length ) { + location.reload( false ); + return; + } info = { changes: $changesListContents.length ? $changesListContents : 'NO_RESULTS', @@ -298,8 +311,13 @@ }; if ( !areResults ) { - isTimeout = !!$root.find( '.mw-changeslist-timeout' ).length; - info.noResultsDetails = isTimeout ? 'NO_RESULTS_TIMEOUT' : 'NO_RESULTS_NORMAL'; + if ( $root.find( '.mw-changeslist-timeout' ).length ) { + info.noResultsDetails = 'NO_RESULTS_TIMEOUT'; + } else if ( $root.find( '.mw-changeslist-notargetpage' ).length ) { + info.noResultsDetails = 'NO_RESULTS_NO_TARGET_PAGE'; + } else { + info.noResultsDetails = 'NO_RESULTS_NORMAL'; + } } return info; @@ -400,9 +418,12 @@ * Reset to default filters */ mw.rcfilters.Controller.prototype.resetToDefaults = function () { - if ( this.applyParamChange( this._getDefaultParams() ) ) { + var params = this._getDefaultParams(); + if ( this.applyParamChange( params ) ) { // Only update the changes list if there was a change to actual filters this.updateChangesList(); + } else { + this.uriProcessor.updateURL( params ); } }; @@ -412,7 +433,7 @@ * @return {boolean} Defaults are all false */ mw.rcfilters.Controller.prototype.areDefaultsEmpty = function () { - return $.isEmptyObject( this._getDefaultParams( true ) ); + return $.isEmptyObject( this._getDefaultParams() ); }; /** @@ -425,6 +446,8 @@ if ( this.applyParamChange( {} ) ) { // Only update the changes list if there was a change to actual filters this.updateChangesList(); + } else { + this.uriProcessor.updateURL(); } if ( highlightedFilterNames ) { @@ -507,7 +530,6 @@ */ mw.rcfilters.Controller.prototype.toggleInvertedNamespaces = function () { this.filtersModel.toggleInvertedNamespaces(); - if ( this.filtersModel.getFiltersByView( 'namespaces' ).filter( function ( filterItem ) { return filterItem.isSelected(); } @@ -515,9 +537,38 @@ ) { // Only re-fetch results if there are namespace items that are actually selected this.updateChangesList(); + } else { + this.uriProcessor.updateURL(); + } + }; + + /** + * Set the value of the 'showlinkedto' parameter + * @param {boolean} value + */ + mw.rcfilters.Controller.prototype.setShowLinkedTo = function ( value ) { + var targetItem = this.filtersModel.getGroup( 'page' ).getItemByParamName( 'target' ), + showLinkedToItem = this.filtersModel.getGroup( 'toOrFrom' ).getItemByParamName( 'showlinkedto' ); + + this.filtersModel.toggleFilterSelected( showLinkedToItem.getName(), value ); + this.uriProcessor.updateURL(); + // reload the results only when target is set + if ( targetItem.getValue() ) { + this.updateChangesList(); } }; + /** + * Set the target page + * @param {string} page + */ + mw.rcfilters.Controller.prototype.setTargetPage = function ( page ) { + var targetItem = this.filtersModel.getGroup( 'page' ).getItemByParamName( 'target' ); + targetItem.setValue( page ); + this.uriProcessor.updateURL(); + this.updateChangesList(); + }; + /** * Set the highlight color for a filter item * @@ -572,13 +623,26 @@ } this._checkForNewChanges() - .then( function ( newChanges ) { + .then( function ( statusCode ) { + // no result is 204 with the 'peek' param + // logged out is 205 + var newChanges = statusCode === 200; + if ( !this._shouldCheckForNewChanges() ) { // by the time the response is received, // it may not be appropriate anymore return; } + // 205 is the status code returned from server when user's logged in/out + // status is not matching while fetching live update changes. + // This works only on Recent Changes page. For WL, look _extractChangesListInfo. + // Bug: T177717 + if ( statusCode === 205 ) { + location.reload( false ); + return; + } + if ( newChanges ) { if ( this.changesListModel.getLiveUpdate() ) { return this.updateChangesList( null, this.LIVE_UPDATE ); @@ -614,12 +678,12 @@ var params = { limit: 1, peek: 1, // bypasses ChangesList specific UI - from: this.changesListModel.getNextFrom() + from: this.changesListModel.getNextFrom(), + isAnon: mw.user.isAnon() }; return this._queryChangesList( 'liveUpdate', params ).then( function ( data ) { - // no result is 204 with the 'peek' param - return data.status === 200; + return data.status; } ); }; @@ -712,6 +776,8 @@ if ( this.applyParamChange( params ) ) { // Update changes list only if there was a difference in filter selection this.updateChangesList(); + } else { + this.uriProcessor.updateURL( params ); } // Log filter grouping @@ -783,72 +849,48 @@ /** * Update the limit default value * - * param {number} newValue New value + * @param {number} newValue New value */ - mw.rcfilters.Controller.prototype.updateLimitDefault = function ( /* newValue */ ) { - // HACK: Temporarily remove this from being sticky - // See T172156 - - /* - if ( !$.isNumeric( newValue ) ) { - return; - } - - newValue = Number( newValue ); - - if ( mw.user.options.get( 'rcfilters-rclimit' ) !== newValue ) { - // Save the preference - new mw.Api().saveOption( 'rcfilters-rclimit', newValue ); - // Update the preference for this session - mw.user.options.set( 'rcfilters-rclimit', newValue ); - } - */ - return; + mw.rcfilters.Controller.prototype.updateLimitDefault = function ( newValue ) { + this.updateNumericPreference( this.limitPreferenceName, newValue ); }; /** * Update the days default value * - * param {number} newValue New value + * @param {number} newValue New value */ - mw.rcfilters.Controller.prototype.updateDaysDefault = function ( /* newValue */ ) { - // HACK: Temporarily remove this from being sticky - // See T172156 - - /* - if ( !$.isNumeric( newValue ) ) { - return; - } - - newValue = Number( newValue ); - - if ( mw.user.options.get( 'rcdays' ) !== newValue ) { - // Save the preference - new mw.Api().saveOption( 'rcdays', newValue ); - // Update the preference for this session - mw.user.options.set( 'rcdays', newValue ); - } - */ - return; + mw.rcfilters.Controller.prototype.updateDaysDefault = function ( newValue ) { + this.updateNumericPreference( this.daysPreferenceName, newValue ); }; /** * Update the group by page default value * - * @param {number} newValue New value + * @param {boolean} newValue New value */ mw.rcfilters.Controller.prototype.updateGroupByPageDefault = function ( newValue ) { + this.updateNumericPreference( 'usenewrc', Number( newValue ) ); + }; + + /** + * Update a numeric preference with a new value + * + * @param {string} prefName Preference name + * @param {number|string} newValue New value + */ + mw.rcfilters.Controller.prototype.updateNumericPreference = function ( prefName, newValue ) { if ( !$.isNumeric( newValue ) ) { return; } newValue = Number( newValue ); - if ( mw.user.options.get( 'usenewrc' ) !== newValue ) { + if ( mw.user.options.get( prefName ) !== newValue ) { // Save the preference - new mw.Api().saveOption( 'usenewrc', newValue ); + new mw.Api().saveOption( prefName, newValue ); // Update the preference for this session - mw.user.options.set( 'usenewrc', newValue ); + mw.user.options.set( prefName, newValue ); } }; @@ -870,7 +912,7 @@ mw.rcfilters.Controller.prototype.updateStateFromUrl = function ( fetchChangesList ) { fetchChangesList = fetchChangesList === undefined ? true : !!fetchChangesList; - this.uriProcessor.updateModelBasedOnQuery( new mw.Uri().query ); + this.uriProcessor.updateModelBasedOnQuery(); // Update the sticky preferences, in case we received a value // from the URL @@ -926,14 +968,13 @@ * Get an object representing the default parameter state, whether * it is from the model defaults or from the saved queries. * - * @param {boolean} [excludeHiddenParams] Exclude hidden and sticky params * @return {Object} Default parameters */ - mw.rcfilters.Controller.prototype._getDefaultParams = function ( excludeHiddenParams ) { + mw.rcfilters.Controller.prototype._getDefaultParams = function () { if ( this.savedQueriesModel.getDefault() ) { - return this.savedQueriesModel.getDefaultParams( excludeHiddenParams ); + return this.savedQueriesModel.getDefaultParams(); } else { - return this.filtersModel.getDefaultParams( excludeHiddenParams ); + return this.filtersModel.getDefaultParams(); } }; @@ -1021,10 +1062,11 @@ }; } - $parsed = $( '
' ).append( $( $.parseHTML( data.content ) ) ); - - return this._extractChangesListInfo( $parsed ); + $parsed = $( '
' ).append( $( $.parseHTML( + data ? data.content : '' + ) ) ); + return this._extractChangesListInfo( $parsed, data.status ); }.bind( this ) ); };