Merge "resourceloader: Fix @covers for CSSMin tests"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / dm / mw.rcfilters.dm.SavedQueriesModel.js
1 ( function ( mw, $ ) {
2 /**
3 * View model for saved queries
4 *
5 * @class
6 * @mixins OO.EventEmitter
7 * @mixins OO.EmitterList
8 *
9 * @constructor
10 * @param {Object} [config] Configuration options
11 * @cfg {string} [default] Default query ID
12 */
13 mw.rcfilters.dm.SavedQueriesModel = function MwRcfiltersDmSavedQueriesModel( config ) {
14 config = config || {};
15
16 // Mixin constructor
17 OO.EventEmitter.call( this );
18 OO.EmitterList.call( this );
19
20 this.default = config.default;
21
22 // Events
23 this.aggregate( { update: 'itemUpdate' } );
24 };
25
26 /* Initialization */
27
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 );
31
32 /* Events */
33
34 /**
35 * @event initialize
36 *
37 * Model is initialized
38 */
39
40 /**
41 * @event itemUpdate
42 * @param {mw.rcfilters.dm.SavedQueryItemModel} Changed item
43 *
44 * An item has changed
45 */
46
47 /* Methods */
48
49 /**
50 * Initialize the saved queries model by reading it from the user's settings.
51 * The structure of the saved queries is:
52 * {
53 * default: (string) Query ID
54 * queries:{
55 * query_id_1: {
56 * data:{
57 * filters: (Object) Minimal definition of the filters
58 * highlights: (Object) Definition of the highlights
59 * },
60 * label: (optional) Name of this query
61 * }
62 * }
63 * }
64 *
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
69 * @fires initialize
70 */
71 mw.rcfilters.dm.SavedQueriesModel.prototype.initialize = function ( savedQueries, baseState ) {
72 var items = [],
73 defaultItem = null;
74
75 savedQueries = savedQueries || {};
76
77 this.baseState = baseState;
78
79 this.clearItems();
80 $.each( savedQueries.queries || {}, function ( id, obj ) {
81 var item,
82 normalizedData = $.extend( true, {}, baseState, obj.data ),
83 isDefault = String( savedQueries.default ) === String( id );
84
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 );
93
94 item = new mw.rcfilters.dm.SavedQueryItemModel(
95 id,
96 obj.label,
97 normalizedData,
98 { 'default': isDefault }
99 );
100
101 if ( isDefault ) {
102 defaultItem = item;
103 }
104
105 items.push( item );
106 } );
107
108 if ( defaultItem ) {
109 this.default = defaultItem.getID();
110 }
111
112 this.addItems( items );
113
114 this.emit( 'initialize' );
115 };
116
117 /**
118 * Add a query item
119 *
120 * @param {string} label Label for the new query
121 * @param {Object} data Data for the new query
122 */
123 mw.rcfilters.dm.SavedQueriesModel.prototype.addNewQuery = function ( label, data ) {
124 var randomID = ( new Date() ).getTime(),
125 normalizedData = $.extend( true, {}, this.baseState, data );
126
127 // Add item
128 this.addItems( [
129 new mw.rcfilters.dm.SavedQueryItemModel(
130 randomID,
131 label,
132 normalizedData
133 )
134 ] );
135 };
136
137 /**
138 * Remove query from model
139 *
140 * @param {string} queryID Query ID
141 */
142 mw.rcfilters.dm.SavedQueriesModel.prototype.removeQuery = function ( queryID ) {
143 var query = this.getItemByID( queryID );
144
145 if ( query ) {
146 // Check if this item was the default
147 if ( String( this.getDefault() ) === String( queryID ) ) {
148 // Nulify the default
149 this.setDefault( null );
150 }
151
152 this.removeItems( [ query ] );
153 }
154 };
155
156 /**
157 * Get an item that matches the requested query
158 *
159 * @param {Object} fullQueryComparison Object representing all filters and highlights to compare
160 * @return {mw.rcfilters.dm.SavedQueryItemModel} Matching item model
161 */
162 mw.rcfilters.dm.SavedQueriesModel.prototype.findMatchingQuery = function ( fullQueryComparison ) {
163 return this.getItems().filter( function ( item ) {
164 return OO.compare(
165 item.getData(),
166 fullQueryComparison
167 );
168 } )[ 0 ];
169 };
170
171 /**
172 * Get query by its identifier
173 *
174 * @param {string} queryID Query identifier
175 * @return {mw.rcfilters.dm.SavedQueryItemModel|undefined} Item matching
176 * the search. Undefined if not found.
177 */
178 mw.rcfilters.dm.SavedQueriesModel.prototype.getItemByID = function ( queryID ) {
179 return this.getItems().filter( function ( item ) {
180 return item.getID() === queryID;
181 } )[ 0 ];
182 };
183
184 /**
185 * Get the object representing the state of the entire model and items
186 *
187 * @return {Object} Object representing the state of the model and items
188 */
189 mw.rcfilters.dm.SavedQueriesModel.prototype.getState = function () {
190 var obj = { queries: {} };
191
192 // Translate the items to the saved object
193 this.getItems().forEach( function ( item ) {
194 var itemState = item.getState();
195
196 obj.queries[ item.getID() ] = itemState;
197 } );
198
199 if ( this.getDefault() ) {
200 obj.default = this.getDefault();
201 }
202
203 return obj;
204 };
205
206 /**
207 * Set a default query. Null to unset default.
208 *
209 * @param {string} itemID Query identifier
210 * @fires default
211 */
212 mw.rcfilters.dm.SavedQueriesModel.prototype.setDefault = function ( itemID ) {
213 if ( this.default !== itemID ) {
214 this.default = itemID;
215
216 // Set for individual itens
217 this.getItems().forEach( function ( item ) {
218 item.toggleDefault( item.getID() === itemID );
219 } );
220 }
221 };
222
223 /**
224 * Get the default query ID
225 *
226 * @return {string} Default query identifier
227 */
228 mw.rcfilters.dm.SavedQueriesModel.prototype.getDefault = function () {
229 return this.default;
230 };
231 }( mediaWiki, jQuery ) );