2 /* eslint no-underscore-dangle: "off" */
4 * Controller for the filters in Recent Changes
8 * @param {mw.rcfilters.dm.FiltersViewModel} filtersModel Filters view model
9 * @param {mw.rcfilters.dm.ChangesListViewModel} changesListModel Changes list view model
10 * @param {mw.rcfilters.dm.SavedQueriesModel} savedQueriesModel Saved queries model
12 mw
.rcfilters
.Controller
= function MwRcfiltersController( filtersModel
, changesListModel
, savedQueriesModel
) {
13 this.filtersModel
= filtersModel
;
14 this.changesListModel
= changesListModel
;
15 this.savedQueriesModel
= savedQueriesModel
;
16 this.requestCounter
= 0;
17 this.baseFilterState
= {};
18 this.uriProcessor
= null;
19 this.initializing
= false;
21 this.prevLoggedItems
= [];
25 OO
.initClass( mw
.rcfilters
.Controller
);
28 * Initialize the filter and parameter states
30 * @param {Array} filterStructure Filter definition and structure for the model
31 * @param {Object} [namespaceStructure] Namespace definition
32 * @param {Object} [tagList] Tag definition
34 mw
.rcfilters
.Controller
.prototype.initialize = function ( filterStructure
, namespaceStructure
, tagList
) {
35 var parsedSavedQueries
,
40 $changesList
= $( '.mw-changeslist' ).first().contents();
43 if ( namespaceStructure
) {
45 $.each( namespaceStructure
, function ( namespaceID
, label
) {
46 // Build and clean up the individual namespace items definition
49 label
: label
|| mw
.msg( 'blanknamespace' ),
52 ( namespaceID
< 0 || namespaceID
% 2 === 0 ) ?
55 cssClass
: 'mw-changeslist-ns-' + namespaceID
60 title
: mw
.msg( 'namespaces' ),
63 // Group definition (single group)
64 name
: 'namespace', // parameter name is singular
65 type
: 'string_options',
66 title
: mw
.msg( 'namespaces' ),
67 labelPrefixKey
: { 'default': 'rcfilters-tag-prefix-namespace', inverted
: 'rcfilters-tag-prefix-namespace-inverted' },
76 title
: mw
.msg( 'rcfilters-view-tags' ),
79 // Group definition (single group)
80 name
: 'tagfilter', // Parameter name
81 type
: 'string_options',
82 title
: 'rcfilters-view-tags', // Message key
83 labelPrefixKey
: 'rcfilters-tag-prefix-tags',
91 // Add parameter range operations
96 type
: 'single_option',
97 title
: '', // Because it's a hidden group, this title actually appears nowhere
100 validate
: $.isNumeric
,
101 sortFunc: function ( a
, b
) { return Number( a
.name
) - Number( b
.name
); },
102 'default': mw
.user
.options
.get( 'rclimit' ),
103 filters
: [ 50, 100, 250, 500 ].map( function ( num
) {
104 return controller
._createFilterDataFromNumber( num
, num
);
109 type
: 'single_option',
110 title
: '', // Because it's a hidden group, this title actually appears nowhere
112 allowArbitrary
: true,
113 validate
: $.isNumeric
,
114 sortFunc: function ( a
, b
) { return Number( a
.name
) - Number( b
.name
); },
115 numToLabelFunc: function ( i
) {
116 return Number( i
) < 1 ?
117 ( Number( i
) * 24 ).toFixed( 2 ) :
120 'default': mw
.user
.options
.get( 'rcdays' ),
122 // Hours (1, 2, 6, 12)
123 0.04166, 0.0833, 0.25, 0.5,
126 ].map( function ( num
) {
127 return controller
._createFilterDataFromNumber(
129 // Convert fractions of days to number of hours for the labels
130 num
< 1 ? Math
.round( num
* 24 ) : num
137 // Before we do anything, we need to see if we require additional items in the
138 // groups that have 'AllowArbitrary'. For the moment, those are only single_option
139 // groups; if we ever expand it, this might need further generalization:
140 $.each( views
, function ( viewName
, viewData
) {
141 viewData
.groups
.forEach( function ( groupData
) {
142 var extraValues
= [];
143 if ( groupData
.allowArbitrary
) {
144 // If the value in the URI isn't in the group, add it
145 if ( uri
.query
[ groupData
.name
] !== undefined ) {
146 extraValues
.push( uri
.query
[ groupData
.name
] );
148 // If the default value isn't in the group, add it
149 if ( groupData
.default !== undefined ) {
150 extraValues
.push( String( groupData
.default ) );
152 controller
.addNumberValuesToGroup( groupData
, extraValues
);
157 // Initialize the model
158 this.filtersModel
.initializeFilters( filterStructure
, views
);
160 this._buildBaseFilterState();
162 this.uriProcessor
= new mw
.rcfilters
.UriProcessor(
167 parsedSavedQueries
= JSON
.parse( mw
.user
.options
.get( 'rcfilters-saved-queries' ) || '{}' );
169 parsedSavedQueries
= {};
172 // The queries are saved in a minimized state, so we need
173 // to send over the base state so the saved queries model
174 // can normalize them per each query item
175 this.savedQueriesModel
.initialize(
177 this._getBaseFilterState()
180 // Check whether we need to load defaults.
181 // We do this by checking whether the current URI query
182 // contains any parameters recognized by the system.
183 // If it does, we load the given state.
184 // If it doesn't, we have no values at all, and we assume
185 // the user loads the base-page and we load defaults.
186 // Defaults should only be applied on load (if necessary)
188 this.initializing
= true;
190 this.savedQueriesModel
.getDefault() &&
191 !this.uriProcessor
.doesQueryContainRecognizedParams( uri
.query
)
193 // We have defaults from a saved query.
194 // We will load them straight-forward (as if
195 // they were clicked in the menu) so we trigger
196 // a full ajax request and change of URL
197 this.applySavedQuery( this.savedQueriesModel
.getDefault() );
199 // There are either recognized parameters in the URL
200 // or there are none, but there is also no default
201 // saved query (so defaults are from the backend)
202 // We want to update the state but not fetch results
204 this.updateStateFromUrl( false );
206 // Update the changes list with the existing data
207 // so it gets processed
208 this.changesListModel
.update(
209 $changesList
.length
? $changesList
: 'NO_RESULTS',
210 $( 'fieldset.rcoptions' ).first(),
211 true // We're using existing DOM elements
215 this.initializing
= false;
216 this.switchView( 'default' );
220 * Create filter data from a number, for the filters that are numerical value
222 * @param {Number} num Number
223 * @param {Number} numForDisplay Number for the label
224 * @return {Object} Filter data
226 mw
.rcfilters
.Controller
.prototype._createFilterDataFromNumber = function ( num
, numForDisplay
) {
229 label
: mw
.language
.convertNumber( numForDisplay
)
234 * Add an arbitrary values to groups that allow arbitrary values
236 * @param {Object} groupData Group data
237 * @param {string|string[]} arbitraryValues An array of arbitrary values to add to the group
239 mw
.rcfilters
.Controller
.prototype.addNumberValuesToGroup = function ( groupData
, arbitraryValues
) {
240 var controller
= this;
242 arbitraryValues
= Array
.isArray( arbitraryValues
) ? arbitraryValues
: [ arbitraryValues
];
244 // This is only true for single_option group
245 // We assume these are the only groups that will allow for
246 // arbitrary, since it doesn't make any sense for the other
248 arbitraryValues
.forEach( function ( val
) {
250 // If the group allows for arbitrary data
251 groupData
.allowArbitrary
&&
252 // and it is single_option (or string_options, but we
253 // don't have cases of those yet, nor do we plan to)
254 groupData
.type
=== 'single_option' &&
255 // and, if there is a validate method and it passes on
257 ( !groupData
.validate
|| groupData
.validate( val
) ) &&
258 // but if that value isn't already in the definition
260 .map( function ( filterData
) {
261 return filterData
.name
;
263 .indexOf( val
) === -1
265 // Add the filter information
266 groupData
.filters
.push( controller
._createFilterDataFromNumber(
268 groupData
.numToLabelFunc
?
269 groupData
.numToLabelFunc( val
) :
273 // If there's a sort function set up, re-sort the values
274 if ( groupData
.sortFunc
) {
275 groupData
.filters
.sort( groupData
.sortFunc
);
282 * Switch the view of the filters model
284 * @param {string} view Requested view
286 mw
.rcfilters
.Controller
.prototype.switchView = function ( view
) {
287 this.filtersModel
.switchView( view
);
291 * Reset to default filters
293 mw
.rcfilters
.Controller
.prototype.resetToDefaults = function () {
294 this.uriProcessor
.updateModelBasedOnQuery( this._getDefaultParams() );
295 this.updateChangesList();
299 * Empty all selected filters
301 mw
.rcfilters
.Controller
.prototype.emptyFilters = function () {
302 var highlightedFilterNames
= this.filtersModel
303 .getHighlightedItems()
304 .map( function ( filterItem
) { return { name
: filterItem
.getName() }; } );
306 this.filtersModel
.emptyAllFilters();
307 this.filtersModel
.clearAllHighlightColors();
308 // Check all filter interactions
309 this.filtersModel
.reassessFilterInteractions();
311 this.updateChangesList();
313 if ( highlightedFilterNames
) {
314 this._trackHighlight( 'clearAll', highlightedFilterNames
);
319 * Update the selected state of a filter
321 * @param {string} filterName Filter name
322 * @param {boolean} [isSelected] Filter selected state
324 mw
.rcfilters
.Controller
.prototype.toggleFilterSelect = function ( filterName
, isSelected
) {
325 var filterItem
= this.filtersModel
.getItemByName( filterName
);
328 // If no filter was found, break
332 isSelected
= isSelected
=== undefined ? !filterItem
.isSelected() : isSelected
;
334 if ( filterItem
.isSelected() !== isSelected
) {
335 this.filtersModel
.toggleFilterSelected( filterName
, isSelected
);
337 this.updateChangesList();
339 // Check filter interactions
340 this.filtersModel
.reassessFilterInteractions( filterItem
);
345 * Clear both highlight and selection of a filter
347 * @param {string} filterName Name of the filter item
349 mw
.rcfilters
.Controller
.prototype.clearFilter = function ( filterName
) {
350 var filterItem
= this.filtersModel
.getItemByName( filterName
),
351 isHighlighted
= filterItem
.isHighlighted();
353 if ( filterItem
.isSelected() || isHighlighted
) {
354 this.filtersModel
.clearHighlightColor( filterName
);
355 this.filtersModel
.toggleFilterSelected( filterName
, false );
356 this.updateChangesList();
357 this.filtersModel
.reassessFilterInteractions( filterItem
);
359 // Log filter grouping
360 this.trackFilterGroupings( 'removefilter' );
363 if ( isHighlighted
) {
364 this._trackHighlight( 'clear', filterName
);
369 * Toggle the highlight feature on and off
371 mw
.rcfilters
.Controller
.prototype.toggleHighlight = function () {
372 this.filtersModel
.toggleHighlight();
375 if ( this.filtersModel
.isHighlightEnabled() ) {
376 mw
.hook( 'RcFilters.highlight.enable' ).fire();
381 * Toggle the namespaces inverted feature on and off
383 mw
.rcfilters
.Controller
.prototype.toggleInvertedNamespaces = function () {
384 this.filtersModel
.toggleInvertedNamespaces();
385 this.updateChangesList();
389 * Set the highlight color for a filter item
391 * @param {string} filterName Name of the filter item
392 * @param {string} color Selected color
394 mw
.rcfilters
.Controller
.prototype.setHighlightColor = function ( filterName
, color
) {
395 this.filtersModel
.setHighlightColor( filterName
, color
);
397 this._trackHighlight( 'set', { name
: filterName
, color
: color
} );
401 * Clear highlight for a filter item
403 * @param {string} filterName Name of the filter item
405 mw
.rcfilters
.Controller
.prototype.clearHighlightColor = function ( filterName
) {
406 this.filtersModel
.clearHighlightColor( filterName
);
408 this._trackHighlight( 'clear', filterName
);
412 * Enable or disable live updates.
413 * @param {boolean} enable True to enable, false to disable
415 mw
.rcfilters
.Controller
.prototype.toggleLiveUpdate = function ( enable
) {
416 if ( enable
&& !this.liveUpdateTimeout
) {
417 this._scheduleLiveUpdate();
418 } else if ( !enable
&& this.liveUpdateTimeout
) {
419 clearTimeout( this.liveUpdateTimeout
);
420 this.liveUpdateTimeout
= null;
425 * Set a timeout for the next live update.
428 mw
.rcfilters
.Controller
.prototype._scheduleLiveUpdate = function () {
429 this.liveUpdateTimeout
= setTimeout( this._doLiveUpdate
.bind( this ), 3000 );
433 * Perform a live update.
436 mw
.rcfilters
.Controller
.prototype._doLiveUpdate = function () {
437 var controller
= this;
438 this.updateChangesList( {}, true )
439 .always( function () {
440 if ( controller
.liveUpdateTimeout
) {
441 // Live update was not disabled in the meantime
442 controller
._scheduleLiveUpdate();
448 * Save the current model state as a saved query
450 * @param {string} [label] Label of the saved query
451 * @param {boolean} [setAsDefault=false] This query should be set as the default
453 mw
.rcfilters
.Controller
.prototype.saveCurrentQuery = function ( label
, setAsDefault
) {
455 highlightedItems
= {},
456 highlightEnabled
= this.filtersModel
.isHighlightEnabled();
458 // Prepare highlights
459 this.filtersModel
.getHighlightedItems().forEach( function ( item
) {
460 highlightedItems
[ item
.getName() ] = highlightEnabled
?
461 item
.getHighlightColor() : null;
463 // These are filter states; highlight is stored as boolean
464 highlightedItems
.highlight
= this.filtersModel
.isHighlightEnabled();
467 queryID
= this.savedQueriesModel
.addNewQuery(
468 label
|| mw
.msg( 'rcfilters-savedqueries-defaultlabel' ),
470 filters
: this.filtersModel
.getSelectedState(),
471 highlights
: highlightedItems
,
472 invert
: this.filtersModel
.areNamespacesInverted()
476 if ( setAsDefault
) {
477 this.savedQueriesModel
.setDefault( queryID
);
481 this._saveSavedQueries();
485 * Remove a saved query
487 * @param {string} queryID Query id
489 mw
.rcfilters
.Controller
.prototype.removeSavedQuery = function ( queryID
) {
490 this.savedQueriesModel
.removeQuery( queryID
);
492 this._saveSavedQueries();
496 * Rename a saved query
498 * @param {string} queryID Query id
499 * @param {string} newLabel New label for the query
501 mw
.rcfilters
.Controller
.prototype.renameSavedQuery = function ( queryID
, newLabel
) {
502 var queryItem
= this.savedQueriesModel
.getItemByID( queryID
);
505 queryItem
.updateLabel( newLabel
);
507 this._saveSavedQueries();
511 * Set a saved query as default
513 * @param {string} queryID Query Id. If null is given, default
516 mw
.rcfilters
.Controller
.prototype.setDefaultSavedQuery = function ( queryID
) {
517 this.savedQueriesModel
.setDefault( queryID
);
518 this._saveSavedQueries();
524 * @param {string} queryID Query id
526 mw
.rcfilters
.Controller
.prototype.applySavedQuery = function ( queryID
) {
527 var data
, highlights
,
528 queryItem
= this.savedQueriesModel
.getItemByID( queryID
);
531 data
= queryItem
.getData();
532 highlights
= data
.highlights
;
534 // Backwards compatibility; initial version mispelled 'highlight' with 'highlights'
535 highlights
.highlight
= highlights
.highlights
|| highlights
.highlight
;
537 // Update model state from filters
538 this.filtersModel
.toggleFiltersSelected( data
.filters
);
540 // Update namespace inverted property
541 this.filtersModel
.toggleInvertedNamespaces( !!Number( data
.invert
) );
543 // Update highlight state
544 this.filtersModel
.toggleHighlight( !!Number( highlights
.highlight
) );
545 this.filtersModel
.getItems().forEach( function ( filterItem
) {
546 var color
= highlights
[ filterItem
.getName() ];
548 filterItem
.setHighlightColor( color
);
550 filterItem
.clearHighlightColor();
554 // Check all filter interactions
555 this.filtersModel
.reassessFilterInteractions();
557 this.updateChangesList();
559 // Log filter grouping
560 this.trackFilterGroupings( 'savedfilters' );
565 * Check whether the current filter and highlight state exists
566 * in the saved queries model.
568 * @return {boolean} Query exists
570 mw
.rcfilters
.Controller
.prototype.findQueryMatchingCurrentState = function () {
571 var highlightedItems
= {};
573 // Prepare highlights of the current query
574 this.filtersModel
.getItemsSupportingHighlights().forEach( function ( item
) {
575 highlightedItems
[ item
.getName() ] = item
.getHighlightColor();
577 highlightedItems
.highlight
= this.filtersModel
.isHighlightEnabled();
579 return this.savedQueriesModel
.findMatchingQuery(
581 filters
: this.filtersModel
.getSelectedState(),
582 highlights
: highlightedItems
,
583 invert
: this.filtersModel
.areNamespacesInverted()
589 * Get an object representing the base state of parameters
592 * This is meant to make sure that the saved queries that are
593 * in memory are always the same structure as what we would get
594 * by calling the current model's "getSelectedState" and by checking
597 * In cases where a user saved a query when the system had a certain
598 * set of filters, and then a filter was added to the system, we want
599 * to make sure that the stored queries can still be comparable to
600 * the current state, which means that we need the base state for
603 * - Saved queries are stored in "minimal" view (only changed filters
604 * are stored); When we initialize the system, we merge each minimal
605 * query with the base state (using 'getNormalizedFilters') so all
606 * saved queries have the exact same structure as what we would get
607 * by checking the getSelectedState of the filter.
608 * - When we save the queries, we minimize the object to only represent
609 * whatever has actually changed, rather than store the entire
610 * object. To check what actually is different so we can store it,
611 * we need to obtain a base state to compare against, this is
612 * what #_getMinimalFilterList does
614 mw
.rcfilters
.Controller
.prototype._buildBaseFilterState = function () {
615 var defaultParams
= this.filtersModel
.getDefaultParams(),
616 highlightedItems
= {};
618 // Prepare highlights
619 this.filtersModel
.getItemsSupportingHighlights().forEach( function ( item
) {
620 highlightedItems
[ item
.getName() ] = null;
622 highlightedItems
.highlight
= false;
624 this.baseFilterState
= {
625 filters
: this.filtersModel
.getFiltersFromParameters( defaultParams
),
626 highlights
: highlightedItems
,
632 * Get an object representing the base filter state of both
633 * filters and highlights. The structure is similar to what we use
634 * to store each query in the saved queries object:
640 * filterName: (string|null)
644 * @return {Object} Object representing the base state of
645 * parameters and highlights
647 mw
.rcfilters
.Controller
.prototype._getBaseFilterState = function () {
648 return this.baseFilterState
;
652 * Get an object that holds only the parameters and highlights that have
653 * values different than the base default value.
655 * This is the reverse of the normalization we do initially on loading and
656 * initializing the saved queries model.
658 * @param {Object} valuesObject Object representing the state of both
659 * filters and highlights in its normalized version, to be minimized.
660 * @return {Object} Minimal filters and highlights list
662 mw
.rcfilters
.Controller
.prototype._getMinimalFilterList = function ( valuesObject
) {
663 var result
= { filters
: {}, highlights
: {} },
664 baseState
= this._getBaseFilterState();
667 $.each( valuesObject
.filters
, function ( name
, value
) {
668 if ( baseState
.filters
!== undefined && baseState
.filters
[ name
] !== value
) {
669 result
.filters
[ name
] = value
;
673 $.each( valuesObject
.highlights
, function ( name
, value
) {
674 if ( baseState
.highlights
!== undefined && baseState
.highlights
[ name
] !== value
) {
675 result
.highlights
[ name
] = value
;
683 * Save the current state of the saved queries model with all
684 * query item representation in the user settings.
686 mw
.rcfilters
.Controller
.prototype._saveSavedQueries = function () {
688 state
= this.savedQueriesModel
.getState(),
691 // Minimize before save
692 $.each( state
.queries
, function ( queryID
, info
) {
693 state
.queries
[ queryID
].data
= controller
._getMinimalFilterList( info
.data
);
697 stringified
= JSON
.stringify( state
);
699 if ( stringified
.length
> 65535 ) {
700 // Sanity check, since the preference can only hold that.
704 // Save the preference
705 new mw
.Api().saveOption( 'rcfilters-saved-queries', stringified
);
706 // Update the preference for this session
707 mw
.user
.options
.set( 'rcfilters-saved-queries', stringified
);
711 * Synchronize the URL with the current state of the filters
712 * without adding an history entry.
714 mw
.rcfilters
.Controller
.prototype.replaceUrl = function () {
715 mw
.rcfilters
.UriProcessor
.static.replaceState( this._getUpdatedUri() );
719 * Update filter state (selection and highlighting) based
720 * on current URL values.
722 * @param {boolean} [fetchChangesList=true] Fetch new results into the changes
723 * list based on the updated model.
725 mw
.rcfilters
.Controller
.prototype.updateStateFromUrl = function ( fetchChangesList
) {
726 fetchChangesList
= fetchChangesList
=== undefined ? true : !!fetchChangesList
;
728 this.uriProcessor
.updateModelBasedOnQuery( new mw
.Uri().query
);
730 // Only update and fetch new results if it is requested
731 if ( fetchChangesList
) {
732 this.updateChangesList();
737 * Update the list of changes and notify the model
739 * @param {Object} [params] Extra parameters to add to the API call
740 * @param {boolean} [isLiveUpdate] Don't update the URL or invalidate the changes list
741 * @return {jQuery.Promise} Promise that is resolved when the update is complete
743 mw
.rcfilters
.Controller
.prototype.updateChangesList = function ( params
, isLiveUpdate
) {
744 if ( !isLiveUpdate
) {
745 this._updateURL( params
);
746 this.changesListModel
.invalidate();
748 return this._fetchChangesList()
751 function ( pieces
) {
752 var $changesListContent
= pieces
.changes
,
753 $fieldset
= pieces
.fieldset
;
754 this.changesListModel
.update( $changesListContent
, $fieldset
);
756 // Do nothing for failure
761 * Get an object representing the default parameter state, whether
762 * it is from the model defaults or from the saved queries.
764 * @return {Object} Default parameters
766 mw
.rcfilters
.Controller
.prototype._getDefaultParams = function () {
767 var data
, queryHighlights
,
769 savedHighlights
= {},
770 defaultSavedQueryItem
= this.savedQueriesModel
.getItemByID( this.savedQueriesModel
.getDefault() );
772 if ( mw
.config
.get( 'wgStructuredChangeFiltersEnableSaving' ) &&
773 defaultSavedQueryItem
) {
775 data
= defaultSavedQueryItem
.getData();
777 queryHighlights
= data
.highlights
|| {};
778 savedParams
= this.filtersModel
.getParametersFromFilters( data
.filters
|| {} );
780 // Translate highlights to parameters
781 savedHighlights
.highlight
= String( Number( queryHighlights
.highlight
) );
782 $.each( queryHighlights
, function ( filterName
, color
) {
783 if ( filterName
!== 'highlights' ) {
784 savedHighlights
[ filterName
+ '_color' ] = color
;
788 return $.extend( true, {}, savedParams
, savedHighlights
);
791 return this.filtersModel
.getDefaultParams();
795 * Update the URL of the page to reflect current filters
797 * This should not be called directly from outside the controller.
798 * If an action requires changing the URL, it should either use the
799 * highlighting actions below, or call #updateChangesList which does
800 * the uri corrections already.
802 * @param {Object} [params] Extra parameters to add to the API call
804 mw
.rcfilters
.Controller
.prototype._updateURL = function ( params
) {
805 var currentUri
= new mw
.Uri(),
806 updatedUri
= this._getUpdatedUri();
808 updatedUri
.extend( params
|| {} );
811 this.uriProcessor
.getVersion( currentUri
.query
) !== 2 ||
812 this.uriProcessor
.isNewState( currentUri
.query
, updatedUri
.query
)
814 mw
.rcfilters
.UriProcessor
.static.replaceState( updatedUri
);
819 * Get an updated mw.Uri object based on the model state
821 * @return {mw.Uri} Updated Uri
823 mw
.rcfilters
.Controller
.prototype._getUpdatedUri = function () {
824 var uri
= new mw
.Uri();
827 uri
.query
= this.uriProcessor
.minimizeQuery(
831 // We want to retain unrecognized params
832 // The uri params from model will override
833 // any recognized value in the current uri
834 // query, retain unrecognized params, and
835 // the result will then be minimized
837 this.uriProcessor
.getUriParametersFromModel(),
846 * Fetch the list of changes from the server for the current filters
848 * @return {jQuery.Promise} Promise object that will resolve with the changes list
849 * or with a string denoting no results.
851 mw
.rcfilters
.Controller
.prototype._fetchChangesList = function () {
852 var uri
= this._getUpdatedUri(),
853 requestId
= ++this.requestCounter
,
854 latestRequest = function () {
855 return requestId
=== this.requestCounter
;
858 return $.ajax( uri
.toString(), { contentType
: 'html' } )
863 if ( !latestRequest() ) {
864 return $.Deferred().reject();
867 $parsed
= $( $.parseHTML( html
) );
871 changes
: $parsed
.find( '.mw-changeslist' ).first().contents(),
873 fieldset
: $parsed
.find( 'fieldset.rcoptions' ).first()
877 function ( responseObj
) {
880 if ( !latestRequest() ) {
881 return $.Deferred().reject();
884 $parsed
= $( $.parseHTML( responseObj
.responseText
) );
886 // Force a resolve state to this promise
887 return $.Deferred().resolve( {
888 changes
: 'NO_RESULTS',
889 fieldset
: $parsed
.find( 'fieldset.rcoptions' ).first()
896 * Track usage of highlight feature
898 * @param {string} action
899 * @param {Array|Object|string} filters
901 mw
.rcfilters
.Controller
.prototype._trackHighlight = function ( action
, filters
) {
902 filters
= typeof filters
=== 'string' ? { name
: filters
} : filters
;
903 filters
= !Array
.isArray( filters
) ? [ filters
] : filters
;
905 'event.ChangesListHighlights',
909 userId
: mw
.user
.getId()
915 * Track filter grouping usage
917 * @param {string} action Action taken
919 mw
.rcfilters
.Controller
.prototype.trackFilterGroupings = function ( action
) {
920 var controller
= this,
921 rightNow
= new Date().getTime(),
922 randomIdentifier
= String( mw
.user
.sessionId() ) + String( rightNow
) + String( Math
.random() ),
923 // Get all current filters
924 filters
= this.filtersModel
.getSelectedItems().map( function ( item
) {
925 return item
.getName();
928 action
= action
|| 'filtermenu';
930 // Check if these filters were the ones we just logged previously
931 // (Don't log the same grouping twice, in case the user opens/closes)
932 // the menu without action, or with the same result
934 // Only log if the two arrays are different in size
935 filters
.length
!== this.prevLoggedItems
.length
||
936 // Or if any filters are not the same as the cached filters
937 filters
.some( function ( filterName
) {
938 return controller
.prevLoggedItems
.indexOf( filterName
) === -1;
940 // Or if any cached filters are not the same as given filters
941 this.prevLoggedItems
.some( function ( filterName
) {
942 return filters
.indexOf( filterName
) === -1;
945 filters
.forEach( function ( filterName
) {
947 'event.ChangesListFilterGrouping',
950 groupIdentifier
: randomIdentifier
,
952 userId
: mw
.user
.getId()
957 // Cache the filter names
958 this.prevLoggedItems
= filters
;
961 }( mediaWiki
, jQuery
) );