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
51 * Initialize the saved queries model by reading it from the user's settings.
52 * The structure of the saved queries is:
54 * default: (string) Query ID
58 * filters: (Object) Minimal definition of the filters
59 * highlights: (Object) Definition of the highlights
61 * label: (optional) Name of this query
66 * @param {Object} [savedQueries] An object with the saved queries with
67 * the above structure.
68 * @param {Object} [baseState] An object representing the base state
69 * so we can normalize the data
70 * @param {string[]} [ignoreFilters] Filters to ignore and remove from
74 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.initialize = function ( savedQueries
, baseState
, ignoreFilters
) {
78 savedQueries
= savedQueries
|| {};
79 ignoreFilters
= ignoreFilters
|| {};
81 this.baseState
= baseState
;
84 $.each( savedQueries
.queries
|| {}, function ( id
, obj
) {
86 normalizedData
= $.extend( true, {}, baseState
, obj
.data
),
87 isDefault
= String( savedQueries
.default ) === String( id
);
89 // Backwards-compat fix: We stored the 'highlight' state with
90 // "1" and "0" instead of true/false; for already-stored states,
91 // we need to fix that.
92 // NOTE: Since this feature is only available in beta, we should
93 // not need this line when we release this to the general wikis.
94 // This method will automatically fix all saved queries anyways
95 // for existing users, who are only betalabs users at the moment.
96 normalizedData
.highlights
.highlight
= !!Number( normalizedData
.highlights
.highlight
);
98 // Backwards-compat fix: Remove sticky parameters from the 'ignoreFilters' list
99 ignoreFilters
.forEach( function ( name
) {
100 delete normalizedData
.filters
[ name
];
103 item
= new mw
.rcfilters
.dm
.SavedQueryItemModel(
107 { 'default': isDefault
}
118 this.default = defaultItem
.getID();
121 this.addItems( items
);
123 this.emit( 'initialize' );
129 * @param {string} label Label for the new query
130 * @param {Object} data Data for the new query
131 * @return {string} ID of the newly added query
133 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.addNewQuery = function ( label
, data
) {
134 var randomID
= ( new Date() ).getTime(),
135 normalizedData
= $.extend( true, {}, this.baseState
, data
);
139 new mw
.rcfilters
.dm
.SavedQueryItemModel(
150 * Remove query from model
152 * @param {string} queryID Query ID
154 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.removeQuery = function ( queryID
) {
155 var query
= this.getItemByID( queryID
);
158 // Check if this item was the default
159 if ( String( this.getDefault() ) === String( queryID
) ) {
160 // Nulify the default
161 this.setDefault( null );
164 this.removeItems( [ query
] );
169 * Get an item that matches the requested query
171 * @param {Object} fullQueryComparison Object representing all filters and highlights to compare
172 * @return {mw.rcfilters.dm.SavedQueryItemModel} Matching item model
174 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.findMatchingQuery = function ( fullQueryComparison
) {
177 fullQueryComparison
= this.getDifferenceFromBase( fullQueryComparison
);
179 return this.getItems().filter( function ( item
) {
180 var comparedData
= model
.getDifferenceFromBase( item
.getData() );
189 * Get a minimal representation of the state for comparison
191 * @param {Object} state Given state
192 * @return {Object} Minimal state
194 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getDifferenceFromBase = function ( state
) {
195 var result
= { filters
: {}, highlights
: {}, invert
: state
.invert
},
196 baseState
= this.baseState
;
199 $.each( state
.filters
, function ( name
, value
) {
200 if ( baseState
.filters
!== undefined && baseState
.filters
[ name
] !== value
) {
201 result
.filters
[ name
] = value
;
205 $.each( state
.highlights
, function ( name
, value
) {
206 if ( baseState
.highlights
!== undefined && baseState
.highlights
[ name
] !== value
&& name
!== 'highlight' ) {
207 result
.highlights
[ name
] = value
;
214 * Get query by its identifier
216 * @param {string} queryID Query identifier
217 * @return {mw.rcfilters.dm.SavedQueryItemModel|undefined} Item matching
218 * the search. Undefined if not found.
220 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getItemByID = function ( queryID
) {
221 return this.getItems().filter( function ( item
) {
222 return item
.getID() === queryID
;
227 * Get the object representing the state of the entire model and items
229 * @return {Object} Object representing the state of the model and items
231 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getState = function () {
232 var obj
= { queries
: {} };
234 // Translate the items to the saved object
235 this.getItems().forEach( function ( item
) {
236 var itemState
= item
.getState();
238 obj
.queries
[ item
.getID() ] = itemState
;
241 if ( this.getDefault() ) {
242 obj
.default = this.getDefault();
249 * Set a default query. Null to unset default.
251 * @param {string} itemID Query identifier
254 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.setDefault = function ( itemID
) {
255 if ( this.default !== itemID
) {
256 this.default = itemID
;
258 // Set for individual itens
259 this.getItems().forEach( function ( item
) {
260 item
.toggleDefault( item
.getID() === itemID
);
266 * Get the default query ID
268 * @return {string} Default query identifier
270 mw
.rcfilters
.dm
.SavedQueriesModel
.prototype.getDefault = function () {
273 }( mediaWiki
, jQuery
) );