Merge "Preferences: Remove unwise caching of Preferences::getPreferences()"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / mw.rcfilters.UriProcessor.js
1 ( function ( mw, $ ) {
2 /* eslint no-underscore-dangle: "off" */
3 /**
4 * URI Processor for RCFilters
5 *
6 * @param {mw.rcfilters.dm.FiltersViewModel} filtersModel Filters view model
7 */
8 mw.rcfilters.UriProcessor = function MwRcfiltersController( filtersModel ) {
9 this.filtersModel = filtersModel;
10 };
11
12 /* Initialization */
13 OO.initClass( mw.rcfilters.UriProcessor );
14
15 /* Static methods */
16
17 /**
18 * Replace the url history through replaceState
19 *
20 * @param {mw.Uri} newUri New URI to replace
21 */
22 mw.rcfilters.UriProcessor.static.replaceState = function ( newUri ) {
23 window.history.replaceState(
24 { tag: 'rcfilters' },
25 document.title,
26 newUri.toString()
27 );
28 };
29
30 /**
31 * Push the url to history through pushState
32 *
33 * @param {mw.Uri} newUri New URI to push
34 */
35 mw.rcfilters.UriProcessor.static.pushState = function ( newUri ) {
36 window.history.pushState(
37 { tag: 'rcfilters' },
38 document.title,
39 newUri.toString()
40 );
41 };
42
43 /* Methods */
44
45 /**
46 * Get the version that this URL query is tagged with.
47 *
48 * @param {Object} [uriQuery] URI query
49 * @return {number} URL version
50 */
51 mw.rcfilters.UriProcessor.prototype.getVersion = function ( uriQuery ) {
52 uriQuery = uriQuery || new mw.Uri().query;
53
54 return Number( uriQuery.urlversion || 1 );
55 };
56
57 /**
58 * Get an updated mw.Uri object based on the model state
59 *
60 * @param {Object} [uriQuery] An external URI query to build the new uri
61 * with. This is mainly for tests, to be able to supply external parameters
62 * and make sure they are retained.
63 * @return {mw.Uri} Updated Uri
64 */
65 mw.rcfilters.UriProcessor.prototype.getUpdatedUri = function ( uriQuery ) {
66 var uri = new mw.Uri(),
67 unrecognizedParams = this.getUnrecognizedParams( uriQuery || uri.query );
68
69 if ( uriQuery ) {
70 // This is mainly for tests, to be able to give the method
71 // an initial URI Query and test that it retains parameters
72 uri.query = uriQuery;
73 }
74
75 uri.query = this.filtersModel.getMinimizedParamRepresentation(
76 $.extend(
77 true,
78 {},
79 uri.query,
80 // The representation must be expanded so it can
81 // override the uri query params but we then output
82 // a minimized version for the entire URI representation
83 // for the method
84 this.filtersModel.getExpandedParamRepresentation()
85 )
86 );
87
88 // Reapply unrecognized params and url version
89 uri.query = $.extend( true, {}, uri.query, unrecognizedParams, { urlversion: '2' } );
90
91 return uri;
92 };
93
94 /**
95 * Get an object representing given parameters that are unrecognized by the model
96 *
97 * @param {Object} params Full params object
98 * @return {Object} Unrecognized params
99 */
100 mw.rcfilters.UriProcessor.prototype.getUnrecognizedParams = function ( params ) {
101 // Start with full representation
102 var givenParamNames = Object.keys( params ),
103 unrecognizedParams = $.extend( true, {}, params );
104
105 // Extract unrecognized parameters
106 Object.keys( this.filtersModel.getEmptyParameterState() ).forEach( function ( paramName ) {
107 // Remove recognized params
108 if ( givenParamNames.indexOf( paramName ) > -1 ) {
109 delete unrecognizedParams[ paramName ];
110 }
111 } );
112
113 return unrecognizedParams;
114 };
115
116 /**
117 * Update the URL of the page to reflect current filters
118 *
119 * This should not be called directly from outside the controller.
120 * If an action requires changing the URL, it should either use the
121 * highlighting actions below, or call #updateChangesList which does
122 * the uri corrections already.
123 *
124 * @param {Object} [params] Extra parameters to add to the API call
125 */
126 mw.rcfilters.UriProcessor.prototype.updateURL = function ( params ) {
127 var currentUri = new mw.Uri(),
128 updatedUri = this.getUpdatedUri();
129
130 updatedUri.extend( params || {} );
131
132 if (
133 this.getVersion( currentUri.query ) !== 2 ||
134 this.isNewState( currentUri.query, updatedUri.query )
135 ) {
136 this.constructor.static.replaceState( updatedUri );
137 }
138 };
139
140 /**
141 * Update the filters model based on the URI query
142 * This happens on initialization, and from this moment on,
143 * we consider the system synchronized, and the model serves
144 * as the source of truth for the URL.
145 *
146 * This methods should only be called once on initialiation.
147 * After initialization, the model updates the URL, not the
148 * other way around.
149 *
150 * @param {Object} [uriQuery] URI query
151 */
152 mw.rcfilters.UriProcessor.prototype.updateModelBasedOnQuery = function ( uriQuery ) {
153 this.filtersModel.updateStateFromParams(
154 this._getNormalizedQueryParams( uriQuery || new mw.Uri().query )
155 );
156 };
157
158 /**
159 * Compare two URI queries to decide whether they are different
160 * enough to represent a new state.
161 *
162 * @param {Object} currentUriQuery Current Uri query
163 * @param {Object} updatedUriQuery Updated Uri query
164 * @return {boolean} This is a new state
165 */
166 mw.rcfilters.UriProcessor.prototype.isNewState = function ( currentUriQuery, updatedUriQuery ) {
167 var currentParamState, updatedParamState,
168 notEquivalent = function ( obj1, obj2 ) {
169 var keys = Object.keys( obj1 ).concat( Object.keys( obj2 ) );
170 return keys.some( function ( key ) {
171 return obj1[ key ] != obj2[ key ]; // eslint-disable-line eqeqeq
172 } );
173 };
174
175 // Compare states instead of parameters
176 // This will allow us to always have a proper check of whether
177 // the requested new url is one to change or not, regardless of
178 // actual parameter visibility/representation in the URL
179 currentParamState = $.extend(
180 true,
181 {},
182 this.filtersModel.getMinimizedParamRepresentation( currentUriQuery ),
183 this.getUnrecognizedParams( currentUriQuery )
184 );
185 updatedParamState = $.extend(
186 true,
187 {},
188 this.filtersModel.getMinimizedParamRepresentation( updatedUriQuery ),
189 this.getUnrecognizedParams( updatedUriQuery )
190 );
191
192 return notEquivalent( currentParamState, updatedParamState );
193 };
194
195 /**
196 * Check whether the given query has parameters that are
197 * recognized as parameters we should load the system with
198 *
199 * @param {mw.Uri} [uriQuery] Given URI query
200 * @return {boolean} Query contains valid recognized parameters
201 */
202 mw.rcfilters.UriProcessor.prototype.doesQueryContainRecognizedParams = function ( uriQuery ) {
203 var anyValidInUrl,
204 validParameterNames = Object.keys( this.filtersModel.getEmptyParameterState() );
205
206 uriQuery = uriQuery || new mw.Uri().query;
207
208 anyValidInUrl = Object.keys( uriQuery ).some( function ( parameter ) {
209 return validParameterNames.indexOf( parameter ) > -1;
210 } );
211
212 // URL version 2 is allowed to be empty or within nonrecognized params
213 return anyValidInUrl || this.getVersion( uriQuery ) === 2;
214 };
215
216 /**
217 * Get the adjusted URI params based on the url version
218 * If the urlversion is not 2, the parameters are merged with
219 * the model's defaults.
220 * Always merge in the hidden parameter defaults.
221 *
222 * @private
223 * @param {Object} uriQuery Current URI query
224 * @return {Object} Normalized parameters
225 */
226 mw.rcfilters.UriProcessor.prototype._getNormalizedQueryParams = function ( uriQuery ) {
227 // Check whether we are dealing with urlversion=2
228 // If we are, we do not merge the initial request with
229 // defaults. Not having urlversion=2 means we need to
230 // reproduce the server-side request and merge the
231 // requested parameters (or starting state) with the
232 // wiki default.
233 // Any subsequent change of the URL through the RCFilters
234 // system will receive 'urlversion=2'
235 var hiddenParamDefaults = this.filtersModel.getDefaultHiddenParams(),
236 base = this.getVersion( uriQuery ) === 2 ?
237 {} :
238 this.filtersModel.getDefaultParams();
239
240 return $.extend(
241 true,
242 {},
243 this.filtersModel.getMinimizedParamRepresentation(
244 $.extend( true, {}, hiddenParamDefaults, base, uriQuery )
245 ),
246 { urlversion: '2' }
247 );
248 };
249 }( mediaWiki, jQuery ) );