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
;
48 OO
.initClass( mw
.rcfilters
.dm
.ItemModel
);
49 OO
.mixinClass( mw
.rcfilters
.dm
.ItemModel
, OO
.EventEmitter
);
56 * The state of this filter has changed
62 * Return the representation of the state of this item.
64 * @return {Object} State of the object
66 mw
.rcfilters
.dm
.ItemModel
.prototype.getState = function () {
68 selected
: this.isSelected()
73 * Get the name of this filter
75 * @return {string} Filter name
77 mw
.rcfilters
.dm
.ItemModel
.prototype.getName = function () {
82 * Get the message key to use to wrap the label. This message takes the label as a parameter.
84 * @param {boolean} inverted Whether this item should be considered inverted
85 * @return {string|null} Message key, or null if no message
87 mw
.rcfilters
.dm
.ItemModel
.prototype.getLabelMessageKey = function ( inverted
) {
88 if ( this.labelPrefixKey
) {
89 if ( typeof this.labelPrefixKey
=== 'string' ) {
90 return this.labelPrefixKey
;
92 return this.labelPrefixKey
[
93 // Only use inverted-prefix if the item is selected
94 // Highlight-only an inverted item makes no sense
95 inverted
&& this.isSelected() ?
96 'inverted' : 'default'
103 * Get the param name or value of this filter
105 * @return {string} Filter param name
107 mw
.rcfilters
.dm
.ItemModel
.prototype.getParamName = function () {
112 * Get the message representing the state of this model.
114 * @return {string} State message
116 mw
.rcfilters
.dm
.ItemModel
.prototype.getStateMessage = function () {
117 // Display description
118 return this.getDescription();
122 * Get the label of this filter
124 * @return {string} Filter label
126 mw
.rcfilters
.dm
.ItemModel
.prototype.getLabel = function () {
131 * Get the description of this filter
133 * @return {string} Filter description
135 mw
.rcfilters
.dm
.ItemModel
.prototype.getDescription = function () {
136 return this.description
;
140 * Get the default value of this filter
142 * @return {boolean} Filter default
144 mw
.rcfilters
.dm
.ItemModel
.prototype.getDefault = function () {
149 * Get the selected state of this filter
151 * @return {boolean} Filter is selected
153 mw
.rcfilters
.dm
.ItemModel
.prototype.isSelected = function () {
154 return this.selected
;
158 * Toggle the selected state of the item
160 * @param {boolean} [isSelected] Filter is selected
163 mw
.rcfilters
.dm
.ItemModel
.prototype.toggleSelected = function ( isSelected
) {
164 isSelected
= isSelected
=== undefined ? !this.selected
: isSelected
;
166 if ( this.selected
!== isSelected
) {
167 this.selected
= isSelected
;
168 this.emit( 'update' );
173 * Set the highlight color
175 * @param {string|null} highlightColor
177 mw
.rcfilters
.dm
.ItemModel
.prototype.setHighlightColor = function ( highlightColor
) {
178 if ( !this.isHighlightSupported() ) {
182 if ( this.highlightColor
!== highlightColor
) {
183 this.highlightColor
= highlightColor
;
184 this.emit( 'update' );
189 * Clear the highlight color
191 mw
.rcfilters
.dm
.ItemModel
.prototype.clearHighlightColor = function () {
192 this.setHighlightColor( null );
196 * Get the highlight color, or null if none is configured
198 * @return {string|null}
200 mw
.rcfilters
.dm
.ItemModel
.prototype.getHighlightColor = function () {
201 return this.highlightColor
;
205 * Get the CSS class that matches changes that fit this filter
206 * or null if none is configured
208 * @return {string|null}
210 mw
.rcfilters
.dm
.ItemModel
.prototype.getCssClass = function () {
211 return this.cssClass
;
215 * Get the item's identifiers
219 mw
.rcfilters
.dm
.ItemModel
.prototype.getIdentifiers = function () {
220 return this.identifiers
;
224 * Check if the highlight feature is supported for this filter
228 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlightSupported = function () {
229 return !!this.getCssClass();
233 * Check if the filter is currently highlighted
237 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlighted = function () {
238 return !!this.getHighlightColor();