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 a prefixed label
85 * @param {boolean} inverted This item should be considered inverted
86 * @return {string} Prefixed label (HTML)
88 mw
.rcfilters
.dm
.ItemModel
.prototype.getPrefixedLabel = function ( inverted
) {
89 var escapedLabel
= mw
.html
.escape( this.getLabel() );
90 if ( this.labelPrefixKey
) {
91 if ( typeof this.labelPrefixKey
=== 'string' ) {
92 return mw
.message( this.labelPrefixKey
, escapedLabel
).parse();
96 // Only use inverted-prefix if the item is selected
97 // Highlight-only an inverted item makes no sense
98 inverted
&& this.isSelected() ?
99 'inverted' : 'default'
110 * Get the param name or value of this filter
112 * @return {string} Filter param name
114 mw
.rcfilters
.dm
.ItemModel
.prototype.getParamName = function () {
119 * Get the message representing the state of this model.
121 * @return {string} State message
123 mw
.rcfilters
.dm
.ItemModel
.prototype.getStateMessage = function () {
124 // Display description
125 return this.getDescription();
129 * Get the label of this filter
131 * @return {string} Filter label
133 mw
.rcfilters
.dm
.ItemModel
.prototype.getLabel = function () {
138 * Get the description of this filter
140 * @return {string} Filter description
142 mw
.rcfilters
.dm
.ItemModel
.prototype.getDescription = function () {
143 return this.description
;
147 * Get the default value of this filter
149 * @return {boolean} Filter default
151 mw
.rcfilters
.dm
.ItemModel
.prototype.getDefault = function () {
156 * Get the selected state of this filter
158 * @return {boolean} Filter is selected
160 mw
.rcfilters
.dm
.ItemModel
.prototype.isSelected = function () {
161 return this.selected
;
165 * Toggle the selected state of the item
167 * @param {boolean} [isSelected] Filter is selected
170 mw
.rcfilters
.dm
.ItemModel
.prototype.toggleSelected = function ( isSelected
) {
171 isSelected
= isSelected
=== undefined ? !this.selected
: isSelected
;
173 if ( this.selected
!== isSelected
) {
174 this.selected
= isSelected
;
175 this.emit( 'update' );
180 * Set the highlight color
182 * @param {string|null} highlightColor
184 mw
.rcfilters
.dm
.ItemModel
.prototype.setHighlightColor = function ( highlightColor
) {
185 if ( !this.isHighlightSupported() ) {
189 if ( this.highlightColor
!== highlightColor
) {
190 this.highlightColor
= highlightColor
;
191 this.emit( 'update' );
196 * Clear the highlight color
198 mw
.rcfilters
.dm
.ItemModel
.prototype.clearHighlightColor = function () {
199 this.setHighlightColor( null );
203 * Get the highlight color, or null if none is configured
205 * @return {string|null}
207 mw
.rcfilters
.dm
.ItemModel
.prototype.getHighlightColor = function () {
208 return this.highlightColor
;
212 * Get the CSS class that matches changes that fit this filter
213 * or null if none is configured
215 * @return {string|null}
217 mw
.rcfilters
.dm
.ItemModel
.prototype.getCssClass = function () {
218 return this.cssClass
;
222 * Get the item's identifiers
226 mw
.rcfilters
.dm
.ItemModel
.prototype.getIdentifiers = function () {
227 return this.identifiers
;
231 * Check if the highlight feature is supported for this filter
235 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlightSupported = function () {
236 return !!this.getCssClass();
240 * Check if the filter is currently highlighted
244 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlighted = function () {
245 return !!this.getHighlightColor();