Merge "Revert "Temporary hax to hide cawiki's hacked in search sidebar""
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / dm / mw.rcfilters.dm.FilterItem.js
1 ( function ( mw ) {
2 /**
3 * Filter item model
4 *
5 * @mixins OO.EventEmitter
6 *
7 * @constructor
8 * @param {string} name Filter name
9 * @param {mw.rcfilters.dm.FilterGroup} groupModel Filter group model
10 * @param {Object} config Configuration object
11 * @cfg {string} [group] The group this item belongs to
12 * @cfg {string} [label] The label for the filter
13 * @cfg {string} [description] The description of the filter
14 * @cfg {boolean} [active=true] The filter is active and affecting the result
15 * @cfg {string[]} [excludes=[]] A list of filter names this filter, if
16 * selected, makes inactive.
17 * @cfg {boolean} [selected] The item is selected
18 * @cfg {string[]} [subset] Defining the names of filters that are a subset of this filter
19 * @cfg {string[]} [conflictsWith] Defining the names of filters that conflict with this item
20 */
21 mw.rcfilters.dm.FilterItem = function MwRcfiltersDmFilterItem( name, groupModel, config ) {
22 config = config || {};
23
24 // Mixin constructor
25 OO.EventEmitter.call( this );
26
27 this.name = name;
28 this.groupModel = groupModel;
29
30 this.label = config.label || this.name;
31 this.description = config.description;
32 this.selected = !!config.selected;
33
34 // Interaction definitions
35 this.subset = config.subset || [];
36 this.conflicts = config.conflicts || [];
37 this.superset = [];
38
39 // Interaction states
40 this.included = false;
41 this.conflicted = false;
42 this.fullyCovered = false;
43 };
44
45 /* Initialization */
46
47 OO.initClass( mw.rcfilters.dm.FilterItem );
48 OO.mixinClass( mw.rcfilters.dm.FilterItem, OO.EventEmitter );
49
50 /* Events */
51
52 /**
53 * @event update
54 *
55 * The state of this filter has changed
56 */
57
58 /* Methods */
59
60 /**
61 * Get the name of this filter
62 *
63 * @return {string} Filter name
64 */
65 mw.rcfilters.dm.FilterItem.prototype.getName = function () {
66 return this.name;
67 };
68
69 /**
70 * Get the model of the group this filter belongs to
71 *
72 * @return {mw.rcfilters.dm.FilterGroup} Filter group model
73 */
74 mw.rcfilters.dm.FilterItem.prototype.getGroupModel = function () {
75 return this.groupModel;
76 };
77
78 /**
79 * Get the group name this filter belongs to
80 *
81 * @return {string} Filter group name
82 */
83 mw.rcfilters.dm.FilterItem.prototype.getGroupName = function () {
84 return this.groupModel.getName();
85 };
86
87 /**
88 * Get the label of this filter
89 *
90 * @return {string} Filter label
91 */
92 mw.rcfilters.dm.FilterItem.prototype.getLabel = function () {
93 return this.label;
94 };
95
96 /**
97 * Get the description of this filter
98 *
99 * @return {string} Filter description
100 */
101 mw.rcfilters.dm.FilterItem.prototype.getDescription = function () {
102 return this.description;
103 };
104
105 /**
106 * Get the default value of this filter
107 *
108 * @return {boolean} Filter default
109 */
110 mw.rcfilters.dm.FilterItem.prototype.getDefault = function () {
111 return this.default;
112 };
113
114 /**
115 * Get filter subset
116 * This is a list of filter names that are defined to be included
117 * when this filter is selected.
118 *
119 * @return {string[]} Filter subset
120 */
121 mw.rcfilters.dm.FilterItem.prototype.getSubset = function () {
122 return this.subset;
123 };
124
125 /**
126 * Get filter superset
127 * This is a generated list of filters that define this filter
128 * to be included when either of them is selected.
129 *
130 * @return {string[]} Filter superset
131 */
132 mw.rcfilters.dm.FilterItem.prototype.getSuperset = function () {
133 return this.superset;
134 };
135
136 /**
137 * Get the selected state of this filter
138 *
139 * @return {boolean} Filter is selected
140 */
141 mw.rcfilters.dm.FilterItem.prototype.isSelected = function () {
142 return this.selected;
143 };
144
145 /**
146 * Check whether the filter is currently in a conflict state
147 *
148 * @return {boolean} Filter is in conflict state
149 */
150 mw.rcfilters.dm.FilterItem.prototype.isConflicted = function () {
151 return this.conflicted;
152 };
153
154 /**
155 * Check whether the filter is currently in an already included subset
156 *
157 * @return {boolean} Filter is in an already-included subset
158 */
159 mw.rcfilters.dm.FilterItem.prototype.isIncluded = function () {
160 return this.included;
161 };
162
163 /**
164 * Check whether the filter is currently fully covered
165 *
166 * @return {boolean} Filter is in fully-covered state
167 */
168 mw.rcfilters.dm.FilterItem.prototype.isFullyCovered = function () {
169 return this.fullyCovered;
170 };
171
172 /**
173 * Get filter conflicts
174 *
175 * @return {string[]} Filter conflicts
176 */
177 mw.rcfilters.dm.FilterItem.prototype.getConflicts = function () {
178 return this.conflicts;
179 };
180
181 /**
182 * Set filter conflicts
183 *
184 * @param {string[]} conflicts Filter conflicts
185 */
186 mw.rcfilters.dm.FilterItem.prototype.setConflicts = function ( conflicts ) {
187 this.conflicts = conflicts || [];
188 };
189
190 /**
191 * Set filter superset
192 *
193 * @param {string[]} superset Filter superset
194 */
195 mw.rcfilters.dm.FilterItem.prototype.setSuperset = function ( superset ) {
196 this.superset = superset || [];
197 };
198
199 /**
200 * Check whether a filter exists in the subset list for this filter
201 *
202 * @param {string} filterName Filter name
203 * @return {boolean} Filter name is in the subset list
204 */
205 mw.rcfilters.dm.FilterItem.prototype.existsInSubset = function ( filterName ) {
206 return this.subset.indexOf( filterName ) > -1;
207 };
208
209 /**
210 * Check whether this item has a potential conflict with the given item
211 *
212 * This checks whether the given item is in the list of conflicts of
213 * the current item, but makes no judgment about whether the conflict
214 * is currently at play (either one of the items may not be selected)
215 *
216 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item
217 * @return {boolean} This item has a conflict with the given item
218 */
219 mw.rcfilters.dm.FilterItem.prototype.existsInConflicts = function ( filterItem ) {
220 return this.conflicts.indexOf( filterItem.getName() ) > -1;
221 };
222
223 /**
224 * Set the state of this filter as being conflicted
225 * (This means any filters in its conflicts are selected)
226 *
227 * @param {boolean} [conflicted] Filter is in conflict state
228 * @fires update
229 */
230 mw.rcfilters.dm.FilterItem.prototype.toggleConflicted = function ( conflicted ) {
231 conflicted = conflicted === undefined ? !this.conflicted : conflicted;
232
233 if ( this.conflicted !== conflicted ) {
234 this.conflicted = conflicted;
235 this.emit( 'update' );
236 }
237 };
238
239 /**
240 * Set the state of this filter as being already included
241 * (This means any filters in its superset are selected)
242 *
243 * @param {boolean} [included] Filter is included as part of a subset
244 * @fires update
245 */
246 mw.rcfilters.dm.FilterItem.prototype.toggleIncluded = function ( included ) {
247 included = included === undefined ? !this.included : included;
248
249 if ( this.included !== included ) {
250 this.included = included;
251 this.emit( 'update' );
252 }
253 };
254
255 /**
256 * Toggle the selected state of the item
257 *
258 * @param {boolean} [isSelected] Filter is selected
259 * @fires update
260 */
261 mw.rcfilters.dm.FilterItem.prototype.toggleSelected = function ( isSelected ) {
262 isSelected = isSelected === undefined ? !this.selected : isSelected;
263
264 if ( this.selected !== isSelected ) {
265 this.selected = isSelected;
266 this.emit( 'update' );
267 }
268 };
269
270 /**
271 * Toggle the fully covered state of the item
272 *
273 * @param {boolean} [isFullyCovered] Filter is fully covered
274 * @fires update
275 */
276 mw.rcfilters.dm.FilterItem.prototype.toggleFullyCovered = function ( isFullyCovered ) {
277 isFullyCovered = isFullyCovered === undefined ? !this.fullycovered : isFullyCovered;
278
279 if ( this.fullyCovered !== isFullyCovered ) {
280 this.fullyCovered = isFullyCovered;
281 this.emit( 'update' );
282 }
283 };
284 }( mediaWiki ) );