* with 'default' and 'inverted' as keys.
* @cfg {boolean} [active=true] The filter is active and affecting the result
* @cfg {boolean} [selected] The item is selected
+ * @cfg {*} [value] The value of this item
* @cfg {string} [namePrefix='item_'] A prefix to add to the param name to act as a unique
* identifier
* @cfg {string} [cssClass] The class identifying the results that match this filter
* @cfg {string[]} [identifiers] An array of identifiers for this item. They will be
* added and considered in the view.
- * @cfg {string} [defaultHighlightColor] If set, highlight this filter by default with this color
+ * @cfg {string} [defaultHighlightColor=null] If set, highlight this filter by default with this color
*/
mw.rcfilters.dm.ItemModel = function MwRcfiltersDmItemModel( param, config ) {
config = config || {};
this.label = config.label || this.name;
this.labelPrefixKey = config.labelPrefixKey;
this.description = config.description || '';
- this.selected = !!config.selected;
+ this.setValue( config.value || config.selected );
this.identifiers = config.identifiers || [];
// Highlight
this.cssClass = config.cssClass;
- this.highlightColor = config.defaultHighlightColor;
+ this.highlightColor = config.defaultHighlightColor || null;
};
/* Initialization */
* @return {boolean} Filter is selected
*/
mw.rcfilters.dm.ItemModel.prototype.isSelected = function () {
- return this.selected;
+ return !!this.value;
};
/**
* @fires update
*/
mw.rcfilters.dm.ItemModel.prototype.toggleSelected = function ( isSelected ) {
- isSelected = isSelected === undefined ? !this.selected : isSelected;
+ isSelected = isSelected === undefined ? !this.isSelected() : isSelected;
+ this.setValue( isSelected );
+ };
+
+ /**
+ * Get the value
+ *
+ * @return {*}
+ */
+ mw.rcfilters.dm.ItemModel.prototype.getValue = function () {
+ return this.value;
+ };
+
+ /**
+ * Convert a given value to the appropriate representation based on group type
+ *
+ * @param {*} value
+ * @return {*}
+ */
+ mw.rcfilters.dm.ItemModel.prototype.coerceValue = function ( value ) {
+ return this.getGroupModel().getType() === 'any_value' ? value : !!value;
+ };
- if ( this.selected !== isSelected ) {
- this.selected = isSelected;
+ /**
+ * Set the value
+ *
+ * @param {*} newValue
+ */
+ mw.rcfilters.dm.ItemModel.prototype.setValue = function ( newValue ) {
+ newValue = this.coerceValue( newValue );
+ if ( this.value !== newValue ) {
+ this.value = newValue;
this.emit( 'update' );
}
};
if ( !this.isHighlightSupported() ) {
return;
}
+ // If the highlight color on the item and in the parameter is null/undefined, return early.
+ if ( !this.highlightColor && !highlightColor ) {
+ return;
+ }
if ( this.highlightColor !== highlightColor ) {
this.highlightColor = highlightColor;