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
) {
73 savedQueries
= savedQueries
|| {};
75 this.baseState
= baseState
;
78 $.each( savedQueries
.queries
|| {}, function ( id
, obj
) {
79 var normalizedData
= $.extend( true, {}, baseState
, obj
.data
);
81 // Backwards-compat fix: We stored the 'highlight' state with
82 // "1" and "0" instead of true/false; for already-stored states,
83 // we need to fix that.
84 // NOTE: Since this feature is only available in beta, we should
85 // not need this line when we release this to the general wikis.
86 // This method will automatically fix all saved queries anyways
87 // for existing users, who are only betalabs users at the moment.
88 normalizedData
.highlights
.highlight
= !!Number( normalizedData
.highlights
.highlight
);
91 new mw
.rcfilters
.dm
.SavedQueryItemModel(
95 { 'default': savedQueries
.default === id
}
100 this.default = savedQueries
.default;
102 this.addItems( items
);
104 this.emit( 'initialize' );
110 * @param {string} label Label for the new query
111 * @param {Object} data Data for the new query
113 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.addNewQuery = function ( label
, data
) {
114 var randomID
= ( new Date() ).getTime(),
115 normalizedData
= $.extend( true, {}, this.baseState
, data
);
119 new mw
.rcfilters
.dm
.SavedQueryItemModel(
128 * Get an item that matches the requested query
130 * @param {Object} fullQueryComparison Object representing all filters and highlights to compare
131 * @return {mw.rcfilters.dm.SavedQueryItemModel} Matching item model
133 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.findMatchingQuery = function ( fullQueryComparison
) {
134 return this.getItems().filter( function ( item
) {
143 * Get query by its identifier
145 * @param {string} queryID Query identifier
146 * @return {mw.rcfilters.dm.SavedQueryItemModel|undefined} Item matching
147 * the search. Undefined if not found.
149 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getItemByID = function ( queryID
) {
150 return this.getItems().filter( function ( item
) {
151 return item
.getID() === queryID
;
156 * Get the object representing the state of the entire model and items
158 * @return {Object} Object representing the state of the model and items
160 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getState = function () {
161 var obj
= { queries
: {} };
163 // Translate the items to the saved object
164 this.getItems().forEach( function ( item
) {
165 var itemState
= item
.getState();
167 obj
.queries
[ item
.getID() ] = itemState
;
170 if ( this.getDefault() ) {
171 obj
.default = this.getDefault();
178 * Set a default query. Null to unset default.
180 * @param {string} itemID Query identifier
183 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.setDefault = function ( itemID
) {
184 if ( this.default !== itemID
) {
185 this.default = itemID
;
187 // Set for individual itens
188 this.getItems().forEach( function ( item
) {
189 item
.toggleDefault( item
.getID() === itemID
);
195 * Get the default query ID
197 * @return {string} Default query identifier
199 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getDefault = function () {
202 }( mediaWiki
, jQuery
) );