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