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 // Reapply unrecognized params and url version
89 uri
.query
= $.extend( true, {}, uri
.query
, unrecognizedParams
, { urlversion
: '2' } );
95 * Get an object representing given parameters that are unrecognized by the model
97 * @param {Object} params Full params object
98 * @return {Object} Unrecognized params
100 mw
.rcfilters
.UriProcessor
.prototype.getUnrecognizedParams = function ( params
) {
101 // Start with full representation
102 var givenParamNames
= Object
.keys( params
),
103 unrecognizedParams
= $.extend( true, {}, params
);
105 // Extract unrecognized parameters
106 Object
.keys( this.filtersModel
.getEmptyParameterState() ).forEach( function ( paramName
) {
107 // Remove recognized params
108 if ( givenParamNames
.indexOf( paramName
) > -1 ) {
109 delete unrecognizedParams
[ paramName
];
113 return unrecognizedParams
;
117 * Update the URL of the page to reflect current filters
119 * This should not be called directly from outside the controller.
120 * If an action requires changing the URL, it should either use the
121 * highlighting actions below, or call #updateChangesList which does
122 * the uri corrections already.
124 * @param {Object} [params] Extra parameters to add to the API call
126 mw
.rcfilters
.UriProcessor
.prototype.updateURL = function ( params
) {
127 var currentUri
= new mw
.Uri(),
128 updatedUri
= this.getUpdatedUri();
130 updatedUri
.extend( params
|| {} );
133 this.getVersion( currentUri
.query
) !== 2 ||
134 this.isNewState( currentUri
.query
, updatedUri
.query
)
136 this.constructor.static.replaceState( updatedUri
);
141 * Update the filters model based on the URI query
142 * This happens on initialization, and from this moment on,
143 * we consider the system synchronized, and the model serves
144 * as the source of truth for the URL.
146 * This methods should only be called once on initialiation.
147 * After initialization, the model updates the URL, not the
150 * @param {Object} [uriQuery] URI query
152 mw
.rcfilters
.UriProcessor
.prototype.updateModelBasedOnQuery = function ( uriQuery
) {
153 this.filtersModel
.updateStateFromParams(
154 this._getNormalizedQueryParams( uriQuery
|| new mw
.Uri().query
)
159 * Compare two URI queries to decide whether they are different
160 * enough to represent a new state.
162 * @param {Object} currentUriQuery Current Uri query
163 * @param {Object} updatedUriQuery Updated Uri query
164 * @return {boolean} This is a new state
166 mw
.rcfilters
.UriProcessor
.prototype.isNewState = function ( currentUriQuery
, updatedUriQuery
) {
167 var currentParamState
, updatedParamState
,
168 notEquivalent = function ( obj1
, obj2
) {
169 var keys
= Object
.keys( obj1
).concat( Object
.keys( obj2
) );
170 return keys
.some( function ( key
) {
171 return obj1
[ key
] != obj2
[ key
]; // eslint-disable-line eqeqeq
175 // Compare states instead of parameters
176 // This will allow us to always have a proper check of whether
177 // the requested new url is one to change or not, regardless of
178 // actual parameter visibility/representation in the URL
179 currentParamState
= $.extend(
182 this.filtersModel
.getMinimizedParamRepresentation( currentUriQuery
),
183 this.getUnrecognizedParams( currentUriQuery
)
185 updatedParamState
= $.extend(
188 this.filtersModel
.getMinimizedParamRepresentation( updatedUriQuery
),
189 this.getUnrecognizedParams( updatedUriQuery
)
192 return notEquivalent( currentParamState
, updatedParamState
);
196 * Check whether the given query has parameters that are
197 * recognized as parameters we should load the system with
199 * @param {mw.Uri} [uriQuery] Given URI query
200 * @return {boolean} Query contains valid recognized parameters
202 mw
.rcfilters
.UriProcessor
.prototype.doesQueryContainRecognizedParams = function ( uriQuery
) {
204 validParameterNames
= Object
.keys( this.filtersModel
.getEmptyParameterState() );
206 uriQuery
= uriQuery
|| new mw
.Uri().query
;
208 anyValidInUrl
= Object
.keys( uriQuery
).some( function ( parameter
) {
209 return validParameterNames
.indexOf( parameter
) > -1;
212 // URL version 2 is allowed to be empty or within nonrecognized params
213 return anyValidInUrl
|| this.getVersion( uriQuery
) === 2;
217 * Get the adjusted URI params based on the url version
218 * If the urlversion is not 2, the parameters are merged with
219 * the model's defaults.
220 * Always merge in the hidden parameter defaults.
223 * @param {Object} uriQuery Current URI query
224 * @return {Object} Normalized parameters
226 mw
.rcfilters
.UriProcessor
.prototype._getNormalizedQueryParams = function ( uriQuery
) {
227 // Check whether we are dealing with urlversion=2
228 // If we are, we do not merge the initial request with
229 // defaults. Not having urlversion=2 means we need to
230 // reproduce the server-side request and merge the
231 // requested parameters (or starting state) with the
233 // Any subsequent change of the URL through the RCFilters
234 // system will receive 'urlversion=2'
235 var hiddenParamDefaults
= this.filtersModel
.getDefaultHiddenParams(),
236 base
= this.getVersion( uriQuery
) === 2 ?
238 this.filtersModel
.getDefaultParams();
243 this.filtersModel
.getMinimizedParamRepresentation(
244 $.extend( true, {}, hiddenParamDefaults
, base
, uriQuery
)
249 }( mediaWiki
, jQuery
) );