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();
63 this.updateChangesList();
67 * Empty all selected filters
69 mw
.rcfilters
.Controller
.prototype.emptyFilters = function () {
70 this.filtersModel
.emptyAllFilters();
71 this.filtersModel
.clearAllHighlightColors();
73 this.updateChangesList();
77 * Update the state of a filter
79 * @param {string} filterName Filter name
80 * @param {boolean} isSelected Filter selected state
82 mw
.rcfilters
.Controller
.prototype.updateFilter = function ( filterName
, isSelected
) {
84 filterItem
= this.filtersModel
.getItemByName( filterName
);
86 if ( filterItem
.isSelected() !== isSelected
) {
87 obj
[ filterName
] = isSelected
;
88 this.filtersModel
.updateFilters( obj
);
91 this.updateChangesList();
93 // Check filter interactions
94 this.filtersModel
.reassessFilterInteractions( this.filtersModel
.getItemByName( filterName
) );
99 * Update the URL of the page to reflect current filters
101 mw
.rcfilters
.Controller
.prototype.updateURL = function () {
102 var uri
= this.getUpdatedUri();
103 window
.history
.pushState( { tag
: 'rcfilters' }, document
.title
, uri
.toString() );
107 * Get an updated mw.Uri object based on the model state
109 * @return {mw.Uri} Updated Uri
111 mw
.rcfilters
.Controller
.prototype.getUpdatedUri = function () {
112 var uri
= new mw
.Uri(),
113 highlightParams
= this.filtersModel
.getHighlightParameters();
115 // Add to existing queries in URL
116 // TODO: Clean up the list of filters; perhaps 'falsy' filters
117 // shouldn't appear at all? Or compare to existing query string
118 // and see if current state of a specific filter is needed?
119 uri
.extend( this.filtersModel
.getParametersFromFilters() );
122 Object
.keys( highlightParams
).forEach( function ( paramName
) {
123 if ( highlightParams
[ paramName
] ) {
124 uri
.query
[ paramName
] = highlightParams
[ paramName
];
126 delete uri
.query
[ paramName
];
134 * Fetch the list of changes from the server for the current filters
136 * @return {jQuery.Promise} Promise object that will resolve with the changes list
138 mw
.rcfilters
.Controller
.prototype.fetchChangesList = function () {
139 var uri
= this.getUpdatedUri(),
140 requestId
= ++this.requestCounter
,
141 latestRequest = function () {
142 return requestId
=== this.requestCounter
;
144 uri
.extend( this.filtersModel
.getParametersFromFilters() );
145 return $.ajax( uri
.toString(), { contentType
: 'html' } )
146 .then( function ( html
) {
147 return latestRequest() ?
148 $( $.parseHTML( html
) ).find( '.mw-changeslist' ).first().contents() :
150 } ).then( null, function () {
151 return latestRequest() ? 'NO_RESULTS' : null;
156 * Update the list of changes and notify the model
158 mw
.rcfilters
.Controller
.prototype.updateChangesList = function () {
159 this.changesListModel
.invalidate();
160 this.fetchChangesList()
161 .always( function ( changesListContent
) {
162 if ( changesListContent
) {
163 this.changesListModel
.update( changesListContent
);
169 * Toggle the highlight feature on and off
171 mw
.rcfilters
.Controller
.prototype.toggleHighlight = function () {
172 this.filtersModel
.toggleHighlight();
177 * Set the highlight color for a filter item
179 * @param {string} filterName Name of the filter item
180 * @param {string} color Selected color
182 mw
.rcfilters
.Controller
.prototype.setHighlightColor = function ( filterName
, color
) {
183 this.filtersModel
.setHighlightColor( filterName
, color
);
188 * Clear highlight for a filter item
190 * @param {string} filterName Name of the filter item
192 mw
.rcfilters
.Controller
.prototype.clearHighlightColor = function ( filterName
) {
193 this.filtersModel
.clearHighlightColor( filterName
);
196 }( mediaWiki
, jQuery
) );