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 new mw
.rcfilters
.dm
.SavedQueryItemModel(
85 { 'default': savedQueries
.default === id
}
90 this.default = savedQueries
.default;
92 this.addItems( items
);
94 this.emit( 'initialize' );
100 * @param {string} label Label for the new query
101 * @param {Object} data Data for the new query
103 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.addNewQuery = function ( label
, data
) {
104 var randomID
= ( new Date() ).getTime(),
105 normalizedData
= $.extend( true, {}, this.baseState
, data
);
109 new mw
.rcfilters
.dm
.SavedQueryItemModel(
118 * Get an item that matches the requested query
120 * @param {Object} fullQueryComparison Object representing all filters and highlights to compare
121 * @return {mw.rcfilters.dm.SavedQueryItemModel} Matching item model
123 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.findMatchingQuery = function ( fullQueryComparison
) {
124 return this.getItems().filter( function ( item
) {
133 * Get query by its identifier
135 * @param {string} queryID Query identifier
136 * @return {mw.rcfilters.dm.SavedQueryItemModel|undefined} Item matching
137 * the search. Undefined if not found.
139 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getItemByID = function ( queryID
) {
140 return this.getItems().filter( function ( item
) {
141 return item
.getID() === queryID
;
146 * Get the object representing the state of the entire model and items
148 * @return {Object} Object representing the state of the model and items
150 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getState = function () {
151 var obj
= { queries
: {} };
153 // Translate the items to the saved object
154 this.getItems().forEach( function ( item
) {
155 var itemState
= item
.getState();
157 obj
.queries
[ item
.getID() ] = itemState
;
160 if ( this.getDefault() ) {
161 obj
.default = this.getDefault();
168 * Set a default query. Null to unset default.
170 * @param {string} itemID Query identifier
173 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.setDefault = function ( itemID
) {
174 if ( this.default !== itemID
) {
175 this.default = itemID
;
177 // Set for individual itens
178 this.getItems().forEach( function ( item
) {
179 item
.toggleDefault( item
.getID() === itemID
);
185 * Get the default query ID
187 * @return {string} Default query identifier
189 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getDefault = function () {
192 }( mediaWiki
, jQuery
) );