304929b78ea09cb56355150d41ba4d6b42b5d4cb
3 * Widget defining the behavior used to choose from a set of values
4 * in a single_value group
7 * @extends OO.ui.Widget
8 * @mixins OO.ui.mixin.LabelElement
11 * @param {mw.rcfilters.dm.FilterGroup} model Group model
12 * @param {Object} [config] Configuration object
13 * @cfg {Function} [itemFilter] A filter function for the items from the
14 * model. If not given, all items will be included. The function must
15 * handle item models and return a boolean whether the item is included
16 * or not. Example: function ( itemModel ) { return itemModel.isSelected(); }
18 mw
.rcfilters
.ui
.ValuePickerWidget
= function MwRcfiltersUiValuePickerWidget( model
, config
) {
19 config
= config
|| {};
22 mw
.rcfilters
.ui
.ValuePickerWidget
.parent
.call( this, config
);
24 OO
.ui
.mixin
.LabelElement
.call( this, config
);
27 this.itemFilter
= config
.itemFilter
|| function () { return true; };
29 // Build the selection from the item models
30 this.selectWidget
= new OO
.ui
.ButtonSelectWidget();
31 this.initializeSelectWidget();
34 this.model
.connect( this, { update
: 'onModelUpdate' } );
35 this.selectWidget
.connect( this, { choose
: 'onSelectWidgetChoose' } );
39 .addClass( 'mw-rcfilters-ui-valuePickerWidget' )
42 .addClass( 'mw-rcfilters-ui-valuePickerWidget-title' ),
43 this.selectWidget
.$element
49 OO
.inheritClass( mw
.rcfilters
.ui
.ValuePickerWidget
, OO
.ui
.Widget
);
50 OO
.mixinClass( mw
.rcfilters
.ui
.ValuePickerWidget
, OO
.ui
.mixin
.LabelElement
);
56 * @param {string} name Item name
58 * An item has been chosen
64 * Respond to model update event
66 mw
.rcfilters
.ui
.ValuePickerWidget
.prototype.onModelUpdate = function () {
67 this.selectCurrentModelItem();
71 * Respond to select widget choose event
73 * @param {OO.ui.ButtonOptionWidget} chosenItem Chosen item
76 mw
.rcfilters
.ui
.ValuePickerWidget
.prototype.onSelectWidgetChoose = function ( chosenItem
) {
77 this.emit( 'choose', chosenItem
.getData() );
81 * Initialize the select widget
83 mw
.rcfilters
.ui
.ValuePickerWidget
.prototype.initializeSelectWidget = function () {
84 var items
= this.model
.getItems()
85 .filter( this.itemFilter
)
86 .map( function ( filterItem
) {
87 return new OO
.ui
.ButtonOptionWidget( {
88 data
: filterItem
.getName(),
89 label
: filterItem
.getLabel()
93 this.selectWidget
.clearItems();
94 this.selectWidget
.addItems( items
);
96 this.selectCurrentModelItem();
100 * Select the current item that corresponds with the model item
101 * that is currently selected
103 mw
.rcfilters
.ui
.ValuePickerWidget
.prototype.selectCurrentModelItem = function () {
104 var selectedItem
= this.model
.findSelectedItems()[ 0 ];
106 if ( selectedItem
) {
107 this.selectWidget
.selectItemByData( selectedItem
.getName() );