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 {Object} filterStructure Filter definition and structure for the model
22 mw
.rcfilters
.Controller
.prototype.initialize = function ( filterStructure
) {
23 var uri
= new mw
.Uri();
25 // Initialize the model
26 this.filtersModel
.initializeFilters( filterStructure
);
28 // Set filter states based on defaults and URL params
29 this.filtersModel
.updateFilters(
30 this.filtersModel
.getFiltersFromParameters(
31 // Merge defaults with URL params for initialization
35 this.filtersModel
.getDefaultParams(),
36 // URI query overrides defaults
42 // Initialize highlights
43 this.filtersModel
.toggleHighlight( !!uri
.query
.highlight
);
44 this.filtersModel
.getItems().forEach( function ( filterItem
) {
45 var color
= uri
.query
[ filterItem
.getName() + '_color' ];
50 filterItem
.setHighlightColor( color
);
53 // Check all filter interactions
54 this.filtersModel
.reassessFilterInteractions();
58 * Reset to default filters
60 mw
.rcfilters
.Controller
.prototype.resetToDefaults = function () {
61 this.filtersModel
.setFiltersToDefaults();
62 // Check all filter interactions
63 this.filtersModel
.reassessFilterInteractions();
65 this.updateChangesList();
69 * Empty all selected filters
71 mw
.rcfilters
.Controller
.prototype.emptyFilters = function () {
72 this.filtersModel
.emptyAllFilters();
73 this.filtersModel
.clearAllHighlightColors();
74 // Check all filter interactions
75 this.filtersModel
.reassessFilterInteractions();
77 this.updateChangesList();
81 * Update the selected state of a filter
83 * @param {string} filterName Filter name
84 * @param {boolean} [isSelected] Filter selected state
86 mw
.rcfilters
.Controller
.prototype.toggleFilterSelect = function ( filterName
, isSelected
) {
88 filterItem
= this.filtersModel
.getItemByName( filterName
);
90 isSelected
= isSelected
=== undefined ? !filterItem
.isSelected() : isSelected
;
92 if ( filterItem
.isSelected() !== isSelected
) {
93 obj
[ filterName
] = isSelected
;
94 this.filtersModel
.updateFilters( obj
);
96 this.updateChangesList();
98 // Check filter interactions
99 this.filtersModel
.reassessFilterInteractions( this.filtersModel
.getItemByName( filterName
) );
104 * Update the URL of the page to reflect current filters
106 * This should not be called directly from outside the controller.
107 * If an action requires changing the URL, it should either use the
108 * highlighting actions below, or call #updateChangesList which does
109 * the uri corrections already.
112 * @param {Object} [params] Extra parameters to add to the API call
114 mw
.rcfilters
.Controller
.prototype.updateURL = function ( params
) {
117 params
= params
|| {};
119 uri
= this.getUpdatedUri();
120 uri
.extend( params
);
122 window
.history
.pushState( { tag
: 'rcfilters' }, document
.title
, uri
.toString() );
126 * Get an updated mw.Uri object based on the model state
128 * @return {mw.Uri} Updated Uri
130 mw
.rcfilters
.Controller
.prototype.getUpdatedUri = function () {
131 var uri
= new mw
.Uri(),
132 highlightParams
= this.filtersModel
.getHighlightParameters();
134 // Add to existing queries in URL
135 // TODO: Clean up the list of filters; perhaps 'falsy' filters
136 // shouldn't appear at all? Or compare to existing query string
137 // and see if current state of a specific filter is needed?
138 uri
.extend( this.filtersModel
.getParametersFromFilters() );
141 Object
.keys( highlightParams
).forEach( function ( paramName
) {
142 if ( highlightParams
[ paramName
] ) {
143 uri
.query
[ paramName
] = highlightParams
[ paramName
];
145 delete uri
.query
[ paramName
];
153 * Fetch the list of changes from the server for the current filters
155 * @return {jQuery.Promise} Promise object that will resolve with the changes list
156 * or with a string denoting no results.
158 mw
.rcfilters
.Controller
.prototype.fetchChangesList = function () {
159 var uri
= this.getUpdatedUri(),
160 requestId
= ++this.requestCounter
,
161 latestRequest = function () {
162 return requestId
=== this.requestCounter
;
165 return $.ajax( uri
.toString(), { contentType
: 'html' } )
170 if ( !latestRequest() ) {
171 return $.Deferred().reject();
174 $parsed
= $( $.parseHTML( html
) );
178 changes
: $parsed
.find( '.mw-changeslist' ).first().contents(),
180 fieldset
: $parsed
.find( 'fieldset.rcoptions' ).first()
184 function ( responseObj
) {
187 if ( !latestRequest() ) {
188 return $.Deferred().reject();
191 $parsed
= $( $.parseHTML( responseObj
.responseText
) );
193 // Force a resolve state to this promise
194 return $.Deferred().resolve( {
195 changes
: 'NO_RESULTS',
196 fieldset
: $parsed
.find( 'fieldset.rcoptions' ).first()
203 * Update the list of changes and notify the model
205 * @param {Object} [params] Extra parameters to add to the API call
207 mw
.rcfilters
.Controller
.prototype.updateChangesList = function ( params
) {
208 this.updateURL( params
);
209 this.changesListModel
.invalidate();
210 this.fetchChangesList()
213 function ( pieces
) {
214 var $changesListContent
= pieces
.changes
,
215 $fieldset
= pieces
.fieldset
;
217 this.changesListModel
.update( $changesListContent
, $fieldset
);
219 // Do nothing for failure
224 * Toggle the highlight feature on and off
226 mw
.rcfilters
.Controller
.prototype.toggleHighlight = function () {
227 this.filtersModel
.toggleHighlight();
232 * Set the highlight color for a filter item
234 * @param {string} filterName Name of the filter item
235 * @param {string} color Selected color
237 mw
.rcfilters
.Controller
.prototype.setHighlightColor = function ( filterName
, color
) {
238 this.filtersModel
.setHighlightColor( filterName
, color
);
243 * Clear highlight for a filter item
245 * @param {string} filterName Name of the filter item
247 mw
.rcfilters
.Controller
.prototype.clearHighlightColor = function ( filterName
) {
248 this.filtersModel
.clearHighlightColor( filterName
);
251 }( mediaWiki
, jQuery
) );