b4ea8af24b299ac461051446caf49c3803323dad
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.emptyParameterState
= {};
10 this.filtersModel
= filtersModel
;
13 this._buildEmptyParameterState();
17 OO
.initClass( mw
.rcfilters
.UriProcessor
);
22 * Replace the url history through replaceState
24 * @param {mw.Uri} newUri New URI to replace
26 mw
.rcfilters
.UriProcessor
.static.replaceState = function ( newUri
) {
27 window
.history
.replaceState(
35 * Push the url to history through pushState
37 * @param {mw.Uri} newUri New URI to push
39 mw
.rcfilters
.UriProcessor
.static.pushState = function ( newUri
) {
40 window
.history
.pushState(
50 * Get the version that this URL query is tagged with.
52 * @param {Object} [uriQuery] URI query
53 * @return {number} URL version
55 mw
.rcfilters
.UriProcessor
.prototype.getVersion = function ( uriQuery
) {
56 uriQuery
= uriQuery
|| new mw
.Uri().query
;
58 return Number( uriQuery
.urlversion
|| 1 );
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.
67 * This methods should only be called once on initialiation.
68 * After initialization, the model updates the URL, not the
71 * @param {Object} [uriQuery] URI query
73 mw
.rcfilters
.UriProcessor
.prototype.updateModelBasedOnQuery = function ( uriQuery
) {
74 var parameters
= this._getNormalizedQueryParams( uriQuery
|| new mw
.Uri().query
);
76 // Update filter states
77 this.filtersModel
.toggleFiltersSelected(
78 this.filtersModel
.getFiltersFromParameters(
83 this.filtersModel
.toggleInvertedNamespaces( !!Number( parameters
.invert
) );
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' ];
90 filterItem
.setHighlightColor( color
);
92 filterItem
.clearHighlightColor();
96 // Check all filter interactions
97 this.filtersModel
.reassessFilterInteractions();
101 * Get parameters representing the current state of the model
103 * @return {Object} Uri query parameters
105 mw
.rcfilters
.UriProcessor
.prototype.getUriParametersFromModel = function () {
109 this.filtersModel
.getParametersFromFilters(),
110 this.filtersModel
.getHighlightParameters(),
112 highlight
: String( Number( this.filtersModel
.isHighlightEnabled() ) ),
113 invert
: String( Number( this.filtersModel
.areNamespacesInverted() ) )
119 * Build the full parameter representation based on given query parameters
122 * @param {Object} uriQuery Given URI query
123 * @return {Object} Full parameter state representing the URI query
125 mw
.rcfilters
.UriProcessor
.prototype._expandModelParameters = function ( uriQuery
) {
126 var filterRepresentation
= this.filtersModel
.getFiltersFromParameters( uriQuery
);
128 return $.extend( true,
131 this.filtersModel
.getParametersFromFilters( filterRepresentation
),
132 this.filtersModel
.extractHighlightValues( uriQuery
),
134 highlight
: String( Number( uriQuery
.highlight
) ),
135 invert
: String( Number( uriQuery
.invert
) )
141 * Compare two URI queries to decide whether they are different
142 * enough to represent a new state.
144 * @param {Object} currentUriQuery Current Uri query
145 * @param {Object} updatedUriQuery Updated Uri query
146 * @return {boolean} This is a new state
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
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
);
164 return notEquivalent( currentParamState
, updatedParamState
);
168 * Check whether the given query has parameters that are
169 * recognized as parameters we should load the system with
171 * @param {mw.Uri} [uriQuery] Given URI query
172 * @return {boolean} Query contains valid recognized parameters
174 mw
.rcfilters
.UriProcessor
.prototype.doesQueryContainRecognizedParams = function ( uriQuery
) {
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';
184 uriQuery
= uriQuery
|| new mw
.Uri().query
;
186 anyValidInUrl
= Object
.keys( uriQuery
).some( function ( parameter
) {
187 return validParameterNames
.indexOf( parameter
) > -1;
190 // URL version 2 is allowed to be empty or within nonrecognized params
191 return anyValidInUrl
|| this.getVersion( uriQuery
) === 2;
195 * Remove all parameters that have the same value as the base state
196 * This method expects uri queries of the urlversion=2 format
199 * @param {Object} uriQuery Current uri query
200 * @return {Object} Minimized query
202 mw
.rcfilters
.UriProcessor
.prototype.minimizeQuery = function ( uriQuery
) {
203 var baseParams
= this._getEmptyParameterState(),
204 uriResult
= $.extend( true, {}, uriQuery
);
206 $.each( uriResult
, function ( paramName
, paramValue
) {
208 baseParams
[ paramName
] !== undefined &&
209 baseParams
[ paramName
] === paramValue
211 // Remove parameter from query
212 delete uriResult
[ paramName
];
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.
225 * @param {Object} uriQuery Current URI query
226 * @return {Object} Normalized parameters
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
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 ?
240 this.filtersModel
.getDefaultParams();
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() );
251 return this.minimizeQuery(
252 $.extend( true, {}, hiddenParamDefaults
, base
, uriQuery
, { urlversion
: '2' } )
257 * Get the representation of an empty parameter state
260 * @return {Object} Empty parameter state
262 mw
.rcfilters
.UriProcessor
.prototype._getEmptyParameterState = function () {
263 return this.emptyParameterState
;
267 * Build an empty representation of the parameters, where all parameters
268 * are either set to '0' or '' depending on their type.
269 * This must run during initialization, before highlights are set.
273 mw
.rcfilters
.UriProcessor
.prototype._buildEmptyParameterState = function () {
274 var emptyParams
= this.filtersModel
.getParametersFromFilters( {} ),
275 emptyHighlights
= this.filtersModel
.getHighlightParameters();
277 this.emptyParameterState
= $.extend(
282 { highlight
: '0', invert
: '0' }
285 }( mediaWiki
, jQuery
) );