/**
* URI Processor for RCFilters
*
+ * @class
+ *
+ * @constructor
* @param {mw.rcfilters.dm.FiltersViewModel} filtersModel Filters view model
+ * @param {Object} [config] Configuration object
+ * @cfg {boolean} [normalizeTarget] Dictates whether or not to go through the
+ * title normalization to separate title subpage/parts into the target= url
+ * parameter
*/
- mw.rcfilters.UriProcessor = function MwRcfiltersController( filtersModel ) {
+ mw.rcfilters.UriProcessor = function MwRcfiltersController( filtersModel, config ) {
+ config = config || {};
this.filtersModel = filtersModel;
+
+ this.normalizeTarget = !!config.normalizeTarget;
};
/* Initialization */
return Number( uriQuery.urlversion || 1 );
};
- /**
- * Replace the current URI with an updated one from the model state
- */
- mw.rcfilters.UriProcessor.prototype.replaceUpdatedUri = function () {
- this.constructor.static.replaceState( this.getUpdatedUri() );
- };
-
/**
* Get an updated mw.Uri object based on the model state
*
- * @param {Object} [uriQuery] An external URI query to build the new uri
- * with. This is mainly for tests, to be able to supply external parameters
- * and make sure they are retained.
+ * @param {mw.Uri} [uri] An external URI to build the new uri
+ * with. This is mainly for tests, to be able to supply external query
+ * parameters and make sure they are retained.
* @return {mw.Uri} Updated Uri
*/
- mw.rcfilters.UriProcessor.prototype.getUpdatedUri = function ( uriQuery ) {
- var uri = new mw.Uri(),
- unrecognizedParams = this.getUnrecognizedParams( uriQuery || uri.query );
-
- if ( uriQuery ) {
- // This is mainly for tests, to be able to give the method
- // an initial URI Query and test that it retains parameters
- uri.query = uriQuery;
- }
+ mw.rcfilters.UriProcessor.prototype.getUpdatedUri = function ( uri ) {
+ var normalizedUri = this._normalizeTargetInUri( uri || new mw.Uri() ),
+ unrecognizedParams = this.getUnrecognizedParams( normalizedUri.query );
- uri.query = this.filtersModel.getMinimizedParamRepresentation(
+ normalizedUri.query = this.filtersModel.getMinimizedParamRepresentation(
$.extend(
true,
{},
- uri.query,
+ normalizedUri.query,
// The representation must be expanded so it can
// override the uri query params but we then output
// a minimized version for the entire URI representation
);
// Reapply unrecognized params and url version
- uri.query = $.extend( true, {}, uri.query, unrecognizedParams, { urlversion: '2' } );
+ normalizedUri.query = $.extend(
+ true,
+ {},
+ normalizedUri.query,
+ unrecognizedParams,
+ { urlversion: '2' }
+ );
+
+ return normalizedUri;
+ };
+
+ /**
+ * Move the subpage to the target parameter
+ *
+ * @param {mw.Uri} uri
+ * @return {mw.Uri}
+ * @private
+ */
+ mw.rcfilters.UriProcessor.prototype._normalizeTargetInUri = function ( uri ) {
+ var parts,
+ // matches [/wiki/]SpecialNS:RCL/[Namespace:]Title/Subpage/Subsubpage/etc
+ re = /^((?:\/.+?\/)?.*?:.*?)\/(.*)$/;
+
+ if ( !this.normalizeTarget ) {
+ return uri;
+ }
+
+ // target in title param
+ if ( uri.query.title ) {
+ parts = uri.query.title.match( re );
+ if ( parts ) {
+ uri.query.title = parts[ 1 ];
+ uri.query.target = parts[ 2 ];
+ }
+ }
+
+ // target in path
+ parts = mw.Uri.decode( uri.path ).match( re );
+ if ( parts ) {
+ uri.path = parts[ 1 ];
+ uri.query.target = parts[ 2 ];
+ }
return uri;
};
* we consider the system synchronized, and the model serves
* as the source of truth for the URL.
*
- * This methods should only be called once on initialiation.
+ * This methods should only be called once on initialization.
* After initialization, the model updates the URL, not the
* other way around.
*
* @param {Object} [uriQuery] URI query
*/
mw.rcfilters.UriProcessor.prototype.updateModelBasedOnQuery = function ( uriQuery ) {
+ uriQuery = uriQuery || this._normalizeTargetInUri( new mw.Uri() ).query;
this.filtersModel.updateStateFromParams(
- this._getNormalizedQueryParams( uriQuery || new mw.Uri().query )
+ this._getNormalizedQueryParams( uriQuery )
);
};
*/
mw.rcfilters.UriProcessor.prototype.doesQueryContainRecognizedParams = function ( uriQuery ) {
var anyValidInUrl,
- validParameterNames = Object.keys( this.filtersModel.getEmptyParameterState() )
- .filter( function ( param ) {
- // Remove 'highlight' parameter from this check;
- // if it's the only parameter in the URL we still
- // want to consider the URL 'empty' for defaults to load
- return param !== 'highlight';
- } );
+ validParameterNames = Object.keys( this.filtersModel.getEmptyParameterState() );
uriQuery = uriQuery || new mw.Uri().query;
// wiki default.
// Any subsequent change of the URL through the RCFilters
// system will receive 'urlversion=2'
- var hiddenParamDefaults = this.filtersModel.getDefaultHiddenParams(),
- base = this.getVersion( uriQuery ) === 2 ?
- {} :
- this.filtersModel.getDefaultParams();
+ var base = this.getVersion( uriQuery ) === 2 ?
+ {} :
+ this.filtersModel.getDefaultParams();
return $.extend(
true,
{},
this.filtersModel.getMinimizedParamRepresentation(
- $.extend( true, {}, hiddenParamDefaults, base, uriQuery )
+ $.extend( true, {}, base, uriQuery )
),
{ urlversion: '2' }
);