1 /* eslint-disable no-restricted-properties */
4 * View model for saved queries
7 * @mixins OO.EventEmitter
8 * @mixins OO.EmitterList
11 * @param {mw.rcfilters.dm.FiltersViewModel} filtersModel Filters model
12 * @param {Object} [config] Configuration options
13 * @cfg {string} [default] Default query ID
15 mw
.rcfilters
.dm
.SavedQueriesModel
= function MwRcfiltersDmSavedQueriesModel( filtersModel
, config
) {
16 config
= config
|| {};
19 OO
.EventEmitter
.call( this );
20 OO
.EmitterList
.call( this );
22 this.default = config
.default;
23 this.filtersModel
= filtersModel
;
24 this.converted
= false;
27 this.aggregate( { update
: 'itemUpdate' } );
32 OO
.initClass( mw
.rcfilters
.dm
.SavedQueriesModel
);
33 OO
.mixinClass( mw
.rcfilters
.dm
.SavedQueriesModel
, OO
.EventEmitter
);
34 OO
.mixinClass( mw
.rcfilters
.dm
.SavedQueriesModel
, OO
.EmitterList
);
41 * Model is initialized
46 * @param {mw.rcfilters.dm.SavedQueryItemModel} Changed item
53 * @param {string} New default ID
55 * The default has changed
61 * Initialize the saved queries model by reading it from the user's settings.
62 * The structure of the saved queries is:
64 * version: (string) Version number; if version 2, the query represents
65 * parameters. Otherwise, the older version represented filters
66 * and needs to be readjusted,
67 * default: (string) Query ID
71 * filters: (Object) Minimal definition of the filters
72 * highlights: (Object) Definition of the highlights
74 * label: (optional) Name of this query
79 * @param {Object} [savedQueries] An object with the saved queries with
80 * the above structure.
83 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.initialize = function ( savedQueries
) {
86 savedQueries
= savedQueries
|| {};
90 this.converted
= false;
92 if ( savedQueries
.version
!== '2' ) {
93 // Old version dealt with filter names. We need to migrate to the new structure
96 // version: (string) '2',
97 // default: (string) Query ID,
100 // label: (string) Name of the query
102 // params: (object) Representing all the parameter states
103 // highlights: (object) Representing all the filter highlight states
107 // eslint-disable-next-line jquery/no-each-util
108 $.each( savedQueries
.queries
|| {}, function ( id
, obj
) {
109 if ( obj
.data
&& obj
.data
.filters
) {
110 obj
.data
= model
.convertToParameters( obj
.data
);
114 this.converted
= true;
115 savedQueries
.version
= '2';
118 // Initialize the query items
119 // eslint-disable-next-line jquery/no-each-util
120 $.each( savedQueries
.queries
|| {}, function ( id
, obj
) {
121 var normalizedData
= obj
.data
,
122 isDefault
= String( savedQueries
.default ) === String( id
);
124 if ( normalizedData
&& normalizedData
.params
) {
125 // Backwards-compat fix: Remove sticky parameters from
126 // the given data, if they exist
127 normalizedData
.params
= model
.filtersModel
.removeStickyParams( normalizedData
.params
);
129 // Correct the invert state for effective selection
130 if ( normalizedData
.params
.invert
&& !normalizedData
.params
.namespace ) {
131 delete normalizedData
.params
.invert
;
134 model
.cleanupHighlights( normalizedData
);
138 // Skip the addNewQuery method because we don't want to unnecessarily manipulate
139 // the given saved queries unless we literally intend to (like in backwards compat fixes)
140 // And the addNewQuery method also uses a minimization routine that checks for the
141 // validity of items and minimizes the query. This isn't necessary for queries loaded
142 // from the backend, and has the risk of removing values if they're temporarily
143 // invalid (example: if we temporarily removed a cssClass from a filter in the backend)
145 new mw
.rcfilters
.dm
.SavedQueryItemModel(
149 { 'default': isDefault
}
159 this.emit( 'initialize' );
163 * Clean up highlight parameters.
164 * 'highlight' used to be stored, it's not inferred based on the presence of absence of
167 * @param {Object} data Saved query data
169 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.cleanupHighlights = function ( data
) {
171 data
.params
.highlight
=== '0' &&
172 data
.highlights
&& Object
.keys( data
.highlights
).length
174 data
.highlights
= {};
176 delete data
.params
.highlight
;
180 * Convert from representation of filters to representation of parameters
182 * @param {Object} data Query data
183 * @return {Object} New converted query data
185 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.convertToParameters = function ( data
) {
187 defaultFilters
= this.filtersModel
.getFiltersFromParameters( this.filtersModel
.getDefaultParams() ),
188 fullFilterRepresentation
= $.extend( true, {}, defaultFilters
, data
.filters
),
189 highlightEnabled
= data
.highlights
.highlight
;
191 delete data
.highlights
.highlight
;
194 newData
.params
= this.filtersModel
.getMinimizedParamRepresentation(
195 this.filtersModel
.getParametersFromFilters( fullFilterRepresentation
)
198 // Highlights: appending _color to keys
199 newData
.highlights
= {};
200 // eslint-disable-next-line jquery/no-each-util
201 $.each( data
.highlights
, function ( highlightedFilterName
, value
) {
203 newData
.highlights
[ highlightedFilterName
+ '_color' ] = data
.highlights
[ highlightedFilterName
];
208 newData
.params
.highlight
= String( Number( highlightEnabled
|| 0 ) );
216 * @param {string} label Label for the new query
217 * @param {Object} fulldata Full data representation for the new query, combining highlights and filters
218 * @param {boolean} isDefault Item is default
219 * @param {string} [id] Query ID, if exists. If this isn't given, a random
220 * new ID will be created.
221 * @return {string} ID of the newly added query
223 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.addNewQuery = function ( label
, fulldata
, isDefault
, id
) {
224 var normalizedData
= { params
: {}, highlights
: {} },
225 highlightParamNames
= Object
.keys( this.filtersModel
.getEmptyHighlightParameters() ),
226 randomID
= String( id
|| ( new Date() ).getTime() ),
227 data
= this.filtersModel
.getMinimizedParamRepresentation( fulldata
);
229 // Split highlight/params
230 // eslint-disable-next-line jquery/no-each-util
231 $.each( data
, function ( param
, value
) {
232 if ( param
!== 'highlight' && highlightParamNames
.indexOf( param
) > -1 ) {
233 normalizedData
.highlights
[ param
] = value
;
235 normalizedData
.params
[ param
] = value
;
239 // Correct the invert state for effective selection
240 if ( normalizedData
.params
.invert
&& !this.filtersModel
.areNamespacesEffectivelyInverted() ) {
241 delete normalizedData
.params
.invert
;
246 new mw
.rcfilters
.dm
.SavedQueryItemModel(
250 { 'default': isDefault
}
255 this.setDefault( randomID
);
262 * Remove query from model
264 * @param {string} queryID Query ID
266 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.removeQuery = function ( queryID
) {
267 var query
= this.getItemByID( queryID
);
270 // Check if this item was the default
271 if ( String( this.getDefault() ) === String( queryID
) ) {
272 // Nulify the default
273 this.setDefault( null );
276 this.removeItems( [ query
] );
281 * Get an item that matches the requested query
283 * @param {Object} fullQueryComparison Object representing all filters and highlights to compare
284 * @return {mw.rcfilters.dm.SavedQueryItemModel} Matching item model
286 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.findMatchingQuery = function ( fullQueryComparison
) {
287 // Minimize before comparison
288 fullQueryComparison
= this.filtersModel
.getMinimizedParamRepresentation( fullQueryComparison
);
290 // Correct the invert state for effective selection
291 if ( fullQueryComparison
.invert
&& !this.filtersModel
.areNamespacesEffectivelyInverted() ) {
292 delete fullQueryComparison
.invert
;
295 return this.getItems().filter( function ( item
) {
297 item
.getCombinedData(),
304 * Get query by its identifier
306 * @param {string} queryID Query identifier
307 * @return {mw.rcfilters.dm.SavedQueryItemModel|undefined} Item matching
308 * the search. Undefined if not found.
310 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getItemByID = function ( queryID
) {
311 return this.getItems().filter( function ( item
) {
312 return item
.getID() === queryID
;
317 * Get the full data representation of the default query, if it exists
319 * @return {Object|null} Representation of the default params if exists.
320 * Null if default doesn't exist or if the user is not logged in.
322 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getDefaultParams = function () {
323 return ( !mw
.user
.isAnon() && this.getItemParams( this.getDefault() ) ) || {};
327 * Get a full parameter representation of an item data
329 * @param {Object} queryID Query ID
330 * @return {Object} Parameter representation
332 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getItemParams = function ( queryID
) {
333 var item
= this.getItemByID( queryID
),
334 data
= item
? item
.getData() : {};
336 return !$.isEmptyObject( data
) ? this.buildParamsFromData( data
) : {};
340 * Build a full parameter representation given item data and model sticky values state
342 * @param {Object} data Item data
343 * @return {Object} Full param representation
345 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.buildParamsFromData = function ( data
) {
347 // Return parameter representation
348 return this.filtersModel
.getMinimizedParamRepresentation( $.extend( true, {},
355 * Get the object representing the state of the entire model and items
357 * @return {Object} Object representing the state of the model and items
359 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getState = function () {
360 var obj
= { queries
: {}, version
: '2' };
362 // Translate the items to the saved object
363 this.getItems().forEach( function ( item
) {
364 obj
.queries
[ item
.getID() ] = item
.getState();
367 if ( this.getDefault() ) {
368 obj
.default = this.getDefault();
375 * Set a default query. Null to unset default.
377 * @param {string} itemID Query identifier
380 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.setDefault = function ( itemID
) {
381 if ( this.default !== itemID
) {
382 this.default = itemID
;
384 // Set for individual itens
385 this.getItems().forEach( function ( item
) {
386 item
.toggleDefault( item
.getID() === itemID
);
389 this.emit( 'default', itemID
);
394 * Get the default query ID
396 * @return {string} Default query identifier
398 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getDefault = function () {
403 * Check if the saved queries were converted
405 * @return {boolean} Saved queries were converted from the previous
406 * version to the new version
408 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.isConverted = function () {
409 return this.converted
;