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