3 * RCFilter base item model
5 * @class mw.rcfilters.dm.ItemModel
6 * @mixins OO.EventEmitter
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
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
26 var ItemModel
= function MwRcfiltersDmItemModel( param
, config
) {
27 config
= config
|| {};
30 OO
.EventEmitter
.call( this );
33 this.namePrefix
= config
.namePrefix
|| 'item_';
34 this.name
= this.namePrefix
+ param
;
36 this.label
= config
.label
|| this.name
;
37 this.labelPrefixKey
= config
.labelPrefixKey
;
38 this.description
= config
.description
|| '';
39 this.setValue( config
.value
|| config
.selected
);
41 this.identifiers
= config
.identifiers
|| [];
44 this.cssClass
= config
.cssClass
;
45 this.highlightColor
= config
.defaultHighlightColor
|| null;
50 OO
.initClass( ItemModel
);
51 OO
.mixinClass( ItemModel
, OO
.EventEmitter
);
58 * The state of this filter has changed
64 * Return the representation of the state of this item.
66 * @return {Object} State of the object
68 ItemModel
.prototype.getState = function () {
70 selected
: this.isSelected()
75 * Get the name of this filter
77 * @return {string} Filter name
79 ItemModel
.prototype.getName = function () {
84 * Get the message key to use to wrap the label. This message takes the label as a parameter.
86 * @param {boolean} inverted Whether this item should be considered inverted
87 * @return {string|null} Message key, or null if no message
89 ItemModel
.prototype.getLabelMessageKey = function ( inverted
) {
90 if ( this.labelPrefixKey
) {
91 if ( typeof this.labelPrefixKey
=== 'string' ) {
92 return this.labelPrefixKey
;
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'
105 * Get the param name or value of this filter
107 * @return {string} Filter param name
109 ItemModel
.prototype.getParamName = function () {
114 * Get the message representing the state of this model.
116 * @return {string} State message
118 ItemModel
.prototype.getStateMessage = function () {
119 // Display description
120 return this.getDescription();
124 * Get the label of this filter
126 * @return {string} Filter label
128 ItemModel
.prototype.getLabel = function () {
133 * Get the description of this filter
135 * @return {string} Filter description
137 ItemModel
.prototype.getDescription = function () {
138 return this.description
;
142 * Get the default value of this filter
144 * @return {boolean} Filter default
146 ItemModel
.prototype.getDefault = function () {
151 * Get the selected state of this filter
153 * @return {boolean} Filter is selected
155 ItemModel
.prototype.isSelected = function () {
160 * Toggle the selected state of the item
162 * @param {boolean} [isSelected] Filter is selected
165 ItemModel
.prototype.toggleSelected = function ( isSelected
) {
166 isSelected
= isSelected
=== undefined ? !this.isSelected() : isSelected
;
167 this.setValue( isSelected
);
175 ItemModel
.prototype.getValue = function () {
180 * Convert a given value to the appropriate representation based on group type
185 ItemModel
.prototype.coerceValue = function ( value
) {
186 return this.getGroupModel().getType() === 'any_value' ? value
: !!value
;
192 * @param {*} newValue
194 ItemModel
.prototype.setValue = function ( newValue
) {
195 newValue
= this.coerceValue( newValue
);
196 if ( this.value
!== newValue
) {
197 this.value
= newValue
;
198 this.emit( 'update' );
203 * Set the highlight color
205 * @param {string|null} highlightColor
207 ItemModel
.prototype.setHighlightColor = function ( highlightColor
) {
208 if ( !this.isHighlightSupported() ) {
211 // If the highlight color on the item and in the parameter is null/undefined, return early.
212 if ( !this.highlightColor
&& !highlightColor
) {
216 if ( this.highlightColor
!== highlightColor
) {
217 this.highlightColor
= highlightColor
;
218 this.emit( 'update' );
223 * Clear the highlight color
225 ItemModel
.prototype.clearHighlightColor = function () {
226 this.setHighlightColor( null );
230 * Get the highlight color, or null if none is configured
232 * @return {string|null}
234 ItemModel
.prototype.getHighlightColor = function () {
235 return this.highlightColor
;
239 * Get the CSS class that matches changes that fit this filter
240 * or null if none is configured
242 * @return {string|null}
244 ItemModel
.prototype.getCssClass = function () {
245 return this.cssClass
;
249 * Get the item's identifiers
253 ItemModel
.prototype.getIdentifiers = function () {
254 return this.identifiers
;
258 * Check if the highlight feature is supported for this filter
262 ItemModel
.prototype.isHighlightSupported = function () {
263 return !!this.getCssClass();
267 * Check if the filter is currently highlighted
271 ItemModel
.prototype.isHighlighted = function () {
272 return !!this.getHighlightColor();
275 module
.exports
= ItemModel
;