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 {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
65 mw
.rcfilters
.UriProcessor
.prototype.getUpdatedUri = function ( uri
) {
66 var normalizedUri
= this._normalizeTargetInUri( uri
|| new mw
.Uri() ),
67 unrecognizedParams
= this.getUnrecognizedParams( normalizedUri
.query
);
69 normalizedUri
.query
= this.filtersModel
.getMinimizedParamRepresentation(
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
78 this.filtersModel
.getExpandedParamRepresentation()
82 // Reapply unrecognized params and url version
83 normalizedUri
.query
= $.extend(
95 * Move the subpage to the target parameter
101 mw
.rcfilters
.UriProcessor
.prototype._normalizeTargetInUri = function ( uri
) {
103 re
= /^((?:\/.+\/)?.+:.+)\/(.+)$/; // matches [namespace:]Title/Subpage
105 // target in title param
106 if ( uri
.query
.title
) {
107 parts
= uri
.query
.title
.match( re
);
109 uri
.query
.title
= parts
[ 1 ];
110 uri
.query
.target
= parts
[ 2 ];
115 parts
= uri
.path
.match( re
);
117 uri
.path
= parts
[ 1 ];
118 uri
.query
.target
= parts
[ 2 ];
125 * Get an object representing given parameters that are unrecognized by the model
127 * @param {Object} params Full params object
128 * @return {Object} Unrecognized params
130 mw
.rcfilters
.UriProcessor
.prototype.getUnrecognizedParams = function ( params
) {
131 // Start with full representation
132 var givenParamNames
= Object
.keys( params
),
133 unrecognizedParams
= $.extend( true, {}, params
);
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
];
143 return unrecognizedParams
;
147 * Update the URL of the page to reflect current filters
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.
154 * @param {Object} [params] Extra parameters to add to the API call
156 mw
.rcfilters
.UriProcessor
.prototype.updateURL = function ( params
) {
157 var currentUri
= new mw
.Uri(),
158 updatedUri
= this.getUpdatedUri();
160 updatedUri
.extend( params
|| {} );
163 this.getVersion( currentUri
.query
) !== 2 ||
164 this.isNewState( currentUri
.query
, updatedUri
.query
)
166 this.constructor.static.replaceState( updatedUri
);
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.
176 * This methods should only be called once on initialization.
177 * After initialization, the model updates the URL, not the
180 * @param {Object} [uriQuery] URI query
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
)
190 * Compare two URI queries to decide whether they are different
191 * enough to represent a new state.
193 * @param {Object} currentUriQuery Current Uri query
194 * @param {Object} updatedUriQuery Updated Uri query
195 * @return {boolean} This is a new state
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
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(
213 this.filtersModel
.getMinimizedParamRepresentation( currentUriQuery
),
214 this.getUnrecognizedParams( currentUriQuery
)
216 updatedParamState
= $.extend(
219 this.filtersModel
.getMinimizedParamRepresentation( updatedUriQuery
),
220 this.getUnrecognizedParams( updatedUriQuery
)
223 return notEquivalent( currentParamState
, updatedParamState
);
227 * Check whether the given query has parameters that are
228 * recognized as parameters we should load the system with
230 * @param {mw.Uri} [uriQuery] Given URI query
231 * @return {boolean} Query contains valid recognized parameters
233 mw
.rcfilters
.UriProcessor
.prototype.doesQueryContainRecognizedParams = function ( uriQuery
) {
235 validParameterNames
= Object
.keys( this.filtersModel
.getEmptyParameterState() );
237 uriQuery
= uriQuery
|| new mw
.Uri().query
;
239 anyValidInUrl
= Object
.keys( uriQuery
).some( function ( parameter
) {
240 return validParameterNames
.indexOf( parameter
) > -1;
243 // URL version 2 is allowed to be empty or within nonrecognized params
244 return anyValidInUrl
|| this.getVersion( uriQuery
) === 2;
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.
254 * @param {Object} uriQuery Current URI query
255 * @return {Object} Normalized parameters
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
264 // Any subsequent change of the URL through the RCFilters
265 // system will receive 'urlversion=2'
266 var base
= this.getVersion( uriQuery
) === 2 ?
268 this.filtersModel
.getDefaultParams();
273 this.filtersModel
.getMinimizedParamRepresentation(
274 $.extend( true, {}, base
, uriQuery
)
279 }( mediaWiki
, jQuery
) );