3 var byteLength
= require( 'mediawiki.String' ).byteLength
;
5 /* eslint no-underscore-dangle: "off" */
7 * Controller for the filters in Recent Changes
11 * @param {mw.rcfilters.dm.FiltersViewModel} filtersModel Filters view model
12 * @param {mw.rcfilters.dm.ChangesListViewModel} changesListModel Changes list view model
13 * @param {mw.rcfilters.dm.SavedQueriesModel} savedQueriesModel Saved queries model
14 * @param {Object} config Additional configuration
15 * @cfg {string} savedQueriesPreferenceName Where to save the saved queries
16 * @cfg {string} daysPreferenceName Preference name for the days filter
17 * @cfg {string} limitPreferenceName Preference name for the limit filter
18 * @cfg {boolean} [normalizeTarget] Dictates whether or not to go through the
19 * title normalization to separate title subpage/parts into the target= url
22 mw
.rcfilters
.Controller
= function MwRcfiltersController( filtersModel
, changesListModel
, savedQueriesModel
, config
) {
23 this.filtersModel
= filtersModel
;
24 this.changesListModel
= changesListModel
;
25 this.savedQueriesModel
= savedQueriesModel
;
26 this.savedQueriesPreferenceName
= config
.savedQueriesPreferenceName
;
27 this.daysPreferenceName
= config
.daysPreferenceName
;
28 this.limitPreferenceName
= config
.limitPreferenceName
;
29 this.normalizeTarget
= !!config
.normalizeTarget
;
31 this.requestCounter
= {};
32 this.baseFilterState
= {};
33 this.uriProcessor
= null;
34 this.initializing
= false;
35 this.wereSavedQueriesSaved
= false;
37 this.prevLoggedItems
= [];
39 this.FILTER_CHANGE
= 'filterChange';
40 this.SHOW_NEW_CHANGES
= 'showNewChanges';
41 this.LIVE_UPDATE
= 'liveUpdate';
45 OO
.initClass( mw
.rcfilters
.Controller
);
48 * Initialize the filter and parameter states
50 * @param {Array} filterStructure Filter definition and structure for the model
51 * @param {Object} [namespaceStructure] Namespace definition
52 * @param {Object} [tagList] Tag definition
53 * @param {Object} [conditionalViews] Conditional view definition
55 mw
.rcfilters
.Controller
.prototype.initialize = function ( filterStructure
, namespaceStructure
, tagList
, conditionalViews
) {
56 var parsedSavedQueries
, pieces
,
57 displayConfig
= mw
.config
.get( 'StructuredChangeFiltersDisplayConfig' ),
58 defaultSavedQueryExists
= mw
.config
.get( 'wgStructuredChangeFiltersDefaultSavedQueryExists' ),
60 views
= $.extend( true, {}, conditionalViews
),
65 if ( namespaceStructure
) {
67 $.each( namespaceStructure
, function ( namespaceID
, label
) {
68 // Build and clean up the individual namespace items definition
71 label
: label
|| mw
.msg( 'blanknamespace' ),
74 ( namespaceID
< 0 || namespaceID
% 2 === 0 ) ?
77 cssClass
: 'mw-changeslist-ns-' + namespaceID
82 title
: mw
.msg( 'namespaces' ),
85 // Group definition (single group)
86 name
: 'namespace', // parameter name is singular
87 type
: 'string_options',
88 title
: mw
.msg( 'namespaces' ),
89 labelPrefixKey
: { 'default': 'rcfilters-tag-prefix-namespace', inverted
: 'rcfilters-tag-prefix-namespace-inverted' },
110 title
: mw
.msg( 'rcfilters-view-tags' ),
113 // Group definition (single group)
114 name
: 'tagfilter', // Parameter name
115 type
: 'string_options',
116 title
: 'rcfilters-view-tags', // Message key
117 labelPrefixKey
: 'rcfilters-tag-prefix-tags',
125 // Add parameter range operations
130 type
: 'single_option',
131 title
: '', // Because it's a hidden group, this title actually appears nowhere
133 allowArbitrary
: true,
134 validate
: $.isNumeric
,
136 min
: 0, // The server normalizes negative numbers to 0 results
139 sortFunc: function ( a
, b
) { return Number( a
.name
) - Number( b
.name
); },
140 'default': mw
.user
.options
.get( this.limitPreferenceName
, displayConfig
.limitDefault
),
142 filters
: displayConfig
.limitArray
.map( function ( num
) {
143 return controller
._createFilterDataFromNumber( num
, num
);
148 type
: 'single_option',
149 title
: '', // Because it's a hidden group, this title actually appears nowhere
151 allowArbitrary
: true,
152 validate
: $.isNumeric
,
155 max
: displayConfig
.maxDays
157 sortFunc: function ( a
, b
) { return Number( a
.name
) - Number( b
.name
); },
158 numToLabelFunc: function ( i
) {
159 return Number( i
) < 1 ?
160 ( Number( i
) * 24 ).toFixed( 2 ) :
163 'default': mw
.user
.options
.get( this.daysPreferenceName
, displayConfig
.daysDefault
),
166 // Hours (1, 2, 6, 12)
167 0.04166, 0.0833, 0.25, 0.5
169 ].concat( displayConfig
.daysArray
)
170 .map( function ( num
) {
171 return controller
._createFilterDataFromNumber(
173 // Convert fractions of days to number of hours for the labels
174 num
< 1 ? Math
.round( num
* 24 ) : num
186 title
: '', // Because it's a hidden group, this title actually appears nowhere
192 'default': String( mw
.user
.options
.get( 'usenewrc', 0 ) )
199 // Before we do anything, we need to see if we require additional items in the
200 // groups that have 'AllowArbitrary'. For the moment, those are only single_option
201 // groups; if we ever expand it, this might need further generalization:
202 $.each( views
, function ( viewName
, viewData
) {
203 viewData
.groups
.forEach( function ( groupData
) {
204 var extraValues
= [];
205 if ( groupData
.allowArbitrary
) {
206 // If the value in the URI isn't in the group, add it
207 if ( uri
.query
[ groupData
.name
] !== undefined ) {
208 extraValues
.push( uri
.query
[ groupData
.name
] );
210 // If the default value isn't in the group, add it
211 if ( groupData
.default !== undefined ) {
212 extraValues
.push( String( groupData
.default ) );
214 controller
.addNumberValuesToGroup( groupData
, extraValues
);
219 // Initialize the model
220 this.filtersModel
.initializeFilters( filterStructure
, views
);
222 this.uriProcessor
= new mw
.rcfilters
.UriProcessor(
224 { normalizeTarget
: this.normalizeTarget
}
227 if ( !mw
.user
.isAnon() ) {
229 parsedSavedQueries
= JSON
.parse( mw
.user
.options
.get( this.savedQueriesPreferenceName
) || '{}' );
231 parsedSavedQueries
= {};
234 // Initialize saved queries
235 this.savedQueriesModel
.initialize( parsedSavedQueries
);
236 if ( this.savedQueriesModel
.isConverted() ) {
237 // Since we know we converted, we're going to re-save
238 // the queries so they are now migrated to the new format
239 this._saveSavedQueries();
243 // Check whether we need to load defaults.
244 // We do this by checking whether the current URI query
245 // contains any parameters recognized by the system.
246 // If it does, we load the given state.
247 // If it doesn't, we have no values at all, and we assume
248 // the user loads the base-page and we load defaults.
249 // Defaults should only be applied on load (if necessary)
251 this.initializing
= true;
253 if ( defaultSavedQueryExists
) {
254 // This came from the server, meaning that we have a default
255 // saved query, but the server could not load it, probably because
256 // it was pre-conversion to the new format.
257 // We need to load this query again
258 this.applySavedQuery( this.savedQueriesModel
.getDefault() );
260 // There are either recognized parameters in the URL
261 // or there are none, but there is also no default
262 // saved query (so defaults are from the backend)
263 // We want to update the state but not fetch results
265 this.updateStateFromUrl( false );
267 pieces
= this._extractChangesListInfo( $( '#mw-content-text' ) );
269 // Update the changes list with the existing data
270 // so it gets processed
271 this.changesListModel
.update(
274 pieces
.noResultsDetails
,
275 true // We're using existing DOM elements
279 this.initializing
= false;
280 this.switchView( 'default' );
282 this.pollingRate
= mw
.config
.get( 'StructuredChangeFiltersLiveUpdatePollingRate' );
283 if ( this.pollingRate
) {
284 this._scheduleLiveUpdate();
289 * Extracts information from the changes list DOM
291 * @param {jQuery} $root Root DOM to find children from
292 * @param {boolean} [statusCode] Server response status code
293 * @return {Object} Information about changes list
294 * @return {Object|string} return.changes Changes list, or 'NO_RESULTS' if there are no results
295 * (either normally or as an error)
296 * @return {string} [return.noResultsDetails] 'NO_RESULTS_NORMAL' for a normal 0-result set,
297 * 'NO_RESULTS_TIMEOUT' for no results due to a timeout, or omitted for more than 0 results
298 * @return {jQuery} return.fieldset Fieldset
300 mw
.rcfilters
.Controller
.prototype._extractChangesListInfo = function ( $root
, statusCode
) {
302 $changesListContents
= $root
.find( '.mw-changeslist' ).first().contents(),
303 areResults
= !!$changesListContents
.length
,
304 checkForLogout
= !areResults
&& statusCode
=== 200;
306 // We check if user logged out on different tab/browser or the session has expired.
307 // 205 status code returned from the server, which indicates that we need to reload the page
308 // is not usable on WL page, because we get redirected to login page, which gives 200 OK
309 // status code (if everything else goes well).
311 if ( checkForLogout
&& !!$root
.find( '#wpName1' ).length
) {
312 location
.reload( false );
317 changes
: $changesListContents
.length
? $changesListContents
: 'NO_RESULTS',
318 fieldset
: $root
.find( 'fieldset.cloptions' ).first()
322 if ( $root
.find( '.mw-changeslist-timeout' ).length
) {
323 info
.noResultsDetails
= 'NO_RESULTS_TIMEOUT';
324 } else if ( $root
.find( '.mw-changeslist-notargetpage' ).length
) {
325 info
.noResultsDetails
= 'NO_RESULTS_NO_TARGET_PAGE';
326 } else if ( $root
.find( '.mw-changeslist-invalidtargetpage' ).length
) {
327 info
.noResultsDetails
= 'NO_RESULTS_INVALID_TARGET_PAGE';
329 info
.noResultsDetails
= 'NO_RESULTS_NORMAL';
337 * Create filter data from a number, for the filters that are numerical value
339 * @param {Number} num Number
340 * @param {Number} numForDisplay Number for the label
341 * @return {Object} Filter data
343 mw
.rcfilters
.Controller
.prototype._createFilterDataFromNumber = function ( num
, numForDisplay
) {
346 label
: mw
.language
.convertNumber( numForDisplay
)
351 * Add an arbitrary values to groups that allow arbitrary values
353 * @param {Object} groupData Group data
354 * @param {string|string[]} arbitraryValues An array of arbitrary values to add to the group
356 mw
.rcfilters
.Controller
.prototype.addNumberValuesToGroup = function ( groupData
, arbitraryValues
) {
357 var controller
= this,
358 normalizeWithinRange = function ( range
, val
) {
359 if ( val
< range
.min
) {
360 return range
.min
; // Min
361 } else if ( val
>= range
.max
) {
362 return range
.max
; // Max
367 arbitraryValues
= Array
.isArray( arbitraryValues
) ? arbitraryValues
: [ arbitraryValues
];
369 // Normalize the arbitrary values and the default value for a range
370 if ( groupData
.range
) {
371 arbitraryValues
= arbitraryValues
.map( function ( val
) {
372 return normalizeWithinRange( groupData
.range
, val
);
375 // Normalize the default, since that's user defined
376 if ( groupData
.default !== undefined ) {
377 groupData
.default = String( normalizeWithinRange( groupData
.range
, groupData
.default ) );
381 // This is only true for single_option group
382 // We assume these are the only groups that will allow for
383 // arbitrary, since it doesn't make any sense for the other
385 arbitraryValues
.forEach( function ( val
) {
387 // If the group allows for arbitrary data
388 groupData
.allowArbitrary
&&
389 // and it is single_option (or string_options, but we
390 // don't have cases of those yet, nor do we plan to)
391 groupData
.type
=== 'single_option' &&
392 // and, if there is a validate method and it passes on
394 ( !groupData
.validate
|| groupData
.validate( val
) ) &&
395 // but if that value isn't already in the definition
397 .map( function ( filterData
) {
398 return String( filterData
.name
);
400 .indexOf( String( val
) ) === -1
402 // Add the filter information
403 groupData
.filters
.push( controller
._createFilterDataFromNumber(
405 groupData
.numToLabelFunc
?
406 groupData
.numToLabelFunc( val
) :
410 // If there's a sort function set up, re-sort the values
411 if ( groupData
.sortFunc
) {
412 groupData
.filters
.sort( groupData
.sortFunc
);
419 * Reset to default filters
421 mw
.rcfilters
.Controller
.prototype.resetToDefaults = function () {
422 var params
= this._getDefaultParams();
423 if ( this.applyParamChange( params
) ) {
424 // Only update the changes list if there was a change to actual filters
425 this.updateChangesList();
427 this.uriProcessor
.updateURL( params
);
432 * Check whether the default values of the filters are all false.
434 * @return {boolean} Defaults are all false
436 mw
.rcfilters
.Controller
.prototype.areDefaultsEmpty = function () {
437 return $.isEmptyObject( this._getDefaultParams() );
441 * Empty all selected filters
443 mw
.rcfilters
.Controller
.prototype.emptyFilters = function () {
444 var highlightedFilterNames
= this.filtersModel
.getHighlightedItems()
445 .map( function ( filterItem
) { return { name
: filterItem
.getName() }; } );
447 if ( this.applyParamChange( {} ) ) {
448 // Only update the changes list if there was a change to actual filters
449 this.updateChangesList();
451 this.uriProcessor
.updateURL();
454 if ( highlightedFilterNames
) {
455 this._trackHighlight( 'clearAll', highlightedFilterNames
);
460 * Update the selected state of a filter
462 * @param {string} filterName Filter name
463 * @param {boolean} [isSelected] Filter selected state
465 mw
.rcfilters
.Controller
.prototype.toggleFilterSelect = function ( filterName
, isSelected
) {
466 var filterItem
= this.filtersModel
.getItemByName( filterName
);
469 // If no filter was found, break
473 isSelected
= isSelected
=== undefined ? !filterItem
.isSelected() : isSelected
;
475 if ( filterItem
.isSelected() !== isSelected
) {
476 this.filtersModel
.toggleFilterSelected( filterName
, isSelected
);
478 this.updateChangesList();
480 // Check filter interactions
481 this.filtersModel
.reassessFilterInteractions( filterItem
);
486 * Clear both highlight and selection of a filter
488 * @param {string} filterName Name of the filter item
490 mw
.rcfilters
.Controller
.prototype.clearFilter = function ( filterName
) {
491 var filterItem
= this.filtersModel
.getItemByName( filterName
),
492 isHighlighted
= filterItem
.isHighlighted(),
493 isSelected
= filterItem
.isSelected();
495 if ( isSelected
|| isHighlighted
) {
496 this.filtersModel
.clearHighlightColor( filterName
);
497 this.filtersModel
.toggleFilterSelected( filterName
, false );
500 // Only update the changes list if the filter changed
501 // its selection state. If it only changed its highlight
503 this.updateChangesList();
506 this.filtersModel
.reassessFilterInteractions( filterItem
);
508 // Log filter grouping
509 this.trackFilterGroupings( 'removefilter' );
512 if ( isHighlighted
) {
513 this._trackHighlight( 'clear', filterName
);
518 * Toggle the highlight feature on and off
520 mw
.rcfilters
.Controller
.prototype.toggleHighlight = function () {
521 this.filtersModel
.toggleHighlight();
522 this.uriProcessor
.updateURL();
524 if ( this.filtersModel
.isHighlightEnabled() ) {
525 mw
.hook( 'RcFilters.highlight.enable' ).fire();
530 * Toggle the namespaces inverted feature on and off
532 mw
.rcfilters
.Controller
.prototype.toggleInvertedNamespaces = function () {
533 this.filtersModel
.toggleInvertedNamespaces();
535 this.filtersModel
.getFiltersByView( 'namespaces' ).filter(
536 function ( filterItem
) { return filterItem
.isSelected(); }
539 // Only re-fetch results if there are namespace items that are actually selected
540 this.updateChangesList();
542 this.uriProcessor
.updateURL();
547 * Set the value of the 'showlinkedto' parameter
548 * @param {boolean} value
550 mw
.rcfilters
.Controller
.prototype.setShowLinkedTo = function ( value
) {
551 var targetItem
= this.filtersModel
.getGroup( 'page' ).getItemByParamName( 'target' ),
552 showLinkedToItem
= this.filtersModel
.getGroup( 'toOrFrom' ).getItemByParamName( 'showlinkedto' );
554 this.filtersModel
.toggleFilterSelected( showLinkedToItem
.getName(), value
);
555 this.uriProcessor
.updateURL();
556 // reload the results only when target is set
557 if ( targetItem
.getValue() ) {
558 this.updateChangesList();
563 * Set the target page
564 * @param {string} page
566 mw
.rcfilters
.Controller
.prototype.setTargetPage = function ( page
) {
567 var targetItem
= this.filtersModel
.getGroup( 'page' ).getItemByParamName( 'target' );
568 targetItem
.setValue( page
);
569 this.uriProcessor
.updateURL();
570 this.updateChangesList();
574 * Set the highlight color for a filter item
576 * @param {string} filterName Name of the filter item
577 * @param {string} color Selected color
579 mw
.rcfilters
.Controller
.prototype.setHighlightColor = function ( filterName
, color
) {
580 this.filtersModel
.setHighlightColor( filterName
, color
);
581 this.uriProcessor
.updateURL();
582 this._trackHighlight( 'set', { name
: filterName
, color
: color
} );
586 * Clear highlight for a filter item
588 * @param {string} filterName Name of the filter item
590 mw
.rcfilters
.Controller
.prototype.clearHighlightColor = function ( filterName
) {
591 this.filtersModel
.clearHighlightColor( filterName
);
592 this.uriProcessor
.updateURL();
593 this._trackHighlight( 'clear', filterName
);
597 * Enable or disable live updates.
598 * @param {boolean} enable True to enable, false to disable
600 mw
.rcfilters
.Controller
.prototype.toggleLiveUpdate = function ( enable
) {
601 this.changesListModel
.toggleLiveUpdate( enable
);
602 if ( this.changesListModel
.getLiveUpdate() && this.changesListModel
.getNewChangesExist() ) {
603 this.updateChangesList( null, this.LIVE_UPDATE
);
608 * Set a timeout for the next live update.
611 mw
.rcfilters
.Controller
.prototype._scheduleLiveUpdate = function () {
612 setTimeout( this._doLiveUpdate
.bind( this ), this.pollingRate
* 1000 );
616 * Perform a live update.
619 mw
.rcfilters
.Controller
.prototype._doLiveUpdate = function () {
620 if ( !this._shouldCheckForNewChanges() ) {
621 // skip this turn and check back later
622 this._scheduleLiveUpdate();
626 this._checkForNewChanges()
627 .then( function ( statusCode
) {
628 // no result is 204 with the 'peek' param
630 var newChanges
= statusCode
=== 200;
632 if ( !this._shouldCheckForNewChanges() ) {
633 // by the time the response is received,
634 // it may not be appropriate anymore
638 // 205 is the status code returned from server when user's logged in/out
639 // status is not matching while fetching live update changes.
640 // This works only on Recent Changes page. For WL, look _extractChangesListInfo.
642 if ( statusCode
=== 205 ) {
643 location
.reload( false );
648 if ( this.changesListModel
.getLiveUpdate() ) {
649 return this.updateChangesList( null, this.LIVE_UPDATE
);
651 this.changesListModel
.setNewChangesExist( true );
655 .always( this._scheduleLiveUpdate
.bind( this ) );
659 * @return {boolean} It's appropriate to check for new changes now
662 mw
.rcfilters
.Controller
.prototype._shouldCheckForNewChanges = function () {
663 return !document
.hidden
&&
664 !this.filtersModel
.hasConflict() &&
665 !this.changesListModel
.getNewChangesExist() &&
666 !this.updatingChangesList
&&
667 this.changesListModel
.getNextFrom();
671 * Check if new changes, newer than those currently shown, are available
673 * @return {jQuery.Promise} Promise object that resolves with a bool
674 * specifying if there are new changes or not
678 mw
.rcfilters
.Controller
.prototype._checkForNewChanges = function () {
681 peek
: 1, // bypasses ChangesList specific UI
682 from: this.changesListModel
.getNextFrom(),
683 isAnon
: mw
.user
.isAnon()
685 return this._queryChangesList( 'liveUpdate', params
).then(
693 * Show the new changes
695 * @return {jQuery.Promise} Promise object that resolves after
696 * fetching and showing the new changes
698 mw
.rcfilters
.Controller
.prototype.showNewChanges = function () {
699 return this.updateChangesList( null, this.SHOW_NEW_CHANGES
);
703 * Save the current model state as a saved query
705 * @param {string} [label] Label of the saved query
706 * @param {boolean} [setAsDefault=false] This query should be set as the default
708 mw
.rcfilters
.Controller
.prototype.saveCurrentQuery = function ( label
, setAsDefault
) {
710 this.savedQueriesModel
.addNewQuery(
711 label
|| mw
.msg( 'rcfilters-savedqueries-defaultlabel' ),
712 this.filtersModel
.getCurrentParameterState( true ),
717 this._saveSavedQueries();
721 * Remove a saved query
723 * @param {string} queryID Query id
725 mw
.rcfilters
.Controller
.prototype.removeSavedQuery = function ( queryID
) {
726 this.savedQueriesModel
.removeQuery( queryID
);
728 this._saveSavedQueries();
732 * Rename a saved query
734 * @param {string} queryID Query id
735 * @param {string} newLabel New label for the query
737 mw
.rcfilters
.Controller
.prototype.renameSavedQuery = function ( queryID
, newLabel
) {
738 var queryItem
= this.savedQueriesModel
.getItemByID( queryID
);
741 queryItem
.updateLabel( newLabel
);
743 this._saveSavedQueries();
747 * Set a saved query as default
749 * @param {string} queryID Query Id. If null is given, default
752 mw
.rcfilters
.Controller
.prototype.setDefaultSavedQuery = function ( queryID
) {
753 this.savedQueriesModel
.setDefault( queryID
);
754 this._saveSavedQueries();
760 * @param {string} queryID Query id
762 mw
.rcfilters
.Controller
.prototype.applySavedQuery = function ( queryID
) {
763 var currentMatchingQuery
,
764 params
= this.savedQueriesModel
.getItemParams( queryID
);
766 currentMatchingQuery
= this.findQueryMatchingCurrentState();
769 currentMatchingQuery
&&
770 currentMatchingQuery
.getID() === queryID
772 // If the query we want to load is the one that is already
773 // loaded, don't reload it
777 if ( this.applyParamChange( params
) ) {
778 // Update changes list only if there was a difference in filter selection
779 this.updateChangesList();
781 this.uriProcessor
.updateURL( params
);
784 // Log filter grouping
785 this.trackFilterGroupings( 'savedfilters' );
789 * Check whether the current filter and highlight state exists
790 * in the saved queries model.
792 * @return {mw.rcfilters.dm.SavedQueryItemModel} Matching item model
794 mw
.rcfilters
.Controller
.prototype.findQueryMatchingCurrentState = function () {
795 return this.savedQueriesModel
.findMatchingQuery(
796 this.filtersModel
.getCurrentParameterState( true )
801 * Save the current state of the saved queries model with all
802 * query item representation in the user settings.
804 mw
.rcfilters
.Controller
.prototype._saveSavedQueries = function () {
805 var stringified
, oldPrefValue
,
806 backupPrefName
= this.savedQueriesPreferenceName
+ '-versionbackup',
807 state
= this.savedQueriesModel
.getState();
810 stringified
= JSON
.stringify( state
);
812 if ( byteLength( stringified
) > 65535 ) {
813 // Sanity check, since the preference can only hold that.
817 if ( !this.wereSavedQueriesSaved
&& this.savedQueriesModel
.isConverted() ) {
818 // The queries were converted from the previous version
819 // Keep the old string in the [prefname]-versionbackup
820 oldPrefValue
= mw
.user
.options
.get( this.savedQueriesPreferenceName
);
822 // Save the old preference in the backup preference
823 new mw
.Api().saveOption( backupPrefName
, oldPrefValue
);
824 // Update the preference for this session
825 mw
.user
.options
.set( backupPrefName
, oldPrefValue
);
828 // Save the preference
829 new mw
.Api().saveOption( this.savedQueriesPreferenceName
, stringified
);
830 // Update the preference for this session
831 mw
.user
.options
.set( this.savedQueriesPreferenceName
, stringified
);
833 // Tag as already saved so we don't do this again
834 this.wereSavedQueriesSaved
= true;
838 * Update sticky preferences with current model state
840 mw
.rcfilters
.Controller
.prototype.updateStickyPreferences = function () {
841 // Update default sticky values with selected, whether they came from
842 // the initial defaults or from the URL value that is being normalized
843 this.updateDaysDefault( this.filtersModel
.getGroup( 'days' ).getSelectedItems()[ 0 ].getParamName() );
844 this.updateLimitDefault( this.filtersModel
.getGroup( 'limit' ).getSelectedItems()[ 0 ].getParamName() );
846 // TODO: Make these automatic by having the model go over sticky
847 // items and update their default values automatically
851 * Update the limit default value
853 * @param {number} newValue New value
855 mw
.rcfilters
.Controller
.prototype.updateLimitDefault = function ( newValue
) {
856 this.updateNumericPreference( this.limitPreferenceName
, newValue
);
860 * Update the days default value
862 * @param {number} newValue New value
864 mw
.rcfilters
.Controller
.prototype.updateDaysDefault = function ( newValue
) {
865 this.updateNumericPreference( this.daysPreferenceName
, newValue
);
869 * Update the group by page default value
871 * @param {boolean} newValue New value
873 mw
.rcfilters
.Controller
.prototype.updateGroupByPageDefault = function ( newValue
) {
874 this.updateNumericPreference( 'usenewrc', Number( newValue
) );
878 * Update a numeric preference with a new value
880 * @param {string} prefName Preference name
881 * @param {number|string} newValue New value
883 mw
.rcfilters
.Controller
.prototype.updateNumericPreference = function ( prefName
, newValue
) {
884 if ( !$.isNumeric( newValue
) ) {
888 newValue
= Number( newValue
);
890 if ( mw
.user
.options
.get( prefName
) !== newValue
) {
891 // Save the preference
892 new mw
.Api().saveOption( prefName
, newValue
);
893 // Update the preference for this session
894 mw
.user
.options
.set( prefName
, newValue
);
899 * Synchronize the URL with the current state of the filters
900 * without adding an history entry.
902 mw
.rcfilters
.Controller
.prototype.replaceUrl = function () {
903 this.uriProcessor
.updateURL();
907 * Update filter state (selection and highlighting) based
908 * on current URL values.
910 * @param {boolean} [fetchChangesList=true] Fetch new results into the changes
911 * list based on the updated model.
913 mw
.rcfilters
.Controller
.prototype.updateStateFromUrl = function ( fetchChangesList
) {
914 fetchChangesList
= fetchChangesList
=== undefined ? true : !!fetchChangesList
;
916 this.uriProcessor
.updateModelBasedOnQuery();
918 // Update the sticky preferences, in case we received a value
920 this.updateStickyPreferences();
922 // Only update and fetch new results if it is requested
923 if ( fetchChangesList
) {
924 this.updateChangesList();
929 * Update the list of changes and notify the model
931 * @param {Object} [params] Extra parameters to add to the API call
932 * @param {string} [updateMode='filterChange'] One of 'filterChange', 'liveUpdate', 'showNewChanges', 'markSeen'
933 * @return {jQuery.Promise} Promise that is resolved when the update is complete
935 mw
.rcfilters
.Controller
.prototype.updateChangesList = function ( params
, updateMode
) {
936 updateMode
= updateMode
=== undefined ? this.FILTER_CHANGE
: updateMode
;
938 if ( updateMode
=== this.FILTER_CHANGE
) {
939 this.uriProcessor
.updateURL( params
);
941 if ( updateMode
=== this.FILTER_CHANGE
|| updateMode
=== this.SHOW_NEW_CHANGES
) {
942 this.changesListModel
.invalidate();
944 this.changesListModel
.setNewChangesExist( false );
945 this.updatingChangesList
= true;
946 return this._fetchChangesList()
949 function ( pieces
) {
950 var $changesListContent
= pieces
.changes
,
951 $fieldset
= pieces
.fieldset
;
952 this.changesListModel
.update(
955 pieces
.noResultsDetails
,
957 // separator between old and new changes
958 updateMode
=== this.SHOW_NEW_CHANGES
|| updateMode
=== this.LIVE_UPDATE
961 // Do nothing for failure
963 .always( function () {
964 this.updatingChangesList
= false;
969 * Get an object representing the default parameter state, whether
970 * it is from the model defaults or from the saved queries.
972 * @return {Object} Default parameters
974 mw
.rcfilters
.Controller
.prototype._getDefaultParams = function () {
975 if ( this.savedQueriesModel
.getDefault() ) {
976 return this.savedQueriesModel
.getDefaultParams();
978 return this.filtersModel
.getDefaultParams();
983 * Query the list of changes from the server for the current filters
985 * @param {string} counterId Id for this request. To allow concurrent requests
986 * not to invalidate each other.
987 * @param {Object} [params={}] Parameters to add to the query
989 * @return {jQuery.Promise} Promise object resolved with { content, status }
991 mw
.rcfilters
.Controller
.prototype._queryChangesList = function ( counterId
, params
) {
992 var uri
= this.uriProcessor
.getUpdatedUri(),
993 stickyParams
= this.filtersModel
.getStickyParamsValues(),
997 params
= params
|| {};
998 params
.action
= 'render'; // bypasses MW chrome
1000 uri
.extend( params
);
1002 this.requestCounter
[ counterId
] = this.requestCounter
[ counterId
] || 0;
1003 requestId
= ++this.requestCounter
[ counterId
];
1004 latestRequest = function () {
1005 return requestId
=== this.requestCounter
[ counterId
];
1008 // Sticky parameters override the URL params
1009 // this is to make sure that whether we represent
1010 // the sticky params in the URL or not (they may
1011 // be normalized out) the sticky parameters are
1012 // always being sent to the server with their
1013 // current/default values
1014 uri
.extend( stickyParams
);
1016 return $.ajax( uri
.toString(), { contentType
: 'html' } )
1018 function ( content
, message
, jqXHR
) {
1019 if ( !latestRequest() ) {
1020 return $.Deferred().reject();
1024 status
: jqXHR
.status
1027 // RC returns 404 when there is no results
1028 function ( jqXHR
) {
1029 if ( latestRequest() ) {
1030 return $.Deferred().resolve(
1032 content
: jqXHR
.responseText
,
1033 status
: jqXHR
.status
1042 * Fetch the list of changes from the server for the current filters
1044 * @return {jQuery.Promise} Promise object that will resolve with the changes list
1047 mw
.rcfilters
.Controller
.prototype._fetchChangesList = function () {
1048 return this._queryChangesList( 'updateChangesList' )
1053 // Status code 0 is not HTTP status code,
1054 // but is valid value of XMLHttpRequest status.
1055 // It is used for variety of network errors, for example
1056 // when an AJAX call was cancelled before getting the response
1057 if ( data
&& data
.status
=== 0 ) {
1059 changes
: 'NO_RESULTS',
1060 // We need empty result set, to avoid exceptions because of undefined value
1062 noResultsDetails
: 'NO_RESULTS_NETWORK_ERROR'
1066 $parsed
= $( '<div>' ).append( $( $.parseHTML(
1067 data
? data
.content
: ''
1070 return this._extractChangesListInfo( $parsed
, data
.status
);
1076 * Track usage of highlight feature
1078 * @param {string} action
1079 * @param {Array|Object|string} filters
1081 mw
.rcfilters
.Controller
.prototype._trackHighlight = function ( action
, filters
) {
1082 filters
= typeof filters
=== 'string' ? { name
: filters
} : filters
;
1083 filters
= !Array
.isArray( filters
) ? [ filters
] : filters
;
1085 'event.ChangesListHighlights',
1089 userId
: mw
.user
.getId()
1095 * Track filter grouping usage
1097 * @param {string} action Action taken
1099 mw
.rcfilters
.Controller
.prototype.trackFilterGroupings = function ( action
) {
1100 var controller
= this,
1101 rightNow
= new Date().getTime(),
1102 randomIdentifier
= String( mw
.user
.sessionId() ) + String( rightNow
) + String( Math
.random() ),
1103 // Get all current filters
1104 filters
= this.filtersModel
.getSelectedItems().map( function ( item
) {
1105 return item
.getName();
1108 action
= action
|| 'filtermenu';
1110 // Check if these filters were the ones we just logged previously
1111 // (Don't log the same grouping twice, in case the user opens/closes)
1112 // the menu without action, or with the same result
1114 // Only log if the two arrays are different in size
1115 filters
.length
!== this.prevLoggedItems
.length
||
1116 // Or if any filters are not the same as the cached filters
1117 filters
.some( function ( filterName
) {
1118 return controller
.prevLoggedItems
.indexOf( filterName
) === -1;
1120 // Or if any cached filters are not the same as given filters
1121 this.prevLoggedItems
.some( function ( filterName
) {
1122 return filters
.indexOf( filterName
) === -1;
1125 filters
.forEach( function ( filterName
) {
1127 'event.ChangesListFilterGrouping',
1130 groupIdentifier
: randomIdentifier
,
1132 userId
: mw
.user
.getId()
1137 // Cache the filter names
1138 this.prevLoggedItems
= filters
;
1143 * Apply a change of parameters to the model state, and check whether
1144 * the new state is different than the old state.
1146 * @param {Object} newParamState New parameter state to apply
1147 * @return {boolean} New applied model state is different than the previous state
1149 mw
.rcfilters
.Controller
.prototype.applyParamChange = function ( newParamState
) {
1151 before
= this.filtersModel
.getSelectedState();
1153 this.filtersModel
.updateStateFromParams( newParamState
);
1155 after
= this.filtersModel
.getSelectedState();
1157 return !OO
.compare( before
, after
);
1161 * Mark all changes as seen on Watchlist
1163 mw
.rcfilters
.Controller
.prototype.markAllChangesAsSeen = function () {
1164 var api
= new mw
.Api();
1165 api
.postWithToken( 'csrf', {
1167 action
: 'setnotificationtimestamp',
1168 entirewatchlist
: true
1169 } ).then( function () {
1170 this.updateChangesList( null, 'markSeen' );
1175 * Set the current search for the system.
1177 * @param {string} searchQuery Search query, including triggers
1179 mw
.rcfilters
.Controller
.prototype.setSearch = function ( searchQuery
) {
1180 this.filtersModel
.setSearch( searchQuery
);
1184 * Switch the view by changing the search query trigger
1185 * without changing the search term
1187 * @param {string} view View to change to
1189 mw
.rcfilters
.Controller
.prototype.switchView = function ( view
) {
1191 this.filtersModel
.getViewTrigger( view
) +
1192 this.filtersModel
.removeViewTriggers( this.filtersModel
.getSearch() )
1197 * Reset the search for a specific view. This means we null the search query
1198 * and replace it with the relevant trigger for the requested view
1200 * @param {string} [view='default'] View to change to
1202 mw
.rcfilters
.Controller
.prototype.resetSearchForView = function ( view
) {
1203 view
= view
|| 'default';
1206 this.filtersModel
.getViewTrigger( view
)
1209 }( mediaWiki
, jQuery
) );