3 * View model for saved queries
5 * @mixins OO.EventEmitter
6 * @mixins OO.EmitterList
9 * @param {Object} [config] Configuration options
10 * @cfg {string} [default] Default query ID
12 mw
.rcfilters
.dm
.SavedQueriesModel
= function MwRcfiltersDmSavedQueriesModel( config
) {
13 config
= config
|| {};
16 OO
.EventEmitter
.call( this );
17 OO
.EmitterList
.call( this );
19 this.default = config
.default;
22 this.aggregate( { update
: 'itemUpdate' } );
27 OO
.initClass( mw
.rcfilters
.dm
.SavedQueriesModel
);
28 OO
.mixinClass( mw
.rcfilters
.dm
.SavedQueriesModel
, OO
.EventEmitter
);
29 OO
.mixinClass( mw
.rcfilters
.dm
.SavedQueriesModel
, OO
.EmitterList
);
36 * Model is initialized
41 * @param {mw.rcfilters.dm.SavedQueryItemModel} Changed item
49 * Initialize the saved queries model by reading it from the user's settings.
50 * The structure of the saved queries is:
52 * default: (string) Query ID
56 * filters: (Object) Minimal definition of the filters
57 * highlights: (Object) Definition of the highlights
59 * label: (optional) Name of this query
64 * @param {Object} [savedQueries] An object with the saved queries with
65 * the above structure.
66 * @param {Object} [baseState] An object representing the base state
67 * so we can normalize the data
70 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.initialize = function ( savedQueries
, baseState
) {
74 savedQueries
= savedQueries
|| {};
76 this.baseState
= baseState
;
79 $.each( savedQueries
.queries
|| {}, function ( id
, obj
) {
81 normalizedData
= $.extend( true, {}, baseState
, obj
.data
),
82 isDefault
= String( savedQueries
.default ) === String( id
);
84 // Backwards-compat fix: We stored the 'highlight' state with
85 // "1" and "0" instead of true/false; for already-stored states,
86 // we need to fix that.
87 // NOTE: Since this feature is only available in beta, we should
88 // not need this line when we release this to the general wikis.
89 // This method will automatically fix all saved queries anyways
90 // for existing users, who are only betalabs users at the moment.
91 normalizedData
.highlights
.highlight
= !!Number( normalizedData
.highlights
.highlight
);
93 item
= new mw
.rcfilters
.dm
.SavedQueryItemModel(
97 { 'default': isDefault
}
108 this.default = defaultItem
.getID();
111 this.addItems( items
);
113 this.emit( 'initialize' );
119 * @param {string} label Label for the new query
120 * @param {Object} data Data for the new query
122 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.addNewQuery = function ( label
, data
) {
123 var randomID
= ( new Date() ).getTime(),
124 normalizedData
= $.extend( true, {}, this.baseState
, data
);
128 new mw
.rcfilters
.dm
.SavedQueryItemModel(
137 * Remove query from model
139 * @param {string} queryID Query ID
141 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.removeQuery = function ( queryID
) {
142 var query
= this.getItemByID( queryID
);
145 // Check if this item was the default
146 if ( String( this.getDefault() ) === String( queryID
) ) {
147 // Nulify the default
148 this.setDefault( null );
151 this.removeItems( [ query
] );
156 * Get an item that matches the requested query
158 * @param {Object} fullQueryComparison Object representing all filters and highlights to compare
159 * @return {mw.rcfilters.dm.SavedQueryItemModel} Matching item model
161 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.findMatchingQuery = function ( fullQueryComparison
) {
162 return this.getItems().filter( function ( item
) {
171 * Get query by its identifier
173 * @param {string} queryID Query identifier
174 * @return {mw.rcfilters.dm.SavedQueryItemModel|undefined} Item matching
175 * the search. Undefined if not found.
177 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getItemByID = function ( queryID
) {
178 return this.getItems().filter( function ( item
) {
179 return item
.getID() === queryID
;
184 * Get the object representing the state of the entire model and items
186 * @return {Object} Object representing the state of the model and items
188 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getState = function () {
189 var obj
= { queries
: {} };
191 // Translate the items to the saved object
192 this.getItems().forEach( function ( item
) {
193 var itemState
= item
.getState();
195 obj
.queries
[ item
.getID() ] = itemState
;
198 if ( this.getDefault() ) {
199 obj
.default = this.getDefault();
206 * Set a default query. Null to unset default.
208 * @param {string} itemID Query identifier
211 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.setDefault = function ( itemID
) {
212 if ( this.default !== itemID
) {
213 this.default = itemID
;
215 // Set for individual itens
216 this.getItems().forEach( function ( item
) {
217 item
.toggleDefault( item
.getID() === itemID
);
223 * Get the default query ID
225 * @return {string} Default query identifier
227 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getDefault = function () {
230 }( mediaWiki
, jQuery
) );