3 * RCFilter base item model
5 * @mixins OO.EventEmitter
8 * @param {string} param Filter param name
9 * @param {Object} config Configuration object
10 * @cfg {string} [label] The label for the filter
11 * @cfg {string} [description] The description of the filter
12 * @cfg {string|Object} [labelPrefixKey] An i18n key defining the prefix label for this
13 * group. If the prefix has 'invert' state, the parameter is expected to be an object
14 * with 'default' and 'inverted' as keys.
15 * @cfg {boolean} [active=true] The filter is active and affecting the result
16 * @cfg {boolean} [selected] The item is selected
17 * @cfg {*} [value] The value of this item
18 * @cfg {string} [namePrefix='item_'] A prefix to add to the param name to act as a unique
20 * @cfg {string} [cssClass] The class identifying the results that match this filter
21 * @cfg {string[]} [identifiers] An array of identifiers for this item. They will be
22 * added and considered in the view.
23 * @cfg {string} [defaultHighlightColor=null] If set, highlight this filter by default with this color
25 mw
.rcfilters
.dm
.ItemModel
= function MwRcfiltersDmItemModel( param
, config
) {
26 config
= config
|| {};
29 OO
.EventEmitter
.call( this );
32 this.namePrefix
= config
.namePrefix
|| 'item_';
33 this.name
= this.namePrefix
+ param
;
35 this.label
= config
.label
|| this.name
;
36 this.labelPrefixKey
= config
.labelPrefixKey
;
37 this.description
= config
.description
|| '';
38 this.setValue( config
.value
|| config
.selected
);
40 this.identifiers
= config
.identifiers
|| [];
43 this.cssClass
= config
.cssClass
;
44 this.highlightColor
= config
.defaultHighlightColor
|| null;
49 OO
.initClass( mw
.rcfilters
.dm
.ItemModel
);
50 OO
.mixinClass( mw
.rcfilters
.dm
.ItemModel
, OO
.EventEmitter
);
57 * The state of this filter has changed
63 * Return the representation of the state of this item.
65 * @return {Object} State of the object
67 mw
.rcfilters
.dm
.ItemModel
.prototype.getState = function () {
69 selected
: this.isSelected()
74 * Get the name of this filter
76 * @return {string} Filter name
78 mw
.rcfilters
.dm
.ItemModel
.prototype.getName = function () {
83 * Get the message key to use to wrap the label. This message takes the label as a parameter.
85 * @param {boolean} inverted Whether this item should be considered inverted
86 * @return {string|null} Message key, or null if no message
88 mw
.rcfilters
.dm
.ItemModel
.prototype.getLabelMessageKey = function ( inverted
) {
89 if ( this.labelPrefixKey
) {
90 if ( typeof this.labelPrefixKey
=== 'string' ) {
91 return this.labelPrefixKey
;
93 return this.labelPrefixKey
[
94 // Only use inverted-prefix if the item is selected
95 // Highlight-only an inverted item makes no sense
96 inverted
&& this.isSelected() ?
97 'inverted' : 'default'
104 * Get the param name or value of this filter
106 * @return {string} Filter param name
108 mw
.rcfilters
.dm
.ItemModel
.prototype.getParamName = function () {
113 * Get the message representing the state of this model.
115 * @return {string} State message
117 mw
.rcfilters
.dm
.ItemModel
.prototype.getStateMessage = function () {
118 // Display description
119 return this.getDescription();
123 * Get the label of this filter
125 * @return {string} Filter label
127 mw
.rcfilters
.dm
.ItemModel
.prototype.getLabel = function () {
132 * Get the description of this filter
134 * @return {string} Filter description
136 mw
.rcfilters
.dm
.ItemModel
.prototype.getDescription = function () {
137 return this.description
;
141 * Get the default value of this filter
143 * @return {boolean} Filter default
145 mw
.rcfilters
.dm
.ItemModel
.prototype.getDefault = function () {
150 * Get the selected state of this filter
152 * @return {boolean} Filter is selected
154 mw
.rcfilters
.dm
.ItemModel
.prototype.isSelected = function () {
159 * Toggle the selected state of the item
161 * @param {boolean} [isSelected] Filter is selected
164 mw
.rcfilters
.dm
.ItemModel
.prototype.toggleSelected = function ( isSelected
) {
165 isSelected
= isSelected
=== undefined ? !this.isSelected() : isSelected
;
166 this.setValue( isSelected
);
174 mw
.rcfilters
.dm
.ItemModel
.prototype.getValue = function () {
179 * Convert a given value to the appropriate representation based on group type
184 mw
.rcfilters
.dm
.ItemModel
.prototype.coerceValue = function ( value
) {
185 return this.getGroupModel().getType() === 'any_value' ? value
: !!value
;
191 * @param {*} newValue
193 mw
.rcfilters
.dm
.ItemModel
.prototype.setValue = function ( newValue
) {
194 newValue
= this.coerceValue( newValue
);
195 if ( this.value
!== newValue
) {
196 this.value
= newValue
;
197 this.emit( 'update' );
202 * Set the highlight color
204 * @param {string|null} highlightColor
206 mw
.rcfilters
.dm
.ItemModel
.prototype.setHighlightColor = function ( highlightColor
) {
207 if ( !this.isHighlightSupported() ) {
210 // If the highlight color on the item and in the parameter is null/undefined, return early.
211 if ( !this.highlightColor
&& !highlightColor
) {
215 if ( this.highlightColor
!== highlightColor
) {
216 this.highlightColor
= highlightColor
;
217 this.emit( 'update' );
222 * Clear the highlight color
224 mw
.rcfilters
.dm
.ItemModel
.prototype.clearHighlightColor = function () {
225 this.setHighlightColor( null );
229 * Get the highlight color, or null if none is configured
231 * @return {string|null}
233 mw
.rcfilters
.dm
.ItemModel
.prototype.getHighlightColor = function () {
234 return this.highlightColor
;
238 * Get the CSS class that matches changes that fit this filter
239 * or null if none is configured
241 * @return {string|null}
243 mw
.rcfilters
.dm
.ItemModel
.prototype.getCssClass = function () {
244 return this.cssClass
;
248 * Get the item's identifiers
252 mw
.rcfilters
.dm
.ItemModel
.prototype.getIdentifiers = function () {
253 return this.identifiers
;
257 * Check if the highlight feature is supported for this filter
261 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlightSupported = function () {
262 return !!this.getCssClass();
266 * Check if the filter is currently highlighted
270 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlighted = function () {
271 return !!this.getHighlightColor();