2 /* eslint no-underscore-dangle: "off" */
4 * URI Processor for RCFilters
6 * @param {mw.rcfilters.dm.FiltersViewModel} filtersModel Filters view model
8 mw
.rcfilters
.UriProcessor
= function MwRcfiltersController( filtersModel
) {
9 this.filtersModel
= filtersModel
;
13 OO
.initClass( mw
.rcfilters
.UriProcessor
);
18 * Replace the url history through replaceState
20 * @param {mw.Uri} newUri New URI to replace
22 mw
.rcfilters
.UriProcessor
.static.replaceState = function ( newUri
) {
23 window
.history
.replaceState(
31 * Push the url to history through pushState
33 * @param {mw.Uri} newUri New URI to push
35 mw
.rcfilters
.UriProcessor
.static.pushState = function ( newUri
) {
36 window
.history
.pushState(
46 * Get the version that this URL query is tagged with.
48 * @param {Object} [uriQuery] URI query
49 * @return {number} URL version
51 mw
.rcfilters
.UriProcessor
.prototype.getVersion = function ( uriQuery
) {
52 uriQuery
= uriQuery
|| new mw
.Uri().query
;
54 return Number( uriQuery
.urlversion
|| 1 );
58 * Get an updated mw.Uri object based on the model state
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
65 mw
.rcfilters
.UriProcessor
.prototype.getUpdatedUri = function ( uriQuery
) {
66 var uri
= new mw
.Uri(),
67 unrecognizedParams
= this.getUnrecognizedParams( uriQuery
|| uri
.query
);
70 // This is mainly for tests, to be able to give the method
71 // an initial URI Query and test that it retains parameters
75 uri
.query
= this.filtersModel
.getMinimizedParamRepresentation(
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
84 this.filtersModel
.getExpandedParamRepresentation()
88 // Remove excluded params from the url
89 uri
.query
= this.filtersModel
.removeExcludedParams( uri
.query
);
91 // Reapply unrecognized params and url version
92 uri
.query
= $.extend( true, {}, uri
.query
, unrecognizedParams
, { urlversion
: '2' } );
98 * Get an object representing given parameters that are unrecognized by the model
100 * @param {Object} params Full params object
101 * @return {Object} Unrecognized params
103 mw
.rcfilters
.UriProcessor
.prototype.getUnrecognizedParams = function ( params
) {
104 // Start with full representation
105 var givenParamNames
= Object
.keys( params
),
106 unrecognizedParams
= $.extend( true, {}, params
);
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
];
116 return unrecognizedParams
;
120 * Update the URL of the page to reflect current filters
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.
127 * @param {Object} [params] Extra parameters to add to the API call
129 mw
.rcfilters
.UriProcessor
.prototype.updateURL = function ( params
) {
130 var currentUri
= new mw
.Uri(),
131 updatedUri
= this.getUpdatedUri();
133 updatedUri
.extend( params
|| {} );
136 this.getVersion( currentUri
.query
) !== 2 ||
137 this.isNewState( currentUri
.query
, updatedUri
.query
)
139 this.constructor.static.replaceState( updatedUri
);
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.
149 * This methods should only be called once on initialiation.
150 * After initialization, the model updates the URL, not the
153 * @param {Object} [uriQuery] URI query
155 mw
.rcfilters
.UriProcessor
.prototype.updateModelBasedOnQuery = function ( uriQuery
) {
156 this.filtersModel
.updateStateFromParams(
157 this._getNormalizedQueryParams( uriQuery
|| new mw
.Uri().query
)
162 * Compare two URI queries to decide whether they are different
163 * enough to represent a new state.
165 * @param {Object} currentUriQuery Current Uri query
166 * @param {Object} updatedUriQuery Updated Uri query
167 * @return {boolean} This is a new state
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
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(
185 this.filtersModel
.getMinimizedParamRepresentation( currentUriQuery
),
186 this.getUnrecognizedParams( currentUriQuery
)
188 updatedParamState
= $.extend(
191 this.filtersModel
.getMinimizedParamRepresentation( updatedUriQuery
),
192 this.getUnrecognizedParams( updatedUriQuery
)
195 return notEquivalent( currentParamState
, updatedParamState
);
199 * Check whether the given query has parameters that are
200 * recognized as parameters we should load the system with
202 * @param {mw.Uri} [uriQuery] Given URI query
203 * @return {boolean} Query contains valid recognized parameters
205 mw
.rcfilters
.UriProcessor
.prototype.doesQueryContainRecognizedParams = function ( uriQuery
) {
207 validParameterNames
= Object
.keys( this.filtersModel
.getEmptyParameterState() );
209 uriQuery
= uriQuery
|| new mw
.Uri().query
;
211 anyValidInUrl
= Object
.keys( uriQuery
).some( function ( parameter
) {
212 return validParameterNames
.indexOf( parameter
) > -1;
215 // URL version 2 is allowed to be empty or within nonrecognized params
216 return anyValidInUrl
|| this.getVersion( uriQuery
) === 2;
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.
226 * @param {Object} uriQuery Current URI query
227 * @return {Object} Normalized parameters
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
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 ?
241 this.filtersModel
.getDefaultParams();
246 this.filtersModel
.getMinimizedParamRepresentation(
247 $.extend( true, {}, hiddenParamDefaults
, base
, uriQuery
)
252 }( mediaWiki
, jQuery
) );