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 {boolean} [inverted] The item is inverted, meaning the search is excluding
19 * @cfg {string} [namePrefix='item_'] A prefix to add to the param name to act as a unique
21 * @cfg {string} [cssClass] The class identifying the results that match this filter
22 * @cfg {string[]} [identifiers] An array of identifiers for this item. They will be
23 * added and considered in the view.
24 * @cfg {string} [defaultHighlightColor] If set, highlight this filter by default with this color
26 mw
.rcfilters
.dm
.ItemModel
= function MwRcfiltersDmItemModel( param
, config
) {
27 config
= config
|| {};
30 OO
.EventEmitter
.call( this );
33 this.namePrefix
= config
.namePrefix
|| 'item_';
34 this.name
= this.namePrefix
+ param
;
36 this.label
= config
.label
|| this.name
;
37 this.labelPrefixKey
= config
.labelPrefixKey
;
38 this.description
= config
.description
|| '';
39 this.selected
= !!config
.selected
;
41 this.inverted
= !!config
.inverted
;
42 this.identifiers
= config
.identifiers
|| [];
45 this.cssClass
= config
.cssClass
;
46 this.highlightColor
= config
.defaultHighlightColor
;
47 this.highlightEnabled
= !!config
.defaultHighlightColor
;
52 OO
.initClass( mw
.rcfilters
.dm
.ItemModel
);
53 OO
.mixinClass( mw
.rcfilters
.dm
.ItemModel
, OO
.EventEmitter
);
60 * The state of this filter has changed
66 * Return the representation of the state of this item.
68 * @return {Object} State of the object
70 mw
.rcfilters
.dm
.ItemModel
.prototype.getState = function () {
72 selected
: this.isSelected(),
73 inverted
: this.isInverted()
78 * Get the name of this filter
80 * @return {string} Filter name
82 mw
.rcfilters
.dm
.ItemModel
.prototype.getName = function () {
87 * Get a prefixed label
89 * @return {string} Prefixed label
91 mw
.rcfilters
.dm
.ItemModel
.prototype.getPrefixedLabel = function () {
92 if ( this.labelPrefixKey
) {
93 if ( typeof this.labelPrefixKey
=== 'string' ) {
94 return mw
.message( this.labelPrefixKey
, this.getLabel() ).parse();
98 // Only use inverted-prefix if the item is selected
99 // Highlight-only an inverted item makes no sense
100 this.isInverted() && this.isSelected() ?
101 'inverted' : 'default'
107 return this.getLabel();
112 * Get the param name or value of this filter
114 * @return {string} Filter param name
116 mw
.rcfilters
.dm
.ItemModel
.prototype.getParamName = function () {
121 * Get the message representing the state of this model.
123 * @return {string} State message
125 mw
.rcfilters
.dm
.ItemModel
.prototype.getStateMessage = function () {
126 // Display description
127 return this.getDescription();
131 * Get the label of this filter
133 * @return {string} Filter label
135 mw
.rcfilters
.dm
.ItemModel
.prototype.getLabel = function () {
140 * Get the description of this filter
142 * @return {string} Filter description
144 mw
.rcfilters
.dm
.ItemModel
.prototype.getDescription = function () {
145 return this.description
;
149 * Get the default value of this filter
151 * @return {boolean} Filter default
153 mw
.rcfilters
.dm
.ItemModel
.prototype.getDefault = function () {
158 * Get the selected state of this filter
160 * @return {boolean} Filter is selected
162 mw
.rcfilters
.dm
.ItemModel
.prototype.isSelected = function () {
163 return this.selected
;
167 * Toggle the selected state of the item
169 * @param {boolean} [isSelected] Filter is selected
172 mw
.rcfilters
.dm
.ItemModel
.prototype.toggleSelected = function ( isSelected
) {
173 isSelected
= isSelected
=== undefined ? !this.selected
: isSelected
;
175 if ( this.selected
!== isSelected
) {
176 this.selected
= isSelected
;
177 this.emit( 'update' );
182 * Get the inverted state of this item
184 * @return {boolean} Item is inverted
186 mw
.rcfilters
.dm
.ItemModel
.prototype.isInverted = function () {
187 return this.inverted
;
191 * Toggle the inverted state of the item
193 * @param {boolean} [isInverted] Item is inverted
196 mw
.rcfilters
.dm
.ItemModel
.prototype.toggleInverted = function ( isInverted
) {
197 isInverted
= isInverted
=== undefined ? !this.inverted
: isInverted
;
199 if ( this.inverted
!== isInverted
) {
200 this.inverted
= isInverted
;
201 this.emit( 'update' );
206 * Set the highlight color
208 * @param {string|null} highlightColor
210 mw
.rcfilters
.dm
.ItemModel
.prototype.setHighlightColor = function ( highlightColor
) {
211 if ( this.highlightColor
!== highlightColor
) {
212 this.highlightColor
= highlightColor
;
213 this.emit( 'update' );
218 * Clear the highlight color
220 mw
.rcfilters
.dm
.ItemModel
.prototype.clearHighlightColor = function () {
221 this.setHighlightColor( null );
225 * Get the highlight color, or null if none is configured
227 * @return {string|null}
229 mw
.rcfilters
.dm
.ItemModel
.prototype.getHighlightColor = function () {
230 return this.highlightColor
;
234 * Get the CSS class that matches changes that fit this filter
235 * or null if none is configured
237 * @return {string|null}
239 mw
.rcfilters
.dm
.ItemModel
.prototype.getCssClass = function () {
240 return this.cssClass
;
244 * Get the item's identifiers
248 mw
.rcfilters
.dm
.ItemModel
.prototype.getIdentifiers = function () {
249 return this.identifiers
;
253 * Toggle the highlight feature on and off for this filter.
254 * It only works if highlight is supported for this filter.
256 * @param {boolean} enable Highlight should be enabled
258 mw
.rcfilters
.dm
.ItemModel
.prototype.toggleHighlight = function ( enable
) {
259 enable
= enable
=== undefined ? !this.highlightEnabled
: enable
;
261 if ( !this.isHighlightSupported() ) {
265 if ( enable
=== this.highlightEnabled
) {
269 this.highlightEnabled
= enable
;
270 this.emit( 'update' );
274 * Check if the highlight feature is currently enabled for this filter
278 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlightEnabled = function () {
279 return !!this.highlightEnabled
;
283 * Check if the highlight feature is supported for this filter
287 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlightSupported = function () {
288 return !!this.getCssClass();
292 * Check if the filter is currently highlighted
296 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlighted = function () {
297 return this.isHighlightEnabled() && !!this.getHighlightColor();