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
27 * @param {Object} [namespaceStructure] Namespace definition
28 * @param {Object} [tagList] Tag definition
30 mw
.rcfilters
.Controller
.prototype.initialize = function ( filterStructure
, namespaceStructure
, tagList
) {
31 var parsedSavedQueries
,
33 $changesList
= $( '.mw-changeslist' ).first().contents();
35 // Initialize the model
36 this.filtersModel
.initializeFilters( filterStructure
, namespaceStructure
, tagList
);
38 this._buildBaseFilterState();
40 this.uriProcessor
= new mw
.rcfilters
.UriProcessor(
45 parsedSavedQueries
= JSON
.parse( mw
.user
.options
.get( 'rcfilters-saved-queries' ) || '{}' );
47 parsedSavedQueries
= {};
50 // The queries are saved in a minimized state, so we need
51 // to send over the base state so the saved queries model
52 // can normalize them per each query item
53 this.savedQueriesModel
.initialize(
55 this._getBaseFilterState()
58 // Check whether we need to load defaults.
59 // We do this by checking whether the current URI query
60 // contains any parameters recognized by the system.
61 // If it does, we load the given state.
62 // If it doesn't, we have no values at all, and we assume
63 // the user loads the base-page and we load defaults.
64 // Defaults should only be applied on load (if necessary)
66 this.initializing
= true;
68 this.savedQueriesModel
.getDefault() &&
69 !this.uriProcessor
.doesQueryContainRecognizedParams( uri
.query
)
71 // We have defaults from a saved query.
72 // We will load them straight-forward (as if
73 // they were clicked in the menu) so we trigger
74 // a full ajax request and change of URL
75 this.applySavedQuery( this.savedQueriesModel
.getDefault() );
77 // There are either recognized parameters in the URL
78 // or there are none, but there is also no default
79 // saved query (so defaults are from the backend)
80 // We want to update the state but not fetch results
82 this.updateStateFromUrl( false );
84 // Update the changes list with the existing data
85 // so it gets processed
86 this.changesListModel
.update(
87 $changesList
.length
? $changesList
: 'NO_RESULTS',
88 $( 'fieldset.rcoptions' ).first()
92 this.initializing
= false;
93 this.switchView( 'default' );
97 * Switch the view of the filters model
99 * @param {string} view Requested view
101 mw
.rcfilters
.Controller
.prototype.switchView = function ( view
) {
102 this.filtersModel
.switchView( view
);
106 * Reset to default filters
108 mw
.rcfilters
.Controller
.prototype.resetToDefaults = function () {
109 this.uriProcessor
.updateModelBasedOnQuery( this._getDefaultParams() );
110 this.updateChangesList();
114 * Empty all selected filters
116 mw
.rcfilters
.Controller
.prototype.emptyFilters = function () {
117 var highlightedFilterNames
= this.filtersModel
118 .getHighlightedItems()
119 .map( function ( filterItem
) { return { name
: filterItem
.getName() }; } );
121 this.filtersModel
.emptyAllFilters();
122 this.filtersModel
.clearAllHighlightColors();
123 // Check all filter interactions
124 this.filtersModel
.reassessFilterInteractions();
126 this.updateChangesList();
128 if ( highlightedFilterNames
) {
129 this._trackHighlight( 'clearAll', highlightedFilterNames
);
134 * Update the selected state of a filter
136 * @param {string} filterName Filter name
137 * @param {boolean} [isSelected] Filter selected state
139 mw
.rcfilters
.Controller
.prototype.toggleFilterSelect = function ( filterName
, isSelected
) {
140 var filterItem
= this.filtersModel
.getItemByName( filterName
);
143 // If no filter was found, break
147 isSelected
= isSelected
=== undefined ? !filterItem
.isSelected() : isSelected
;
149 if ( filterItem
.isSelected() !== isSelected
) {
150 this.filtersModel
.toggleFilterSelected( filterName
, isSelected
);
152 this.updateChangesList();
154 // Check filter interactions
155 this.filtersModel
.reassessFilterInteractions( filterItem
);
160 * Clear both highlight and selection of a filter
162 * @param {string} filterName Name of the filter item
164 mw
.rcfilters
.Controller
.prototype.clearFilter = function ( filterName
) {
165 var filterItem
= this.filtersModel
.getItemByName( filterName
),
166 isHighlighted
= filterItem
.isHighlighted();
168 if ( filterItem
.isSelected() || isHighlighted
) {
169 this.filtersModel
.clearHighlightColor( filterName
);
170 this.filtersModel
.toggleFilterSelected( filterName
, false );
171 this.updateChangesList();
172 this.filtersModel
.reassessFilterInteractions( filterItem
);
175 if ( isHighlighted
) {
176 this._trackHighlight( 'clear', filterName
);
181 * Toggle the highlight feature on and off
183 mw
.rcfilters
.Controller
.prototype.toggleHighlight = function () {
184 this.filtersModel
.toggleHighlight();
187 if ( this.filtersModel
.isHighlightEnabled() ) {
188 mw
.hook( 'RcFilters.highlight.enable' ).fire();
193 * Toggle the namespaces inverted feature on and off
195 mw
.rcfilters
.Controller
.prototype.toggleInvertedNamespaces = function () {
196 this.filtersModel
.toggleInvertedNamespaces();
197 this.updateChangesList();
201 * Set the highlight color for a filter item
203 * @param {string} filterName Name of the filter item
204 * @param {string} color Selected color
206 mw
.rcfilters
.Controller
.prototype.setHighlightColor = function ( filterName
, color
) {
207 this.filtersModel
.setHighlightColor( filterName
, color
);
209 this._trackHighlight( 'set', { name
: filterName
, color
: color
} );
213 * Clear highlight for a filter item
215 * @param {string} filterName Name of the filter item
217 mw
.rcfilters
.Controller
.prototype.clearHighlightColor = function ( filterName
) {
218 this.filtersModel
.clearHighlightColor( filterName
);
220 this._trackHighlight( 'clear', filterName
);
224 * Save the current model state as a saved query
226 * @param {string} [label] Label of the saved query
228 mw
.rcfilters
.Controller
.prototype.saveCurrentQuery = function ( label
) {
229 var highlightedItems
= {},
230 highlightEnabled
= this.filtersModel
.isHighlightEnabled();
232 // Prepare highlights
233 this.filtersModel
.getHighlightedItems().forEach( function ( item
) {
234 highlightedItems
[ item
.getName() ] = highlightEnabled
?
235 item
.getHighlightColor() : null;
237 // These are filter states; highlight is stored as boolean
238 highlightedItems
.highlight
= this.filtersModel
.isHighlightEnabled();
241 this.savedQueriesModel
.addNewQuery(
242 label
|| mw
.msg( 'rcfilters-savedqueries-defaultlabel' ),
244 filters
: this.filtersModel
.getSelectedState(),
245 highlights
: highlightedItems
,
246 invert
: this.filtersModel
.areNamespacesInverted()
251 this._saveSavedQueries();
255 * Remove a saved query
257 * @param {string} queryID Query id
259 mw
.rcfilters
.Controller
.prototype.removeSavedQuery = function ( queryID
) {
260 this.savedQueriesModel
.removeQuery( queryID
);
262 this._saveSavedQueries();
266 * Rename a saved query
268 * @param {string} queryID Query id
269 * @param {string} newLabel New label for the query
271 mw
.rcfilters
.Controller
.prototype.renameSavedQuery = function ( queryID
, newLabel
) {
272 var queryItem
= this.savedQueriesModel
.getItemByID( queryID
);
275 queryItem
.updateLabel( newLabel
);
277 this._saveSavedQueries();
281 * Set a saved query as default
283 * @param {string} queryID Query Id. If null is given, default
286 mw
.rcfilters
.Controller
.prototype.setDefaultSavedQuery = function ( queryID
) {
287 this.savedQueriesModel
.setDefault( queryID
);
288 this._saveSavedQueries();
294 * @param {string} queryID Query id
296 mw
.rcfilters
.Controller
.prototype.applySavedQuery = function ( queryID
) {
297 var data
, highlights
,
298 queryItem
= this.savedQueriesModel
.getItemByID( queryID
);
301 data
= queryItem
.getData();
302 highlights
= data
.highlights
;
304 // Backwards compatibility; initial version mispelled 'highlight' with 'highlights'
305 highlights
.highlight
= highlights
.highlights
|| highlights
.highlight
;
307 // Update model state from filters
308 this.filtersModel
.toggleFiltersSelected( data
.filters
);
310 // Update namespace inverted property
311 this.filtersModel
.toggleInvertedNamespaces( !!Number( data
.invert
) );
313 // Update highlight state
314 this.filtersModel
.toggleHighlight( !!Number( highlights
.highlight
) );
315 this.filtersModel
.getItems().forEach( function ( filterItem
) {
316 var color
= highlights
[ filterItem
.getName() ];
318 filterItem
.setHighlightColor( color
);
320 filterItem
.clearHighlightColor();
324 // Check all filter interactions
325 this.filtersModel
.reassessFilterInteractions();
327 this.updateChangesList();
332 * Check whether the current filter and highlight state exists
333 * in the saved queries model.
335 * @return {boolean} Query exists
337 mw
.rcfilters
.Controller
.prototype.findQueryMatchingCurrentState = function () {
338 var highlightedItems
= {};
340 // Prepare highlights of the current query
341 this.filtersModel
.getItemsSupportingHighlights().forEach( function ( item
) {
342 highlightedItems
[ item
.getName() ] = item
.getHighlightColor();
344 highlightedItems
.highlight
= this.filtersModel
.isHighlightEnabled();
346 return this.savedQueriesModel
.findMatchingQuery(
348 filters
: this.filtersModel
.getSelectedState(),
349 highlights
: highlightedItems
,
350 invert
: this.filtersModel
.areNamespacesInverted()
356 * Get an object representing the base state of parameters
359 * This is meant to make sure that the saved queries that are
360 * in memory are always the same structure as what we would get
361 * by calling the current model's "getSelectedState" and by checking
364 * In cases where a user saved a query when the system had a certain
365 * set of filters, and then a filter was added to the system, we want
366 * to make sure that the stored queries can still be comparable to
367 * the current state, which means that we need the base state for
370 * - Saved queries are stored in "minimal" view (only changed filters
371 * are stored); When we initialize the system, we merge each minimal
372 * query with the base state (using 'getNormalizedFilters') so all
373 * saved queries have the exact same structure as what we would get
374 * by checking the getSelectedState of the filter.
375 * - When we save the queries, we minimize the object to only represent
376 * whatever has actually changed, rather than store the entire
377 * object. To check what actually is different so we can store it,
378 * we need to obtain a base state to compare against, this is
379 * what #_getMinimalFilterList does
381 mw
.rcfilters
.Controller
.prototype._buildBaseFilterState = function () {
382 var defaultParams
= this.filtersModel
.getDefaultParams(),
383 highlightedItems
= {};
385 // Prepare highlights
386 this.filtersModel
.getItemsSupportingHighlights().forEach( function ( item
) {
387 highlightedItems
[ item
.getName() ] = null;
389 highlightedItems
.highlight
= false;
391 this.baseFilterState
= {
392 filters
: this.filtersModel
.getFiltersFromParameters( defaultParams
),
393 highlights
: highlightedItems
,
399 * Get an object representing the base filter state of both
400 * filters and highlights. The structure is similar to what we use
401 * to store each query in the saved queries object:
407 * filterName: (string|null)
411 * @return {Object} Object representing the base state of
412 * parameters and highlights
414 mw
.rcfilters
.Controller
.prototype._getBaseFilterState = function () {
415 return this.baseFilterState
;
419 * Get an object that holds only the parameters and highlights that have
420 * values different than the base default value.
422 * This is the reverse of the normalization we do initially on loading and
423 * initializing the saved queries model.
425 * @param {Object} valuesObject Object representing the state of both
426 * filters and highlights in its normalized version, to be minimized.
427 * @return {Object} Minimal filters and highlights list
429 mw
.rcfilters
.Controller
.prototype._getMinimalFilterList = function ( valuesObject
) {
430 var result
= { filters
: {}, highlights
: {} },
431 baseState
= this._getBaseFilterState();
434 $.each( valuesObject
.filters
, function ( name
, value
) {
435 if ( baseState
.filters
!== undefined && baseState
.filters
[ name
] !== value
) {
436 result
.filters
[ name
] = value
;
440 $.each( valuesObject
.highlights
, function ( name
, value
) {
441 if ( baseState
.highlights
!== undefined && baseState
.highlights
[ name
] !== value
) {
442 result
.highlights
[ name
] = value
;
450 * Save the current state of the saved queries model with all
451 * query item representation in the user settings.
453 mw
.rcfilters
.Controller
.prototype._saveSavedQueries = function () {
455 state
= this.savedQueriesModel
.getState(),
458 // Minimize before save
459 $.each( state
.queries
, function ( queryID
, info
) {
460 state
.queries
[ queryID
].data
= controller
._getMinimalFilterList( info
.data
);
464 stringified
= JSON
.stringify( state
);
466 if ( stringified
.length
> 65535 ) {
467 // Sanity check, since the preference can only hold that.
471 // Save the preference
472 new mw
.Api().saveOption( 'rcfilters-saved-queries', stringified
);
473 // Update the preference for this session
474 mw
.user
.options
.set( 'rcfilters-saved-queries', stringified
);
478 * Synchronize the URL with the current state of the filters
479 * without adding an history entry.
481 mw
.rcfilters
.Controller
.prototype.replaceUrl = function () {
482 mw
.rcfilters
.UriProcessor
.static.replaceState( this._getUpdatedUri() );
486 * Update filter state (selection and highlighting) based
487 * on current URL values.
489 * @param {boolean} [fetchChangesList=true] Fetch new results into the changes
490 * list based on the updated model.
492 mw
.rcfilters
.Controller
.prototype.updateStateFromUrl = function ( fetchChangesList
) {
493 fetchChangesList
= fetchChangesList
=== undefined ? true : !!fetchChangesList
;
495 this.uriProcessor
.updateModelBasedOnQuery( new mw
.Uri().query
);
497 // Only update and fetch new results if it is requested
498 if ( fetchChangesList
) {
499 this.updateChangesList();
504 * Update the list of changes and notify the model
506 * @param {Object} [params] Extra parameters to add to the API call
508 mw
.rcfilters
.Controller
.prototype.updateChangesList = function ( params
) {
509 this._updateURL( params
);
510 this.changesListModel
.invalidate();
511 this._fetchChangesList()
514 function ( pieces
) {
515 var $changesListContent
= pieces
.changes
,
516 $fieldset
= pieces
.fieldset
;
517 this.changesListModel
.update( $changesListContent
, $fieldset
);
519 // Do nothing for failure
524 * Get an object representing the default parameter state, whether
525 * it is from the model defaults or from the saved queries.
527 * @return {Object} Default parameters
529 mw
.rcfilters
.Controller
.prototype._getDefaultParams = function () {
530 var data
, queryHighlights
,
532 savedHighlights
= {},
533 defaultSavedQueryItem
= this.savedQueriesModel
.getItemByID( this.savedQueriesModel
.getDefault() );
535 if ( mw
.config
.get( 'wgStructuredChangeFiltersEnableSaving' ) &&
536 defaultSavedQueryItem
) {
538 data
= defaultSavedQueryItem
.getData();
540 queryHighlights
= data
.highlights
|| {};
541 savedParams
= this.filtersModel
.getParametersFromFilters( data
.filters
|| {} );
543 // Translate highlights to parameters
544 savedHighlights
.highlight
= String( Number( queryHighlights
.highlight
) );
545 $.each( queryHighlights
, function ( filterName
, color
) {
546 if ( filterName
!== 'highlights' ) {
547 savedHighlights
[ filterName
+ '_color' ] = color
;
551 return $.extend( true, {}, savedParams
, savedHighlights
, { invert
: data
.invert
} );
556 this.filtersModel
.getDefaultParams()
561 * Get an object representing the default parameter state, whether
562 * it is from the model defaults or from the saved queries.
564 * @return {Object} Default parameters
566 mw
.rcfilters
.Controller
.prototype._getDefaultParams = function () {
567 var data
, queryHighlights
,
569 savedHighlights
= {},
570 defaultSavedQueryItem
= this.savedQueriesModel
.getItemByID( this.savedQueriesModel
.getDefault() );
572 if ( mw
.config
.get( 'wgStructuredChangeFiltersEnableSaving' ) &&
573 defaultSavedQueryItem
) {
575 data
= defaultSavedQueryItem
.getData();
577 queryHighlights
= data
.highlights
|| {};
578 savedParams
= this.filtersModel
.getParametersFromFilters( data
.filters
|| {} );
580 // Translate highlights to parameters
581 savedHighlights
.highlight
= String( Number( queryHighlights
.highlight
) );
582 $.each( queryHighlights
, function ( filterName
, color
) {
583 if ( filterName
!== 'highlights' ) {
584 savedHighlights
[ filterName
+ '_color' ] = color
;
588 return $.extend( true, {}, savedParams
, savedHighlights
);
591 return this.filtersModel
.getDefaultParams();
595 * Update the URL of the page to reflect current filters
597 * This should not be called directly from outside the controller.
598 * If an action requires changing the URL, it should either use the
599 * highlighting actions below, or call #updateChangesList which does
600 * the uri corrections already.
602 * @param {Object} [params] Extra parameters to add to the API call
604 mw
.rcfilters
.Controller
.prototype._updateURL = function ( params
) {
605 var currentUri
= new mw
.Uri(),
606 updatedUri
= this._getUpdatedUri();
608 updatedUri
.extend( params
|| {} );
611 this.uriProcessor
.getVersion( currentUri
.query
) !== 2 ||
612 this.uriProcessor
.isNewState( currentUri
.query
, updatedUri
.query
)
614 mw
.rcfilters
.UriProcessor
.static.replaceState( updatedUri
);
619 * Get an updated mw.Uri object based on the model state
621 * @return {mw.Uri} Updated Uri
623 mw
.rcfilters
.Controller
.prototype._getUpdatedUri = function () {
624 var uri
= new mw
.Uri();
627 uri
.query
= this.uriProcessor
.minimizeQuery(
631 // We want to retain unrecognized params
632 // The uri params from model will override
633 // any recognized value in the current uri
634 // query, retain unrecognized params, and
635 // the result will then be minimized
637 this.uriProcessor
.getUriParametersFromModel(),
646 * Fetch the list of changes from the server for the current filters
648 * @return {jQuery.Promise} Promise object that will resolve with the changes list
649 * or with a string denoting no results.
651 mw
.rcfilters
.Controller
.prototype._fetchChangesList = function () {
652 var uri
= this._getUpdatedUri(),
653 requestId
= ++this.requestCounter
,
654 latestRequest = function () {
655 return requestId
=== this.requestCounter
;
658 return $.ajax( uri
.toString(), { contentType
: 'html' } )
663 if ( !latestRequest() ) {
664 return $.Deferred().reject();
667 $parsed
= $( $.parseHTML( html
) );
671 changes
: $parsed
.find( '.mw-changeslist' ).first().contents(),
673 fieldset
: $parsed
.find( 'fieldset.rcoptions' ).first()
677 function ( responseObj
) {
680 if ( !latestRequest() ) {
681 return $.Deferred().reject();
684 $parsed
= $( $.parseHTML( responseObj
.responseText
) );
686 // Force a resolve state to this promise
687 return $.Deferred().resolve( {
688 changes
: 'NO_RESULTS',
689 fieldset
: $parsed
.find( 'fieldset.rcoptions' ).first()
696 * Track usage of highlight feature
698 * @param {string} action
699 * @param {array|object|string} filters
701 mw
.rcfilters
.Controller
.prototype._trackHighlight = function ( action
, filters
) {
702 filters
= typeof filters
=== 'string' ? { name
: filters
} : filters
;
703 filters
= !Array
.isArray( filters
) ? [ filters
] : filters
;
705 'event.ChangesListHighlights',
709 userId
: mw
.user
.getId()
714 }( mediaWiki
, jQuery
) );