025dc89bc3a91b26b816d2baafc615a8d4292c65
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / mw.rcfilters.Controller.js
1 ( function ( mw ) {
2 /**
3 * Controller for the filters in Recent Changes
4 *
5 * @param {mw.rcfilters.dm.FiltersViewModel} model View model
6 */
7 mw.rcfilters.Controller = function MwRcfiltersController( model ) {
8 this.model = model;
9 // TODO: When we are ready, update the URL when a filter is updated
10 // this.model.connect( this, { itemUpdate: 'updateURL' } );
11 };
12
13 /* Initialization */
14 OO.initClass( mw.rcfilters.Controller );
15
16 /**
17 * Initialize the filter and parameter states
18 */
19 mw.rcfilters.Controller.prototype.initialize = function () {
20 var uri = new mw.Uri();
21
22 // Give the model a full parameter state from which to
23 // update the filters
24 this.model.updateFilters(
25 // Translate the url params to filter select states
26 this.model.getFiltersFromParameters( uri.query )
27 );
28 };
29
30 /**
31 * Update the state of a filter
32 *
33 * @param {string} filterName Filter name
34 * @param {boolean} isSelected Filter selected state
35 */
36 mw.rcfilters.Controller.prototype.updateFilter = function ( filterName, isSelected ) {
37 var obj = {};
38
39 obj[ filterName ] = isSelected;
40 this.model.updateFilters( obj );
41 };
42
43 /**
44 * Update the URL of the page to reflect current filters
45 */
46 mw.rcfilters.Controller.prototype.updateURL = function () {
47 var uri = new mw.Uri();
48
49 // Add to existing queries in URL
50 // TODO: Clean up the list of filters; perhaps 'falsy' filters
51 // shouldn't appear at all? Or compare to existing query string
52 // and see if current state of a specific filter is needed?
53 uri.extend( this.model.getParametersFromFilters() );
54
55 // Update the URL itself
56 window.history.pushState( { tag: 'rcfilters' }, document.title, uri.toString() );
57 };
58 }( mediaWiki ) );