3 * View model for saved queries
6 * @mixins OO.EventEmitter
7 * @mixins OO.EmitterList
10 * @param {Object} [config] Configuration options
11 * @cfg {string} [default] Default query ID
13 mw
.rcfilters
.dm
.SavedQueriesModel
= function MwRcfiltersDmSavedQueriesModel( config
) {
14 config
= config
|| {};
17 OO
.EventEmitter
.call( this );
18 OO
.EmitterList
.call( this );
20 this.default = config
.default;
24 this.aggregate( { update
: 'itemUpdate' } );
29 OO
.initClass( mw
.rcfilters
.dm
.SavedQueriesModel
);
30 OO
.mixinClass( mw
.rcfilters
.dm
.SavedQueriesModel
, OO
.EventEmitter
);
31 OO
.mixinClass( mw
.rcfilters
.dm
.SavedQueriesModel
, OO
.EmitterList
);
38 * Model is initialized
43 * @param {mw.rcfilters.dm.SavedQueryItemModel} Changed item
50 * @param {string} New default ID
52 * The default has changed
58 * Initialize the saved queries model by reading it from the user's settings.
59 * The structure of the saved queries is:
61 * default: (string) Query ID
65 * filters: (Object) Minimal definition of the filters
66 * highlights: (Object) Definition of the highlights
68 * label: (optional) Name of this query
73 * @param {Object} [savedQueries] An object with the saved queries with
74 * the above structure.
75 * @param {Object} [baseState] An object representing the base state
76 * so we can normalize the data
77 * @param {string[]} [ignoreFilters] Filters to ignore and remove from
81 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.initialize = function ( savedQueries
, baseState
, ignoreFilters
) {
85 savedQueries
= savedQueries
|| {};
86 ignoreFilters
= ignoreFilters
|| {};
88 this.baseState
= baseState
;
91 $.each( savedQueries
.queries
|| {}, function ( id
, obj
) {
93 normalizedData
= $.extend( true, {}, baseState
, obj
.data
),
94 isDefault
= String( savedQueries
.default ) === String( id
);
96 // Backwards-compat fix: We stored the 'highlight' state with
97 // "1" and "0" instead of true/false; for already-stored states,
98 // we need to fix that.
99 // NOTE: Since this feature is only available in beta, we should
100 // not need this line when we release this to the general wikis.
101 // This method will automatically fix all saved queries anyways
102 // for existing users, who are only betalabs users at the moment.
103 normalizedData
.highlights
.highlight
= !!Number( normalizedData
.highlights
.highlight
);
105 // Backwards-compat fix: Remove sticky parameters from the 'ignoreFilters' list
106 ignoreFilters
.forEach( function ( name
) {
107 delete normalizedData
.filters
[ name
];
110 item
= new mw
.rcfilters
.dm
.SavedQueryItemModel(
114 { 'default': isDefault
}
125 this.default = defaultItem
.getID();
128 this.addItems( items
);
130 this.emit( 'initialize' );
136 * @param {string} label Label for the new query
137 * @param {Object} data Data for the new query
138 * @return {string} ID of the newly added query
140 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.addNewQuery = function ( label
, data
) {
141 var randomID
= ( new Date() ).getTime(),
142 normalizedData
= $.extend( true, {}, this.baseState
, data
);
146 new mw
.rcfilters
.dm
.SavedQueryItemModel(
157 * Remove query from model
159 * @param {string} queryID Query ID
161 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.removeQuery = function ( queryID
) {
162 var query
= this.getItemByID( queryID
);
165 // Check if this item was the default
166 if ( String( this.getDefault() ) === String( queryID
) ) {
167 // Nulify the default
168 this.setDefault( null );
171 this.removeItems( [ query
] );
176 * Get an item that matches the requested query
178 * @param {Object} fullQueryComparison Object representing all filters and highlights to compare
179 * @return {mw.rcfilters.dm.SavedQueryItemModel} Matching item model
181 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.findMatchingQuery = function ( fullQueryComparison
) {
184 fullQueryComparison
= this.getDifferenceFromBase( fullQueryComparison
);
186 return this.getItems().filter( function ( item
) {
187 var comparedData
= model
.getDifferenceFromBase( item
.getData() );
196 * Get a minimal representation of the state for comparison
198 * @param {Object} state Given state
199 * @return {Object} Minimal state
201 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getDifferenceFromBase = function ( state
) {
202 var result
= { filters
: {}, highlights
: {}, invert
: state
.invert
},
203 baseState
= this.baseState
;
206 $.each( state
.filters
, function ( name
, value
) {
207 if ( baseState
.filters
!== undefined && baseState
.filters
[ name
] !== value
) {
208 result
.filters
[ name
] = value
;
212 $.each( state
.highlights
, function ( name
, value
) {
213 if ( baseState
.highlights
!== undefined && baseState
.highlights
[ name
] !== value
&& name
!== 'highlight' ) {
214 result
.highlights
[ name
] = value
;
221 * Get query by its identifier
223 * @param {string} queryID Query identifier
224 * @return {mw.rcfilters.dm.SavedQueryItemModel|undefined} Item matching
225 * the search. Undefined if not found.
227 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getItemByID = function ( queryID
) {
228 return this.getItems().filter( function ( item
) {
229 return item
.getID() === queryID
;
234 * Get the object representing the state of the entire model and items
236 * @return {Object} Object representing the state of the model and items
238 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getState = function () {
239 var obj
= { queries
: {} };
241 // Translate the items to the saved object
242 this.getItems().forEach( function ( item
) {
243 var itemState
= item
.getState();
245 obj
.queries
[ item
.getID() ] = itemState
;
248 if ( this.getDefault() ) {
249 obj
.default = this.getDefault();
256 * Set a default query. Null to unset default.
258 * @param {string} itemID Query identifier
261 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.setDefault = function ( itemID
) {
262 if ( this.default !== itemID
) {
263 this.default = itemID
;
265 // Set for individual itens
266 this.getItems().forEach( function ( item
) {
267 item
.toggleDefault( item
.getID() === itemID
);
270 this.emit( 'default', itemID
);
275 * Get the default query ID
277 * @return {string} Default query identifier
279 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getDefault = function () {
282 }( mediaWiki
, jQuery
) );