Merge "Revised styling of sister-search sidebar."
[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 items.push(
81 new mw.rcfilters.dm.SavedQueryItemModel(
82 id,
83 obj.label,
84 normalizedData,
85 { 'default': savedQueries.default === id }
86 )
87 );
88 } );
89
90 this.default = savedQueries.default;
91
92 this.addItems( items );
93
94 this.emit( 'initialize' );
95 };
96
97 /**
98 * Add a query item
99 *
100 * @param {string} label Label for the new query
101 * @param {Object} data Data for the new query
102 */
103 mw.rcfilters.dm.SavedQueriesModel.prototype.addNewQuery = function ( label, data ) {
104 var randomID = ( new Date() ).getTime(),
105 normalizedData = $.extend( true, {}, this.baseState, data );
106
107 // Add item
108 this.addItems( [
109 new mw.rcfilters.dm.SavedQueryItemModel(
110 randomID,
111 label,
112 normalizedData
113 )
114 ] );
115 };
116
117 /**
118 * Get an item that matches the requested query
119 *
120 * @param {Object} fullQueryComparison Object representing all filters and highlights to compare
121 * @return {mw.rcfilters.dm.SavedQueryItemModel} Matching item model
122 */
123 mw.rcfilters.dm.SavedQueriesModel.prototype.findMatchingQuery = function ( fullQueryComparison ) {
124 return this.getItems().filter( function ( item ) {
125 return OO.compare(
126 item.getData(),
127 fullQueryComparison
128 );
129 } )[ 0 ];
130 };
131
132 /**
133 * Get query by its identifier
134 *
135 * @param {string} queryID Query identifier
136 * @return {mw.rcfilters.dm.SavedQueryItemModel|undefined} Item matching
137 * the search. Undefined if not found.
138 */
139 mw.rcfilters.dm.SavedQueriesModel.prototype.getItemByID = function ( queryID ) {
140 return this.getItems().filter( function ( item ) {
141 return item.getID() === queryID;
142 } )[ 0 ];
143 };
144
145 /**
146 * Get the object representing the state of the entire model and items
147 *
148 * @return {Object} Object representing the state of the model and items
149 */
150 mw.rcfilters.dm.SavedQueriesModel.prototype.getState = function () {
151 var obj = { queries: {} };
152
153 // Translate the items to the saved object
154 this.getItems().forEach( function ( item ) {
155 var itemState = item.getState();
156
157 obj.queries[ item.getID() ] = itemState;
158 } );
159
160 if ( this.getDefault() ) {
161 obj.default = this.getDefault();
162 }
163
164 return obj;
165 };
166
167 /**
168 * Set a default query. Null to unset default.
169 *
170 * @param {string} itemID Query identifier
171 * @fires default
172 */
173 mw.rcfilters.dm.SavedQueriesModel.prototype.setDefault = function ( itemID ) {
174 if ( this.default !== itemID ) {
175 this.default = itemID;
176
177 // Set for individual itens
178 this.getItems().forEach( function ( item ) {
179 item.toggleDefault( item.getID() === itemID );
180 } );
181 }
182 };
183
184 /**
185 * Get the default query ID
186 *
187 * @return {string} Default query identifier
188 */
189 mw.rcfilters.dm.SavedQueriesModel.prototype.getDefault = function () {
190 return this.default;
191 };
192 }( mediaWiki, jQuery ) );