Merge "Follow-up to '3D' file type addition"
[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 * @return {string} ID of the newly added query
123 */
124 mw.rcfilters.dm.SavedQueriesModel.prototype.addNewQuery = function ( label, data ) {
125 var randomID = ( new Date() ).getTime(),
126 normalizedData = $.extend( true, {}, this.baseState, data );
127
128 // Add item
129 this.addItems( [
130 new mw.rcfilters.dm.SavedQueryItemModel(
131 randomID,
132 label,
133 normalizedData
134 )
135 ] );
136
137 return randomID;
138 };
139
140 /**
141 * Remove query from model
142 *
143 * @param {string} queryID Query ID
144 */
145 mw.rcfilters.dm.SavedQueriesModel.prototype.removeQuery = function ( queryID ) {
146 var query = this.getItemByID( queryID );
147
148 if ( query ) {
149 // Check if this item was the default
150 if ( String( this.getDefault() ) === String( queryID ) ) {
151 // Nulify the default
152 this.setDefault( null );
153 }
154
155 this.removeItems( [ query ] );
156 }
157 };
158
159 /**
160 * Get an item that matches the requested query
161 *
162 * @param {Object} fullQueryComparison Object representing all filters and highlights to compare
163 * @return {mw.rcfilters.dm.SavedQueryItemModel} Matching item model
164 */
165 mw.rcfilters.dm.SavedQueriesModel.prototype.findMatchingQuery = function ( fullQueryComparison ) {
166 return this.getItems().filter( function ( item ) {
167 return OO.compare(
168 item.getData(),
169 fullQueryComparison
170 );
171 } )[ 0 ];
172 };
173
174 /**
175 * Get query by its identifier
176 *
177 * @param {string} queryID Query identifier
178 * @return {mw.rcfilters.dm.SavedQueryItemModel|undefined} Item matching
179 * the search. Undefined if not found.
180 */
181 mw.rcfilters.dm.SavedQueriesModel.prototype.getItemByID = function ( queryID ) {
182 return this.getItems().filter( function ( item ) {
183 return item.getID() === queryID;
184 } )[ 0 ];
185 };
186
187 /**
188 * Get the object representing the state of the entire model and items
189 *
190 * @return {Object} Object representing the state of the model and items
191 */
192 mw.rcfilters.dm.SavedQueriesModel.prototype.getState = function () {
193 var obj = { queries: {} };
194
195 // Translate the items to the saved object
196 this.getItems().forEach( function ( item ) {
197 var itemState = item.getState();
198
199 obj.queries[ item.getID() ] = itemState;
200 } );
201
202 if ( this.getDefault() ) {
203 obj.default = this.getDefault();
204 }
205
206 return obj;
207 };
208
209 /**
210 * Set a default query. Null to unset default.
211 *
212 * @param {string} itemID Query identifier
213 * @fires default
214 */
215 mw.rcfilters.dm.SavedQueriesModel.prototype.setDefault = function ( itemID ) {
216 if ( this.default !== itemID ) {
217 this.default = itemID;
218
219 // Set for individual itens
220 this.getItems().forEach( function ( item ) {
221 item.toggleDefault( item.getID() === itemID );
222 } );
223 }
224 };
225
226 /**
227 * Get the default query ID
228 *
229 * @return {string} Default query identifier
230 */
231 mw.rcfilters.dm.SavedQueriesModel.prototype.getDefault = function () {
232 return this.default;
233 };
234 }( mediaWiki, jQuery ) );