aa82e218f868a1719a8933bce585286e2253cf3a
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.
25 mw
.rcfilters
.dm
.ItemModel
= function MwRcfiltersDmItemModel( param
, config
) {
26 config
= config
|| {};
29 OO
.EventEmitter
.call( this );
32 this.namePrefix
= config
.namePrefix
|| 'item_';
33 this.name
= this.namePrefix
+ param
;
35 this.label
= config
.label
|| this.name
;
36 this.labelPrefixKey
= config
.labelPrefixKey
;
37 this.description
= config
.description
|| '';
38 this.selected
= !!config
.selected
;
40 this.inverted
= !!config
.inverted
;
41 this.identifiers
= config
.identifiers
|| [];
44 this.cssClass
= config
.cssClass
;
45 this.highlightColor
= null;
46 this.highlightEnabled
= false;
51 OO
.initClass( mw
.rcfilters
.dm
.ItemModel
);
52 OO
.mixinClass( mw
.rcfilters
.dm
.ItemModel
, OO
.EventEmitter
);
59 * The state of this filter has changed
65 * Return the representation of the state of this item.
67 * @return {Object} State of the object
69 mw
.rcfilters
.dm
.ItemModel
.prototype.getState = function () {
71 selected
: this.isSelected(),
72 inverted
: this.isInverted()
77 * Get the name of this filter
79 * @return {string} Filter name
81 mw
.rcfilters
.dm
.ItemModel
.prototype.getName = function () {
86 * Get a prefixed label
88 * @return {string} Prefixed label
90 mw
.rcfilters
.dm
.ItemModel
.prototype.getPrefixedLabel = function () {
91 if ( this.labelPrefixKey
) {
92 if ( typeof this.labelPrefixKey
=== 'string' ) {
93 return mw
.message( this.labelPrefixKey
, this.getLabel() ).parse();
97 // Only use inverted-prefix if the item is selected
98 // Highlight-only an inverted item makes no sense
99 this.isInverted() && this.isSelected() ?
100 'inverted' : 'default'
106 return this.getLabel();
111 * Get the param name or value of this filter
113 * @return {string} Filter param name
115 mw
.rcfilters
.dm
.ItemModel
.prototype.getParamName = function () {
120 * Get the message representing the state of this model.
122 * @return {string} State message
124 mw
.rcfilters
.dm
.ItemModel
.prototype.getStateMessage = function () {
125 // Display description
126 return this.getDescription();
130 * Get the label of this filter
132 * @return {string} Filter label
134 mw
.rcfilters
.dm
.ItemModel
.prototype.getLabel = function () {
139 * Get the description of this filter
141 * @return {string} Filter description
143 mw
.rcfilters
.dm
.ItemModel
.prototype.getDescription = function () {
144 return this.description
;
148 * Get the default value of this filter
150 * @return {boolean} Filter default
152 mw
.rcfilters
.dm
.ItemModel
.prototype.getDefault = function () {
157 * Get the selected state of this filter
159 * @return {boolean} Filter is selected
161 mw
.rcfilters
.dm
.ItemModel
.prototype.isSelected = function () {
162 return this.selected
;
166 * Toggle the selected state of the item
168 * @param {boolean} [isSelected] Filter is selected
171 mw
.rcfilters
.dm
.ItemModel
.prototype.toggleSelected = function ( isSelected
) {
172 isSelected
= isSelected
=== undefined ? !this.selected
: isSelected
;
174 if ( this.selected
!== isSelected
) {
175 this.selected
= isSelected
;
176 this.emit( 'update' );
181 * Get the inverted state of this item
183 * @return {boolean} Item is inverted
185 mw
.rcfilters
.dm
.ItemModel
.prototype.isInverted = function () {
186 return this.inverted
;
190 * Toggle the inverted state of the item
192 * @param {boolean} [isInverted] Item is inverted
195 mw
.rcfilters
.dm
.ItemModel
.prototype.toggleInverted = function ( isInverted
) {
196 isInverted
= isInverted
=== undefined ? !this.inverted
: isInverted
;
198 if ( this.inverted
!== isInverted
) {
199 this.inverted
= isInverted
;
200 this.emit( 'update' );
205 * Set the highlight color
207 * @param {string|null} highlightColor
209 mw
.rcfilters
.dm
.ItemModel
.prototype.setHighlightColor = function ( highlightColor
) {
210 if ( this.highlightColor
!== highlightColor
) {
211 this.highlightColor
= highlightColor
;
212 this.emit( 'update' );
217 * Clear the highlight color
219 mw
.rcfilters
.dm
.ItemModel
.prototype.clearHighlightColor = function () {
220 this.setHighlightColor( null );
224 * Get the highlight color, or null if none is configured
226 * @return {string|null}
228 mw
.rcfilters
.dm
.ItemModel
.prototype.getHighlightColor = function () {
229 return this.highlightColor
;
233 * Get the CSS class that matches changes that fit this filter
234 * or null if none is configured
236 * @return {string|null}
238 mw
.rcfilters
.dm
.ItemModel
.prototype.getCssClass = function () {
239 return this.cssClass
;
243 * Get the item's identifiers
247 mw
.rcfilters
.dm
.ItemModel
.prototype.getIdentifiers = function () {
248 return this.identifiers
;
252 * Toggle the highlight feature on and off for this filter.
253 * It only works if highlight is supported for this filter.
255 * @param {boolean} enable Highlight should be enabled
257 mw
.rcfilters
.dm
.ItemModel
.prototype.toggleHighlight = function ( enable
) {
258 enable
= enable
=== undefined ? !this.highlightEnabled
: enable
;
260 if ( !this.isHighlightSupported() ) {
264 if ( enable
=== this.highlightEnabled
) {
268 this.highlightEnabled
= enable
;
269 this.emit( 'update' );
273 * Check if the highlight feature is currently enabled for this filter
277 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlightEnabled = function () {
278 return !!this.highlightEnabled
;
282 * Check if the highlight feature is supported for this filter
286 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlightSupported = function () {
287 return !!this.getCssClass();
291 * Check if the filter is currently highlighted
295 mw
.rcfilters
.dm
.ItemModel
.prototype.isHighlighted = function () {
296 return this.isHighlightEnabled() && !!this.getHighlightColor();