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
,
37 $changesList
= $( '.mw-changeslist' ).first().contents(),
38 createFilterDataFromNumber = function ( num
, convertedNumForLabel
) {
41 label
: mw
.language
.convertNumber( convertedNumForLabel
)
46 if ( namespaceStructure
) {
48 $.each( namespaceStructure
, function ( namespaceID
, label
) {
49 // Build and clean up the individual namespace items definition
52 label
: label
|| mw
.msg( 'blanknamespace' ),
55 ( namespaceID
< 0 || namespaceID
% 2 === 0 ) ?
58 cssClass
: 'mw-changeslist-ns-' + namespaceID
63 title
: mw
.msg( 'namespaces' ),
66 // Group definition (single group)
67 name
: 'namespace', // parameter name is singular
68 type
: 'string_options',
69 title
: mw
.msg( 'namespaces' ),
70 labelPrefixKey
: { 'default': 'rcfilters-tag-prefix-namespace', inverted
: 'rcfilters-tag-prefix-namespace-inverted' },
79 title
: mw
.msg( 'rcfilters-view-tags' ),
82 // Group definition (single group)
83 name
: 'tagfilter', // Parameter name
84 type
: 'string_options',
85 title
: 'rcfilters-view-tags', // Message key
86 labelPrefixKey
: 'rcfilters-tag-prefix-tags',
94 // Add parameter range operations
99 type
: 'single_option',
100 title
: '', // Because it's a hidden group, this title actually appears nowhere
102 allowArbitrary
: true,
103 validate
: $.isNumeric
,
104 sortFunc: function ( a
, b
) { return Number( a
.name
) - Number( b
.name
); },
106 filters
: [ 50, 100, 250, 500 ].map( function ( num
) {
107 return createFilterDataFromNumber( num
, num
);
112 type
: 'single_option',
113 title
: '', // Because it's a hidden group, this title actually appears nowhere
115 allowArbitrary
: true,
116 validate
: $.isNumeric
,
117 sortFunc: function ( a
, b
) { return Number( a
.name
) - Number( b
.name
); },
120 // Hours (1, 2, 6, 12)
121 // TEMPORARY: Hide hours temporarily
122 // 0.04166, 0.0833, 0.25, 0.5,
125 ].map( function ( num
) {
126 return 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 another item 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 // This is only true for single_option and string_options
142 // We assume these are the only groups that will allow for
143 // arbitrary, since it doesn't make any sense for the other
145 var uriValue
= uri
.query
[ groupData
.name
];
148 // If the group allows for arbitrary data
149 groupData
.allowArbitrary
&&
150 // and it is single_option (or string_options, but we
151 // don't have cases of those yet, nor do we plan to)
152 groupData
.type
=== 'single_option' &&
153 // and if there is a valid value in the URI already
154 uri
.query
[ groupData
.name
] !== undefined &&
155 // and, if there is a validate method and it passes on
157 ( !groupData
.validate
|| groupData
.validate( uri
.query
[ groupData
.name
] ) ) &&
158 // but if that value isn't already in the definition
160 .map( function ( filterData
) {
161 return filterData
.name
;
163 .indexOf( uri
.query
[ groupData
.name
] ) === -1
165 // Add the filter information
166 if ( groupData
.name
=== 'days' ) {
167 // Specific fix for hours/days which go by the same param
168 groupData
.filters
.push( createFilterDataFromNumber(
170 // In this case we don't want to round because it can be arbitrary
171 // weird numbers but we want to round to 2 decimal digits
173 // HACK: Temporarily remove hours from UI
174 // Number( uriValue ) < 1 ?
175 // ( Number( uriValue ) * 24 ).toFixed( 2 ) :
176 // Number( uriValue )
180 groupData
.filters
.push( createFilterDataFromNumber( uriValue
, uriValue
) );
183 // If there's a sort function set up, re-sort the values
184 if ( groupData
.sortFunc
) {
185 groupData
.filters
.sort( groupData
.sortFunc
);
191 // Initialize the model
192 this.filtersModel
.initializeFilters( filterStructure
, views
);
194 this._buildBaseFilterState();
196 this.uriProcessor
= new mw
.rcfilters
.UriProcessor(
201 parsedSavedQueries
= JSON
.parse( mw
.user
.options
.get( 'rcfilters-saved-queries' ) || '{}' );
203 parsedSavedQueries
= {};
206 // The queries are saved in a minimized state, so we need
207 // to send over the base state so the saved queries model
208 // can normalize them per each query item
209 this.savedQueriesModel
.initialize(
211 this._getBaseFilterState()
214 // Check whether we need to load defaults.
215 // We do this by checking whether the current URI query
216 // contains any parameters recognized by the system.
217 // If it does, we load the given state.
218 // If it doesn't, we have no values at all, and we assume
219 // the user loads the base-page and we load defaults.
220 // Defaults should only be applied on load (if necessary)
222 this.initializing
= true;
224 this.savedQueriesModel
.getDefault() &&
225 !this.uriProcessor
.doesQueryContainRecognizedParams( uri
.query
)
227 // We have defaults from a saved query.
228 // We will load them straight-forward (as if
229 // they were clicked in the menu) so we trigger
230 // a full ajax request and change of URL
231 this.applySavedQuery( this.savedQueriesModel
.getDefault() );
233 // There are either recognized parameters in the URL
234 // or there are none, but there is also no default
235 // saved query (so defaults are from the backend)
236 // We want to update the state but not fetch results
238 this.updateStateFromUrl( false );
240 // Update the changes list with the existing data
241 // so it gets processed
242 this.changesListModel
.update(
243 $changesList
.length
? $changesList
: 'NO_RESULTS',
244 $( 'fieldset.rcoptions' ).first(),
245 true // We're using existing DOM elements
249 this.initializing
= false;
250 this.switchView( 'default' );
254 * Switch the view of the filters model
256 * @param {string} view Requested view
258 mw
.rcfilters
.Controller
.prototype.switchView = function ( view
) {
259 this.filtersModel
.switchView( view
);
263 * Reset to default filters
265 mw
.rcfilters
.Controller
.prototype.resetToDefaults = function () {
266 this.uriProcessor
.updateModelBasedOnQuery( this._getDefaultParams() );
267 this.updateChangesList();
271 * Empty all selected filters
273 mw
.rcfilters
.Controller
.prototype.emptyFilters = function () {
274 var highlightedFilterNames
= this.filtersModel
275 .getHighlightedItems()
276 .map( function ( filterItem
) { return { name
: filterItem
.getName() }; } );
278 this.filtersModel
.emptyAllFilters();
279 this.filtersModel
.clearAllHighlightColors();
280 // Check all filter interactions
281 this.filtersModel
.reassessFilterInteractions();
283 this.updateChangesList();
285 if ( highlightedFilterNames
) {
286 this._trackHighlight( 'clearAll', highlightedFilterNames
);
291 * Update the selected state of a filter
293 * @param {string} filterName Filter name
294 * @param {boolean} [isSelected] Filter selected state
296 mw
.rcfilters
.Controller
.prototype.toggleFilterSelect = function ( filterName
, isSelected
) {
297 var filterItem
= this.filtersModel
.getItemByName( filterName
);
300 // If no filter was found, break
304 isSelected
= isSelected
=== undefined ? !filterItem
.isSelected() : isSelected
;
306 if ( filterItem
.isSelected() !== isSelected
) {
307 this.filtersModel
.toggleFilterSelected( filterName
, isSelected
);
309 this.updateChangesList();
311 // Check filter interactions
312 this.filtersModel
.reassessFilterInteractions( filterItem
);
317 * Clear both highlight and selection of a filter
319 * @param {string} filterName Name of the filter item
321 mw
.rcfilters
.Controller
.prototype.clearFilter = function ( filterName
) {
322 var filterItem
= this.filtersModel
.getItemByName( filterName
),
323 isHighlighted
= filterItem
.isHighlighted();
325 if ( filterItem
.isSelected() || isHighlighted
) {
326 this.filtersModel
.clearHighlightColor( filterName
);
327 this.filtersModel
.toggleFilterSelected( filterName
, false );
328 this.updateChangesList();
329 this.filtersModel
.reassessFilterInteractions( filterItem
);
331 // Log filter grouping
332 this.trackFilterGroupings( 'removefilter' );
335 if ( isHighlighted
) {
336 this._trackHighlight( 'clear', filterName
);
341 * Toggle the highlight feature on and off
343 mw
.rcfilters
.Controller
.prototype.toggleHighlight = function () {
344 this.filtersModel
.toggleHighlight();
347 if ( this.filtersModel
.isHighlightEnabled() ) {
348 mw
.hook( 'RcFilters.highlight.enable' ).fire();
353 * Toggle the namespaces inverted feature on and off
355 mw
.rcfilters
.Controller
.prototype.toggleInvertedNamespaces = function () {
356 this.filtersModel
.toggleInvertedNamespaces();
357 this.updateChangesList();
361 * Set the highlight color for a filter item
363 * @param {string} filterName Name of the filter item
364 * @param {string} color Selected color
366 mw
.rcfilters
.Controller
.prototype.setHighlightColor = function ( filterName
, color
) {
367 this.filtersModel
.setHighlightColor( filterName
, color
);
369 this._trackHighlight( 'set', { name
: filterName
, color
: color
} );
373 * Clear highlight for a filter item
375 * @param {string} filterName Name of the filter item
377 mw
.rcfilters
.Controller
.prototype.clearHighlightColor = function ( filterName
) {
378 this.filtersModel
.clearHighlightColor( filterName
);
380 this._trackHighlight( 'clear', filterName
);
384 * Enable or disable live updates.
385 * @param {boolean} enable True to enable, false to disable
387 mw
.rcfilters
.Controller
.prototype.toggleLiveUpdate = function ( enable
) {
388 if ( enable
&& !this.liveUpdateTimeout
) {
389 this._scheduleLiveUpdate();
390 } else if ( !enable
&& this.liveUpdateTimeout
) {
391 clearTimeout( this.liveUpdateTimeout
);
392 this.liveUpdateTimeout
= null;
397 * Set a timeout for the next live update.
400 mw
.rcfilters
.Controller
.prototype._scheduleLiveUpdate = function () {
401 this.liveUpdateTimeout
= setTimeout( this._doLiveUpdate
.bind( this ), 3000 );
405 * Perform a live update.
408 mw
.rcfilters
.Controller
.prototype._doLiveUpdate = function () {
409 var controller
= this;
410 this.updateChangesList( {}, true )
411 .always( function () {
412 if ( controller
.liveUpdateTimeout
) {
413 // Live update was not disabled in the meantime
414 controller
._scheduleLiveUpdate();
420 * Save the current model state as a saved query
422 * @param {string} [label] Label of the saved query
424 mw
.rcfilters
.Controller
.prototype.saveCurrentQuery = function ( label
) {
425 var highlightedItems
= {},
426 highlightEnabled
= this.filtersModel
.isHighlightEnabled();
428 // Prepare highlights
429 this.filtersModel
.getHighlightedItems().forEach( function ( item
) {
430 highlightedItems
[ item
.getName() ] = highlightEnabled
?
431 item
.getHighlightColor() : null;
433 // These are filter states; highlight is stored as boolean
434 highlightedItems
.highlight
= this.filtersModel
.isHighlightEnabled();
437 this.savedQueriesModel
.addNewQuery(
438 label
|| mw
.msg( 'rcfilters-savedqueries-defaultlabel' ),
440 filters
: this.filtersModel
.getSelectedState(),
441 highlights
: highlightedItems
,
442 invert
: this.filtersModel
.areNamespacesInverted()
447 this._saveSavedQueries();
451 * Remove a saved query
453 * @param {string} queryID Query id
455 mw
.rcfilters
.Controller
.prototype.removeSavedQuery = function ( queryID
) {
456 this.savedQueriesModel
.removeQuery( queryID
);
458 this._saveSavedQueries();
462 * Rename a saved query
464 * @param {string} queryID Query id
465 * @param {string} newLabel New label for the query
467 mw
.rcfilters
.Controller
.prototype.renameSavedQuery = function ( queryID
, newLabel
) {
468 var queryItem
= this.savedQueriesModel
.getItemByID( queryID
);
471 queryItem
.updateLabel( newLabel
);
473 this._saveSavedQueries();
477 * Set a saved query as default
479 * @param {string} queryID Query Id. If null is given, default
482 mw
.rcfilters
.Controller
.prototype.setDefaultSavedQuery = function ( queryID
) {
483 this.savedQueriesModel
.setDefault( queryID
);
484 this._saveSavedQueries();
490 * @param {string} queryID Query id
492 mw
.rcfilters
.Controller
.prototype.applySavedQuery = function ( queryID
) {
493 var data
, highlights
,
494 queryItem
= this.savedQueriesModel
.getItemByID( queryID
);
497 data
= queryItem
.getData();
498 highlights
= data
.highlights
;
500 // Backwards compatibility; initial version mispelled 'highlight' with 'highlights'
501 highlights
.highlight
= highlights
.highlights
|| highlights
.highlight
;
503 // Update model state from filters
504 this.filtersModel
.toggleFiltersSelected( data
.filters
);
506 // Update namespace inverted property
507 this.filtersModel
.toggleInvertedNamespaces( !!Number( data
.invert
) );
509 // Update highlight state
510 this.filtersModel
.toggleHighlight( !!Number( highlights
.highlight
) );
511 this.filtersModel
.getItems().forEach( function ( filterItem
) {
512 var color
= highlights
[ filterItem
.getName() ];
514 filterItem
.setHighlightColor( color
);
516 filterItem
.clearHighlightColor();
520 // Check all filter interactions
521 this.filtersModel
.reassessFilterInteractions();
523 this.updateChangesList();
525 // Log filter grouping
526 this.trackFilterGroupings( 'savedfilters' );
531 * Check whether the current filter and highlight state exists
532 * in the saved queries model.
534 * @return {boolean} Query exists
536 mw
.rcfilters
.Controller
.prototype.findQueryMatchingCurrentState = function () {
537 var highlightedItems
= {};
539 // Prepare highlights of the current query
540 this.filtersModel
.getItemsSupportingHighlights().forEach( function ( item
) {
541 highlightedItems
[ item
.getName() ] = item
.getHighlightColor();
543 highlightedItems
.highlight
= this.filtersModel
.isHighlightEnabled();
545 return this.savedQueriesModel
.findMatchingQuery(
547 filters
: this.filtersModel
.getSelectedState(),
548 highlights
: highlightedItems
,
549 invert
: this.filtersModel
.areNamespacesInverted()
555 * Get an object representing the base state of parameters
558 * This is meant to make sure that the saved queries that are
559 * in memory are always the same structure as what we would get
560 * by calling the current model's "getSelectedState" and by checking
563 * In cases where a user saved a query when the system had a certain
564 * set of filters, and then a filter was added to the system, we want
565 * to make sure that the stored queries can still be comparable to
566 * the current state, which means that we need the base state for
569 * - Saved queries are stored in "minimal" view (only changed filters
570 * are stored); When we initialize the system, we merge each minimal
571 * query with the base state (using 'getNormalizedFilters') so all
572 * saved queries have the exact same structure as what we would get
573 * by checking the getSelectedState of the filter.
574 * - When we save the queries, we minimize the object to only represent
575 * whatever has actually changed, rather than store the entire
576 * object. To check what actually is different so we can store it,
577 * we need to obtain a base state to compare against, this is
578 * what #_getMinimalFilterList does
580 mw
.rcfilters
.Controller
.prototype._buildBaseFilterState = function () {
581 var defaultParams
= this.filtersModel
.getDefaultParams(),
582 highlightedItems
= {};
584 // Prepare highlights
585 this.filtersModel
.getItemsSupportingHighlights().forEach( function ( item
) {
586 highlightedItems
[ item
.getName() ] = null;
588 highlightedItems
.highlight
= false;
590 this.baseFilterState
= {
591 filters
: this.filtersModel
.getFiltersFromParameters( defaultParams
),
592 highlights
: highlightedItems
,
598 * Get an object representing the base filter state of both
599 * filters and highlights. The structure is similar to what we use
600 * to store each query in the saved queries object:
606 * filterName: (string|null)
610 * @return {Object} Object representing the base state of
611 * parameters and highlights
613 mw
.rcfilters
.Controller
.prototype._getBaseFilterState = function () {
614 return this.baseFilterState
;
618 * Get an object that holds only the parameters and highlights that have
619 * values different than the base default value.
621 * This is the reverse of the normalization we do initially on loading and
622 * initializing the saved queries model.
624 * @param {Object} valuesObject Object representing the state of both
625 * filters and highlights in its normalized version, to be minimized.
626 * @return {Object} Minimal filters and highlights list
628 mw
.rcfilters
.Controller
.prototype._getMinimalFilterList = function ( valuesObject
) {
629 var result
= { filters
: {}, highlights
: {} },
630 baseState
= this._getBaseFilterState();
633 $.each( valuesObject
.filters
, function ( name
, value
) {
634 if ( baseState
.filters
!== undefined && baseState
.filters
[ name
] !== value
) {
635 result
.filters
[ name
] = value
;
639 $.each( valuesObject
.highlights
, function ( name
, value
) {
640 if ( baseState
.highlights
!== undefined && baseState
.highlights
[ name
] !== value
) {
641 result
.highlights
[ name
] = value
;
649 * Save the current state of the saved queries model with all
650 * query item representation in the user settings.
652 mw
.rcfilters
.Controller
.prototype._saveSavedQueries = function () {
654 state
= this.savedQueriesModel
.getState(),
657 // Minimize before save
658 $.each( state
.queries
, function ( queryID
, info
) {
659 state
.queries
[ queryID
].data
= controller
._getMinimalFilterList( info
.data
);
663 stringified
= JSON
.stringify( state
);
665 if ( stringified
.length
> 65535 ) {
666 // Sanity check, since the preference can only hold that.
670 // Save the preference
671 new mw
.Api().saveOption( 'rcfilters-saved-queries', stringified
);
672 // Update the preference for this session
673 mw
.user
.options
.set( 'rcfilters-saved-queries', stringified
);
677 * Synchronize the URL with the current state of the filters
678 * without adding an history entry.
680 mw
.rcfilters
.Controller
.prototype.replaceUrl = function () {
681 mw
.rcfilters
.UriProcessor
.static.replaceState( this._getUpdatedUri() );
685 * Update filter state (selection and highlighting) based
686 * on current URL values.
688 * @param {boolean} [fetchChangesList=true] Fetch new results into the changes
689 * list based on the updated model.
691 mw
.rcfilters
.Controller
.prototype.updateStateFromUrl = function ( fetchChangesList
) {
692 fetchChangesList
= fetchChangesList
=== undefined ? true : !!fetchChangesList
;
694 this.uriProcessor
.updateModelBasedOnQuery( new mw
.Uri().query
);
696 // Only update and fetch new results if it is requested
697 if ( fetchChangesList
) {
698 this.updateChangesList();
703 * Update the list of changes and notify the model
705 * @param {Object} [params] Extra parameters to add to the API call
706 * @param {boolean} [isLiveUpdate] Don't update the URL or invalidate the changes list
707 * @return {jQuery.Promise} Promise that is resolved when the update is complete
709 mw
.rcfilters
.Controller
.prototype.updateChangesList = function ( params
, isLiveUpdate
) {
710 if ( !isLiveUpdate
) {
711 this._updateURL( params
);
712 this.changesListModel
.invalidate();
714 return this._fetchChangesList()
717 function ( pieces
) {
718 var $changesListContent
= pieces
.changes
,
719 $fieldset
= pieces
.fieldset
;
720 this.changesListModel
.update( $changesListContent
, $fieldset
);
722 // Do nothing for failure
727 * Get an object representing the default parameter state, whether
728 * it is from the model defaults or from the saved queries.
730 * @return {Object} Default parameters
732 mw
.rcfilters
.Controller
.prototype._getDefaultParams = function () {
733 var data
, queryHighlights
,
735 savedHighlights
= {},
736 defaultSavedQueryItem
= this.savedQueriesModel
.getItemByID( this.savedQueriesModel
.getDefault() );
738 if ( mw
.config
.get( 'wgStructuredChangeFiltersEnableSaving' ) &&
739 defaultSavedQueryItem
) {
741 data
= defaultSavedQueryItem
.getData();
743 queryHighlights
= data
.highlights
|| {};
744 savedParams
= this.filtersModel
.getParametersFromFilters( data
.filters
|| {} );
746 // Translate highlights to parameters
747 savedHighlights
.highlight
= String( Number( queryHighlights
.highlight
) );
748 $.each( queryHighlights
, function ( filterName
, color
) {
749 if ( filterName
!== 'highlights' ) {
750 savedHighlights
[ filterName
+ '_color' ] = color
;
754 return $.extend( true, {}, savedParams
, savedHighlights
, { invert
: data
.invert
} );
759 this.filtersModel
.getDefaultParams()
764 * Get an object representing the default parameter state, whether
765 * it is from the model defaults or from the saved queries.
767 * @return {Object} Default parameters
769 mw
.rcfilters
.Controller
.prototype._getDefaultParams = function () {
770 var data
, queryHighlights
,
772 savedHighlights
= {},
773 defaultSavedQueryItem
= this.savedQueriesModel
.getItemByID( this.savedQueriesModel
.getDefault() );
775 if ( mw
.config
.get( 'wgStructuredChangeFiltersEnableSaving' ) &&
776 defaultSavedQueryItem
) {
778 data
= defaultSavedQueryItem
.getData();
780 queryHighlights
= data
.highlights
|| {};
781 savedParams
= this.filtersModel
.getParametersFromFilters( data
.filters
|| {} );
783 // Translate highlights to parameters
784 savedHighlights
.highlight
= String( Number( queryHighlights
.highlight
) );
785 $.each( queryHighlights
, function ( filterName
, color
) {
786 if ( filterName
!== 'highlights' ) {
787 savedHighlights
[ filterName
+ '_color' ] = color
;
791 return $.extend( true, {}, savedParams
, savedHighlights
);
794 return this.filtersModel
.getDefaultParams();
798 * Update the URL of the page to reflect current filters
800 * This should not be called directly from outside the controller.
801 * If an action requires changing the URL, it should either use the
802 * highlighting actions below, or call #updateChangesList which does
803 * the uri corrections already.
805 * @param {Object} [params] Extra parameters to add to the API call
807 mw
.rcfilters
.Controller
.prototype._updateURL = function ( params
) {
808 var currentUri
= new mw
.Uri(),
809 updatedUri
= this._getUpdatedUri();
811 updatedUri
.extend( params
|| {} );
814 this.uriProcessor
.getVersion( currentUri
.query
) !== 2 ||
815 this.uriProcessor
.isNewState( currentUri
.query
, updatedUri
.query
)
817 mw
.rcfilters
.UriProcessor
.static.replaceState( updatedUri
);
822 * Get an updated mw.Uri object based on the model state
824 * @return {mw.Uri} Updated Uri
826 mw
.rcfilters
.Controller
.prototype._getUpdatedUri = function () {
827 var uri
= new mw
.Uri();
830 uri
.query
= this.uriProcessor
.minimizeQuery(
834 // We want to retain unrecognized params
835 // The uri params from model will override
836 // any recognized value in the current uri
837 // query, retain unrecognized params, and
838 // the result will then be minimized
840 this.uriProcessor
.getUriParametersFromModel(),
849 * Fetch the list of changes from the server for the current filters
851 * @return {jQuery.Promise} Promise object that will resolve with the changes list
852 * or with a string denoting no results.
854 mw
.rcfilters
.Controller
.prototype._fetchChangesList = function () {
855 var uri
= this._getUpdatedUri(),
856 requestId
= ++this.requestCounter
,
857 latestRequest = function () {
858 return requestId
=== this.requestCounter
;
861 return $.ajax( uri
.toString(), { contentType
: 'html' } )
866 if ( !latestRequest() ) {
867 return $.Deferred().reject();
870 $parsed
= $( $.parseHTML( html
) );
874 changes
: $parsed
.find( '.mw-changeslist' ).first().contents(),
876 fieldset
: $parsed
.find( 'fieldset.rcoptions' ).first()
880 function ( responseObj
) {
883 if ( !latestRequest() ) {
884 return $.Deferred().reject();
887 $parsed
= $( $.parseHTML( responseObj
.responseText
) );
889 // Force a resolve state to this promise
890 return $.Deferred().resolve( {
891 changes
: 'NO_RESULTS',
892 fieldset
: $parsed
.find( 'fieldset.rcoptions' ).first()
899 * Track usage of highlight feature
901 * @param {string} action
902 * @param {array|object|string} filters
904 mw
.rcfilters
.Controller
.prototype._trackHighlight = function ( action
, filters
) {
905 filters
= typeof filters
=== 'string' ? { name
: filters
} : filters
;
906 filters
= !Array
.isArray( filters
) ? [ filters
] : filters
;
908 'event.ChangesListHighlights',
912 userId
: mw
.user
.getId()
918 * Track filter grouping usage
920 * @param {string} action Action taken
922 mw
.rcfilters
.Controller
.prototype.trackFilterGroupings = function ( action
) {
923 var controller
= this,
924 rightNow
= new Date().getTime(),
925 randomIdentifier
= String( mw
.user
.sessionId() ) + String( rightNow
) + String( Math
.random() ),
926 // Get all current filters
927 filters
= this.filtersModel
.getSelectedItems().map( function ( item
) {
928 return item
.getName();
931 action
= action
|| 'filtermenu';
933 // Check if these filters were the ones we just logged previously
934 // (Don't log the same grouping twice, in case the user opens/closes)
935 // the menu without action, or with the same result
937 // Only log if the two arrays are different in size
938 filters
.length
!== this.prevLoggedItems
.length
||
939 // Or if any filters are not the same as the cached filters
940 filters
.some( function ( filterName
) {
941 return controller
.prevLoggedItems
.indexOf( filterName
) === -1;
943 // Or if any cached filters are not the same as given filters
944 this.prevLoggedItems
.some( function ( filterName
) {
945 return filters
.indexOf( filterName
) === -1;
948 filters
.forEach( function ( filterName
) {
950 'event.ChangesListFilterGrouping',
953 groupIdentifier
: randomIdentifier
,
955 userId
: mw
.user
.getId()
960 // Cache the filter names
961 this.prevLoggedItems
= filters
;
964 }( mediaWiki
, jQuery
) );