e9274f53b9d74c7ccdb2a248327e4ebf46ed5c32
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;
19 OO
.initClass( mw
.rcfilters
.Controller
);
22 * Initialize the filter and parameter states
24 * @param {Array} filterStructure Filter definition and structure for the model
26 mw
.rcfilters
.Controller
.prototype.initialize = function ( filterStructure
) {
27 var parsedSavedQueries
,
28 $changesList
= $( '.mw-changeslist' ).first().contents();
29 // Initialize the model
30 this.filtersModel
.initializeFilters( filterStructure
);
32 this._buildBaseFilterState();
35 parsedSavedQueries
= JSON
.parse( mw
.user
.options
.get( 'rcfilters-saved-queries' ) || '{}' );
37 parsedSavedQueries
= {};
40 // The queries are saved in a minimized state, so we need
41 // to send over the base state so the saved queries model
42 // can normalize them per each query item
43 this.savedQueriesModel
.initialize(
48 this.updateStateBasedOnUrl();
50 // Update the changes list with the existing data
51 // so it gets processed
52 this.changesListModel
.update(
53 $changesList
.length
? $changesList
: 'NO_RESULTS',
54 $( 'fieldset.rcoptions' ).first()
59 * Reset to default filters
61 mw
.rcfilters
.Controller
.prototype.resetToDefaults = function () {
62 this._updateModelState( this._getDefaultParams() );
63 this.updateChangesList();
67 * Empty all selected filters
69 mw
.rcfilters
.Controller
.prototype.emptyFilters = function () {
70 var highlightedFilterNames
= this.filtersModel
71 .getHighlightedItems()
72 .map( function ( filterItem
) { return { name
: filterItem
.getName() }; } );
74 this.filtersModel
.emptyAllFilters();
75 this.filtersModel
.clearAllHighlightColors();
76 // Check all filter interactions
77 this.filtersModel
.reassessFilterInteractions();
79 this.updateChangesList();
81 if ( highlightedFilterNames
) {
82 this._trackHighlight( 'clearAll', highlightedFilterNames
);
87 * Update the selected state of a filter
89 * @param {string} filterName Filter name
90 * @param {boolean} [isSelected] Filter selected state
92 mw
.rcfilters
.Controller
.prototype.toggleFilterSelect = function ( filterName
, isSelected
) {
93 var filterItem
= this.filtersModel
.getItemByName( filterName
);
96 // If no filter was found, break
100 isSelected
= isSelected
=== undefined ? !filterItem
.isSelected() : isSelected
;
102 if ( filterItem
.isSelected() !== isSelected
) {
103 this.filtersModel
.toggleFilterSelected( filterName
, isSelected
);
105 this.updateChangesList();
107 // Check filter interactions
108 this.filtersModel
.reassessFilterInteractions( filterItem
);
113 * Clear both highlight and selection of a filter
115 * @param {string} filterName Name of the filter item
117 mw
.rcfilters
.Controller
.prototype.clearFilter = function ( filterName
) {
118 var filterItem
= this.filtersModel
.getItemByName( filterName
),
119 isHighlighted
= filterItem
.isHighlighted();
121 if ( filterItem
.isSelected() || isHighlighted
) {
122 this.filtersModel
.clearHighlightColor( filterName
);
123 this.filtersModel
.toggleFilterSelected( filterName
, false );
124 this.updateChangesList();
125 this.filtersModel
.reassessFilterInteractions( filterItem
);
128 if ( isHighlighted
) {
129 this._trackHighlight( 'clear', filterName
);
134 * Toggle the highlight feature on and off
136 mw
.rcfilters
.Controller
.prototype.toggleHighlight = function () {
137 this.filtersModel
.toggleHighlight();
140 if ( this.filtersModel
.isHighlightEnabled() ) {
141 mw
.hook( 'RcFilters.highlight.enable' ).fire();
146 * Set the highlight color for a filter item
148 * @param {string} filterName Name of the filter item
149 * @param {string} color Selected color
151 mw
.rcfilters
.Controller
.prototype.setHighlightColor = function ( filterName
, color
) {
152 this.filtersModel
.setHighlightColor( filterName
, color
);
154 this._trackHighlight( 'set', { name
: filterName
, color
: color
} );
158 * Clear highlight for a filter item
160 * @param {string} filterName Name of the filter item
162 mw
.rcfilters
.Controller
.prototype.clearHighlightColor = function ( filterName
) {
163 this.filtersModel
.clearHighlightColor( filterName
);
165 this._trackHighlight( 'clear', filterName
);
169 * Save the current model state as a saved query
171 * @param {string} [label] Label of the saved query
173 mw
.rcfilters
.Controller
.prototype.saveCurrentQuery = function ( label
) {
174 var highlightedItems
= {},
175 highlightEnabled
= this.filtersModel
.isHighlightEnabled();
177 // Prepare highlights
178 this.filtersModel
.getHighlightedItems().forEach( function ( item
) {
179 highlightedItems
[ item
.getName() ] = highlightEnabled
?
180 item
.getHighlightColor() : null;
182 // These are filter states; highlight is stored as boolean
183 highlightedItems
.highlight
= this.filtersModel
.isHighlightEnabled();
186 this.savedQueriesModel
.addNewQuery(
187 label
|| mw
.msg( 'rcfilters-savedqueries-defaultlabel' ),
189 filters
: this.filtersModel
.getSelectedState(),
190 highlights
: highlightedItems
195 this._saveSavedQueries();
199 * Remove a saved query
201 * @param {string} queryID Query id
203 mw
.rcfilters
.Controller
.prototype.removeSavedQuery = function ( queryID
) {
204 var query
= this.savedQueriesModel
.getItemByID( queryID
);
206 this.savedQueriesModel
.removeItems( [ query
] );
208 // Check if this item was the default
209 if ( this.savedQueriesModel
.getDefault() === queryID
) {
210 // Nulify the default
211 this.savedQueriesModel
.setDefault( null );
213 this._saveSavedQueries();
217 * Rename a saved query
219 * @param {string} queryID Query id
220 * @param {string} newLabel New label for the query
222 mw
.rcfilters
.Controller
.prototype.renameSavedQuery = function ( queryID
, newLabel
) {
223 var queryItem
= this.savedQueriesModel
.getItemByID( queryID
);
226 queryItem
.updateLabel( newLabel
);
228 this._saveSavedQueries();
232 * Set a saved query as default
234 * @param {string} queryID Query Id. If null is given, default
237 mw
.rcfilters
.Controller
.prototype.setDefaultSavedQuery = function ( queryID
) {
238 this.savedQueriesModel
.setDefault( queryID
);
239 this._saveSavedQueries();
245 * @param {string} queryID Query id
247 mw
.rcfilters
.Controller
.prototype.applySavedQuery = function ( queryID
) {
248 var data
, highlights
,
249 queryItem
= this.savedQueriesModel
.getItemByID( queryID
);
252 data
= queryItem
.getData();
253 highlights
= data
.highlights
;
255 // Backwards compatibility; initial version mispelled 'highlight' with 'highlights'
256 highlights
.highlight
= highlights
.highlights
|| highlights
.highlight
;
258 // Update model state from filters
259 this.filtersModel
.toggleFiltersSelected( data
.filters
);
261 // Update highlight state
262 this.filtersModel
.toggleHighlight( !!Number( highlights
.highlight
) );
263 this.filtersModel
.getItems().forEach( function ( filterItem
) {
264 var color
= highlights
[ filterItem
.getName() ];
266 filterItem
.setHighlightColor( color
);
268 filterItem
.clearHighlightColor();
272 // Check all filter interactions
273 this.filtersModel
.reassessFilterInteractions();
275 this.updateChangesList();
280 * Check whether the current filter and highlight state exists
281 * in the saved queries model.
283 * @return {boolean} Query exists
285 mw
.rcfilters
.Controller
.prototype.findQueryMatchingCurrentState = function () {
286 var highlightedItems
= {};
288 // Prepare highlights of the current query
289 this.filtersModel
.getItemsSupportingHighlights().forEach( function ( item
) {
290 highlightedItems
[ item
.getName() ] = item
.getHighlightColor();
292 highlightedItems
.highlight
= this.filtersModel
.isHighlightEnabled();
294 return this.savedQueriesModel
.findMatchingQuery(
296 filters
: this.filtersModel
.getSelectedState(),
297 highlights
: highlightedItems
303 * Get an object representing the base state of parameters
306 * This is meant to make sure that the saved queries that are
307 * in memory are always the same structure as what we would get
308 * by calling the current model's "getSelectedState" and by checking
311 * In cases where a user saved a query when the system had a certain
312 * set of filters, and then a filter was added to the system, we want
313 * to make sure that the stored queries can still be comparable to
314 * the current state, which means that we need the base state for
317 * - Saved queries are stored in "minimal" view (only changed filters
318 * are stored); When we initialize the system, we merge each minimal
319 * query with the base state (using 'getNormalizedFilters') so all
320 * saved queries have the exact same structure as what we would get
321 * by checking the getSelectedState of the filter.
322 * - When we save the queries, we minimize the object to only represent
323 * whatever has actually changed, rather than store the entire
324 * object. To check what actually is different so we can store it,
325 * we need to obtain a base state to compare against, this is
326 * what #_getMinimalFilterList does
328 mw
.rcfilters
.Controller
.prototype._buildBaseFilterState = function () {
329 var defaultParams
= this.filtersModel
.getDefaultParams(),
330 highlightedItems
= {};
332 // Prepare highlights
333 this.filtersModel
.getItemsSupportingHighlights().forEach( function ( item
) {
334 highlightedItems
[ item
.getName() ] = null;
336 highlightedItems
.highlight
= false;
339 filters
: this.filtersModel
.getFiltersFromParameters( defaultParams
),
340 highlights
: highlightedItems
345 * Get an object representing the base state of parameters
346 * and highlights. The structure is similar to what we use
347 * to store each query in the saved queries object:
353 * filterName: (string|null)
357 * @return {Object} Object representing the base state of
358 * parameters and highlights
360 mw
.rcfilters
.Controller
.prototype._getBaseState = function () {
361 return this.baseState
;
365 * Get an object that holds only the parameters and highlights that have
366 * values different than the base default value.
368 * This is the reverse of the normalization we do initially on loading and
369 * initializing the saved queries model.
371 * @param {Object} valuesObject Object representing the state of both
372 * filters and highlights in its normalized version, to be minimized.
373 * @return {Object} Minimal filters and highlights list
375 mw
.rcfilters
.Controller
.prototype._getMinimalFilterList = function ( valuesObject
) {
376 var result
= { filters
: {}, highlights
: {} },
377 baseState
= this._getBaseState();
380 $.each( valuesObject
.filters
, function ( name
, value
) {
381 if ( baseState
.filters
!== undefined && baseState
.filters
[ name
] !== value
) {
382 result
.filters
[ name
] = value
;
386 $.each( valuesObject
.highlights
, function ( name
, value
) {
387 if ( baseState
.highlights
!== undefined && baseState
.highlights
[ name
] !== value
) {
388 result
.highlights
[ name
] = value
;
396 * Save the current state of the saved queries model with all
397 * query item representation in the user settings.
399 mw
.rcfilters
.Controller
.prototype._saveSavedQueries = function () {
401 state
= this.savedQueriesModel
.getState(),
404 // Minimize before save
405 $.each( state
.queries
, function ( queryID
, info
) {
406 state
.queries
[ queryID
].data
= controller
._getMinimalFilterList( info
.data
);
410 stringified
= JSON
.stringify( state
);
412 if ( stringified
.length
> 65535 ) {
413 // Sanity check, since the preference can only hold that.
417 // Save the preference
418 new mw
.Api().saveOption( 'rcfilters-saved-queries', stringified
);
419 // Update the preference for this session
420 mw
.user
.options
.set( 'rcfilters-saved-queries', stringified
);
424 * Synchronize the URL with the current state of the filters
425 * without adding an history entry.
427 mw
.rcfilters
.Controller
.prototype.replaceUrl = function () {
428 window
.history
.replaceState(
429 { tag
: 'rcfilters' },
431 this._getUpdatedUri().toString()
436 * Update filter state (selection and highlighting) based
437 * on current URL and default values.
439 mw
.rcfilters
.Controller
.prototype.updateStateBasedOnUrl = function () {
440 var uri
= new mw
.Uri(),
441 defaultParams
= this._getDefaultParams();
443 this._updateModelState( $.extend( {}, defaultParams
, uri
.query
) );
444 this.updateChangesList();
448 * Update the list of changes and notify the model
450 * @param {Object} [params] Extra parameters to add to the API call
452 mw
.rcfilters
.Controller
.prototype.updateChangesList = function ( params
) {
453 this._updateURL( params
);
454 this.changesListModel
.invalidate();
455 this._fetchChangesList()
458 function ( pieces
) {
459 var $changesListContent
= pieces
.changes
,
460 $fieldset
= pieces
.fieldset
;
461 this.changesListModel
.update( $changesListContent
, $fieldset
);
463 // Do nothing for failure
468 * Update the model state from given the given parameters.
470 * This is an internal method, and should only be used from inside
473 * @param {Object} parameters Object representing the parameters for
474 * filters and highlights
476 mw
.rcfilters
.Controller
.prototype._updateModelState = function ( parameters
) {
477 // Update filter states
478 this.filtersModel
.toggleFiltersSelected(
479 this.filtersModel
.getFiltersFromParameters(
484 // Update highlight state
485 this.filtersModel
.toggleHighlight( !!Number( parameters
.highlight
) );
486 this.filtersModel
.getItems().forEach( function ( filterItem
) {
487 var color
= parameters
[ filterItem
.getName() + '_color' ];
489 filterItem
.setHighlightColor( color
);
491 filterItem
.clearHighlightColor();
495 // Check all filter interactions
496 this.filtersModel
.reassessFilterInteractions();
500 * Get an object representing the default parameter state, whether
501 * it is from the model defaults or from the saved queries.
503 * @return {Object} Default parameters
505 mw
.rcfilters
.Controller
.prototype._getDefaultParams = function () {
506 var data
, queryHighlights
,
508 savedHighlights
= {},
509 defaultSavedQueryItem
= this.savedQueriesModel
.getItemByID( this.savedQueriesModel
.getDefault() );
511 if ( mw
.config
.get( 'wgStructuredChangeFiltersEnableSaving' ) &&
512 defaultSavedQueryItem
) {
514 data
= defaultSavedQueryItem
.getData();
516 queryHighlights
= data
.highlights
|| {};
517 savedParams
= this.filtersModel
.getParametersFromFilters( data
.filters
|| {} );
519 // Translate highlights to parameters
520 savedHighlights
.highlight
= String( Number( queryHighlights
.highlight
) );
521 $.each( queryHighlights
, function ( filterName
, color
) {
522 if ( filterName
!== 'highlights' ) {
523 savedHighlights
[ filterName
+ '_color' ] = color
;
527 return $.extend( true, {}, savedParams
, savedHighlights
);
530 return this.filtersModel
.getDefaultParams();
534 * Update the URL of the page to reflect current filters
536 * This should not be called directly from outside the controller.
537 * If an action requires changing the URL, it should either use the
538 * highlighting actions below, or call #updateChangesList which does
539 * the uri corrections already.
541 * @param {Object} [params] Extra parameters to add to the API call
543 mw
.rcfilters
.Controller
.prototype._updateURL = function ( params
) {
545 notEquivalent = function ( obj1
, obj2
) {
546 var keys
= Object
.keys( obj1
).concat( Object
.keys( obj2
) );
547 return keys
.some( function ( key
) {
548 return obj1
[ key
] != obj2
[ key
]; // eslint-disable-line eqeqeq
552 params
= params
|| {};
554 updatedUri
= this._getUpdatedUri();
555 updatedUri
.extend( params
);
557 if ( notEquivalent( updatedUri
.query
, new mw
.Uri().query
) ) {
558 window
.history
.pushState( { tag
: 'rcfilters' }, document
.title
, updatedUri
.toString() );
563 * Get an updated mw.Uri object based on the model state
565 * @return {mw.Uri} Updated Uri
567 mw
.rcfilters
.Controller
.prototype._getUpdatedUri = function () {
568 var uri
= new mw
.Uri(),
569 highlightParams
= this.filtersModel
.getHighlightParameters();
571 // Add to existing queries in URL
572 // TODO: Clean up the list of filters; perhaps 'falsy' filters
573 // shouldn't appear at all? Or compare to existing query string
574 // and see if current state of a specific filter is needed?
575 uri
.extend( this.filtersModel
.getParametersFromFilters() );
578 uri
.query
.highlight
= Number( this.filtersModel
.isHighlightEnabled() );
579 Object
.keys( highlightParams
).forEach( function ( paramName
) {
580 // Always have some value (either the color or null) so that
581 // if we have something in the URL that doesn't have the highlight
582 // intentionally, it can override default with highlight.
583 // Otherwise, the $.extend will always add the highlight that
584 // exists in the default even if the URL query that is being
585 // refreshed has different highlights, or has highlights enabled
586 // but no active highlights anywhere
587 uri
.query
[ paramName
] = highlightParams
[ paramName
] ?
588 highlightParams
[ paramName
] : null;
595 * Fetch the list of changes from the server for the current filters
597 * @return {jQuery.Promise} Promise object that will resolve with the changes list
598 * or with a string denoting no results.
600 mw
.rcfilters
.Controller
.prototype._fetchChangesList = function () {
601 var uri
= this._getUpdatedUri(),
602 requestId
= ++this.requestCounter
,
603 latestRequest = function () {
604 return requestId
=== this.requestCounter
;
607 return $.ajax( uri
.toString(), { contentType
: 'html' } )
612 if ( !latestRequest() ) {
613 return $.Deferred().reject();
616 $parsed
= $( $.parseHTML( html
) );
620 changes
: $parsed
.find( '.mw-changeslist' ).first().contents(),
622 fieldset
: $parsed
.find( 'fieldset.rcoptions' ).first()
626 function ( responseObj
) {
629 if ( !latestRequest() ) {
630 return $.Deferred().reject();
633 $parsed
= $( $.parseHTML( responseObj
.responseText
) );
635 // Force a resolve state to this promise
636 return $.Deferred().resolve( {
637 changes
: 'NO_RESULTS',
638 fieldset
: $parsed
.find( 'fieldset.rcoptions' ).first()
645 * Track usage of highlight feature
647 * @param {string} action
648 * @param {array|object|string} filters
650 mw
.rcfilters
.Controller
.prototype._trackHighlight = function ( action
, filters
) {
651 filters
= typeof filters
=== 'string' ? { name
: filters
} : filters
;
652 filters
= !Array
.isArray( filters
) ? [ filters
] : filters
;
654 'event.ChangesListHighlights',
658 userId
: mw
.user
.getId()
663 }( mediaWiki
, jQuery
) );