RCFilters: Remove redundant replaceUpdatedUrl method
[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 // Remove excluded params from the url
89 uri.query = this.filtersModel.removeExcludedParams( uri.query );
90
91 // Reapply unrecognized params and url version
92 uri.query = $.extend( true, {}, uri.query, unrecognizedParams, { urlversion: '2' } );
93
94 return uri;
95 };
96
97 /**
98 * Get an object representing given parameters that are unrecognized by the model
99 *
100 * @param {Object} params Full params object
101 * @return {Object} Unrecognized params
102 */
103 mw.rcfilters.UriProcessor.prototype.getUnrecognizedParams = function ( params ) {
104 // Start with full representation
105 var givenParamNames = Object.keys( params ),
106 unrecognizedParams = $.extend( true, {}, params );
107
108 // Extract unrecognized parameters
109 Object.keys( this.filtersModel.getEmptyParameterState() ).forEach( function ( paramName ) {
110 // Remove recognized params
111 if ( givenParamNames.indexOf( paramName ) > -1 ) {
112 delete unrecognizedParams[ paramName ];
113 }
114 } );
115
116 return unrecognizedParams;
117 };
118
119 /**
120 * Update the URL of the page to reflect current filters
121 *
122 * This should not be called directly from outside the controller.
123 * If an action requires changing the URL, it should either use the
124 * highlighting actions below, or call #updateChangesList which does
125 * the uri corrections already.
126 *
127 * @param {Object} [params] Extra parameters to add to the API call
128 */
129 mw.rcfilters.UriProcessor.prototype.updateURL = function ( params ) {
130 var currentUri = new mw.Uri(),
131 updatedUri = this.getUpdatedUri();
132
133 updatedUri.extend( params || {} );
134
135 if (
136 this.getVersion( currentUri.query ) !== 2 ||
137 this.isNewState( currentUri.query, updatedUri.query )
138 ) {
139 this.constructor.static.replaceState( updatedUri );
140 }
141 };
142
143 /**
144 * Update the filters model based on the URI query
145 * This happens on initialization, and from this moment on,
146 * we consider the system synchronized, and the model serves
147 * as the source of truth for the URL.
148 *
149 * This methods should only be called once on initialiation.
150 * After initialization, the model updates the URL, not the
151 * other way around.
152 *
153 * @param {Object} [uriQuery] URI query
154 */
155 mw.rcfilters.UriProcessor.prototype.updateModelBasedOnQuery = function ( uriQuery ) {
156 this.filtersModel.updateStateFromParams(
157 this._getNormalizedQueryParams( uriQuery || new mw.Uri().query )
158 );
159 };
160
161 /**
162 * Compare two URI queries to decide whether they are different
163 * enough to represent a new state.
164 *
165 * @param {Object} currentUriQuery Current Uri query
166 * @param {Object} updatedUriQuery Updated Uri query
167 * @return {boolean} This is a new state
168 */
169 mw.rcfilters.UriProcessor.prototype.isNewState = function ( currentUriQuery, updatedUriQuery ) {
170 var currentParamState, updatedParamState,
171 notEquivalent = function ( obj1, obj2 ) {
172 var keys = Object.keys( obj1 ).concat( Object.keys( obj2 ) );
173 return keys.some( function ( key ) {
174 return obj1[ key ] != obj2[ key ]; // eslint-disable-line eqeqeq
175 } );
176 };
177
178 // Compare states instead of parameters
179 // This will allow us to always have a proper check of whether
180 // the requested new url is one to change or not, regardless of
181 // actual parameter visibility/representation in the URL
182 currentParamState = $.extend(
183 true,
184 {},
185 this.filtersModel.getMinimizedParamRepresentation( currentUriQuery ),
186 this.getUnrecognizedParams( currentUriQuery )
187 );
188 updatedParamState = $.extend(
189 true,
190 {},
191 this.filtersModel.getMinimizedParamRepresentation( updatedUriQuery ),
192 this.getUnrecognizedParams( updatedUriQuery )
193 );
194
195 return notEquivalent( currentParamState, updatedParamState );
196 };
197
198 /**
199 * Check whether the given query has parameters that are
200 * recognized as parameters we should load the system with
201 *
202 * @param {mw.Uri} [uriQuery] Given URI query
203 * @return {boolean} Query contains valid recognized parameters
204 */
205 mw.rcfilters.UriProcessor.prototype.doesQueryContainRecognizedParams = function ( uriQuery ) {
206 var anyValidInUrl,
207 validParameterNames = Object.keys( this.filtersModel.getEmptyParameterState() );
208
209 uriQuery = uriQuery || new mw.Uri().query;
210
211 anyValidInUrl = Object.keys( uriQuery ).some( function ( parameter ) {
212 return validParameterNames.indexOf( parameter ) > -1;
213 } );
214
215 // URL version 2 is allowed to be empty or within nonrecognized params
216 return anyValidInUrl || this.getVersion( uriQuery ) === 2;
217 };
218
219 /**
220 * Get the adjusted URI params based on the url version
221 * If the urlversion is not 2, the parameters are merged with
222 * the model's defaults.
223 * Always merge in the hidden parameter defaults.
224 *
225 * @private
226 * @param {Object} uriQuery Current URI query
227 * @return {Object} Normalized parameters
228 */
229 mw.rcfilters.UriProcessor.prototype._getNormalizedQueryParams = function ( uriQuery ) {
230 // Check whether we are dealing with urlversion=2
231 // If we are, we do not merge the initial request with
232 // defaults. Not having urlversion=2 means we need to
233 // reproduce the server-side request and merge the
234 // requested parameters (or starting state) with the
235 // wiki default.
236 // Any subsequent change of the URL through the RCFilters
237 // system will receive 'urlversion=2'
238 var hiddenParamDefaults = this.filtersModel.getDefaultHiddenParams(),
239 base = this.getVersion( uriQuery ) === 2 ?
240 {} :
241 this.filtersModel.getDefaultParams();
242
243 return $.extend(
244 true,
245 {},
246 this.filtersModel.getMinimizedParamRepresentation(
247 $.extend( true, {}, hiddenParamDefaults, base, uriQuery )
248 ),
249 { urlversion: '2' }
250 );
251 };
252 }( mediaWiki, jQuery ) );