| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
const ValuePickerWidget = function MwRcfiltersUiValuePickerWidget( model, config ) { |
| 18 |
config = config || {}; |
| 19 |
|
| 20 |
|
| 21 |
ValuePickerWidget.super.call( this, config ); |
| 22 |
|
| 23 |
OO.ui.mixin.LabelElement.call( this, config ); |
| 24 |
|
| 25 |
this.model = model; |
| 26 |
this.itemFilter = config.itemFilter || function () { |
| 27 |
return true; |
| 28 |
}; |
| 29 |
|
| 30 |
|
| 31 |
this.selectWidget = new OO.ui.ButtonSelectWidget(); |
| 32 |
this.initializeSelectWidget(); |
| 33 |
|
| 34 |
|
| 35 |
this.model.connect( this, { update: 'onModelUpdate' } ); |
| 36 |
this.selectWidget.connect( this, { choose: 'onSelectWidgetChoose' } ); |
| 37 |
|
| 38 |
|
| 39 |
this.$element |
| 40 |
.addClass( 'mw-rcfilters-ui-valuePickerWidget' ) |
| 41 |
.append( |
| 42 |
this.$label |
| 43 |
.addClass( 'mw-rcfilters-ui-valuePickerWidget-title' ), |
| 44 |
this.selectWidget.$element |
| 45 |
); |
| 46 |
}; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
OO.inheritClass( ValuePickerWidget, OO.ui.Widget ); |
| 51 |
OO.mixinClass( ValuePickerWidget, OO.ui.mixin.LabelElement ); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
ValuePickerWidget.prototype.onModelUpdate = function () { |
| 69 |
this.selectCurrentModelItem(); |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
ValuePickerWidget.prototype.onSelectWidgetChoose = function ( chosenItem ) { |
| 79 |
this.emit( 'choose', chosenItem.getData() ); |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 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() |
| 91 |
} ) ); |
| 92 |
|
| 93 |
this.selectWidget.clearItems(); |
| 94 |
this.selectWidget.addItems( items ); |
| 95 |
|
| 96 |
this.selectCurrentModelItem(); |
| 97 |
}; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
ValuePickerWidget.prototype.selectCurrentModelItem = function () { |
| 104 |
const selectedItem = this.model.findSelectedItems()[ 0 ]; |
| 105 |
|
| 106 |
if ( selectedItem ) { |
| 107 |
this.selectWidget.selectItemByData( selectedItem.getName() ); |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
module.exports = ValuePickerWidget; |