Merge "EditPage: Drop 80% width restriction on wpSummary for OOUI form"
[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.emptyParameterState = {};
10 this.filtersModel = filtersModel;
11
12 // Initialize
13 this._buildEmptyParameterState();
14 };
15
16 /* Initialization */
17 OO.initClass( mw.rcfilters.UriProcessor );
18
19 /* Static methods */
20
21 /**
22 * Replace the url history through replaceState
23 *
24 * @param {mw.Uri} newUri New URI to replace
25 */
26 mw.rcfilters.UriProcessor.static.replaceState = function ( newUri ) {
27 window.history.replaceState(
28 { tag: 'rcfilters' },
29 document.title,
30 newUri.toString()
31 );
32 };
33
34 /**
35 * Push the url to history through pushState
36 *
37 * @param {mw.Uri} newUri New URI to push
38 */
39 mw.rcfilters.UriProcessor.static.pushState = function ( newUri ) {
40 window.history.pushState(
41 { tag: 'rcfilters' },
42 document.title,
43 newUri.toString()
44 );
45 };
46
47 /* Methods */
48
49 /**
50 * Get the version that this URL query is tagged with.
51 *
52 * @param {Object} [uriQuery] URI query
53 * @return {number} URL version
54 */
55 mw.rcfilters.UriProcessor.prototype.getVersion = function ( uriQuery ) {
56 uriQuery = uriQuery || new mw.Uri().query;
57
58 return Number( uriQuery.urlversion || 1 );
59 };
60
61 /**
62 * Update the filters model based on the URI query
63 * This happens on initialization, and from this moment on,
64 * we consider the system synchronized, and the model serves
65 * as the source of truth for the URL.
66 *
67 * This methods should only be called once on initialiation.
68 * After initialization, the model updates the URL, not the
69 * other way around.
70 *
71 * @param {Object} [uriQuery] URI query
72 */
73 mw.rcfilters.UriProcessor.prototype.updateModelBasedOnQuery = function ( uriQuery ) {
74 var parameters = this._getNormalizedQueryParams( uriQuery || new mw.Uri().query );
75
76 // Update filter states
77 this.filtersModel.toggleFiltersSelected(
78 this.filtersModel.getFiltersFromParameters(
79 parameters
80 )
81 );
82
83 this.filtersModel.toggleInvertedNamespaces( !!Number( parameters.invert ) );
84
85 // Update highlight state
86 this.filtersModel.toggleHighlight( !!Number( parameters.highlight ) );
87 this.filtersModel.getItems().forEach( function ( filterItem ) {
88 var color = parameters[ filterItem.getName() + '_color' ];
89 if ( color ) {
90 filterItem.setHighlightColor( color );
91 } else {
92 filterItem.clearHighlightColor();
93 }
94 } );
95
96 // Check all filter interactions
97 this.filtersModel.reassessFilterInteractions();
98 };
99
100 /**
101 * Get parameters representing the current state of the model
102 *
103 * @return {Object} Uri query parameters
104 */
105 mw.rcfilters.UriProcessor.prototype.getUriParametersFromModel = function () {
106 return $.extend(
107 true,
108 {},
109 this.filtersModel.getParametersFromFilters(),
110 this.filtersModel.getHighlightParameters(),
111 {
112 highlight: String( Number( this.filtersModel.isHighlightEnabled() ) ),
113 invert: String( Number( this.filtersModel.areNamespacesInverted() ) )
114 }
115 );
116 };
117
118 /**
119 * Build the full parameter representation based on given query parameters
120 *
121 * @private
122 * @param {Object} uriQuery Given URI query
123 * @return {Object} Full parameter state representing the URI query
124 */
125 mw.rcfilters.UriProcessor.prototype._expandModelParameters = function ( uriQuery ) {
126 var filterRepresentation = this.filtersModel.getFiltersFromParameters( uriQuery );
127
128 return $.extend( true,
129 {},
130 uriQuery,
131 this.filtersModel.getParametersFromFilters( filterRepresentation ),
132 this.filtersModel.extractHighlightValues( uriQuery ),
133 {
134 highlight: String( Number( uriQuery.highlight ) ),
135 invert: String( Number( uriQuery.invert ) )
136 }
137 );
138 };
139
140 /**
141 * Compare two URI queries to decide whether they are different
142 * enough to represent a new state.
143 *
144 * @param {Object} currentUriQuery Current Uri query
145 * @param {Object} updatedUriQuery Updated Uri query
146 * @return {boolean} This is a new state
147 */
148 mw.rcfilters.UriProcessor.prototype.isNewState = function ( currentUriQuery, updatedUriQuery ) {
149 var currentParamState, updatedParamState,
150 notEquivalent = function ( obj1, obj2 ) {
151 var keys = Object.keys( obj1 ).concat( Object.keys( obj2 ) );
152 return keys.some( function ( key ) {
153 return obj1[ key ] != obj2[ key ]; // eslint-disable-line eqeqeq
154 } );
155 };
156
157 // Compare states instead of parameters
158 // This will allow us to always have a proper check of whether
159 // the requested new url is one to change or not, regardless of
160 // actual parameter visibility/representation in the URL
161 currentParamState = this._expandModelParameters( currentUriQuery );
162 updatedParamState = this._expandModelParameters( updatedUriQuery );
163
164 return notEquivalent( currentParamState, updatedParamState );
165 };
166
167 /**
168 * Check whether the given query has parameters that are
169 * recognized as parameters we should load the system with
170 *
171 * @param {mw.Uri} [uriQuery] Given URI query
172 * @return {boolean} Query contains valid recognized parameters
173 */
174 mw.rcfilters.UriProcessor.prototype.doesQueryContainRecognizedParams = function ( uriQuery ) {
175 var anyValidInUrl,
176 validParameterNames = Object.keys( this._getEmptyParameterState() )
177 .filter( function ( param ) {
178 // Remove 'highlight' parameter from this check;
179 // if it's the only parameter in the URL we still
180 // want to consider the URL 'empty' for defaults to load
181 return param !== 'highlight';
182 } );
183
184 uriQuery = uriQuery || new mw.Uri().query;
185
186 anyValidInUrl = Object.keys( uriQuery ).some( function ( parameter ) {
187 return validParameterNames.indexOf( parameter ) > -1;
188 } );
189
190 // URL version 2 is allowed to be empty or within nonrecognized params
191 return anyValidInUrl || this.getVersion( uriQuery ) === 2;
192 };
193
194 /**
195 * Remove all parameters that have the same value as the base state
196 * This method expects uri queries of the urlversion=2 format
197 *
198 * @private
199 * @param {Object} uriQuery Current uri query
200 * @return {Object} Minimized query
201 */
202 mw.rcfilters.UriProcessor.prototype.minimizeQuery = function ( uriQuery ) {
203 var baseParams = this._getEmptyParameterState(),
204 uriResult = $.extend( true, {}, uriQuery );
205
206 $.each( uriResult, function ( paramName, paramValue ) {
207 if (
208 baseParams[ paramName ] !== undefined &&
209 baseParams[ paramName ] === paramValue
210 ) {
211 // Remove parameter from query
212 delete uriResult[ paramName ];
213 }
214 } );
215
216 return uriResult;
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 *
224 * @private
225 * @param {Object} uriQuery Current URI query
226 * @return {Object} Normalized parameters
227 */
228 mw.rcfilters.UriProcessor.prototype._getNormalizedQueryParams = function ( uriQuery ) {
229 // Check whether we are dealing with urlversion=2
230 // If we are, we do not merge the initial request with
231 // defaults. Not having urlversion=2 means we need to
232 // reproduce the server-side request and merge the
233 // requested parameters (or starting state) with the
234 // wiki default.
235 // Any subsequent change of the URL through the RCFilters
236 // system will receive 'urlversion=2'
237 var hiddenParamDefaults = {},
238 base = this.getVersion( uriQuery ) === 2 ?
239 {} :
240 this.filtersModel.getDefaultParams();
241
242 // Go over the model and get all hidden parameters' defaults
243 // These defaults should be applied regardless of the urlversion
244 // but be overridden by the URL params if they exist
245 $.each( this.filtersModel.getFilterGroups(), function ( groupName, groupModel ) {
246 if ( groupModel.isHidden() ) {
247 $.extend( true, hiddenParamDefaults, groupModel.getDefaultParams() );
248 }
249 } );
250
251 return this.minimizeQuery(
252 $.extend( true, {}, hiddenParamDefaults, base, uriQuery, { urlversion: '2' } )
253 );
254 };
255
256 /**
257 * Get the representation of an empty parameter state
258 *
259 * @private
260 * @return {Object} Empty parameter state
261 */
262 mw.rcfilters.UriProcessor.prototype._getEmptyParameterState = function () {
263 // Override empty parameter state with the sticky parameter values
264 return $.extend( true, {}, this.emptyParameterState, this.filtersModel.getStickyParams() );
265 };
266
267 /**
268 * Build an empty representation of the parameters, where all parameters
269 * are either set to '0' or '' depending on their type.
270 * This must run during initialization, before highlights are set.
271 *
272 * @private
273 */
274 mw.rcfilters.UriProcessor.prototype._buildEmptyParameterState = function () {
275 var emptyParams = this.filtersModel.getParametersFromFilters( {} ),
276 emptyHighlights = this.filtersModel.getHighlightParameters();
277
278 this.emptyParameterState = $.extend(
279 true,
280 {},
281 emptyParams,
282 emptyHighlights,
283 { highlight: '0', invert: '0' }
284 );
285 };
286 }( mediaWiki, jQuery ) );