e7cb6b680d5b9d2f6de5572565068ea9258185ce
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 $.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 $.each( savedQueries
.queries
|| {}, function ( id
, obj
) {
119 var normalizedData
= obj
.data
,
120 isDefault
= String( savedQueries
.default ) === String( id
);
122 if ( normalizedData
&& normalizedData
.params
) {
123 // Backwards-compat fix: Remove sticky parameters from
124 // the given data, if they exist
125 normalizedData
.params
= model
.filtersModel
.removeStickyParams( normalizedData
.params
);
127 // Correct the invert state for effective selection
128 if ( normalizedData
.params
.invert
&& !normalizedData
.params
.namespace ) {
129 delete normalizedData
.params
.invert
;
132 model
.cleanupHighlights( normalizedData
);
136 // Skip the addNewQuery method because we don't want to unnecessarily manipulate
137 // the given saved queries unless we literally intend to (like in backwards compat fixes)
138 // And the addNewQuery method also uses a minimization routine that checks for the
139 // validity of items and minimizes the query. This isn't necessary for queries loaded
140 // from the backend, and has the risk of removing values if they're temporarily
141 // invalid (example: if we temporarily removed a cssClass from a filter in the backend)
143 new mw
.rcfilters
.dm
.SavedQueryItemModel(
147 { 'default': isDefault
}
157 this.emit( 'initialize' );
161 * Clean up highlight parameters.
162 * 'highlight' used to be stored, it's not inferred based on the presence of absence of
165 * @param {Object} data Saved query data
167 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.cleanupHighlights = function ( data
) {
169 data
.params
.highlight
=== '0' &&
170 data
.highlights
&& Object
.keys( data
.highlights
).length
172 data
.highlights
= {};
174 delete data
.params
.highlight
;
178 * Convert from representation of filters to representation of parameters
180 * @param {Object} data Query data
181 * @return {Object} New converted query data
183 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.convertToParameters = function ( data
) {
185 defaultFilters
= this.filtersModel
.getFiltersFromParameters( this.filtersModel
.getDefaultParams() ),
186 fullFilterRepresentation
= $.extend( true, {}, defaultFilters
, data
.filters
),
187 highlightEnabled
= data
.highlights
.highlight
;
189 delete data
.highlights
.highlight
;
192 newData
.params
= this.filtersModel
.getMinimizedParamRepresentation(
193 this.filtersModel
.getParametersFromFilters( fullFilterRepresentation
)
196 // Highlights: appending _color to keys
197 newData
.highlights
= {};
198 $.each( data
.highlights
, function ( highlightedFilterName
, value
) {
200 newData
.highlights
[ highlightedFilterName
+ '_color' ] = data
.highlights
[ highlightedFilterName
];
205 newData
.params
.highlight
= String( Number( highlightEnabled
|| 0 ) );
213 * @param {string} label Label for the new query
214 * @param {Object} fulldata Full data representation for the new query, combining highlights and filters
215 * @param {boolean} isDefault Item is default
216 * @param {string} [id] Query ID, if exists. If this isn't given, a random
217 * new ID will be created.
218 * @return {string} ID of the newly added query
220 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.addNewQuery = function ( label
, fulldata
, isDefault
, id
) {
221 var normalizedData
= { params
: {}, highlights
: {} },
222 highlightParamNames
= Object
.keys( this.filtersModel
.getEmptyHighlightParameters() ),
223 randomID
= String( id
|| ( new Date() ).getTime() ),
224 data
= this.filtersModel
.getMinimizedParamRepresentation( fulldata
);
226 // Split highlight/params
227 $.each( data
, function ( param
, value
) {
228 if ( param
!== 'highlight' && highlightParamNames
.indexOf( param
) > -1 ) {
229 normalizedData
.highlights
[ param
] = value
;
231 normalizedData
.params
[ param
] = value
;
235 // Correct the invert state for effective selection
236 if ( normalizedData
.params
.invert
&& !this.filtersModel
.areNamespacesEffectivelyInverted() ) {
237 delete normalizedData
.params
.invert
;
242 new mw
.rcfilters
.dm
.SavedQueryItemModel(
246 { 'default': isDefault
}
251 this.setDefault( randomID
);
258 * Remove query from model
260 * @param {string} queryID Query ID
262 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.removeQuery = function ( queryID
) {
263 var query
= this.getItemByID( queryID
);
266 // Check if this item was the default
267 if ( String( this.getDefault() ) === String( queryID
) ) {
268 // Nulify the default
269 this.setDefault( null );
272 this.removeItems( [ query
] );
277 * Get an item that matches the requested query
279 * @param {Object} fullQueryComparison Object representing all filters and highlights to compare
280 * @return {mw.rcfilters.dm.SavedQueryItemModel} Matching item model
282 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.findMatchingQuery = function ( fullQueryComparison
) {
283 // Minimize before comparison
284 fullQueryComparison
= this.filtersModel
.getMinimizedParamRepresentation( fullQueryComparison
);
286 // Correct the invert state for effective selection
287 if ( fullQueryComparison
.invert
&& !this.filtersModel
.areNamespacesEffectivelyInverted() ) {
288 delete fullQueryComparison
.invert
;
291 return this.getItems().filter( function ( item
) {
293 item
.getCombinedData(),
300 * Get query by its identifier
302 * @param {string} queryID Query identifier
303 * @return {mw.rcfilters.dm.SavedQueryItemModel|undefined} Item matching
304 * the search. Undefined if not found.
306 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getItemByID = function ( queryID
) {
307 return this.getItems().filter( function ( item
) {
308 return item
.getID() === queryID
;
313 * Get the full data representation of the default query, if it exists
315 * @return {Object|null} Representation of the default params if exists.
316 * Null if default doesn't exist or if the user is not logged in.
318 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getDefaultParams = function () {
319 return ( !mw
.user
.isAnon() && this.getItemParams( this.getDefault() ) ) || {};
323 * Get a full parameter representation of an item data
325 * @param {Object} queryID Query ID
326 * @return {Object} Parameter representation
328 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getItemParams = function ( queryID
) {
329 var item
= this.getItemByID( queryID
),
330 data
= item
? item
.getData() : {};
332 return !$.isEmptyObject( data
) ? this.buildParamsFromData( data
) : {};
336 * Build a full parameter representation given item data and model sticky values state
338 * @param {Object} data Item data
339 * @return {Object} Full param representation
341 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.buildParamsFromData = function ( data
) {
343 // Return parameter representation
344 return this.filtersModel
.getMinimizedParamRepresentation( $.extend( true, {},
351 * Get the object representing the state of the entire model and items
353 * @return {Object} Object representing the state of the model and items
355 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getState = function () {
356 var obj
= { queries
: {}, version
: '2' };
358 // Translate the items to the saved object
359 this.getItems().forEach( function ( item
) {
360 obj
.queries
[ item
.getID() ] = item
.getState();
363 if ( this.getDefault() ) {
364 obj
.default = this.getDefault();
371 * Set a default query. Null to unset default.
373 * @param {string} itemID Query identifier
376 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.setDefault = function ( itemID
) {
377 if ( this.default !== itemID
) {
378 this.default = itemID
;
380 // Set for individual itens
381 this.getItems().forEach( function ( item
) {
382 item
.toggleDefault( item
.getID() === itemID
);
385 this.emit( 'default', itemID
);
390 * Get the default query ID
392 * @return {string} Default query identifier
394 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getDefault = function () {
399 * Check if the saved queries were converted
401 * @return {boolean} Saved queries were converted from the previous
402 * version to the new version
404 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.isConverted = function () {
405 return this.converted
;
407 }( mediaWiki
, jQuery
) );