Remove "@author Umherirrender" annotations
[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 {boolean} [active=true] The filter is active and affecting the result
13 * @cfg {boolean} [selected] The item is selected
14 * @cfg {boolean} [inverted] The item is inverted, meaning the search is excluding
15 * this parameter.
16 * @cfg {string} [namePrefix='item_'] A prefix to add to the param name to act as a unique
17 * identifier
18 * @cfg {string} [cssClass] The class identifying the results that match this filter
19 */
20 mw.rcfilters.dm.ItemModel = function MwRcfiltersDmItemModel( param, config ) {
21 config = config || {};
22
23 // Mixin constructor
24 OO.EventEmitter.call( this );
25
26 this.param = param;
27 this.namePrefix = config.namePrefix || 'item_';
28 this.name = this.namePrefix + param;
29
30 this.label = config.label || this.name;
31 this.description = config.description;
32 this.selected = !!config.selected;
33
34 this.inverted = !!config.inverted;
35
36 // Highlight
37 this.cssClass = config.cssClass;
38 this.highlightColor = null;
39 this.highlightEnabled = false;
40 };
41
42 /* Initialization */
43
44 OO.initClass( mw.rcfilters.dm.ItemModel );
45 OO.mixinClass( mw.rcfilters.dm.ItemModel, OO.EventEmitter );
46
47 /* Events */
48
49 /**
50 * @event update
51 *
52 * The state of this filter has changed
53 */
54
55 /* Methods */
56
57 /**
58 * Return the representation of the state of this item.
59 *
60 * @return {Object} State of the object
61 */
62 mw.rcfilters.dm.ItemModel.prototype.getState = function () {
63 return {
64 selected: this.isSelected(),
65 inverted: this.isInverted()
66 };
67 };
68
69 /**
70 * Get the name of this filter
71 *
72 * @return {string} Filter name
73 */
74 mw.rcfilters.dm.ItemModel.prototype.getName = function () {
75 return this.name;
76 };
77
78 /**
79 * Get the param name or value of this filter
80 *
81 * @return {string} Filter param name
82 */
83 mw.rcfilters.dm.ItemModel.prototype.getParamName = function () {
84 return this.param;
85 };
86
87 /**
88 * Get the message representing the state of this model.
89 *
90 * @return {string} State message
91 */
92 mw.rcfilters.dm.ItemModel.prototype.getStateMessage = function () {
93 // Display description
94 return this.getDescription();
95 };
96
97 /**
98 * Get the label of this filter
99 *
100 * @return {string} Filter label
101 */
102 mw.rcfilters.dm.ItemModel.prototype.getLabel = function () {
103 return this.label;
104 };
105
106 /**
107 * Get the description of this filter
108 *
109 * @return {string} Filter description
110 */
111 mw.rcfilters.dm.ItemModel.prototype.getDescription = function () {
112 return this.description;
113 };
114
115 /**
116 * Get the default value of this filter
117 *
118 * @return {boolean} Filter default
119 */
120 mw.rcfilters.dm.ItemModel.prototype.getDefault = function () {
121 return this.default;
122 };
123
124 /**
125 * Get the selected state of this filter
126 *
127 * @return {boolean} Filter is selected
128 */
129 mw.rcfilters.dm.ItemModel.prototype.isSelected = function () {
130 return this.selected;
131 };
132
133 /**
134 * Toggle the selected state of the item
135 *
136 * @param {boolean} [isSelected] Filter is selected
137 * @fires update
138 */
139 mw.rcfilters.dm.ItemModel.prototype.toggleSelected = function ( isSelected ) {
140 isSelected = isSelected === undefined ? !this.selected : isSelected;
141
142 if ( this.selected !== isSelected ) {
143 this.selected = isSelected;
144 this.emit( 'update' );
145 }
146 };
147
148 /**
149 * Get the inverted state of this item
150 *
151 * @return {boolean} Item is inverted
152 */
153 mw.rcfilters.dm.ItemModel.prototype.isInverted = function () {
154 return this.inverted;
155 };
156
157 /**
158 * Toggle the inverted state of the item
159 *
160 * @param {boolean} [isInverted] Item is inverted
161 * @fires update
162 */
163 mw.rcfilters.dm.ItemModel.prototype.toggleInverted = function ( isInverted ) {
164 isInverted = isInverted === undefined ? !this.inverted : isInverted;
165
166 if ( this.inverted !== isInverted ) {
167 this.inverted = isInverted;
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.highlightColor !== highlightColor ) {
179 this.highlightColor = highlightColor;
180 this.emit( 'update' );
181 }
182 };
183
184 /**
185 * Clear the highlight color
186 */
187 mw.rcfilters.dm.ItemModel.prototype.clearHighlightColor = function () {
188 this.setHighlightColor( null );
189 };
190
191 /**
192 * Get the highlight color, or null if none is configured
193 *
194 * @return {string|null}
195 */
196 mw.rcfilters.dm.ItemModel.prototype.getHighlightColor = function () {
197 return this.highlightColor;
198 };
199
200 /**
201 * Get the CSS class that matches changes that fit this filter
202 * or null if none is configured
203 *
204 * @return {string|null}
205 */
206 mw.rcfilters.dm.ItemModel.prototype.getCssClass = function () {
207 return this.cssClass;
208 };
209
210 /**
211 * Toggle the highlight feature on and off for this filter.
212 * It only works if highlight is supported for this filter.
213 *
214 * @param {boolean} enable Highlight should be enabled
215 */
216 mw.rcfilters.dm.ItemModel.prototype.toggleHighlight = function ( enable ) {
217 enable = enable === undefined ? !this.highlightEnabled : enable;
218
219 if ( !this.isHighlightSupported() ) {
220 return;
221 }
222
223 if ( enable === this.highlightEnabled ) {
224 return;
225 }
226
227 this.highlightEnabled = enable;
228 this.emit( 'update' );
229 };
230
231 /**
232 * Check if the highlight feature is currently enabled for this filter
233 *
234 * @return {boolean}
235 */
236 mw.rcfilters.dm.ItemModel.prototype.isHighlightEnabled = function () {
237 return !!this.highlightEnabled;
238 };
239
240 /**
241 * Check if the highlight feature is supported for this filter
242 *
243 * @return {boolean}
244 */
245 mw.rcfilters.dm.ItemModel.prototype.isHighlightSupported = function () {
246 return !!this.getCssClass();
247 };
248
249 /**
250 * Check if the filter is currently highlighted
251 *
252 * @return {boolean}
253 */
254 mw.rcfilters.dm.ItemModel.prototype.isHighlighted = function () {
255 return this.isHighlightEnabled() && !!this.getHighlightColor();
256 };
257 }( mediaWiki ) );