2 * RCFilter base item model.
4 * @class mw.rcfilters.dm.ItemModel
6 * @mixes OO.EventEmitter
8 * @param {string} param Filter param name
9 * @param {Object} config Configuration object
10 * @param {string} [config.label] The label for the filter
11 * @param {string} [config.description] The description of the filter
12 * @param {string|Object} [config.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 * @param {boolean} [config.active=true] The filter is active and affecting the result
16 * @param {boolean} [config.selected] The item is selected
17 * @param {any} [config.value] The value of this item
18 * @param {string} [config.namePrefix='item_'] A prefix to add to the param name to act as a unique
20 * @param {string} [config.cssClass] The class identifying the results that match this filter
21 * @param {string|null} [config.helpLink] Link to a help page for this filter
22 * @param {string[]} [config.identifiers] An array of identifiers for this item. They will be
23 * added and considered in the view.
24 * @param {string} [config.defaultHighlightColor=null] If set, highlight this filter by default with this color
26 const 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.setValue( config.value || config.selected );
41 this.identifiers = config.identifiers || [];
44 this.cssClass = config.cssClass;
45 this.helpLink = config.helpLink;
46 this.highlightColor = config.defaultHighlightColor || null;
51 OO.initClass( ItemModel );
52 OO.mixinClass( ItemModel, OO.EventEmitter );
57 * 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 ItemModel.prototype.getState = function () {
72 selected: this.isSelected()
77 * Get the name of this filter
79 * @return {string} Filter name
81 ItemModel.prototype.getName = function () {
86 * Get the message key to use to wrap the label. This message takes the label as a parameter.
88 * @param {boolean} inverted Whether this item should be considered inverted
89 * @return {string|null} Message key, or null if no message
91 ItemModel.prototype.getLabelMessageKey = function ( inverted ) {
92 if ( this.labelPrefixKey ) {
93 if ( typeof this.labelPrefixKey === 'string' ) {
94 return this.labelPrefixKey;
96 return this.labelPrefixKey[
97 // Only use inverted-prefix if the item is selected
98 // Highlight-only an inverted item makes no sense
99 inverted && this.isSelected() ?
100 'inverted' : 'default'
107 * Get the param name or value of this filter
109 * @return {string} Filter param name
111 ItemModel.prototype.getParamName = function () {
116 * Get the message representing the state of this model.
118 * @return {string} State message
120 ItemModel.prototype.getStateMessage = function () {
121 // Display description
122 return this.getDescription();
126 * Get the label of this filter
128 * @return {string} Filter label
130 ItemModel.prototype.getLabel = function () {
135 * Get the description of this filter
137 * @return {string} Filter description
139 ItemModel.prototype.getDescription = function () {
140 return this.description;
144 * Get the default value of this filter
146 * @return {boolean} Filter default
148 ItemModel.prototype.getDefault = function () {
153 * Get the selected state of this filter
155 * @return {boolean} Filter is selected
157 ItemModel.prototype.isSelected = function () {
162 * Toggle the selected state of the item
164 * @param {boolean} [isSelected] Filter is selected
167 ItemModel.prototype.toggleSelected = function ( isSelected ) {
168 isSelected = isSelected === undefined ? !this.isSelected() : isSelected;
169 this.setValue( isSelected );
177 ItemModel.prototype.getValue = function () {
182 * Convert a given value to the appropriate representation based on group type
187 ItemModel.prototype.coerceValue = function ( value ) {
188 return this.getGroupModel().getType() === 'any_value' ? value : !!value;
194 * @param {any} newValue
196 ItemModel.prototype.setValue = function ( newValue ) {
197 newValue = this.coerceValue( newValue );
198 if ( this.value !== newValue ) {
199 this.value = newValue;
200 this.emit( 'update' );
205 * Set the highlight color
207 * @param {string|null} highlightColor
209 ItemModel.prototype.setHighlightColor = function ( highlightColor ) {
210 if ( !this.isHighlightSupported() ) {
213 // If the highlight color on the item and in the parameter is null/undefined, return early.
214 if ( !this.highlightColor && !highlightColor ) {
218 if ( this.highlightColor !== highlightColor ) {
219 this.highlightColor = highlightColor;
220 this.emit( 'update' );
225 * Clear the highlight color
227 ItemModel.prototype.clearHighlightColor = function () {
228 this.setHighlightColor( null );
232 * Get the highlight color, or null if none is configured
234 * @return {string|null}
236 ItemModel.prototype.getHighlightColor = function () {
237 return this.highlightColor;
241 * Get the CSS class that matches changes that fit this filter
242 * or null if none is configured
244 * @return {string|null}
246 ItemModel.prototype.getCssClass = function () {
247 return this.cssClass;
251 * Get a link to a help page for this filter
252 * or null if none is configured
254 * @return {string|null}
256 ItemModel.prototype.getHelpLink = function () {
257 return this.helpLink;
261 * Get the item's identifiers
265 ItemModel.prototype.getIdentifiers = function () {
266 return this.identifiers;
270 * Check if the highlight feature is supported for this filter
274 ItemModel.prototype.isHighlightSupported = function () {
275 return !!this.getCssClass() && !OO.ui.isMobile();
279 * Check if the filter is currently highlighted
283 ItemModel.prototype.isHighlighted = function () {
284 return !!this.getHighlightColor();
287 module.exports = ItemModel;