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 {boolean} [active=true] The filter is active and affecting the result
13 * @cfg {boolean} [selected] The item is selected
14 * @cfg {boolean} [inverted] The item is inverted, meaning the search is excluding
16 * @cfg {string} [namePrefix='item_'] A prefix to add to the param name to act as a unique
18 * @cfg {string} [cssClass] The class identifying the results that match this filter
20 mw
.rcfilters
.dm
.ItemModel
= function MwRcfiltersDmItemModel( param
, config
) {
21 config
= config
|| {};
24 OO
.EventEmitter
.call( this );
27 this.namePrefix
= config
.namePrefix
|| 'item_';
28 this.name
= this.namePrefix
+ param
;
30 this.label
= config
.label
|| this.name
;
31 this.description
= config
.description
;
32 this.selected
= !!config
.selected
;
34 this.inverted
= !!config
.inverted
;
37 this.cssClass
= config
.cssClass
;
38 this.highlightColor
= null;
39 this.highlightEnabled
= false;
44 OO
.initClass( mw
.rcfilters
.dm
.ItemModel
);
45 OO
.mixinClass( mw
.rcfilters
.dm
.ItemModel
, OO
.EventEmitter
);
52 * The state of this filter has changed
58 * Return the representation of the state of this item.
60 * @return {Object} State of the object
62 mw
.rcfilters
.dm
.ItemModel
.prototype.getState = function () {
64 selected
: this.isSelected(),
65 inverted
: this.isInverted()
70 * Get the name of this filter
72 * @return {string} Filter name
74 mw
.rcfilters
.dm
.ItemModel
.prototype.getName = function () {
79 * Get the param name or value of this filter
81 * @return {string} Filter param name
83 mw
.rcfilters
.dm
.ItemModel
.prototype.getParamName = function () {
88 * Get the message representing the state of this model.
90 * @return {string} State message
92 mw
.rcfilters
.dm
.ItemModel
.prototype.getStateMessage = function () {
93 // Display description
94 return this.getDescription();
98 * Get the label of this filter
100 * @return {string} Filter label
102 mw
.rcfilters
.dm
.ItemModel
.prototype.getLabel = function () {
107 * Get the description of this filter
109 * @return {string} Filter description
111 mw
.rcfilters
.dm
.ItemModel
.prototype.getDescription = function () {
112 return this.description
;
116 * Get the default value of this filter
118 * @return {boolean} Filter default
120 mw
.rcfilters
.dm
.ItemModel
.prototype.getDefault = function () {
125 * Get the selected state of this filter
127 * @return {boolean} Filter is selected
129 mw
.rcfilters
.dm
.ItemModel
.prototype.isSelected = function () {
130 return this.selected
;
134 * Toggle the selected state of the item
136 * @param {boolean} [isSelected] Filter is selected
139 mw
.rcfilters
.dm
.ItemModel
.prototype.toggleSelected = function ( isSelected
) {
140 isSelected
= isSelected
=== undefined ? !this.selected
: isSelected
;
142 if ( this.selected
!== isSelected
) {
143 this.selected
= isSelected
;
144 this.emit( 'update' );
149 * Get the inverted state of this item
151 * @return {boolean} Item is inverted
153 mw
.rcfilters
.dm
.ItemModel
.prototype.isInverted = function () {
154 return this.inverted
;
158 * Toggle the inverted state of the item
160 * @param {boolean} [isInverted] Item is inverted
163 mw
.rcfilters
.dm
.ItemModel
.prototype.toggleInverted = function ( isInverted
) {
164 isInverted
= isInverted
=== undefined ? !this.inverted
: isInverted
;
166 if ( this.inverted
!== isInverted
) {
167 this.inverted
= isInverted
;
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.highlightColor
!== highlightColor
) {
179 this.highlightColor
= highlightColor
;
180 this.emit( 'update' );
185 * Clear the highlight color
187 mw
.rcfilters
.dm
.ItemModel
.prototype.clearHighlightColor = function () {
188 this.setHighlightColor( null );
192 * Get the highlight color, or null if none is configured
194 * @return {string|null}
196 mw
.rcfilters
.dm
.ItemModel
.prototype.getHighlightColor = function () {
197 return this.highlightColor
;
201 * Get the CSS class that matches changes that fit this filter
202 * or null if none is configured
204 * @return {string|null}
206 mw
.rcfilters
.dm
.ItemModel
.prototype.getCssClass = function () {
207 return this.cssClass
;
211 * Toggle the highlight feature on and off for this filter.
212 * It only works if highlight is supported for this filter.
214 * @param {boolean} enable Highlight should be enabled
216 mw
.rcfilters
.dm
.ItemModel
.prototype.toggleHighlight = function ( enable
) {
217 enable
= enable
=== undefined ? !this.highlightEnabled
: enable
;
219 if ( !this.isHighlightSupported() ) {
223 if ( enable
=== this.highlightEnabled
) {
227 this.highlightEnabled
= enable
;
228 this.emit( 'update' );
232 * Check if the highlight feature is currently enabled for this filter
236 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlightEnabled = function () {
237 return !!this.highlightEnabled
;
241 * Check if the highlight feature is supported for this filter
245 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlightSupported = function () {
246 return !!this.getCssClass();
250 * Check if the filter is currently highlighted
254 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlighted = function () {
255 return this.isHighlightEnabled() && !!this.getHighlightColor();