1 var ChangesLimitPopupWidget
= require( './ChangesLimitPopupWidget.js' ),
2 DatePopupWidget
= require( './DatePopupWidget.js' ),
3 ChangesLimitAndDateButtonWidget
;
6 * Widget defining the button controlling the popup for the number of results
8 * @class mw.rcfilters.ui.ChangesLimitAndDateButtonWidget
9 * @extends OO.ui.Widget
12 * @param {mw.rcfilters.Controller} controller Controller
13 * @param {mw.rcfilters.dm.FiltersViewModel} model View model
14 * @param {Object} [config] Configuration object
15 * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
17 ChangesLimitAndDateButtonWidget
= function MwRcfiltersUiChangesLimitWidget( controller
, model
, config
) {
18 config
= config
|| {};
21 ChangesLimitAndDateButtonWidget
.parent
.call( this, config
);
23 this.controller
= controller
;
26 this.$overlay
= config
.$overlay
|| this.$element
;
29 this.limitGroupModel
= null;
30 this.groupByPageItemModel
= null;
31 this.daysGroupModel
= null;
33 this.model
.connect( this, {
34 initialize
: 'onModelInitialize'
38 .addClass( 'mw-rcfilters-ui-changesLimitAndDateButtonWidget' );
43 OO
.inheritClass( ChangesLimitAndDateButtonWidget
, OO
.ui
.Widget
);
46 * Respond to model initialize event
48 ChangesLimitAndDateButtonWidget
.prototype.onModelInitialize = function () {
49 var changesLimitPopupWidget
, selectedItem
, currentValue
, datePopupWidget
,
50 displayGroupModel
= this.model
.getGroup( 'display' );
52 this.limitGroupModel
= this.model
.getGroup( 'limit' );
53 this.groupByPageItemModel
= displayGroupModel
.getItemByParamName( 'enhanced' );
54 this.daysGroupModel
= this.model
.getGroup( 'days' );
56 // HACK: We need the model to be ready before we populate the button
57 // and the widget, because we require the filter items for the
58 // limit and their events. This addition is only done after the
59 // model is initialized.
60 // Note: This will be fixed soon!
61 if ( this.limitGroupModel
&& this.daysGroupModel
) {
62 changesLimitPopupWidget
= new ChangesLimitPopupWidget(
64 this.groupByPageItemModel
67 datePopupWidget
= new DatePopupWidget(
70 label
: mw
.msg( 'rcfilters-date-popup-title' )
74 selectedItem
= this.limitGroupModel
.findSelectedItems()[ 0 ];
75 currentValue
= ( selectedItem
&& selectedItem
.getLabel() ) ||
76 mw
.language
.convertNumber( this.limitGroupModel
.getDefaultParamValue() );
78 this.button
= new OO
.ui
.PopupButtonWidget( {
81 label
: mw
.msg( 'rcfilters-limit-and-date-label', currentValue
),
82 $overlay
: this.$overlay
,
88 $autoCloseIgnore
: this.$overlay
,
89 $content
: $( '<div>' ).append(
90 // TODO: Merge ChangesLimitPopupWidget with DatePopupWidget into one common widget
91 changesLimitPopupWidget
.$element
,
92 datePopupWidget
.$element
96 this.updateButtonLabel();
99 this.limitGroupModel
.connect( this, { update
: 'updateButtonLabel' } );
100 this.daysGroupModel
.connect( this, { update
: 'updateButtonLabel' } );
101 changesLimitPopupWidget
.connect( this, {
102 limit
: 'onPopupLimit',
103 groupByPage
: 'onPopupGroupByPage'
105 datePopupWidget
.connect( this, { days
: 'onPopupDays' } );
107 this.$element
.append( this.button
.$element
);
112 * Respond to popup limit change event
114 * @param {string} filterName Chosen filter name
116 ChangesLimitAndDateButtonWidget
.prototype.onPopupLimit = function ( filterName
) {
117 var item
= this.limitGroupModel
.getItemByName( filterName
);
119 this.controller
.toggleFilterSelect( filterName
, true );
120 this.controller
.updateLimitDefault( item
.getParamName() );
121 this.button
.popup
.toggle( false );
125 * Respond to popup limit change event
127 * @param {boolean} isGrouped The result set is grouped by page
129 ChangesLimitAndDateButtonWidget
.prototype.onPopupGroupByPage = function ( isGrouped
) {
130 this.controller
.toggleFilterSelect( this.groupByPageItemModel
.getName(), isGrouped
);
131 this.controller
.updateGroupByPageDefault( isGrouped
);
132 this.button
.popup
.toggle( false );
136 * Respond to popup limit change event
138 * @param {string} filterName Chosen filter name
140 ChangesLimitAndDateButtonWidget
.prototype.onPopupDays = function ( filterName
) {
141 var item
= this.daysGroupModel
.getItemByName( filterName
);
143 this.controller
.toggleFilterSelect( filterName
, true );
144 this.controller
.updateDaysDefault( item
.getParamName() );
145 this.button
.popup
.toggle( false );
149 * Respond to limit choose event
151 ChangesLimitAndDateButtonWidget
.prototype.updateButtonLabel = function () {
153 limit
= this.limitGroupModel
.findSelectedItems()[ 0 ],
154 label
= limit
&& limit
.getLabel(),
155 days
= this.daysGroupModel
.findSelectedItems()[ 0 ],
156 daysParamName
= Number( days
.getParamName() ) < 1 ?
157 'rcfilters-days-show-hours' :
158 'rcfilters-days-show-days';
161 if ( label
&& days
) {
162 message
= mw
.msg( 'rcfilters-limit-and-date-label', label
,
163 mw
.msg( daysParamName
, days
.getLabel() )
165 this.button
.setLabel( message
);
169 module
.exports
= ChangesLimitAndDateButtonWidget
;