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;
23 this.aggregate( { update
: 'itemUpdate' } );
28 OO
.initClass( mw
.rcfilters
.dm
.SavedQueriesModel
);
29 OO
.mixinClass( mw
.rcfilters
.dm
.SavedQueriesModel
, OO
.EventEmitter
);
30 OO
.mixinClass( mw
.rcfilters
.dm
.SavedQueriesModel
, OO
.EmitterList
);
37 * Model is initialized
42 * @param {mw.rcfilters.dm.SavedQueryItemModel} Changed item
50 * Initialize the saved queries model by reading it from the user's settings.
51 * The structure of the saved queries is:
53 * default: (string) Query ID
57 * filters: (Object) Minimal definition of the filters
58 * highlights: (Object) Definition of the highlights
60 * label: (optional) Name of this query
65 * @param {Object} [savedQueries] An object with the saved queries with
66 * the above structure.
67 * @param {Object} [baseState] An object representing the base state
68 * so we can normalize the data
71 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.initialize = function ( savedQueries
, baseState
) {
75 savedQueries
= savedQueries
|| {};
77 this.baseState
= baseState
;
80 $.each( savedQueries
.queries
|| {}, function ( id
, obj
) {
82 normalizedData
= $.extend( true, {}, baseState
, obj
.data
),
83 isDefault
= String( savedQueries
.default ) === String( id
);
85 // Backwards-compat fix: We stored the 'highlight' state with
86 // "1" and "0" instead of true/false; for already-stored states,
87 // we need to fix that.
88 // NOTE: Since this feature is only available in beta, we should
89 // not need this line when we release this to the general wikis.
90 // This method will automatically fix all saved queries anyways
91 // for existing users, who are only betalabs users at the moment.
92 normalizedData
.highlights
.highlight
= !!Number( normalizedData
.highlights
.highlight
);
94 item
= new mw
.rcfilters
.dm
.SavedQueryItemModel(
98 { 'default': isDefault
}
109 this.default = defaultItem
.getID();
112 this.addItems( items
);
114 this.emit( 'initialize' );
120 * @param {string} label Label for the new query
121 * @param {Object} data Data for the new query
122 * @return {string} ID of the newly added query
124 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.addNewQuery = function ( label
, data
) {
125 var randomID
= ( new Date() ).getTime(),
126 normalizedData
= $.extend( true, {}, this.baseState
, data
);
130 new mw
.rcfilters
.dm
.SavedQueryItemModel(
141 * Remove query from model
143 * @param {string} queryID Query ID
145 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.removeQuery = function ( queryID
) {
146 var query
= this.getItemByID( queryID
);
149 // Check if this item was the default
150 if ( String( this.getDefault() ) === String( queryID
) ) {
151 // Nulify the default
152 this.setDefault( null );
155 this.removeItems( [ query
] );
160 * Get an item that matches the requested query
162 * @param {Object} fullQueryComparison Object representing all filters and highlights to compare
163 * @return {mw.rcfilters.dm.SavedQueryItemModel} Matching item model
165 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.findMatchingQuery = function ( fullQueryComparison
) {
166 return this.getItems().filter( function ( item
) {
175 * Get query by its identifier
177 * @param {string} queryID Query identifier
178 * @return {mw.rcfilters.dm.SavedQueryItemModel|undefined} Item matching
179 * the search. Undefined if not found.
181 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getItemByID = function ( queryID
) {
182 return this.getItems().filter( function ( item
) {
183 return item
.getID() === queryID
;
188 * Get the object representing the state of the entire model and items
190 * @return {Object} Object representing the state of the model and items
192 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getState = function () {
193 var obj
= { queries
: {} };
195 // Translate the items to the saved object
196 this.getItems().forEach( function ( item
) {
197 var itemState
= item
.getState();
199 obj
.queries
[ item
.getID() ] = itemState
;
202 if ( this.getDefault() ) {
203 obj
.default = this.getDefault();
210 * Set a default query. Null to unset default.
212 * @param {string} itemID Query identifier
215 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.setDefault = function ( itemID
) {
216 if ( this.default !== itemID
) {
217 this.default = itemID
;
219 // Set for individual itens
220 this.getItems().forEach( function ( item
) {
221 item
.toggleDefault( item
.getID() === itemID
);
227 * Get the default query ID
229 * @return {string} Default query identifier
231 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getDefault = function () {
234 }( mediaWiki
, jQuery
) );