2 * Widget defining the behavior used to choose from a set of values
3 * in a single_value group.
5 * @class mw.rcfilters.ui.ValuePickerWidget
7 * @extends OO.ui.Widget
8 * @mixes OO.ui.mixin.LabelElement
10 * @param {mw.rcfilters.dm.FilterGroup} model Group model
11 * @param {Object} [config] Configuration object
12 * @param {Function} [config.itemFilter] A filter function for the items from the
13 * model. If not given, all items will be included. The function must
14 * handle item models and return a boolean whether the item is included
15 * or not. Example: function ( itemModel ) { return itemModel.isSelected(); }
17 const ValuePickerWidget = function MwRcfiltersUiValuePickerWidget( model, config ) {
18 config = config || {};
21 ValuePickerWidget.super.call( this, config );
23 OO.ui.mixin.LabelElement.call( this, config );
26 this.itemFilter = config.itemFilter || function () {
30 // Build the selection from the item models
31 this.selectWidget = new OO.ui.ButtonSelectWidget();
32 this.initializeSelectWidget();
35 this.model.connect( this, { update: 'onModelUpdate' } );
36 this.selectWidget.connect( this, { choose: 'onSelectWidgetChoose' } );
40 .addClass( 'mw-rcfilters-ui-valuePickerWidget' )
43 .addClass( 'mw-rcfilters-ui-valuePickerWidget-title' ),
44 this.selectWidget.$element
50 OO.inheritClass( ValuePickerWidget, OO.ui.Widget );
51 OO.mixinClass( ValuePickerWidget, OO.ui.mixin.LabelElement );
56 * An item has been chosen.
59 * @param {string} name Item name
66 * Respond to model update event
68 ValuePickerWidget.prototype.onModelUpdate = function () {
69 this.selectCurrentModelItem();
73 * Respond to select widget choose event
75 * @param {OO.ui.ButtonOptionWidget} chosenItem Chosen item
78 ValuePickerWidget.prototype.onSelectWidgetChoose = function ( chosenItem ) {
79 this.emit( 'choose', chosenItem.getData() );
83 * Initialize the select widget
85 ValuePickerWidget.prototype.initializeSelectWidget = function () {
86 const items = this.model.getItems()
87 .filter( this.itemFilter )
88 .map( ( filterItem ) => new OO.ui.ButtonOptionWidget( {
89 data: filterItem.getName(),
90 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 ValuePickerWidget.prototype.selectCurrentModelItem = function () {
104 const selectedItem = this.model.findSelectedItems()[ 0 ];
106 if ( selectedItem ) {
107 this.selectWidget.selectItemByData( selectedItem.getName() );
111 module.exports = ValuePickerWidget;