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;
21 OO
.initClass( mw
.rcfilters
.Controller
);
24 * Initialize the filter and parameter states
26 * @param {Array} filterStructure Filter definition and structure for the model
28 mw
.rcfilters
.Controller
.prototype.initialize = function ( filterStructure
) {
29 var parsedSavedQueries
,
31 $changesList
= $( '.mw-changeslist' ).first().contents();
33 // Initialize the model
34 this.filtersModel
.initializeFilters( filterStructure
);
36 this._buildBaseFilterState();
37 this.uriProcessor
= new mw
.rcfilters
.UriProcessor(
42 parsedSavedQueries
= JSON
.parse( mw
.user
.options
.get( 'rcfilters-saved-queries' ) || '{}' );
44 parsedSavedQueries
= {};
47 // The queries are saved in a minimized state, so we need
48 // to send over the base state so the saved queries model
49 // can normalize them per each query item
50 this.savedQueriesModel
.initialize(
52 this._getBaseFilterState()
55 // Check whether we need to load defaults.
56 // We do this by checking whether the current URI query
57 // contains any parameters recognized by the system.
58 // If it does, we load the given state.
59 // If it doesn't, we have no values at all, and we assume
60 // the user loads the base-page and we load defaults.
61 // Defaults should only be applied on load (if necessary)
63 this.initializing
= true;
65 this.savedQueriesModel
.getDefault() &&
66 !this.uriProcessor
.doesQueryContainRecognizedParams( uri
.query
)
68 // We have defaults from a saved query.
69 // We will load them straight-forward (as if
70 // they were clicked in the menu) so we trigger
71 // a full ajax request and change of URL
72 this.applySavedQuery( this.savedQueriesModel
.getDefault() );
74 // There are either recognized parameters in the URL
75 // or there are none, but there is also no default
76 // saved query (so defaults are from the backend)
77 // We want to update the state but not fetch results
79 this.updateStateFromUrl( false );
81 // Update the changes list with the existing data
82 // so it gets processed
83 this.changesListModel
.update(
84 $changesList
.length
? $changesList
: 'NO_RESULTS',
85 $( 'fieldset.rcoptions' ).first()
88 this.initializing
= false;
92 * Reset to default filters
94 mw
.rcfilters
.Controller
.prototype.resetToDefaults = function () {
95 this.uriProcessor
.updateModelBasedOnQuery( this._getDefaultParams() );
96 this.updateChangesList();
100 * Empty all selected filters
102 mw
.rcfilters
.Controller
.prototype.emptyFilters = function () {
103 var highlightedFilterNames
= this.filtersModel
104 .getHighlightedItems()
105 .map( function ( filterItem
) { return { name
: filterItem
.getName() }; } );
107 this.filtersModel
.emptyAllFilters();
108 this.filtersModel
.clearAllHighlightColors();
109 // Check all filter interactions
110 this.filtersModel
.reassessFilterInteractions();
112 this.updateChangesList();
114 if ( highlightedFilterNames
) {
115 this._trackHighlight( 'clearAll', highlightedFilterNames
);
120 * Update the selected state of a filter
122 * @param {string} filterName Filter name
123 * @param {boolean} [isSelected] Filter selected state
125 mw
.rcfilters
.Controller
.prototype.toggleFilterSelect = function ( filterName
, isSelected
) {
126 var filterItem
= this.filtersModel
.getItemByName( filterName
);
129 // If no filter was found, break
133 isSelected
= isSelected
=== undefined ? !filterItem
.isSelected() : isSelected
;
135 if ( filterItem
.isSelected() !== isSelected
) {
136 this.filtersModel
.toggleFilterSelected( filterName
, isSelected
);
138 this.updateChangesList();
140 // Check filter interactions
141 this.filtersModel
.reassessFilterInteractions( filterItem
);
146 * Clear both highlight and selection of a filter
148 * @param {string} filterName Name of the filter item
150 mw
.rcfilters
.Controller
.prototype.clearFilter = function ( filterName
) {
151 var filterItem
= this.filtersModel
.getItemByName( filterName
),
152 isHighlighted
= filterItem
.isHighlighted();
154 if ( filterItem
.isSelected() || isHighlighted
) {
155 this.filtersModel
.clearHighlightColor( filterName
);
156 this.filtersModel
.toggleFilterSelected( filterName
, false );
157 this.updateChangesList();
158 this.filtersModel
.reassessFilterInteractions( filterItem
);
161 if ( isHighlighted
) {
162 this._trackHighlight( 'clear', filterName
);
167 * Toggle the highlight feature on and off
169 mw
.rcfilters
.Controller
.prototype.toggleHighlight = function () {
170 this.filtersModel
.toggleHighlight();
173 if ( this.filtersModel
.isHighlightEnabled() ) {
174 mw
.hook( 'RcFilters.highlight.enable' ).fire();
179 * Set the highlight color for a filter item
181 * @param {string} filterName Name of the filter item
182 * @param {string} color Selected color
184 mw
.rcfilters
.Controller
.prototype.setHighlightColor = function ( filterName
, color
) {
185 this.filtersModel
.setHighlightColor( filterName
, color
);
187 this._trackHighlight( 'set', { name
: filterName
, color
: color
} );
191 * Clear highlight for a filter item
193 * @param {string} filterName Name of the filter item
195 mw
.rcfilters
.Controller
.prototype.clearHighlightColor = function ( filterName
) {
196 this.filtersModel
.clearHighlightColor( filterName
);
198 this._trackHighlight( 'clear', filterName
);
202 * Save the current model state as a saved query
204 * @param {string} [label] Label of the saved query
206 mw
.rcfilters
.Controller
.prototype.saveCurrentQuery = function ( label
) {
207 var highlightedItems
= {},
208 highlightEnabled
= this.filtersModel
.isHighlightEnabled();
210 // Prepare highlights
211 this.filtersModel
.getHighlightedItems().forEach( function ( item
) {
212 highlightedItems
[ item
.getName() ] = highlightEnabled
?
213 item
.getHighlightColor() : null;
215 // These are filter states; highlight is stored as boolean
216 highlightedItems
.highlight
= this.filtersModel
.isHighlightEnabled();
219 this.savedQueriesModel
.addNewQuery(
220 label
|| mw
.msg( 'rcfilters-savedqueries-defaultlabel' ),
222 filters
: this.filtersModel
.getSelectedState(),
223 highlights
: highlightedItems
228 this._saveSavedQueries();
232 * Remove a saved query
234 * @param {string} queryID Query id
236 mw
.rcfilters
.Controller
.prototype.removeSavedQuery = function ( queryID
) {
237 var query
= this.savedQueriesModel
.getItemByID( queryID
);
239 this.savedQueriesModel
.removeItems( [ query
] );
241 // Check if this item was the default
242 if ( this.savedQueriesModel
.getDefault() === queryID
) {
243 // Nulify the default
244 this.savedQueriesModel
.setDefault( null );
246 this._saveSavedQueries();
250 * Rename a saved query
252 * @param {string} queryID Query id
253 * @param {string} newLabel New label for the query
255 mw
.rcfilters
.Controller
.prototype.renameSavedQuery = function ( queryID
, newLabel
) {
256 var queryItem
= this.savedQueriesModel
.getItemByID( queryID
);
259 queryItem
.updateLabel( newLabel
);
261 this._saveSavedQueries();
265 * Set a saved query as default
267 * @param {string} queryID Query Id. If null is given, default
270 mw
.rcfilters
.Controller
.prototype.setDefaultSavedQuery = function ( queryID
) {
271 this.savedQueriesModel
.setDefault( queryID
);
272 this._saveSavedQueries();
278 * @param {string} queryID Query id
280 mw
.rcfilters
.Controller
.prototype.applySavedQuery = function ( queryID
) {
281 var data
, highlights
,
282 queryItem
= this.savedQueriesModel
.getItemByID( queryID
);
285 data
= queryItem
.getData();
286 highlights
= data
.highlights
;
288 // Backwards compatibility; initial version mispelled 'highlight' with 'highlights'
289 highlights
.highlight
= highlights
.highlights
|| highlights
.highlight
;
291 // Update model state from filters
292 this.filtersModel
.toggleFiltersSelected( data
.filters
);
294 // Update highlight state
295 this.filtersModel
.toggleHighlight( !!Number( highlights
.highlight
) );
296 this.filtersModel
.getItems().forEach( function ( filterItem
) {
297 var color
= highlights
[ filterItem
.getName() ];
299 filterItem
.setHighlightColor( color
);
301 filterItem
.clearHighlightColor();
305 // Check all filter interactions
306 this.filtersModel
.reassessFilterInteractions();
308 this.updateChangesList();
313 * Check whether the current filter and highlight state exists
314 * in the saved queries model.
316 * @return {boolean} Query exists
318 mw
.rcfilters
.Controller
.prototype.findQueryMatchingCurrentState = function () {
319 var highlightedItems
= {};
321 // Prepare highlights of the current query
322 this.filtersModel
.getItemsSupportingHighlights().forEach( function ( item
) {
323 highlightedItems
[ item
.getName() ] = item
.getHighlightColor();
325 highlightedItems
.highlight
= this.filtersModel
.isHighlightEnabled();
327 return this.savedQueriesModel
.findMatchingQuery(
329 filters
: this.filtersModel
.getSelectedState(),
330 highlights
: highlightedItems
336 * Get an object representing the base state of parameters
339 * This is meant to make sure that the saved queries that are
340 * in memory are always the same structure as what we would get
341 * by calling the current model's "getSelectedState" and by checking
344 * In cases where a user saved a query when the system had a certain
345 * set of filters, and then a filter was added to the system, we want
346 * to make sure that the stored queries can still be comparable to
347 * the current state, which means that we need the base state for
350 * - Saved queries are stored in "minimal" view (only changed filters
351 * are stored); When we initialize the system, we merge each minimal
352 * query with the base state (using 'getNormalizedFilters') so all
353 * saved queries have the exact same structure as what we would get
354 * by checking the getSelectedState of the filter.
355 * - When we save the queries, we minimize the object to only represent
356 * whatever has actually changed, rather than store the entire
357 * object. To check what actually is different so we can store it,
358 * we need to obtain a base state to compare against, this is
359 * what #_getMinimalFilterList does
361 mw
.rcfilters
.Controller
.prototype._buildBaseFilterState = function () {
362 var defaultParams
= this.filtersModel
.getDefaultParams(),
363 highlightedItems
= {};
365 // Prepare highlights
366 this.filtersModel
.getItemsSupportingHighlights().forEach( function ( item
) {
367 highlightedItems
[ item
.getName() ] = null;
369 highlightedItems
.highlight
= false;
371 this.baseFilterState
= {
372 filters
: this.filtersModel
.getFiltersFromParameters( defaultParams
),
373 highlights
: highlightedItems
378 * Get an object representing the base filter state of both
379 * filters and highlights. The structure is similar to what we use
380 * to store each query in the saved queries object:
386 * filterName: (string|null)
390 * @return {Object} Object representing the base state of
391 * parameters and highlights
393 mw
.rcfilters
.Controller
.prototype._getBaseFilterState = function () {
394 return this.baseFilterState
;
398 * Get an object that holds only the parameters and highlights that have
399 * values different than the base default value.
401 * This is the reverse of the normalization we do initially on loading and
402 * initializing the saved queries model.
404 * @param {Object} valuesObject Object representing the state of both
405 * filters and highlights in its normalized version, to be minimized.
406 * @return {Object} Minimal filters and highlights list
408 mw
.rcfilters
.Controller
.prototype._getMinimalFilterList = function ( valuesObject
) {
409 var result
= { filters
: {}, highlights
: {} },
410 baseState
= this._getBaseFilterState();
413 $.each( valuesObject
.filters
, function ( name
, value
) {
414 if ( baseState
.filters
!== undefined && baseState
.filters
[ name
] !== value
) {
415 result
.filters
[ name
] = value
;
419 $.each( valuesObject
.highlights
, function ( name
, value
) {
420 if ( baseState
.highlights
!== undefined && baseState
.highlights
[ name
] !== value
) {
421 result
.highlights
[ name
] = value
;
429 * Save the current state of the saved queries model with all
430 * query item representation in the user settings.
432 mw
.rcfilters
.Controller
.prototype._saveSavedQueries = function () {
434 state
= this.savedQueriesModel
.getState(),
437 // Minimize before save
438 $.each( state
.queries
, function ( queryID
, info
) {
439 state
.queries
[ queryID
].data
= controller
._getMinimalFilterList( info
.data
);
443 stringified
= JSON
.stringify( state
);
445 if ( stringified
.length
> 65535 ) {
446 // Sanity check, since the preference can only hold that.
450 // Save the preference
451 new mw
.Api().saveOption( 'rcfilters-saved-queries', stringified
);
452 // Update the preference for this session
453 mw
.user
.options
.set( 'rcfilters-saved-queries', stringified
);
457 * Synchronize the URL with the current state of the filters
458 * without adding an history entry.
460 mw
.rcfilters
.Controller
.prototype.replaceUrl = function () {
461 mw
.rcfilters
.UriProcessor
.static.replaceState( this._getUpdatedUri() );
465 * Update filter state (selection and highlighting) based
466 * on current URL values.
468 * @param {boolean} [fetchChangesList=true] Fetch new results into the changes
469 * list based on the updated model.
471 mw
.rcfilters
.Controller
.prototype.updateStateFromUrl = function ( fetchChangesList
) {
472 fetchChangesList
= fetchChangesList
=== undefined ? true : !!fetchChangesList
;
474 this.uriProcessor
.updateModelBasedOnQuery( new mw
.Uri().query
);
476 // Only update and fetch new results if it is requested
477 if ( fetchChangesList
) {
478 this.updateChangesList();
483 * Update the list of changes and notify the model
485 * @param {Object} [params] Extra parameters to add to the API call
487 mw
.rcfilters
.Controller
.prototype.updateChangesList = function ( params
) {
488 this._updateURL( params
);
489 this.changesListModel
.invalidate();
490 this._fetchChangesList()
493 function ( pieces
) {
494 var $changesListContent
= pieces
.changes
,
495 $fieldset
= pieces
.fieldset
;
496 this.changesListModel
.update( $changesListContent
, $fieldset
);
498 // Do nothing for failure
503 * Get an object representing the default parameter state, whether
504 * it is from the model defaults or from the saved queries.
506 * @return {Object} Default parameters
508 mw
.rcfilters
.Controller
.prototype._getDefaultParams = function () {
509 var data
, queryHighlights
,
511 savedHighlights
= {},
512 defaultSavedQueryItem
= this.savedQueriesModel
.getItemByID( this.savedQueriesModel
.getDefault() );
514 if ( mw
.config
.get( 'wgStructuredChangeFiltersEnableSaving' ) &&
515 defaultSavedQueryItem
) {
517 data
= defaultSavedQueryItem
.getData();
519 queryHighlights
= data
.highlights
|| {};
520 savedParams
= this.filtersModel
.getParametersFromFilters( data
.filters
|| {} );
522 // Translate highlights to parameters
523 savedHighlights
.highlight
= String( Number( queryHighlights
.highlight
) );
524 $.each( queryHighlights
, function ( filterName
, color
) {
525 if ( filterName
!== 'highlights' ) {
526 savedHighlights
[ filterName
+ '_color' ] = color
;
530 return $.extend( true, {}, savedParams
, savedHighlights
);
535 this.filtersModel
.getDefaultParams()
540 * Get an object representing the default parameter state, whether
541 * it is from the model defaults or from the saved queries.
543 * @return {Object} Default parameters
545 mw
.rcfilters
.Controller
.prototype._getDefaultParams = function () {
546 var data
, queryHighlights
,
548 savedHighlights
= {},
549 defaultSavedQueryItem
= this.savedQueriesModel
.getItemByID( this.savedQueriesModel
.getDefault() );
551 if ( mw
.config
.get( 'wgStructuredChangeFiltersEnableSaving' ) &&
552 defaultSavedQueryItem
) {
554 data
= defaultSavedQueryItem
.getData();
556 queryHighlights
= data
.highlights
|| {};
557 savedParams
= this.filtersModel
.getParametersFromFilters( data
.filters
|| {} );
559 // Translate highlights to parameters
560 savedHighlights
.highlight
= String( Number( queryHighlights
.highlight
) );
561 $.each( queryHighlights
, function ( filterName
, color
) {
562 if ( filterName
!== 'highlights' ) {
563 savedHighlights
[ filterName
+ '_color' ] = color
;
567 return $.extend( true, {}, savedParams
, savedHighlights
);
570 return this.filtersModel
.getDefaultParams();
574 * Update the URL of the page to reflect current filters
576 * This should not be called directly from outside the controller.
577 * If an action requires changing the URL, it should either use the
578 * highlighting actions below, or call #updateChangesList which does
579 * the uri corrections already.
581 * @param {Object} [params] Extra parameters to add to the API call
583 mw
.rcfilters
.Controller
.prototype._updateURL = function ( params
) {
584 var currentUri
= new mw
.Uri(),
585 updatedUri
= this._getUpdatedUri();
587 updatedUri
.extend( params
|| {} );
590 this.uriProcessor
.getVersion( currentUri
.query
) !== 2 ||
591 this.uriProcessor
.isNewState( currentUri
.query
, updatedUri
.query
)
593 if ( this.initializing
) {
594 // Initially, when we just build the first page load
595 // out of defaults, we want to replace the history
596 mw
.rcfilters
.UriProcessor
.static.replaceState( updatedUri
);
598 mw
.rcfilters
.UriProcessor
.static.pushState( updatedUri
);
604 * Get an updated mw.Uri object based on the model state
606 * @return {mw.Uri} Updated Uri
608 mw
.rcfilters
.Controller
.prototype._getUpdatedUri = function () {
609 var uri
= new mw
.Uri();
612 uri
.query
= this.uriProcessor
.minimizeQuery(
616 // We want to retain unrecognized params
617 // The uri params from model will override
618 // any recognized value in the current uri
619 // query, retain unrecognized params, and
620 // the result will then be minimized
622 this.uriProcessor
.getUriParametersFromModel(),
631 * Fetch the list of changes from the server for the current filters
633 * @return {jQuery.Promise} Promise object that will resolve with the changes list
634 * or with a string denoting no results.
636 mw
.rcfilters
.Controller
.prototype._fetchChangesList = function () {
637 var uri
= this._getUpdatedUri(),
638 requestId
= ++this.requestCounter
,
639 latestRequest = function () {
640 return requestId
=== this.requestCounter
;
643 return $.ajax( uri
.toString(), { contentType
: 'html' } )
648 if ( !latestRequest() ) {
649 return $.Deferred().reject();
652 $parsed
= $( $.parseHTML( html
) );
656 changes
: $parsed
.find( '.mw-changeslist' ).first().contents(),
658 fieldset
: $parsed
.find( 'fieldset.rcoptions' ).first()
662 function ( responseObj
) {
665 if ( !latestRequest() ) {
666 return $.Deferred().reject();
669 $parsed
= $( $.parseHTML( responseObj
.responseText
) );
671 // Force a resolve state to this promise
672 return $.Deferred().resolve( {
673 changes
: 'NO_RESULTS',
674 fieldset
: $parsed
.find( 'fieldset.rcoptions' ).first()
681 * Track usage of highlight feature
683 * @param {string} action
684 * @param {array|object|string} filters
686 mw
.rcfilters
.Controller
.prototype._trackHighlight = function ( action
, filters
) {
687 filters
= typeof filters
=== 'string' ? { name
: filters
} : filters
;
688 filters
= !Array
.isArray( filters
) ? [ filters
] : filters
;
690 'event.ChangesListHighlights',
694 userId
: mw
.user
.getId()
699 }( mediaWiki
, jQuery
) );