2 /* eslint no-underscore-dangle: "off" */
4 * Controller for the filters in Recent Changes
6 * @param {mw.rcfilters.dm.FiltersViewModel} filtersModel Filters view model
7 * @param {mw.rcfilters.dm.ChangesListViewModel} changesListModel Changes list view model
8 * @param {mw.rcfilters.dm.SavedQueriesModel} savedQueriesModel Saved queries model
10 mw
.rcfilters
.Controller
= function MwRcfiltersController( filtersModel
, changesListModel
, savedQueriesModel
) {
11 this.filtersModel
= filtersModel
;
12 this.changesListModel
= changesListModel
;
13 this.savedQueriesModel
= savedQueriesModel
;
14 this.requestCounter
= 0;
15 this.baseFilterState
= {};
16 this.uriProcessor
= null;
17 this.initializing
= false;
19 this.prevLoggedItems
= [];
23 OO
.initClass( mw
.rcfilters
.Controller
);
26 * Initialize the filter and parameter states
28 * @param {Array} filterStructure Filter definition and structure for the model
29 * @param {Object} [namespaceStructure] Namespace definition
30 * @param {Object} [tagList] Tag definition
32 mw
.rcfilters
.Controller
.prototype.initialize = function ( filterStructure
, namespaceStructure
, tagList
) {
33 var parsedSavedQueries
,
38 $changesList
= $( '.mw-changeslist' ).first().contents(),
39 experimentalViews
= mw
.config
.get( 'wgStructuredChangeFiltersEnableExperimentalViews' );
42 if ( namespaceStructure
&& experimentalViews
) {
44 $.each( namespaceStructure
, function ( namespaceID
, label
) {
45 // Build and clean up the individual namespace items definition
48 label
: label
|| mw
.msg( 'blanknamespace' ),
51 ( namespaceID
< 0 || namespaceID
% 2 === 0 ) ?
54 cssClass
: 'mw-changeslist-ns-' + namespaceID
59 title
: mw
.msg( 'namespaces' ),
62 // Group definition (single group)
63 name
: 'namespace', // parameter name is singular
64 type
: 'string_options',
65 title
: mw
.msg( 'namespaces' ),
66 labelPrefixKey
: { 'default': 'rcfilters-tag-prefix-namespace', inverted
: 'rcfilters-tag-prefix-namespace-inverted' },
73 if ( tagList
&& experimentalViews
) {
75 title
: mw
.msg( 'rcfilters-view-tags' ),
78 // Group definition (single group)
79 name
: 'tagfilter', // Parameter name
80 type
: 'string_options',
81 title
: 'rcfilters-view-tags', // Message key
82 labelPrefixKey
: 'rcfilters-tag-prefix-tags',
90 // Add parameter range operations
95 type
: 'single_option',
96 title
: '', // Because it's a hidden group, this title actually appears nowhere
99 validate
: $.isNumeric
,
100 sortFunc: function ( a
, b
) { return Number( a
.name
) - Number( b
.name
); },
101 'default': mw
.user
.options
.get( 'rclimit' ),
102 filters
: [ 50, 100, 250, 500 ].map( function ( num
) {
103 return controller
._createFilterDataFromNumber( num
, num
);
108 type
: 'single_option',
109 title
: '', // Because it's a hidden group, this title actually appears nowhere
111 allowArbitrary
: true,
112 validate
: $.isNumeric
,
113 sortFunc: function ( a
, b
) { return Number( a
.name
) - Number( b
.name
); },
114 numToLabelFunc: function ( i
) {
115 return Number( i
) < 1 ?
116 ( Number( i
) * 24 ).toFixed( 2 ) :
119 'default': mw
.user
.options
.get( 'rcdays' ),
121 // Hours (1, 2, 6, 12)
122 0.04166, 0.0833, 0.25, 0.5,
125 ].map( function ( num
) {
126 return controller
._createFilterDataFromNumber(
128 // Convert fractions of days to number of hours for the labels
129 num
< 1 ? Math
.round( num
* 24 ) : num
136 // Before we do anything, we need to see if we require additional items in the
137 // groups that have 'AllowArbitrary'. For the moment, those are only single_option
138 // groups; if we ever expand it, this might need further generalization:
139 $.each( views
, function ( viewName
, viewData
) {
140 viewData
.groups
.forEach( function ( groupData
) {
141 var extraValues
= [];
142 if ( groupData
.allowArbitrary
) {
143 // If the value in the URI isn't in the group, add it
144 if ( uri
.query
[ groupData
.name
] !== undefined ) {
145 extraValues
.push( uri
.query
[ groupData
.name
] );
147 // If the default value isn't in the group, add it
148 if ( groupData
.default !== undefined ) {
149 extraValues
.push( String( groupData
.default ) );
151 controller
.addNumberValuesToGroup( groupData
, extraValues
);
156 // Initialize the model
157 this.filtersModel
.initializeFilters( filterStructure
, views
);
159 this._buildBaseFilterState();
161 this.uriProcessor
= new mw
.rcfilters
.UriProcessor(
166 parsedSavedQueries
= JSON
.parse( mw
.user
.options
.get( 'rcfilters-saved-queries' ) || '{}' );
168 parsedSavedQueries
= {};
171 // The queries are saved in a minimized state, so we need
172 // to send over the base state so the saved queries model
173 // can normalize them per each query item
174 this.savedQueriesModel
.initialize(
176 this._getBaseFilterState()
179 // Check whether we need to load defaults.
180 // We do this by checking whether the current URI query
181 // contains any parameters recognized by the system.
182 // If it does, we load the given state.
183 // If it doesn't, we have no values at all, and we assume
184 // the user loads the base-page and we load defaults.
185 // Defaults should only be applied on load (if necessary)
187 this.initializing
= true;
189 this.savedQueriesModel
.getDefault() &&
190 !this.uriProcessor
.doesQueryContainRecognizedParams( uri
.query
)
192 // We have defaults from a saved query.
193 // We will load them straight-forward (as if
194 // they were clicked in the menu) so we trigger
195 // a full ajax request and change of URL
196 this.applySavedQuery( this.savedQueriesModel
.getDefault() );
198 // There are either recognized parameters in the URL
199 // or there are none, but there is also no default
200 // saved query (so defaults are from the backend)
201 // We want to update the state but not fetch results
203 this.updateStateFromUrl( false );
205 // Update the changes list with the existing data
206 // so it gets processed
207 this.changesListModel
.update(
208 $changesList
.length
? $changesList
: 'NO_RESULTS',
209 $( 'fieldset.rcoptions' ).first(),
210 true // We're using existing DOM elements
214 this.initializing
= false;
215 this.switchView( 'default' );
219 * Create filter data from a number, for the filters that are numerical value
221 * @param {Number} num Number
222 * @param {Number} numForDisplay Number for the label
223 * @return {Object} Filter data
225 mw
.rcfilters
.Controller
.prototype._createFilterDataFromNumber = function ( num
, numForDisplay
) {
228 label
: mw
.language
.convertNumber( numForDisplay
)
233 * Add an arbitrary values to groups that allow arbitrary values
235 * @param {Object} groupData Group data
236 * @param {string|string[]} arbitraryValues An array of arbitrary values to add to the group
238 mw
.rcfilters
.Controller
.prototype.addNumberValuesToGroup = function ( groupData
, arbitraryValues
) {
239 var controller
= this;
241 arbitraryValues
= Array
.isArray( arbitraryValues
) ? arbitraryValues
: [ arbitraryValues
];
243 // This is only true for single_option group
244 // We assume these are the only groups that will allow for
245 // arbitrary, since it doesn't make any sense for the other
247 arbitraryValues
.forEach( function ( val
) {
249 // If the group allows for arbitrary data
250 groupData
.allowArbitrary
&&
251 // and it is single_option (or string_options, but we
252 // don't have cases of those yet, nor do we plan to)
253 groupData
.type
=== 'single_option' &&
254 // and, if there is a validate method and it passes on
256 ( !groupData
.validate
|| groupData
.validate( val
) ) &&
257 // but if that value isn't already in the definition
259 .map( function ( filterData
) {
260 return filterData
.name
;
262 .indexOf( val
) === -1
264 // Add the filter information
265 groupData
.filters
.push( controller
._createFilterDataFromNumber(
267 groupData
.numToLabelFunc
?
268 groupData
.numToLabelFunc( val
) :
272 // If there's a sort function set up, re-sort the values
273 if ( groupData
.sortFunc
) {
274 groupData
.filters
.sort( groupData
.sortFunc
);
281 * Switch the view of the filters model
283 * @param {string} view Requested view
285 mw
.rcfilters
.Controller
.prototype.switchView = function ( view
) {
286 this.filtersModel
.switchView( view
);
290 * Reset to default filters
292 mw
.rcfilters
.Controller
.prototype.resetToDefaults = function () {
293 this.uriProcessor
.updateModelBasedOnQuery( this._getDefaultParams() );
294 this.updateChangesList();
298 * Empty all selected filters
300 mw
.rcfilters
.Controller
.prototype.emptyFilters = function () {
301 var highlightedFilterNames
= this.filtersModel
302 .getHighlightedItems()
303 .map( function ( filterItem
) { return { name
: filterItem
.getName() }; } );
305 this.filtersModel
.emptyAllFilters();
306 this.filtersModel
.clearAllHighlightColors();
307 // Check all filter interactions
308 this.filtersModel
.reassessFilterInteractions();
310 this.updateChangesList();
312 if ( highlightedFilterNames
) {
313 this._trackHighlight( 'clearAll', highlightedFilterNames
);
318 * Update the selected state of a filter
320 * @param {string} filterName Filter name
321 * @param {boolean} [isSelected] Filter selected state
323 mw
.rcfilters
.Controller
.prototype.toggleFilterSelect = function ( filterName
, isSelected
) {
324 var filterItem
= this.filtersModel
.getItemByName( filterName
);
327 // If no filter was found, break
331 isSelected
= isSelected
=== undefined ? !filterItem
.isSelected() : isSelected
;
333 if ( filterItem
.isSelected() !== isSelected
) {
334 this.filtersModel
.toggleFilterSelected( filterName
, isSelected
);
336 this.updateChangesList();
338 // Check filter interactions
339 this.filtersModel
.reassessFilterInteractions( filterItem
);
344 * Clear both highlight and selection of a filter
346 * @param {string} filterName Name of the filter item
348 mw
.rcfilters
.Controller
.prototype.clearFilter = function ( filterName
) {
349 var filterItem
= this.filtersModel
.getItemByName( filterName
),
350 isHighlighted
= filterItem
.isHighlighted();
352 if ( filterItem
.isSelected() || isHighlighted
) {
353 this.filtersModel
.clearHighlightColor( filterName
);
354 this.filtersModel
.toggleFilterSelected( filterName
, false );
355 this.updateChangesList();
356 this.filtersModel
.reassessFilterInteractions( filterItem
);
358 // Log filter grouping
359 this.trackFilterGroupings( 'removefilter' );
362 if ( isHighlighted
) {
363 this._trackHighlight( 'clear', filterName
);
368 * Toggle the highlight feature on and off
370 mw
.rcfilters
.Controller
.prototype.toggleHighlight = function () {
371 this.filtersModel
.toggleHighlight();
374 if ( this.filtersModel
.isHighlightEnabled() ) {
375 mw
.hook( 'RcFilters.highlight.enable' ).fire();
380 * Toggle the namespaces inverted feature on and off
382 mw
.rcfilters
.Controller
.prototype.toggleInvertedNamespaces = function () {
383 this.filtersModel
.toggleInvertedNamespaces();
384 this.updateChangesList();
388 * Set the highlight color for a filter item
390 * @param {string} filterName Name of the filter item
391 * @param {string} color Selected color
393 mw
.rcfilters
.Controller
.prototype.setHighlightColor = function ( filterName
, color
) {
394 this.filtersModel
.setHighlightColor( filterName
, color
);
396 this._trackHighlight( 'set', { name
: filterName
, color
: color
} );
400 * Clear highlight for a filter item
402 * @param {string} filterName Name of the filter item
404 mw
.rcfilters
.Controller
.prototype.clearHighlightColor = function ( filterName
) {
405 this.filtersModel
.clearHighlightColor( filterName
);
407 this._trackHighlight( 'clear', filterName
);
411 * Enable or disable live updates.
412 * @param {boolean} enable True to enable, false to disable
414 mw
.rcfilters
.Controller
.prototype.toggleLiveUpdate = function ( enable
) {
415 if ( enable
&& !this.liveUpdateTimeout
) {
416 this._scheduleLiveUpdate();
417 } else if ( !enable
&& this.liveUpdateTimeout
) {
418 clearTimeout( this.liveUpdateTimeout
);
419 this.liveUpdateTimeout
= null;
424 * Set a timeout for the next live update.
427 mw
.rcfilters
.Controller
.prototype._scheduleLiveUpdate = function () {
428 this.liveUpdateTimeout
= setTimeout( this._doLiveUpdate
.bind( this ), 3000 );
432 * Perform a live update.
435 mw
.rcfilters
.Controller
.prototype._doLiveUpdate = function () {
436 var controller
= this;
437 this.updateChangesList( {}, true )
438 .always( function () {
439 if ( controller
.liveUpdateTimeout
) {
440 // Live update was not disabled in the meantime
441 controller
._scheduleLiveUpdate();
447 * Save the current model state as a saved query
449 * @param {string} [label] Label of the saved query
451 mw
.rcfilters
.Controller
.prototype.saveCurrentQuery = function ( label
) {
452 var highlightedItems
= {},
453 highlightEnabled
= this.filtersModel
.isHighlightEnabled();
455 // Prepare highlights
456 this.filtersModel
.getHighlightedItems().forEach( function ( item
) {
457 highlightedItems
[ item
.getName() ] = highlightEnabled
?
458 item
.getHighlightColor() : null;
460 // These are filter states; highlight is stored as boolean
461 highlightedItems
.highlight
= this.filtersModel
.isHighlightEnabled();
464 this.savedQueriesModel
.addNewQuery(
465 label
|| mw
.msg( 'rcfilters-savedqueries-defaultlabel' ),
467 filters
: this.filtersModel
.getSelectedState(),
468 highlights
: highlightedItems
,
469 invert
: this.filtersModel
.areNamespacesInverted()
474 this._saveSavedQueries();
478 * Remove a saved query
480 * @param {string} queryID Query id
482 mw
.rcfilters
.Controller
.prototype.removeSavedQuery = function ( queryID
) {
483 this.savedQueriesModel
.removeQuery( queryID
);
485 this._saveSavedQueries();
489 * Rename a saved query
491 * @param {string} queryID Query id
492 * @param {string} newLabel New label for the query
494 mw
.rcfilters
.Controller
.prototype.renameSavedQuery = function ( queryID
, newLabel
) {
495 var queryItem
= this.savedQueriesModel
.getItemByID( queryID
);
498 queryItem
.updateLabel( newLabel
);
500 this._saveSavedQueries();
504 * Set a saved query as default
506 * @param {string} queryID Query Id. If null is given, default
509 mw
.rcfilters
.Controller
.prototype.setDefaultSavedQuery = function ( queryID
) {
510 this.savedQueriesModel
.setDefault( queryID
);
511 this._saveSavedQueries();
517 * @param {string} queryID Query id
519 mw
.rcfilters
.Controller
.prototype.applySavedQuery = function ( queryID
) {
520 var data
, highlights
,
521 queryItem
= this.savedQueriesModel
.getItemByID( queryID
);
524 data
= queryItem
.getData();
525 highlights
= data
.highlights
;
527 // Backwards compatibility; initial version mispelled 'highlight' with 'highlights'
528 highlights
.highlight
= highlights
.highlights
|| highlights
.highlight
;
530 // Update model state from filters
531 this.filtersModel
.toggleFiltersSelected( data
.filters
);
533 // Update namespace inverted property
534 this.filtersModel
.toggleInvertedNamespaces( !!Number( data
.invert
) );
536 // Update highlight state
537 this.filtersModel
.toggleHighlight( !!Number( highlights
.highlight
) );
538 this.filtersModel
.getItems().forEach( function ( filterItem
) {
539 var color
= highlights
[ filterItem
.getName() ];
541 filterItem
.setHighlightColor( color
);
543 filterItem
.clearHighlightColor();
547 // Check all filter interactions
548 this.filtersModel
.reassessFilterInteractions();
550 this.updateChangesList();
552 // Log filter grouping
553 this.trackFilterGroupings( 'savedfilters' );
558 * Check whether the current filter and highlight state exists
559 * in the saved queries model.
561 * @return {boolean} Query exists
563 mw
.rcfilters
.Controller
.prototype.findQueryMatchingCurrentState = function () {
564 var highlightedItems
= {};
566 // Prepare highlights of the current query
567 this.filtersModel
.getItemsSupportingHighlights().forEach( function ( item
) {
568 highlightedItems
[ item
.getName() ] = item
.getHighlightColor();
570 highlightedItems
.highlight
= this.filtersModel
.isHighlightEnabled();
572 return this.savedQueriesModel
.findMatchingQuery(
574 filters
: this.filtersModel
.getSelectedState(),
575 highlights
: highlightedItems
,
576 invert
: this.filtersModel
.areNamespacesInverted()
582 * Get an object representing the base state of parameters
585 * This is meant to make sure that the saved queries that are
586 * in memory are always the same structure as what we would get
587 * by calling the current model's "getSelectedState" and by checking
590 * In cases where a user saved a query when the system had a certain
591 * set of filters, and then a filter was added to the system, we want
592 * to make sure that the stored queries can still be comparable to
593 * the current state, which means that we need the base state for
596 * - Saved queries are stored in "minimal" view (only changed filters
597 * are stored); When we initialize the system, we merge each minimal
598 * query with the base state (using 'getNormalizedFilters') so all
599 * saved queries have the exact same structure as what we would get
600 * by checking the getSelectedState of the filter.
601 * - When we save the queries, we minimize the object to only represent
602 * whatever has actually changed, rather than store the entire
603 * object. To check what actually is different so we can store it,
604 * we need to obtain a base state to compare against, this is
605 * what #_getMinimalFilterList does
607 mw
.rcfilters
.Controller
.prototype._buildBaseFilterState = function () {
608 var defaultParams
= this.filtersModel
.getDefaultParams(),
609 highlightedItems
= {};
611 // Prepare highlights
612 this.filtersModel
.getItemsSupportingHighlights().forEach( function ( item
) {
613 highlightedItems
[ item
.getName() ] = null;
615 highlightedItems
.highlight
= false;
617 this.baseFilterState
= {
618 filters
: this.filtersModel
.getFiltersFromParameters( defaultParams
),
619 highlights
: highlightedItems
,
625 * Get an object representing the base filter state of both
626 * filters and highlights. The structure is similar to what we use
627 * to store each query in the saved queries object:
633 * filterName: (string|null)
637 * @return {Object} Object representing the base state of
638 * parameters and highlights
640 mw
.rcfilters
.Controller
.prototype._getBaseFilterState = function () {
641 return this.baseFilterState
;
645 * Get an object that holds only the parameters and highlights that have
646 * values different than the base default value.
648 * This is the reverse of the normalization we do initially on loading and
649 * initializing the saved queries model.
651 * @param {Object} valuesObject Object representing the state of both
652 * filters and highlights in its normalized version, to be minimized.
653 * @return {Object} Minimal filters and highlights list
655 mw
.rcfilters
.Controller
.prototype._getMinimalFilterList = function ( valuesObject
) {
656 var result
= { filters
: {}, highlights
: {} },
657 baseState
= this._getBaseFilterState();
660 $.each( valuesObject
.filters
, function ( name
, value
) {
661 if ( baseState
.filters
!== undefined && baseState
.filters
[ name
] !== value
) {
662 result
.filters
[ name
] = value
;
666 $.each( valuesObject
.highlights
, function ( name
, value
) {
667 if ( baseState
.highlights
!== undefined && baseState
.highlights
[ name
] !== value
) {
668 result
.highlights
[ name
] = value
;
676 * Save the current state of the saved queries model with all
677 * query item representation in the user settings.
679 mw
.rcfilters
.Controller
.prototype._saveSavedQueries = function () {
681 state
= this.savedQueriesModel
.getState(),
684 // Minimize before save
685 $.each( state
.queries
, function ( queryID
, info
) {
686 state
.queries
[ queryID
].data
= controller
._getMinimalFilterList( info
.data
);
690 stringified
= JSON
.stringify( state
);
692 if ( stringified
.length
> 65535 ) {
693 // Sanity check, since the preference can only hold that.
697 // Save the preference
698 new mw
.Api().saveOption( 'rcfilters-saved-queries', stringified
);
699 // Update the preference for this session
700 mw
.user
.options
.set( 'rcfilters-saved-queries', stringified
);
704 * Synchronize the URL with the current state of the filters
705 * without adding an history entry.
707 mw
.rcfilters
.Controller
.prototype.replaceUrl = function () {
708 mw
.rcfilters
.UriProcessor
.static.replaceState( this._getUpdatedUri() );
712 * Update filter state (selection and highlighting) based
713 * on current URL values.
715 * @param {boolean} [fetchChangesList=true] Fetch new results into the changes
716 * list based on the updated model.
718 mw
.rcfilters
.Controller
.prototype.updateStateFromUrl = function ( fetchChangesList
) {
719 fetchChangesList
= fetchChangesList
=== undefined ? true : !!fetchChangesList
;
721 this.uriProcessor
.updateModelBasedOnQuery( new mw
.Uri().query
);
723 // Only update and fetch new results if it is requested
724 if ( fetchChangesList
) {
725 this.updateChangesList();
730 * Update the list of changes and notify the model
732 * @param {Object} [params] Extra parameters to add to the API call
733 * @param {boolean} [isLiveUpdate] Don't update the URL or invalidate the changes list
734 * @return {jQuery.Promise} Promise that is resolved when the update is complete
736 mw
.rcfilters
.Controller
.prototype.updateChangesList = function ( params
, isLiveUpdate
) {
737 if ( !isLiveUpdate
) {
738 this._updateURL( params
);
739 this.changesListModel
.invalidate();
741 return this._fetchChangesList()
744 function ( pieces
) {
745 var $changesListContent
= pieces
.changes
,
746 $fieldset
= pieces
.fieldset
;
747 this.changesListModel
.update( $changesListContent
, $fieldset
);
749 // Do nothing for failure
754 * Get an object representing the default parameter state, whether
755 * it is from the model defaults or from the saved queries.
757 * @return {Object} Default parameters
759 mw
.rcfilters
.Controller
.prototype._getDefaultParams = function () {
760 var data
, queryHighlights
,
762 savedHighlights
= {},
763 defaultSavedQueryItem
= this.savedQueriesModel
.getItemByID( this.savedQueriesModel
.getDefault() );
765 if ( mw
.config
.get( 'wgStructuredChangeFiltersEnableSaving' ) &&
766 defaultSavedQueryItem
) {
768 data
= defaultSavedQueryItem
.getData();
770 queryHighlights
= data
.highlights
|| {};
771 savedParams
= this.filtersModel
.getParametersFromFilters( data
.filters
|| {} );
773 // Translate highlights to parameters
774 savedHighlights
.highlight
= String( Number( queryHighlights
.highlight
) );
775 $.each( queryHighlights
, function ( filterName
, color
) {
776 if ( filterName
!== 'highlights' ) {
777 savedHighlights
[ filterName
+ '_color' ] = color
;
781 return $.extend( true, {}, savedParams
, savedHighlights
);
784 return this.filtersModel
.getDefaultParams();
788 * Update the URL of the page to reflect current filters
790 * This should not be called directly from outside the controller.
791 * If an action requires changing the URL, it should either use the
792 * highlighting actions below, or call #updateChangesList which does
793 * the uri corrections already.
795 * @param {Object} [params] Extra parameters to add to the API call
797 mw
.rcfilters
.Controller
.prototype._updateURL = function ( params
) {
798 var currentUri
= new mw
.Uri(),
799 updatedUri
= this._getUpdatedUri();
801 updatedUri
.extend( params
|| {} );
804 this.uriProcessor
.getVersion( currentUri
.query
) !== 2 ||
805 this.uriProcessor
.isNewState( currentUri
.query
, updatedUri
.query
)
807 mw
.rcfilters
.UriProcessor
.static.replaceState( updatedUri
);
812 * Get an updated mw.Uri object based on the model state
814 * @return {mw.Uri} Updated Uri
816 mw
.rcfilters
.Controller
.prototype._getUpdatedUri = function () {
817 var uri
= new mw
.Uri();
820 uri
.query
= this.uriProcessor
.minimizeQuery(
824 // We want to retain unrecognized params
825 // The uri params from model will override
826 // any recognized value in the current uri
827 // query, retain unrecognized params, and
828 // the result will then be minimized
830 this.uriProcessor
.getUriParametersFromModel(),
839 * Fetch the list of changes from the server for the current filters
841 * @return {jQuery.Promise} Promise object that will resolve with the changes list
842 * or with a string denoting no results.
844 mw
.rcfilters
.Controller
.prototype._fetchChangesList = function () {
845 var uri
= this._getUpdatedUri(),
846 requestId
= ++this.requestCounter
,
847 latestRequest = function () {
848 return requestId
=== this.requestCounter
;
851 return $.ajax( uri
.toString(), { contentType
: 'html' } )
856 if ( !latestRequest() ) {
857 return $.Deferred().reject();
860 $parsed
= $( $.parseHTML( html
) );
864 changes
: $parsed
.find( '.mw-changeslist' ).first().contents(),
866 fieldset
: $parsed
.find( 'fieldset.rcoptions' ).first()
870 function ( responseObj
) {
873 if ( !latestRequest() ) {
874 return $.Deferred().reject();
877 $parsed
= $( $.parseHTML( responseObj
.responseText
) );
879 // Force a resolve state to this promise
880 return $.Deferred().resolve( {
881 changes
: 'NO_RESULTS',
882 fieldset
: $parsed
.find( 'fieldset.rcoptions' ).first()
889 * Track usage of highlight feature
891 * @param {string} action
892 * @param {array|object|string} filters
894 mw
.rcfilters
.Controller
.prototype._trackHighlight = function ( action
, filters
) {
895 filters
= typeof filters
=== 'string' ? { name
: filters
} : filters
;
896 filters
= !Array
.isArray( filters
) ? [ filters
] : filters
;
898 'event.ChangesListHighlights',
902 userId
: mw
.user
.getId()
908 * Track filter grouping usage
910 * @param {string} action Action taken
912 mw
.rcfilters
.Controller
.prototype.trackFilterGroupings = function ( action
) {
913 var controller
= this,
914 rightNow
= new Date().getTime(),
915 randomIdentifier
= String( mw
.user
.sessionId() ) + String( rightNow
) + String( Math
.random() ),
916 // Get all current filters
917 filters
= this.filtersModel
.getSelectedItems().map( function ( item
) {
918 return item
.getName();
921 action
= action
|| 'filtermenu';
923 // Check if these filters were the ones we just logged previously
924 // (Don't log the same grouping twice, in case the user opens/closes)
925 // the menu without action, or with the same result
927 // Only log if the two arrays are different in size
928 filters
.length
!== this.prevLoggedItems
.length
||
929 // Or if any filters are not the same as the cached filters
930 filters
.some( function ( filterName
) {
931 return controller
.prevLoggedItems
.indexOf( filterName
) === -1;
933 // Or if any cached filters are not the same as given filters
934 this.prevLoggedItems
.some( function ( filterName
) {
935 return filters
.indexOf( filterName
) === -1;
938 filters
.forEach( function ( filterName
) {
940 'event.ChangesListFilterGrouping',
943 groupIdentifier
: randomIdentifier
,
945 userId
: mw
.user
.getId()
950 // Cache the filter names
951 this.prevLoggedItems
= filters
;
954 }( mediaWiki
, jQuery
) );