Merge "Improve sorting on SpecialWanted*-Pages"
[mediawiki.git] / resources / src / mediawiki.rcfilters / dm / mw.rcfilters.dm.FilterGroup.js
blob14a610b71ec040e98ce37ea0e21982fa5693eba5
1 ( function ( mw ) {
2         /**
3          * View model for a filter group
4          *
5          * @mixins OO.EventEmitter
6          * @mixins OO.EmitterList
7          *
8          * @constructor
9          * @param {string} name Group name
10          * @param {Object} [config] Configuration options
11          * @cfg {string} [type='send_unselected_if_any'] Group type
12          * @cfg {string} [title] Group title
13          * @cfg {string} [separator='|'] Value separator for 'string_options' groups
14          * @cfg {boolean} [active] Group is active
15          * @cfg {boolean} [fullCoverage] This filters in this group collectively cover all results
16          */
17         mw.rcfilters.dm.FilterGroup = function MwRcfiltersDmFilterGroup( name, config ) {
18                 config = config || {};
20                 // Mixin constructor
21                 OO.EventEmitter.call( this );
22                 OO.EmitterList.call( this );
24                 this.name = name;
25                 this.type = config.type || 'send_unselected_if_any';
26                 this.title = config.title;
27                 this.separator = config.separator || '|';
29                 this.active = !!config.active;
30                 this.fullCoverage = !!config.fullCoverage;
32                 this.aggregate( { update: 'filterItemUpdate' } );
33                 this.connect( this, { filterItemUpdate: 'onFilterItemUpdate' } );
34         };
36         /* Initialization */
37         OO.initClass( mw.rcfilters.dm.FilterGroup );
38         OO.mixinClass( mw.rcfilters.dm.FilterGroup, OO.EventEmitter );
39         OO.mixinClass( mw.rcfilters.dm.FilterGroup, OO.EmitterList );
41         /* Events */
43         /**
44          * @event update
45          *
46          * Group state has been updated
47          */
49         /* Methods */
51         /**
52          * Respond to filterItem update event
53          *
54          * @fires update
55          */
56         mw.rcfilters.dm.FilterGroup.prototype.onFilterItemUpdate = function () {
57                 // Update state
58                 var active = this.areAnySelected();
60                 if ( this.active !== active ) {
61                         this.active = active;
62                         this.emit( 'update' );
63                 }
64         };
66         /**
67          * Get group active state
68          *
69          * @return {boolean} Active state
70          */
71         mw.rcfilters.dm.FilterGroup.prototype.isActive = function () {
72                 return this.active;
73         };
75         /**
76          * Get group name
77          *
78          * @return {string} Group name
79          */
80         mw.rcfilters.dm.FilterGroup.prototype.getName = function () {
81                 return this.name;
82         };
84         /**
85          * Check whether there are any items selected
86          *
87          * @return {boolean} Any items in the group are selected
88          */
89         mw.rcfilters.dm.FilterGroup.prototype.areAnySelected = function () {
90                 return this.getItems().some( function ( filterItem ) {
91                         return filterItem.isSelected();
92                 } );
93         };
95         /**
96          * Check whether all items selected
97          *
98          * @return {boolean} All items are selected
99          */
100         mw.rcfilters.dm.FilterGroup.prototype.areAllSelected = function () {
101                 return this.getItems().every( function ( filterItem ) {
102                         return filterItem.isSelected();
103                 } );
104         };
106         /**
107          * Get all selected items in this group
108          *
109          * @param {mw.rcfilters.dm.FilterItem} [excludeItem] Item to exclude from the list
110          * @return {mw.rcfilters.dm.FilterItem[]} Selected items
111          */
112         mw.rcfilters.dm.FilterGroup.prototype.getSelectedItems = function ( excludeItem ) {
113                 var excludeName = ( excludeItem && excludeItem.getName() ) || '';
115                 return this.getItems().filter( function ( item ) {
116                         return item.getName() !== excludeName && item.isSelected();
117                 } );
118         };
120         /**
121          * Check whether all selected items are in conflict with the given item
122          *
123          * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item to test
124          * @return {boolean} All selected items are in conflict with this item
125          */
126         mw.rcfilters.dm.FilterGroup.prototype.areAllSelectedInConflictWith = function ( filterItem ) {
127                 var selectedItems = this.getSelectedItems( filterItem );
129                 return selectedItems.length > 0 && selectedItems.every( function ( selectedFilter ) {
130                         return selectedFilter.existsInConflicts( filterItem );
131                 } );
132         };
134         /**
135          * Check whether any of the selected items are in conflict with the given item
136          *
137          * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item to test
138          * @return {boolean} Any of the selected items are in conflict with this item
139          */
140         mw.rcfilters.dm.FilterGroup.prototype.areAnySelectedInConflictWith = function ( filterItem ) {
141                 var selectedItems = this.getSelectedItems( filterItem );
143                 return selectedItems.length > 0 && selectedItems.some( function ( selectedFilter ) {
144                         return selectedFilter.existsInConflicts( filterItem );
145                 } );
146         };
148         /**
149          * Get group type
150          *
151          * @return {string} Group type
152          */
153         mw.rcfilters.dm.FilterGroup.prototype.getType = function () {
154                 return this.type;
155         };
157         /**
158          * Get group's title
159          *
160          * @return {string} Title
161          */
162         mw.rcfilters.dm.FilterGroup.prototype.getTitle = function () {
163                 return this.title;
164         };
166         /**
167          * Get group's values separator
168          *
169          * @return {string} Values separator
170          */
171         mw.rcfilters.dm.FilterGroup.prototype.getSeparator = function () {
172                 return this.separator;
173         };
175         /**
176          * Check whether the group is defined as full coverage
177          *
178          * @return {boolean} Group is full coverage
179          */
180         mw.rcfilters.dm.FilterGroup.prototype.isFullCoverage = function () {
181                 return this.fullCoverage;
182         };
183 }( mediaWiki ) );