Merge "Improve sorting on SpecialWanted*-Pages"
[mediawiki.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.FilterGroupWidget.js
bloba750c44f8c514898dac535ec92abb2e887f7b7cd
1 ( function ( mw, $ ) {
2         /**
3          * A group of filters
4          *
5          * @extends OO.ui.Widget
6          * @mixins OO.ui.mixin.GroupWidget
7          * @mixins OO.ui.mixin.LabelElement
8          *
9          * @constructor
10          * @param {mw.rcfilters.Controller} controller Controller
11          * @param {mw.rcfilters.dm.FilterGroup} model Filter group model
12          * @param {Object} config Configuration object
13          */
14         mw.rcfilters.ui.FilterGroupWidget = function MwRcfiltersUiFilterGroupWidget( controller, model, config ) {
15                 config = config || {};
17                 // Parent
18                 mw.rcfilters.ui.FilterGroupWidget.parent.call( this, config );
20                 this.controller = controller;
21                 this.model = model;
22                 this.filters = {};
24                 // Mixin constructors
25                 OO.ui.mixin.GroupWidget.call( this, config );
26                 OO.ui.mixin.LabelElement.call( this, $.extend( {}, config, {
27                         label: this.model.getTitle(),
28                         $label: $( '<div>' )
29                                 .addClass( 'mw-rcfilters-ui-filterGroupWidget-title' )
30                 } ) );
31                 this.$overlay = config.$overlay || this.$element;
33                 // Populate
34                 this.populateFromModel();
36                 this.model.connect( this, { update: 'onModelUpdate' } );
38                 this.$element
39                         .addClass( 'mw-rcfilters-ui-filterGroupWidget' )
40                         .addClass( 'mw-rcfilters-ui-filterGroupWidget-name-' + this.model.getName() )
41                         .append(
42                                 this.$label,
43                                 this.$group
44                                         .addClass( 'mw-rcfilters-ui-filterGroupWidget-group' )
45                         );
46         };
48         /* Initialization */
50         OO.inheritClass( mw.rcfilters.ui.FilterGroupWidget, OO.ui.Widget );
51         OO.mixinClass( mw.rcfilters.ui.FilterGroupWidget, OO.ui.mixin.GroupWidget );
52         OO.mixinClass( mw.rcfilters.ui.FilterGroupWidget, OO.ui.mixin.LabelElement );
54         /**
55          * Respond to model update event
56          */
57         mw.rcfilters.ui.FilterGroupWidget.prototype.onModelUpdate = function () {
58                 this.$element.toggleClass(
59                         'mw-rcfilters-ui-filterGroupWidget-active',
60                         this.model.isActive()
61                 );
62         };
64         /**
65          * Get an item widget from its filter name
66          *
67          * @param {string} filterName Filter name
68          * @return {mw.rcfilters.ui.FilterItemWidget} Item widget
69          */
70         mw.rcfilters.ui.FilterGroupWidget.prototype.getItemWidget = function ( filterName ) {
71                 return this.filters[ filterName ];
72         };
74         /**
75          * Populate data from the model
76          */
77         mw.rcfilters.ui.FilterGroupWidget.prototype.populateFromModel = function () {
78                 var widget = this;
80                 this.clearItems();
81                 this.filters = {};
83                 this.addItems(
84                         this.model.getItems().map( function ( filterItem ) {
85                                 var groupWidget = new mw.rcfilters.ui.FilterItemWidget(
86                                         widget.controller,
87                                         filterItem,
88                                         {
89                                                 label: filterItem.getLabel(),
90                                                 description: filterItem.getDescription(),
91                                                 $overlay: widget.$overlay
92                                         }
93                                 );
95                                 widget.filters[ filterItem.getName() ] = groupWidget;
97                                 return groupWidget;
98                         } )
99                 );
100         };
102         /**
103          * Get the group name
104          *
105          * @return {string} Group name
106          */
107         mw.rcfilters.ui.FilterGroupWidget.prototype.getName = function () {
108                 return this.model.getName();
109         };
110 }( mediaWiki, jQuery ) );