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
88 mw
.rcfilters
.dm
.ItemModel
.prototype.getPrefixedLabel = function ( inverted
) {
89 if ( this.labelPrefixKey
) {
90 if ( typeof this.labelPrefixKey
=== 'string' ) {
91 return mw
.message( this.labelPrefixKey
, this.getLabel() ).parse();
95 // Only use inverted-prefix if the item is selected
96 // Highlight-only an inverted item makes no sense
97 inverted
&& this.isSelected() ?
98 'inverted' : 'default'
104 return this.getLabel();
109 * Get the param name or value of this filter
111 * @return {string} Filter param name
113 mw
.rcfilters
.dm
.ItemModel
.prototype.getParamName = function () {
118 * Get the message representing the state of this model.
120 * @return {string} State message
122 mw
.rcfilters
.dm
.ItemModel
.prototype.getStateMessage = function () {
123 // Display description
124 return this.getDescription();
128 * Get the label of this filter
130 * @return {string} Filter label
132 mw
.rcfilters
.dm
.ItemModel
.prototype.getLabel = function () {
137 * Get the description of this filter
139 * @return {string} Filter description
141 mw
.rcfilters
.dm
.ItemModel
.prototype.getDescription = function () {
142 return this.description
;
146 * Get the default value of this filter
148 * @return {boolean} Filter default
150 mw
.rcfilters
.dm
.ItemModel
.prototype.getDefault = function () {
155 * Get the selected state of this filter
157 * @return {boolean} Filter is selected
159 mw
.rcfilters
.dm
.ItemModel
.prototype.isSelected = function () {
160 return this.selected
;
164 * Toggle the selected state of the item
166 * @param {boolean} [isSelected] Filter is selected
169 mw
.rcfilters
.dm
.ItemModel
.prototype.toggleSelected = function ( isSelected
) {
170 isSelected
= isSelected
=== undefined ? !this.selected
: isSelected
;
172 if ( this.selected
!== isSelected
) {
173 this.selected
= isSelected
;
174 this.emit( 'update' );
179 * Set the highlight color
181 * @param {string|null} highlightColor
183 mw
.rcfilters
.dm
.ItemModel
.prototype.setHighlightColor = function ( highlightColor
) {
184 if ( this.highlightColor
!== highlightColor
) {
185 this.highlightColor
= highlightColor
;
186 this.emit( 'update' );
191 * Clear the highlight color
193 mw
.rcfilters
.dm
.ItemModel
.prototype.clearHighlightColor = function () {
194 this.setHighlightColor( null );
198 * Get the highlight color, or null if none is configured
200 * @return {string|null}
202 mw
.rcfilters
.dm
.ItemModel
.prototype.getHighlightColor = function () {
203 return this.highlightColor
;
207 * Get the CSS class that matches changes that fit this filter
208 * or null if none is configured
210 * @return {string|null}
212 mw
.rcfilters
.dm
.ItemModel
.prototype.getCssClass = function () {
213 return this.cssClass
;
217 * Get the item's identifiers
221 mw
.rcfilters
.dm
.ItemModel
.prototype.getIdentifiers = function () {
222 return this.identifiers
;
226 * Toggle the highlight feature on and off for this filter.
227 * It only works if highlight is supported for this filter.
229 * @param {boolean} enable Highlight should be enabled
231 mw
.rcfilters
.dm
.ItemModel
.prototype.toggleHighlight = function ( enable
) {
232 enable
= enable
=== undefined ? !this.highlightEnabled
: enable
;
234 if ( !this.isHighlightSupported() ) {
238 if ( enable
=== this.highlightEnabled
) {
242 this.highlightEnabled
= enable
;
243 this.emit( 'update' );
247 * Check if the highlight feature is currently enabled for this filter
251 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlightEnabled = function () {
252 return !!this.highlightEnabled
;
256 * Check if the highlight feature is supported for this filter
260 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlightSupported = function () {
261 return !!this.getCssClass();
265 * Check if the filter is currently highlighted
269 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlighted = function () {
270 return this.isHighlightEnabled() && !!this.getHighlightColor();