Merge "Fix Postgres support"
[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
73 savedQueries = savedQueries || {};
74
75 this.baseState = baseState;
76
77 this.clearItems();
78 $.each( savedQueries.queries || {}, function ( id, obj ) {
79 var normalizedData = $.extend( true, {}, baseState, obj.data );
80
81 // Backwards-compat fix: We stored the 'highlight' state with
82 // "1" and "0" instead of true/false; for already-stored states,
83 // we need to fix that.
84 // NOTE: Since this feature is only available in beta, we should
85 // not need this line when we release this to the general wikis.
86 // This method will automatically fix all saved queries anyways
87 // for existing users, who are only betalabs users at the moment.
88 normalizedData.highlights.highlight = !!Number( normalizedData.highlights.highlight );
89
90 items.push(
91 new mw.rcfilters.dm.SavedQueryItemModel(
92 id,
93 obj.label,
94 normalizedData,
95 { 'default': savedQueries.default === id }
96 )
97 );
98 } );
99
100 this.default = savedQueries.default;
101
102 this.addItems( items );
103
104 this.emit( 'initialize' );
105 };
106
107 /**
108 * Add a query item
109 *
110 * @param {string} label Label for the new query
111 * @param {Object} data Data for the new query
112 */
113 mw.rcfilters.dm.SavedQueriesModel.prototype.addNewQuery = function ( label, data ) {
114 var randomID = ( new Date() ).getTime(),
115 normalizedData = $.extend( true, {}, this.baseState, data );
116
117 // Add item
118 this.addItems( [
119 new mw.rcfilters.dm.SavedQueryItemModel(
120 randomID,
121 label,
122 normalizedData
123 )
124 ] );
125 };
126
127 /**
128 * Get an item that matches the requested query
129 *
130 * @param {Object} fullQueryComparison Object representing all filters and highlights to compare
131 * @return {mw.rcfilters.dm.SavedQueryItemModel} Matching item model
132 */
133 mw.rcfilters.dm.SavedQueriesModel.prototype.findMatchingQuery = function ( fullQueryComparison ) {
134 return this.getItems().filter( function ( item ) {
135 return OO.compare(
136 item.getData(),
137 fullQueryComparison
138 );
139 } )[ 0 ];
140 };
141
142 /**
143 * Get query by its identifier
144 *
145 * @param {string} queryID Query identifier
146 * @return {mw.rcfilters.dm.SavedQueryItemModel|undefined} Item matching
147 * the search. Undefined if not found.
148 */
149 mw.rcfilters.dm.SavedQueriesModel.prototype.getItemByID = function ( queryID ) {
150 return this.getItems().filter( function ( item ) {
151 return item.getID() === queryID;
152 } )[ 0 ];
153 };
154
155 /**
156 * Get the object representing the state of the entire model and items
157 *
158 * @return {Object} Object representing the state of the model and items
159 */
160 mw.rcfilters.dm.SavedQueriesModel.prototype.getState = function () {
161 var obj = { queries: {} };
162
163 // Translate the items to the saved object
164 this.getItems().forEach( function ( item ) {
165 var itemState = item.getState();
166
167 obj.queries[ item.getID() ] = itemState;
168 } );
169
170 if ( this.getDefault() ) {
171 obj.default = this.getDefault();
172 }
173
174 return obj;
175 };
176
177 /**
178 * Set a default query. Null to unset default.
179 *
180 * @param {string} itemID Query identifier
181 * @fires default
182 */
183 mw.rcfilters.dm.SavedQueriesModel.prototype.setDefault = function ( itemID ) {
184 if ( this.default !== itemID ) {
185 this.default = itemID;
186
187 // Set for individual itens
188 this.getItems().forEach( function ( item ) {
189 item.toggleDefault( item.getID() === itemID );
190 } );
191 }
192 };
193
194 /**
195 * Get the default query ID
196 *
197 * @return {string} Default query identifier
198 */
199 mw.rcfilters.dm.SavedQueriesModel.prototype.getDefault = function () {
200 return this.default;
201 };
202 }( mediaWiki, jQuery ) );