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 {string} [namePrefix='item_'] A prefix to add to the param name to act as a unique
19 * @cfg {string} [cssClass] The class identifying the results that match this filter
20 * @cfg {string[]} [identifiers] An array of identifiers for this item. They will be
21 * added and considered in the view.
22 * @cfg {string} [defaultHighlightColor] If set, highlight this filter by default with this color
24 mw
.rcfilters
.dm
.ItemModel
= function MwRcfiltersDmItemModel( param
, config
) {
25 config
= config
|| {};
28 OO
.EventEmitter
.call( this );
31 this.namePrefix
= config
.namePrefix
|| 'item_';
32 this.name
= this.namePrefix
+ param
;
34 this.label
= config
.label
|| this.name
;
35 this.labelPrefixKey
= config
.labelPrefixKey
;
36 this.description
= config
.description
|| '';
37 this.selected
= !!config
.selected
;
39 this.identifiers
= config
.identifiers
|| [];
42 this.cssClass
= config
.cssClass
;
43 this.highlightColor
= config
.defaultHighlightColor
;
44 this.highlightEnabled
= !!config
.defaultHighlightColor
;
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 () {
155 return this.selected
;
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.selected
: isSelected
;
167 if ( this.selected
!== isSelected
) {
168 this.selected
= isSelected
;
169 this.emit( 'update' );
174 * Set the highlight color
176 * @param {string|null} highlightColor
178 mw
.rcfilters
.dm
.ItemModel
.prototype.setHighlightColor = function ( highlightColor
) {
179 if ( !this.isHighlightSupported() ) {
183 if ( this.highlightColor
!== highlightColor
) {
184 this.highlightColor
= highlightColor
;
185 this.emit( 'update' );
190 * Clear the highlight color
192 mw
.rcfilters
.dm
.ItemModel
.prototype.clearHighlightColor = function () {
193 this.setHighlightColor( null );
197 * Get the highlight color, or null if none is configured
199 * @return {string|null}
201 mw
.rcfilters
.dm
.ItemModel
.prototype.getHighlightColor = function () {
202 return this.highlightColor
;
206 * Get the CSS class that matches changes that fit this filter
207 * or null if none is configured
209 * @return {string|null}
211 mw
.rcfilters
.dm
.ItemModel
.prototype.getCssClass = function () {
212 return this.cssClass
;
216 * Get the item's identifiers
220 mw
.rcfilters
.dm
.ItemModel
.prototype.getIdentifiers = function () {
221 return this.identifiers
;
225 * Check if the highlight feature is supported for this filter
229 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlightSupported = function () {
230 return !!this.getCssClass();
234 * Check if the filter is currently highlighted
238 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlighted = function () {
239 return !!this.getHighlightColor();