RCFilters: Add range group filters - limit, days and hours
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.ChangesLimitButtonWidget.js
1 ( function ( mw ) {
2 /**
3 * Widget defining the button controlling the popup for the number of results
4 *
5 * @extends OO.ui.Widget
6 *
7 * @constructor
8 * @param {mw.rcfilters.Controller} controller Controller
9 * @param {mw.rcfilters.dm.FiltersViewModel} model View model
10 * @param {Object} [config] Configuration object
11 * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
12 */
13 mw.rcfilters.ui.ChangesLimitButtonWidget = function MwRcfiltersUiChangesLimitWidget( controller, model, config ) {
14 config = config || {};
15
16 // Parent
17 mw.rcfilters.ui.ChangesLimitButtonWidget.parent.call( this, config );
18
19 this.controller = controller;
20 this.model = model;
21
22 this.$overlay = config.$overlay || this.$element;
23
24 this.button = null;
25 this.limitGroupModel = null;
26
27 this.model.connect( this, {
28 initialize: 'onModelInitialize'
29 } );
30
31 this.$element
32 .addClass( 'mw-rcfilters-ui-changesLimitButtonWidget' );
33 };
34
35 /* Initialization */
36
37 OO.inheritClass( mw.rcfilters.ui.ChangesLimitButtonWidget, OO.ui.Widget );
38
39 /**
40 * Respond to model initialize event
41 */
42 mw.rcfilters.ui.ChangesLimitButtonWidget.prototype.onModelInitialize = function () {
43 var changesLimitPopupWidget, selectedItem, currentValue;
44
45 this.limitGroupModel = this.model.getGroup( 'limit' );
46
47 // HACK: We need the model to be ready before we populate the button
48 // and the widget, because we require the filter items for the
49 // limit and their events. This addition is only done after the
50 // model is initialized.
51 // Note: This will be fixed soon!
52 if ( this.limitGroupModel ) {
53 changesLimitPopupWidget = new mw.rcfilters.ui.ChangesLimitPopupWidget(
54 this.limitGroupModel
55 );
56
57 selectedItem = this.limitGroupModel.getSelectedItems()[ 0 ];
58 currentValue = ( selectedItem && selectedItem.getLabel() ) ||
59 mw.language.convertNumber( this.limitGroupModel.getDefaultParamValue() );
60
61 this.button = new OO.ui.PopupButtonWidget( {
62 indicator: 'down',
63 label: mw.msg( 'rcfilters-limit-shownum', currentValue ),
64 $overlay: this.$overlay,
65 popup: {
66 width: 300,
67 padded: true,
68 anchor: false,
69 align: 'backwards',
70 $autoCloseIgnore: this.$overlay,
71 $content: changesLimitPopupWidget.$element
72 }
73 } );
74
75 // Events
76 this.limitGroupModel.connect( this, { update: 'onLimitGroupModelUpdate' } );
77 changesLimitPopupWidget.connect( this, { limit: 'onPopupLimit' } );
78
79 this.$element.append( this.button.$element );
80 }
81 };
82
83 /**
84 * Respond to popup limit change event
85 *
86 * @param {string} filterName Chosen filter name
87 */
88 mw.rcfilters.ui.ChangesLimitButtonWidget.prototype.onPopupLimit = function ( filterName ) {
89 this.controller.toggleFilterSelect( filterName, true );
90 };
91
92 /**
93 * Respond to limit choose event
94 *
95 * @param {string} filterName Filter name
96 */
97 mw.rcfilters.ui.ChangesLimitButtonWidget.prototype.onLimitGroupModelUpdate = function () {
98 var item = this.limitGroupModel.getSelectedItems()[ 0 ],
99 label = item && item.getLabel();
100
101 // Update the label
102 if ( label ) {
103 this.button.setLabel( mw.msg( 'rcfilters-limit-shownum', label ) );
104 }
105 };
106
107 }( mediaWiki ) );