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