RCFilters: some more highlight cleanup
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / dm / mw.rcfilters.dm.ItemModel.js
1 ( function ( mw ) {
2 /**
3 * RCFilter base item model
4 *
5 * @mixins OO.EventEmitter
6 *
7 * @constructor
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
18 * identifier
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
23 */
24 mw.rcfilters.dm.ItemModel = function MwRcfiltersDmItemModel( param, config ) {
25 config = config || {};
26
27 // Mixin constructor
28 OO.EventEmitter.call( this );
29
30 this.param = param;
31 this.namePrefix = config.namePrefix || 'item_';
32 this.name = this.namePrefix + param;
33
34 this.label = config.label || this.name;
35 this.labelPrefixKey = config.labelPrefixKey;
36 this.description = config.description || '';
37 this.selected = !!config.selected;
38
39 this.identifiers = config.identifiers || [];
40
41 // Highlight
42 this.cssClass = config.cssClass;
43 this.highlightColor = config.defaultHighlightColor;
44 };
45
46 /* Initialization */
47
48 OO.initClass( mw.rcfilters.dm.ItemModel );
49 OO.mixinClass( mw.rcfilters.dm.ItemModel, OO.EventEmitter );
50
51 /* Events */
52
53 /**
54 * @event update
55 *
56 * The state of this filter has changed
57 */
58
59 /* Methods */
60
61 /**
62 * Return the representation of the state of this item.
63 *
64 * @return {Object} State of the object
65 */
66 mw.rcfilters.dm.ItemModel.prototype.getState = function () {
67 return {
68 selected: this.isSelected()
69 };
70 };
71
72 /**
73 * Get the name of this filter
74 *
75 * @return {string} Filter name
76 */
77 mw.rcfilters.dm.ItemModel.prototype.getName = function () {
78 return this.name;
79 };
80
81 /**
82 * Get the message key to use to wrap the label. This message takes the label as a parameter.
83 *
84 * @param {boolean} inverted Whether this item should be considered inverted
85 * @return {string|null} Message key, or null if no message
86 */
87 mw.rcfilters.dm.ItemModel.prototype.getLabelMessageKey = function ( inverted ) {
88 if ( this.labelPrefixKey ) {
89 if ( typeof this.labelPrefixKey === 'string' ) {
90 return this.labelPrefixKey;
91 }
92 return this.labelPrefixKey[
93 // Only use inverted-prefix if the item is selected
94 // Highlight-only an inverted item makes no sense
95 inverted && this.isSelected() ?
96 'inverted' : 'default'
97 ];
98 }
99 return null;
100 };
101
102 /**
103 * Get the param name or value of this filter
104 *
105 * @return {string} Filter param name
106 */
107 mw.rcfilters.dm.ItemModel.prototype.getParamName = function () {
108 return this.param;
109 };
110
111 /**
112 * Get the message representing the state of this model.
113 *
114 * @return {string} State message
115 */
116 mw.rcfilters.dm.ItemModel.prototype.getStateMessage = function () {
117 // Display description
118 return this.getDescription();
119 };
120
121 /**
122 * Get the label of this filter
123 *
124 * @return {string} Filter label
125 */
126 mw.rcfilters.dm.ItemModel.prototype.getLabel = function () {
127 return this.label;
128 };
129
130 /**
131 * Get the description of this filter
132 *
133 * @return {string} Filter description
134 */
135 mw.rcfilters.dm.ItemModel.prototype.getDescription = function () {
136 return this.description;
137 };
138
139 /**
140 * Get the default value of this filter
141 *
142 * @return {boolean} Filter default
143 */
144 mw.rcfilters.dm.ItemModel.prototype.getDefault = function () {
145 return this.default;
146 };
147
148 /**
149 * Get the selected state of this filter
150 *
151 * @return {boolean} Filter is selected
152 */
153 mw.rcfilters.dm.ItemModel.prototype.isSelected = function () {
154 return this.selected;
155 };
156
157 /**
158 * Toggle the selected state of the item
159 *
160 * @param {boolean} [isSelected] Filter is selected
161 * @fires update
162 */
163 mw.rcfilters.dm.ItemModel.prototype.toggleSelected = function ( isSelected ) {
164 isSelected = isSelected === undefined ? !this.selected : isSelected;
165
166 if ( this.selected !== isSelected ) {
167 this.selected = isSelected;
168 this.emit( 'update' );
169 }
170 };
171
172 /**
173 * Set the highlight color
174 *
175 * @param {string|null} highlightColor
176 */
177 mw.rcfilters.dm.ItemModel.prototype.setHighlightColor = function ( highlightColor ) {
178 if ( !this.isHighlightSupported() ) {
179 return;
180 }
181
182 if ( this.highlightColor !== highlightColor ) {
183 this.highlightColor = highlightColor;
184 this.emit( 'update' );
185 }
186 };
187
188 /**
189 * Clear the highlight color
190 */
191 mw.rcfilters.dm.ItemModel.prototype.clearHighlightColor = function () {
192 this.setHighlightColor( null );
193 };
194
195 /**
196 * Get the highlight color, or null if none is configured
197 *
198 * @return {string|null}
199 */
200 mw.rcfilters.dm.ItemModel.prototype.getHighlightColor = function () {
201 return this.highlightColor;
202 };
203
204 /**
205 * Get the CSS class that matches changes that fit this filter
206 * or null if none is configured
207 *
208 * @return {string|null}
209 */
210 mw.rcfilters.dm.ItemModel.prototype.getCssClass = function () {
211 return this.cssClass;
212 };
213
214 /**
215 * Get the item's identifiers
216 *
217 * @return {string[]}
218 */
219 mw.rcfilters.dm.ItemModel.prototype.getIdentifiers = function () {
220 return this.identifiers;
221 };
222
223 /**
224 * Check if the highlight feature is supported for this filter
225 *
226 * @return {boolean}
227 */
228 mw.rcfilters.dm.ItemModel.prototype.isHighlightSupported = function () {
229 return !!this.getCssClass();
230 };
231
232 /**
233 * Check if the filter is currently highlighted
234 *
235 * @return {boolean}
236 */
237 mw.rcfilters.dm.ItemModel.prototype.isHighlighted = function () {
238 return !!this.getHighlightColor();
239 };
240 }( mediaWiki ) );