summaryrefslogtreecommitdiff
path: root/www/wiki/resources/src/mediawiki.rcfilters/ui/mw.rcfilters.ui.ValuePickerWidget.js
blob: 304929b78ea09cb56355150d41ba4d6b42b5d4cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
( function ( mw ) {
	/**
	 * Widget defining the behavior used to choose from a set of values
	 * in a single_value group
	 *
	 * @class
	 * @extends OO.ui.Widget
	 * @mixins OO.ui.mixin.LabelElement
	 *
	 * @constructor
	 * @param {mw.rcfilters.dm.FilterGroup} model Group model
	 * @param {Object} [config] Configuration object
	 * @cfg {Function} [itemFilter] A filter function for the items from the
	 *  model. If not given, all items will be included. The function must
	 *  handle item models and return a boolean whether the item is included
	 *  or not. Example: function ( itemModel ) { return itemModel.isSelected(); }
	 */
	mw.rcfilters.ui.ValuePickerWidget = function MwRcfiltersUiValuePickerWidget( model, config ) {
		config = config || {};

		// Parent
		mw.rcfilters.ui.ValuePickerWidget.parent.call( this, config );
		// Mixin constructors
		OO.ui.mixin.LabelElement.call( this, config );

		this.model = model;
		this.itemFilter = config.itemFilter || function () { return true; };

		// Build the selection from the item models
		this.selectWidget = new OO.ui.ButtonSelectWidget();
		this.initializeSelectWidget();

		// Events
		this.model.connect( this, { update: 'onModelUpdate' } );
		this.selectWidget.connect( this, { choose: 'onSelectWidgetChoose' } );

		// Initialize
		this.$element
			.addClass( 'mw-rcfilters-ui-valuePickerWidget' )
			.append(
				this.$label
					.addClass( 'mw-rcfilters-ui-valuePickerWidget-title' ),
				this.selectWidget.$element
			);
	};

	/* Initialization */

	OO.inheritClass( mw.rcfilters.ui.ValuePickerWidget, OO.ui.Widget );
	OO.mixinClass( mw.rcfilters.ui.ValuePickerWidget, OO.ui.mixin.LabelElement );

	/* Events */

	/**
	 * @event choose
	 * @param {string} name Item name
	 *
	 * An item has been chosen
	 */

	/* Methods */

	/**
	 * Respond to model update event
	 */
	mw.rcfilters.ui.ValuePickerWidget.prototype.onModelUpdate = function () {
		this.selectCurrentModelItem();
	};

	/**
	 * Respond to select widget choose event
	 *
	 * @param {OO.ui.ButtonOptionWidget} chosenItem Chosen item
	 * @fires choose
	 */
	mw.rcfilters.ui.ValuePickerWidget.prototype.onSelectWidgetChoose = function ( chosenItem ) {
		this.emit( 'choose', chosenItem.getData() );
	};

	/**
	 * Initialize the select widget
	 */
	mw.rcfilters.ui.ValuePickerWidget.prototype.initializeSelectWidget = function () {
		var items = this.model.getItems()
			.filter( this.itemFilter )
			.map( function ( filterItem ) {
				return new OO.ui.ButtonOptionWidget( {
					data: filterItem.getName(),
					label: filterItem.getLabel()
				} );
			} );

		this.selectWidget.clearItems();
		this.selectWidget.addItems( items );

		this.selectCurrentModelItem();
	};

	/**
	 * Select the current item that corresponds with the model item
	 * that is currently selected
	 */
	mw.rcfilters.ui.ValuePickerWidget.prototype.selectCurrentModelItem = function () {
		var selectedItem = this.model.findSelectedItems()[ 0 ];

		if ( selectedItem ) {
			this.selectWidget.selectItemByData( selectedItem.getName() );
		}
	};
}( mediaWiki ) );