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