Merge "UsersMultiselect widget and form field."
[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 {Object} config Configuration object
10 * @cfg {string} [group] The group this item belongs to
11 * @cfg {string} [label] The label for the filter
12 * @cfg {string} [description] The description of the filter
13 * @cfg {boolean} [active=true] The filter is active and affecting the result
14 * @cfg {string[]} [excludes=[]] A list of filter names this filter, if
15 * selected, makes inactive.
16 * @cfg {boolean} [default] The default state of this filter
17 */
18 mw.rcfilters.dm.FilterItem = function MwRcfiltersDmFilterItem( name, config ) {
19 config = config || {};
20
21 // Mixin constructor
22 OO.EventEmitter.call( this );
23
24 this.name = name;
25 this.group = config.group || '';
26 this.label = config.label || this.name;
27 this.description = config.description;
28 this.default = !!config.default;
29
30 this.active = config.active === undefined ? true : !!config.active;
31 this.excludes = config.excludes || [];
32 this.selected = this.default;
33 };
34
35 /* Initialization */
36
37 OO.initClass( mw.rcfilters.dm.FilterItem );
38 OO.mixinClass( mw.rcfilters.dm.FilterItem, OO.EventEmitter );
39
40 /* Events */
41
42 /**
43 * @event update
44 *
45 * The state of this filter has changed
46 */
47
48 /* Methods */
49
50 /**
51 * Get the name of this filter
52 *
53 * @return {string} Filter name
54 */
55 mw.rcfilters.dm.FilterItem.prototype.getName = function () {
56 return this.name;
57 };
58
59 /**
60 * Get the group name this filter belongs to
61 *
62 * @return {string} Filter group name
63 */
64 mw.rcfilters.dm.FilterItem.prototype.getGroup = function () {
65 return this.group;
66 };
67
68 /**
69 * Get the label of this filter
70 *
71 * @return {string} Filter label
72 */
73 mw.rcfilters.dm.FilterItem.prototype.getLabel = function () {
74 return this.label;
75 };
76
77 /**
78 * Get the description of this filter
79 *
80 * @return {string} Filter description
81 */
82 mw.rcfilters.dm.FilterItem.prototype.getDescription = function () {
83 return this.description;
84 };
85
86 /**
87 * Get the default value of this filter
88 *
89 * @return {boolean} Filter default
90 */
91 mw.rcfilters.dm.FilterItem.prototype.getDefault = function () {
92 return this.default;
93 };
94
95 /**
96 * Get the selected state of this filter
97 *
98 * @return {boolean} Filter is selected
99 */
100 mw.rcfilters.dm.FilterItem.prototype.isSelected = function () {
101 return this.selected;
102 };
103
104 /**
105 * Check if this filter is active
106 *
107 * @return {boolean} Filter is active
108 */
109 mw.rcfilters.dm.FilterItem.prototype.isActive = function () {
110 return this.active;
111 };
112
113 /**
114 * Check if this filter has a list of excluded filters
115 *
116 * @return {boolean} Filter has a list of excluded filters
117 */
118 mw.rcfilters.dm.FilterItem.prototype.hasExcludedFilters = function () {
119 return !!this.excludes.length;
120 };
121
122 /**
123 * Get this filter's list of excluded filters
124 *
125 * @return {string[]} Array of excluded filter names
126 */
127 mw.rcfilters.dm.FilterItem.prototype.getExcludedFilters = function () {
128 return this.excludes;
129 };
130
131 /**
132 * Toggle the active state of the item
133 *
134 * @param {boolean} [isActive] Filter is active
135 * @fires update
136 */
137 mw.rcfilters.dm.FilterItem.prototype.toggleActive = function ( isActive ) {
138 isActive = isActive === undefined ? !this.active : isActive;
139
140 if ( this.active !== isActive ) {
141 this.active = isActive;
142 this.emit( 'update' );
143 }
144 };
145
146 /**
147 * Toggle the selected state of the item
148 *
149 * @param {boolean} [isSelected] Filter is selected
150 * @fires update
151 */
152 mw.rcfilters.dm.FilterItem.prototype.toggleSelected = function ( isSelected ) {
153 isSelected = isSelected === undefined ? !this.selected : isSelected;
154
155 if ( this.selected !== isSelected ) {
156 this.selected = isSelected;
157 this.emit( 'update' );
158 }
159 };
160 }( mediaWiki ) );