3 * Controller for the filters in Recent Changes
5 * @param {mw.rcfilters.dm.FiltersViewModel} filtersModel Filters view model
6 * @param {mw.rcfilters.dm.ChangesListViewModel} changesListModel Changes list view model
8 mw
.rcfilters
.Controller
= function MwRcfiltersController( filtersModel
, changesListModel
) {
9 this.filtersModel
= filtersModel
;
10 this.changesListModel
= changesListModel
;
11 this.requestCounter
= 0;
15 OO
.initClass( mw
.rcfilters
.Controller
);
18 * Initialize the filter and parameter states
20 * @param {Array} filterStructure Filter definition and structure for the model
22 mw
.rcfilters
.Controller
.prototype.initialize = function ( filterStructure
) {
23 var $changesList
= $( '.mw-changeslist' ).first().contents();
24 // Initialize the model
25 this.filtersModel
.initializeFilters( filterStructure
);
26 this.updateStateBasedOnUrl();
28 // Update the changes list with the existing data
29 // so it gets processed
30 this.changesListModel
.update(
31 $changesList
.length
? $changesList
: 'NO_RESULTS',
32 $( 'fieldset.rcoptions' ).first()
38 * Update filter state (selection and highlighting) based
39 * on current URL and default values.
41 mw
.rcfilters
.Controller
.prototype.updateStateBasedOnUrl = function () {
42 var uri
= new mw
.Uri();
44 // Set filter states based on defaults and URL params
45 this.filtersModel
.toggleFiltersSelected(
46 this.filtersModel
.getFiltersFromParameters(
47 // Merge defaults with URL params for initialization
51 this.filtersModel
.getDefaultParams(),
52 // URI query overrides defaults
58 // Initialize highlights
59 this.filtersModel
.toggleHighlight( !!uri
.query
.highlight
);
60 this.filtersModel
.getItems().forEach( function ( filterItem
) {
61 var color
= uri
.query
[ filterItem
.getName() + '_color' ];
63 filterItem
.setHighlightColor( color
);
65 filterItem
.clearHighlightColor();
69 // Check all filter interactions
70 this.filtersModel
.reassessFilterInteractions();
74 * Reset to default filters
76 mw
.rcfilters
.Controller
.prototype.resetToDefaults = function () {
77 this.filtersModel
.setFiltersToDefaults();
78 this.filtersModel
.clearAllHighlightColors();
79 // Check all filter interactions
80 this.filtersModel
.reassessFilterInteractions();
82 this.updateChangesList();
86 * Empty all selected filters
88 mw
.rcfilters
.Controller
.prototype.emptyFilters = function () {
89 var highlightedFilterNames
= this.filtersModel
90 .getHighlightedItems()
91 .map( function ( filterItem
) { return { name
: filterItem
.getName() }; } );
93 this.filtersModel
.emptyAllFilters();
94 this.filtersModel
.clearAllHighlightColors();
95 // Check all filter interactions
96 this.filtersModel
.reassessFilterInteractions();
98 this.updateChangesList();
100 if ( highlightedFilterNames
) {
101 this.trackHighlight( 'clearAll', highlightedFilterNames
);
106 * Update the selected state of a filter
108 * @param {string} filterName Filter name
109 * @param {boolean} [isSelected] Filter selected state
111 mw
.rcfilters
.Controller
.prototype.toggleFilterSelect = function ( filterName
, isSelected
) {
112 var filterItem
= this.filtersModel
.getItemByName( filterName
);
114 isSelected
= isSelected
=== undefined ? !filterItem
.isSelected() : isSelected
;
116 if ( filterItem
.isSelected() !== isSelected
) {
117 this.filtersModel
.toggleFilterSelected( filterName
, isSelected
);
119 this.updateChangesList();
121 // Check filter interactions
122 this.filtersModel
.reassessFilterInteractions( filterItem
);
127 * Update the URL of the page to reflect current filters
129 * This should not be called directly from outside the controller.
130 * If an action requires changing the URL, it should either use the
131 * highlighting actions below, or call #updateChangesList which does
132 * the uri corrections already.
135 * @param {Object} [params] Extra parameters to add to the API call
137 mw
.rcfilters
.Controller
.prototype.updateURL = function ( params
) {
139 notEquivalent = function ( obj1
, obj2
) {
140 var keys
= Object
.keys( obj1
).concat( Object
.keys( obj2
) );
141 return keys
.some( function ( key
) {
142 return obj1
[ key
] != obj2
[ key
]; // eslint-disable-line eqeqeq
146 params
= params
|| {};
148 updatedUri
= this.getUpdatedUri();
149 updatedUri
.extend( params
);
151 if ( notEquivalent( updatedUri
.query
, new mw
.Uri().query
) ) {
152 window
.history
.pushState( { tag
: 'rcfilters' }, document
.title
, updatedUri
.toString() );
157 * Get an updated mw.Uri object based on the model state
159 * @return {mw.Uri} Updated Uri
161 mw
.rcfilters
.Controller
.prototype.getUpdatedUri = function () {
162 var uri
= new mw
.Uri(),
163 highlightParams
= this.filtersModel
.getHighlightParameters();
165 // Add to existing queries in URL
166 // TODO: Clean up the list of filters; perhaps 'falsy' filters
167 // shouldn't appear at all? Or compare to existing query string
168 // and see if current state of a specific filter is needed?
169 uri
.extend( this.filtersModel
.getParametersFromFilters() );
172 Object
.keys( highlightParams
).forEach( function ( paramName
) {
173 if ( highlightParams
[ paramName
] ) {
174 uri
.query
[ paramName
] = highlightParams
[ paramName
];
176 delete uri
.query
[ paramName
];
184 * Fetch the list of changes from the server for the current filters
186 * @return {jQuery.Promise} Promise object that will resolve with the changes list
187 * or with a string denoting no results.
189 mw
.rcfilters
.Controller
.prototype.fetchChangesList = function () {
190 var uri
= this.getUpdatedUri(),
191 requestId
= ++this.requestCounter
,
192 latestRequest = function () {
193 return requestId
=== this.requestCounter
;
196 return $.ajax( uri
.toString(), { contentType
: 'html' } )
201 if ( !latestRequest() ) {
202 return $.Deferred().reject();
205 $parsed
= $( $.parseHTML( html
) );
209 changes
: $parsed
.find( '.mw-changeslist' ).first().contents(),
211 fieldset
: $parsed
.find( 'fieldset.rcoptions' ).first()
215 function ( responseObj
) {
218 if ( !latestRequest() ) {
219 return $.Deferred().reject();
222 $parsed
= $( $.parseHTML( responseObj
.responseText
) );
224 // Force a resolve state to this promise
225 return $.Deferred().resolve( {
226 changes
: 'NO_RESULTS',
227 fieldset
: $parsed
.find( 'fieldset.rcoptions' ).first()
234 * Update the list of changes and notify the model
236 * @param {Object} [params] Extra parameters to add to the API call
238 mw
.rcfilters
.Controller
.prototype.updateChangesList = function ( params
) {
239 this.updateURL( params
);
240 this.changesListModel
.invalidate();
241 this.fetchChangesList()
244 function ( pieces
) {
245 var $changesListContent
= pieces
.changes
,
246 $fieldset
= pieces
.fieldset
;
247 this.changesListModel
.update( $changesListContent
, $fieldset
);
249 // Do nothing for failure
254 * Toggle the highlight feature on and off
256 mw
.rcfilters
.Controller
.prototype.toggleHighlight = function () {
257 this.filtersModel
.toggleHighlight();
262 * Set the highlight color for a filter item
264 * @param {string} filterName Name of the filter item
265 * @param {string} color Selected color
267 mw
.rcfilters
.Controller
.prototype.setHighlightColor = function ( filterName
, color
) {
268 this.filtersModel
.setHighlightColor( filterName
, color
);
270 this.trackHighlight( 'set', { name
: filterName
, color
: color
} );
274 * Clear highlight for a filter item
276 * @param {string} filterName Name of the filter item
278 mw
.rcfilters
.Controller
.prototype.clearHighlightColor = function ( filterName
) {
279 this.filtersModel
.clearHighlightColor( filterName
);
281 this.trackHighlight( 'clear', filterName
);
285 * Clear both highlight and selection of a filter
287 * @param {string} filterName Name of the filter item
289 mw
.rcfilters
.Controller
.prototype.clearFilter = function ( filterName
) {
290 var filterItem
= this.filtersModel
.getItemByName( filterName
),
291 isHighlighted
= filterItem
.isHighlighted();
293 if ( filterItem
.isSelected() || isHighlighted
) {
294 this.filtersModel
.clearHighlightColor( filterName
);
295 this.filtersModel
.toggleFilterSelected( filterName
, false );
296 this.updateChangesList();
297 this.filtersModel
.reassessFilterInteractions( filterItem
);
300 if ( isHighlighted
) {
301 this.trackHighlight( 'clear', filterName
);
306 * Synchronize the URL with the current state of the filters
307 * without adding an history entry.
309 mw
.rcfilters
.Controller
.prototype.replaceUrl = function () {
310 window
.history
.replaceState(
311 { tag
: 'rcfilters' },
313 this.getUpdatedUri().toString()
318 * Track usage of highlight feature
320 * @param {string} action
321 * @param {array|object|string} filters
323 mw
.rcfilters
.Controller
.prototype.trackHighlight = function ( action
, filters
) {
324 filters
= $.type( filters
) === 'string' ? { name
: filters
} : filters
;
325 filters
= $.type( filters
) === 'object' ? [ filters
] : filters
;
327 'event.ChangesListHighlights',
334 }( mediaWiki
, jQuery
) );