3 * View model for saved queries
6 * @mixins OO.EventEmitter
7 * @mixins OO.EmitterList
10 * @param {mw.rcfilters.dm.FiltersViewModel} filtersModel Filters model
11 * @param {Object} [config] Configuration options
12 * @cfg {string} [default] Default query ID
14 mw
.rcfilters
.dm
.SavedQueriesModel
= function MwRcfiltersDmSavedQueriesModel( filtersModel
, config
) {
15 config
= config
|| {};
18 OO
.EventEmitter
.call( this );
19 OO
.EmitterList
.call( this );
21 this.default = config
.default;
22 this.filtersModel
= filtersModel
;
23 this.converted
= false;
26 this.aggregate( { update
: 'itemUpdate' } );
31 OO
.initClass( mw
.rcfilters
.dm
.SavedQueriesModel
);
32 OO
.mixinClass( mw
.rcfilters
.dm
.SavedQueriesModel
, OO
.EventEmitter
);
33 OO
.mixinClass( mw
.rcfilters
.dm
.SavedQueriesModel
, OO
.EmitterList
);
40 * Model is initialized
45 * @param {mw.rcfilters.dm.SavedQueryItemModel} Changed item
52 * @param {string} New default ID
54 * The default has changed
60 * Initialize the saved queries model by reading it from the user's settings.
61 * The structure of the saved queries is:
63 * version: (string) Version number; if version 2, the query represents
64 * parameters. Otherwise, the older version represented filters
65 * and needs to be readjusted,
66 * default: (string) Query ID
70 * filters: (Object) Minimal definition of the filters
71 * highlights: (Object) Definition of the highlights
73 * label: (optional) Name of this query
78 * @param {Object} [savedQueries] An object with the saved queries with
79 * the above structure.
82 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.initialize = function ( savedQueries
) {
85 savedQueries
= savedQueries
|| {};
89 this.converted
= false;
91 if ( savedQueries
.version
!== '2' ) {
92 // Old version dealt with filter names. We need to migrate to the new structure
95 // version: (string) '2',
96 // default: (string) Query ID,
99 // label: (string) Name of the query
101 // params: (object) Representing all the parameter states
102 // highlights: (object) Representing all the filter highlight states
106 // eslint-disable-next-line jquery/no-each-util
107 $.each( savedQueries
.queries
|| {}, function ( id
, obj
) {
108 if ( obj
.data
&& obj
.data
.filters
) {
109 obj
.data
= model
.convertToParameters( obj
.data
);
113 this.converted
= true;
114 savedQueries
.version
= '2';
117 // Initialize the query items
118 // eslint-disable-next-line jquery/no-each-util
119 $.each( savedQueries
.queries
|| {}, function ( id
, obj
) {
120 var normalizedData
= obj
.data
,
121 isDefault
= String( savedQueries
.default ) === String( id
);
123 if ( normalizedData
&& normalizedData
.params
) {
124 // Backwards-compat fix: Remove sticky parameters from
125 // the given data, if they exist
126 normalizedData
.params
= model
.filtersModel
.removeStickyParams( normalizedData
.params
);
128 // Correct the invert state for effective selection
129 if ( normalizedData
.params
.invert
&& !normalizedData
.params
.namespace ) {
130 delete normalizedData
.params
.invert
;
133 model
.cleanupHighlights( normalizedData
);
137 // Skip the addNewQuery method because we don't want to unnecessarily manipulate
138 // the given saved queries unless we literally intend to (like in backwards compat fixes)
139 // And the addNewQuery method also uses a minimization routine that checks for the
140 // validity of items and minimizes the query. This isn't necessary for queries loaded
141 // from the backend, and has the risk of removing values if they're temporarily
142 // invalid (example: if we temporarily removed a cssClass from a filter in the backend)
144 new mw
.rcfilters
.dm
.SavedQueryItemModel(
148 { default: isDefault
}
158 this.emit( 'initialize' );
162 * Clean up highlight parameters.
163 * 'highlight' used to be stored, it's not inferred based on the presence of absence of
166 * @param {Object} data Saved query data
168 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.cleanupHighlights = function ( data
) {
170 data
.params
.highlight
=== '0' &&
171 data
.highlights
&& Object
.keys( data
.highlights
).length
173 data
.highlights
= {};
175 delete data
.params
.highlight
;
179 * Convert from representation of filters to representation of parameters
181 * @param {Object} data Query data
182 * @return {Object} New converted query data
184 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.convertToParameters = function ( data
) {
186 defaultFilters
= this.filtersModel
.getFiltersFromParameters( this.filtersModel
.getDefaultParams() ),
187 fullFilterRepresentation
= $.extend( true, {}, defaultFilters
, data
.filters
),
188 highlightEnabled
= data
.highlights
.highlight
;
190 delete data
.highlights
.highlight
;
193 newData
.params
= this.filtersModel
.getMinimizedParamRepresentation(
194 this.filtersModel
.getParametersFromFilters( fullFilterRepresentation
)
197 // Highlights: appending _color to keys
198 newData
.highlights
= {};
199 // eslint-disable-next-line jquery/no-each-util
200 $.each( data
.highlights
, function ( highlightedFilterName
, value
) {
202 newData
.highlights
[ highlightedFilterName
+ '_color' ] = data
.highlights
[ highlightedFilterName
];
207 newData
.params
.highlight
= String( Number( highlightEnabled
|| 0 ) );
215 * @param {string} label Label for the new query
216 * @param {Object} fulldata Full data representation for the new query, combining highlights and filters
217 * @param {boolean} isDefault Item is default
218 * @param {string} [id] Query ID, if exists. If this isn't given, a random
219 * new ID will be created.
220 * @return {string} ID of the newly added query
222 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.addNewQuery = function ( label
, fulldata
, isDefault
, id
) {
223 var normalizedData
= { params
: {}, highlights
: {} },
224 highlightParamNames
= Object
.keys( this.filtersModel
.getEmptyHighlightParameters() ),
225 randomID
= String( id
|| ( new Date() ).getTime() ),
226 data
= this.filtersModel
.getMinimizedParamRepresentation( fulldata
);
228 // Split highlight/params
229 // eslint-disable-next-line jquery/no-each-util
230 $.each( data
, function ( param
, value
) {
231 if ( param
!== 'highlight' && highlightParamNames
.indexOf( param
) > -1 ) {
232 normalizedData
.highlights
[ param
] = value
;
234 normalizedData
.params
[ param
] = value
;
238 // Correct the invert state for effective selection
239 if ( normalizedData
.params
.invert
&& !this.filtersModel
.areNamespacesEffectivelyInverted() ) {
240 delete normalizedData
.params
.invert
;
245 new mw
.rcfilters
.dm
.SavedQueryItemModel(
249 { default: isDefault
}
254 this.setDefault( randomID
);
261 * Remove query from model
263 * @param {string} queryID Query ID
265 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.removeQuery = function ( queryID
) {
266 var query
= this.getItemByID( queryID
);
269 // Check if this item was the default
270 if ( String( this.getDefault() ) === String( queryID
) ) {
271 // Nulify the default
272 this.setDefault( null );
275 this.removeItems( [ query
] );
280 * Get an item that matches the requested query
282 * @param {Object} fullQueryComparison Object representing all filters and highlights to compare
283 * @return {mw.rcfilters.dm.SavedQueryItemModel} Matching item model
285 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.findMatchingQuery = function ( fullQueryComparison
) {
286 // Minimize before comparison
287 fullQueryComparison
= this.filtersModel
.getMinimizedParamRepresentation( fullQueryComparison
);
289 // Correct the invert state for effective selection
290 if ( fullQueryComparison
.invert
&& !this.filtersModel
.areNamespacesEffectivelyInverted() ) {
291 delete fullQueryComparison
.invert
;
294 return this.getItems().filter( function ( item
) {
296 item
.getCombinedData(),
303 * Get query by its identifier
305 * @param {string} queryID Query identifier
306 * @return {mw.rcfilters.dm.SavedQueryItemModel|undefined} Item matching
307 * the search. Undefined if not found.
309 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getItemByID = function ( queryID
) {
310 return this.getItems().filter( function ( item
) {
311 return item
.getID() === queryID
;
316 * Get the full data representation of the default query, if it exists
318 * @return {Object|null} Representation of the default params if exists.
319 * Null if default doesn't exist or if the user is not logged in.
321 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getDefaultParams = function () {
322 return ( !mw
.user
.isAnon() && this.getItemParams( this.getDefault() ) ) || {};
326 * Get a full parameter representation of an item data
328 * @param {Object} queryID Query ID
329 * @return {Object} Parameter representation
331 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getItemParams = function ( queryID
) {
332 var item
= this.getItemByID( queryID
),
333 data
= item
? item
.getData() : {};
335 return !$.isEmptyObject( data
) ? this.buildParamsFromData( data
) : {};
339 * Build a full parameter representation given item data and model sticky values state
341 * @param {Object} data Item data
342 * @return {Object} Full param representation
344 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.buildParamsFromData = function ( data
) {
346 // Return parameter representation
347 return this.filtersModel
.getMinimizedParamRepresentation( $.extend( true, {},
354 * Get the object representing the state of the entire model and items
356 * @return {Object} Object representing the state of the model and items
358 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getState = function () {
359 var obj
= { queries
: {}, version
: '2' };
361 // Translate the items to the saved object
362 this.getItems().forEach( function ( item
) {
363 obj
.queries
[ item
.getID() ] = item
.getState();
366 if ( this.getDefault() ) {
367 obj
.default = this.getDefault();
374 * Set a default query. Null to unset default.
376 * @param {string} itemID Query identifier
379 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.setDefault = function ( itemID
) {
380 if ( this.default !== itemID
) {
381 this.default = itemID
;
383 // Set for individual itens
384 this.getItems().forEach( function ( item
) {
385 item
.toggleDefault( item
.getID() === itemID
);
388 this.emit( 'default', itemID
);
393 * Get the default query ID
395 * @return {string} Default query identifier
397 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getDefault = function () {
402 * Check if the saved queries were converted
404 * @return {boolean} Saved queries were converted from the previous
405 * version to the new version
407 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.isConverted = function () {
408 return this.converted
;