Merge "Skin: Make skins aware of their registered skin name"
[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 this.highlightEnabled = !!config.defaultHighlightColor;
45 };
46
47 /* Initialization */
48
49 OO.initClass( mw.rcfilters.dm.ItemModel );
50 OO.mixinClass( mw.rcfilters.dm.ItemModel, OO.EventEmitter );
51
52 /* Events */
53
54 /**
55 * @event update
56 *
57 * The state of this filter has changed
58 */
59
60 /* Methods */
61
62 /**
63 * Return the representation of the state of this item.
64 *
65 * @return {Object} State of the object
66 */
67 mw.rcfilters.dm.ItemModel.prototype.getState = function () {
68 return {
69 selected: this.isSelected()
70 };
71 };
72
73 /**
74 * Get the name of this filter
75 *
76 * @return {string} Filter name
77 */
78 mw.rcfilters.dm.ItemModel.prototype.getName = function () {
79 return this.name;
80 };
81
82 /**
83 * Get a prefixed label
84 *
85 * @param {boolean} inverted This item should be considered inverted
86 * @return {string} Prefixed label
87 */
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();
92 } else {
93 return mw.message(
94 this.labelPrefixKey[
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'
99 ],
100 this.getLabel()
101 ).parse();
102 }
103 } else {
104 return this.getLabel();
105 }
106 };
107
108 /**
109 * Get the param name or value of this filter
110 *
111 * @return {string} Filter param name
112 */
113 mw.rcfilters.dm.ItemModel.prototype.getParamName = function () {
114 return this.param;
115 };
116
117 /**
118 * Get the message representing the state of this model.
119 *
120 * @return {string} State message
121 */
122 mw.rcfilters.dm.ItemModel.prototype.getStateMessage = function () {
123 // Display description
124 return this.getDescription();
125 };
126
127 /**
128 * Get the label of this filter
129 *
130 * @return {string} Filter label
131 */
132 mw.rcfilters.dm.ItemModel.prototype.getLabel = function () {
133 return this.label;
134 };
135
136 /**
137 * Get the description of this filter
138 *
139 * @return {string} Filter description
140 */
141 mw.rcfilters.dm.ItemModel.prototype.getDescription = function () {
142 return this.description;
143 };
144
145 /**
146 * Get the default value of this filter
147 *
148 * @return {boolean} Filter default
149 */
150 mw.rcfilters.dm.ItemModel.prototype.getDefault = function () {
151 return this.default;
152 };
153
154 /**
155 * Get the selected state of this filter
156 *
157 * @return {boolean} Filter is selected
158 */
159 mw.rcfilters.dm.ItemModel.prototype.isSelected = function () {
160 return this.selected;
161 };
162
163 /**
164 * Toggle the selected state of the item
165 *
166 * @param {boolean} [isSelected] Filter is selected
167 * @fires update
168 */
169 mw.rcfilters.dm.ItemModel.prototype.toggleSelected = function ( isSelected ) {
170 isSelected = isSelected === undefined ? !this.selected : isSelected;
171
172 if ( this.selected !== isSelected ) {
173 this.selected = isSelected;
174 this.emit( 'update' );
175 }
176 };
177
178 /**
179 * Set the highlight color
180 *
181 * @param {string|null} highlightColor
182 */
183 mw.rcfilters.dm.ItemModel.prototype.setHighlightColor = function ( highlightColor ) {
184 if ( this.highlightColor !== highlightColor ) {
185 this.highlightColor = highlightColor;
186 this.emit( 'update' );
187 }
188 };
189
190 /**
191 * Clear the highlight color
192 */
193 mw.rcfilters.dm.ItemModel.prototype.clearHighlightColor = function () {
194 this.setHighlightColor( null );
195 };
196
197 /**
198 * Get the highlight color, or null if none is configured
199 *
200 * @return {string|null}
201 */
202 mw.rcfilters.dm.ItemModel.prototype.getHighlightColor = function () {
203 return this.highlightColor;
204 };
205
206 /**
207 * Get the CSS class that matches changes that fit this filter
208 * or null if none is configured
209 *
210 * @return {string|null}
211 */
212 mw.rcfilters.dm.ItemModel.prototype.getCssClass = function () {
213 return this.cssClass;
214 };
215
216 /**
217 * Get the item's identifiers
218 *
219 * @return {string[]}
220 */
221 mw.rcfilters.dm.ItemModel.prototype.getIdentifiers = function () {
222 return this.identifiers;
223 };
224
225 /**
226 * Toggle the highlight feature on and off for this filter.
227 * It only works if highlight is supported for this filter.
228 *
229 * @param {boolean} enable Highlight should be enabled
230 */
231 mw.rcfilters.dm.ItemModel.prototype.toggleHighlight = function ( enable ) {
232 enable = enable === undefined ? !this.highlightEnabled : enable;
233
234 if ( !this.isHighlightSupported() ) {
235 return;
236 }
237
238 if ( enable === this.highlightEnabled ) {
239 return;
240 }
241
242 this.highlightEnabled = enable;
243 this.emit( 'update' );
244 };
245
246 /**
247 * Check if the highlight feature is currently enabled for this filter
248 *
249 * @return {boolean}
250 */
251 mw.rcfilters.dm.ItemModel.prototype.isHighlightEnabled = function () {
252 return !!this.highlightEnabled;
253 };
254
255 /**
256 * Check if the highlight feature is supported for this filter
257 *
258 * @return {boolean}
259 */
260 mw.rcfilters.dm.ItemModel.prototype.isHighlightSupported = function () {
261 return !!this.getCssClass();
262 };
263
264 /**
265 * Check if the filter is currently highlighted
266 *
267 * @return {boolean}
268 */
269 mw.rcfilters.dm.ItemModel.prototype.isHighlighted = function () {
270 return this.isHighlightEnabled() && !!this.getHighlightColor();
271 };
272 }( mediaWiki ) );