Fix order of @var parameter in PHP
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / dm / ItemModel.js
1 ( function () {
2 /**
3 * RCFilter base item model
4 *
5 * @class mw.rcfilters.dm.ItemModel
6 * @mixins OO.EventEmitter
7 *
8 * @constructor
9 * @param {string} param Filter param name
10 * @param {Object} config Configuration object
11 * @cfg {string} [label] The label for the filter
12 * @cfg {string} [description] The description of the filter
13 * @cfg {string|Object} [labelPrefixKey] An i18n key defining the prefix label for this
14 * group. If the prefix has 'invert' state, the parameter is expected to be an object
15 * with 'default' and 'inverted' as keys.
16 * @cfg {boolean} [active=true] The filter is active and affecting the result
17 * @cfg {boolean} [selected] The item is selected
18 * @cfg {*} [value] The value of this item
19 * @cfg {string} [namePrefix='item_'] A prefix to add to the param name to act as a unique
20 * identifier
21 * @cfg {string} [cssClass] The class identifying the results that match this filter
22 * @cfg {string[]} [identifiers] An array of identifiers for this item. They will be
23 * added and considered in the view.
24 * @cfg {string} [defaultHighlightColor=null] If set, highlight this filter by default with this color
25 */
26 var ItemModel = function MwRcfiltersDmItemModel( param, config ) {
27 config = config || {};
28
29 // Mixin constructor
30 OO.EventEmitter.call( this );
31
32 this.param = param;
33 this.namePrefix = config.namePrefix || 'item_';
34 this.name = this.namePrefix + param;
35
36 this.label = config.label || this.name;
37 this.labelPrefixKey = config.labelPrefixKey;
38 this.description = config.description || '';
39 this.setValue( config.value || config.selected );
40
41 this.identifiers = config.identifiers || [];
42
43 // Highlight
44 this.cssClass = config.cssClass;
45 this.highlightColor = config.defaultHighlightColor || null;
46 };
47
48 /* Initialization */
49
50 OO.initClass( ItemModel );
51 OO.mixinClass( ItemModel, OO.EventEmitter );
52
53 /* Events */
54
55 /**
56 * @event update
57 *
58 * The state of this filter has changed
59 */
60
61 /* Methods */
62
63 /**
64 * Return the representation of the state of this item.
65 *
66 * @return {Object} State of the object
67 */
68 ItemModel.prototype.getState = function () {
69 return {
70 selected: this.isSelected()
71 };
72 };
73
74 /**
75 * Get the name of this filter
76 *
77 * @return {string} Filter name
78 */
79 ItemModel.prototype.getName = function () {
80 return this.name;
81 };
82
83 /**
84 * Get the message key to use to wrap the label. This message takes the label as a parameter.
85 *
86 * @param {boolean} inverted Whether this item should be considered inverted
87 * @return {string|null} Message key, or null if no message
88 */
89 ItemModel.prototype.getLabelMessageKey = function ( inverted ) {
90 if ( this.labelPrefixKey ) {
91 if ( typeof this.labelPrefixKey === 'string' ) {
92 return this.labelPrefixKey;
93 }
94 return this.labelPrefixKey[
95 // Only use inverted-prefix if the item is selected
96 // Highlight-only an inverted item makes no sense
97 inverted && this.isSelected() ?
98 'inverted' : 'default'
99 ];
100 }
101 return null;
102 };
103
104 /**
105 * Get the param name or value of this filter
106 *
107 * @return {string} Filter param name
108 */
109 ItemModel.prototype.getParamName = function () {
110 return this.param;
111 };
112
113 /**
114 * Get the message representing the state of this model.
115 *
116 * @return {string} State message
117 */
118 ItemModel.prototype.getStateMessage = function () {
119 // Display description
120 return this.getDescription();
121 };
122
123 /**
124 * Get the label of this filter
125 *
126 * @return {string} Filter label
127 */
128 ItemModel.prototype.getLabel = function () {
129 return this.label;
130 };
131
132 /**
133 * Get the description of this filter
134 *
135 * @return {string} Filter description
136 */
137 ItemModel.prototype.getDescription = function () {
138 return this.description;
139 };
140
141 /**
142 * Get the default value of this filter
143 *
144 * @return {boolean} Filter default
145 */
146 ItemModel.prototype.getDefault = function () {
147 return this.default;
148 };
149
150 /**
151 * Get the selected state of this filter
152 *
153 * @return {boolean} Filter is selected
154 */
155 ItemModel.prototype.isSelected = function () {
156 return !!this.value;
157 };
158
159 /**
160 * Toggle the selected state of the item
161 *
162 * @param {boolean} [isSelected] Filter is selected
163 * @fires update
164 */
165 ItemModel.prototype.toggleSelected = function ( isSelected ) {
166 isSelected = isSelected === undefined ? !this.isSelected() : isSelected;
167 this.setValue( isSelected );
168 };
169
170 /**
171 * Get the value
172 *
173 * @return {*}
174 */
175 ItemModel.prototype.getValue = function () {
176 return this.value;
177 };
178
179 /**
180 * Convert a given value to the appropriate representation based on group type
181 *
182 * @param {*} value
183 * @return {*}
184 */
185 ItemModel.prototype.coerceValue = function ( value ) {
186 return this.getGroupModel().getType() === 'any_value' ? value : !!value;
187 };
188
189 /**
190 * Set the value
191 *
192 * @param {*} newValue
193 */
194 ItemModel.prototype.setValue = function ( newValue ) {
195 newValue = this.coerceValue( newValue );
196 if ( this.value !== newValue ) {
197 this.value = newValue;
198 this.emit( 'update' );
199 }
200 };
201
202 /**
203 * Set the highlight color
204 *
205 * @param {string|null} highlightColor
206 */
207 ItemModel.prototype.setHighlightColor = function ( highlightColor ) {
208 if ( !this.isHighlightSupported() ) {
209 return;
210 }
211 // If the highlight color on the item and in the parameter is null/undefined, return early.
212 if ( !this.highlightColor && !highlightColor ) {
213 return;
214 }
215
216 if ( this.highlightColor !== highlightColor ) {
217 this.highlightColor = highlightColor;
218 this.emit( 'update' );
219 }
220 };
221
222 /**
223 * Clear the highlight color
224 */
225 ItemModel.prototype.clearHighlightColor = function () {
226 this.setHighlightColor( null );
227 };
228
229 /**
230 * Get the highlight color, or null if none is configured
231 *
232 * @return {string|null}
233 */
234 ItemModel.prototype.getHighlightColor = function () {
235 return this.highlightColor;
236 };
237
238 /**
239 * Get the CSS class that matches changes that fit this filter
240 * or null if none is configured
241 *
242 * @return {string|null}
243 */
244 ItemModel.prototype.getCssClass = function () {
245 return this.cssClass;
246 };
247
248 /**
249 * Get the item's identifiers
250 *
251 * @return {string[]}
252 */
253 ItemModel.prototype.getIdentifiers = function () {
254 return this.identifiers;
255 };
256
257 /**
258 * Check if the highlight feature is supported for this filter
259 *
260 * @return {boolean}
261 */
262 ItemModel.prototype.isHighlightSupported = function () {
263 return !!this.getCssClass();
264 };
265
266 /**
267 * Check if the filter is currently highlighted
268 *
269 * @return {boolean}
270 */
271 ItemModel.prototype.isHighlighted = function () {
272 return !!this.getHighlightColor();
273 };
274
275 module.exports = ItemModel;
276 }() );