1 const ChangesLimitPopupWidget = require( './ChangesLimitPopupWidget.js' ),
2 DatePopupWidget = require( './DatePopupWidget.js' );
5 * Widget defining the button controlling the popup for the number of results.
7 * @class mw.rcfilters.ui.ChangesLimitAndDateButtonWidget
9 * @extends OO.ui.Widget
11 * @param {mw.rcfilters.Controller} controller Controller
12 * @param {mw.rcfilters.dm.FiltersViewModel} model View model
13 * @param {Object} [config] Configuration object
14 * @param {jQuery} [config.$overlay] A jQuery object serving as overlay for popups
16 const ChangesLimitAndDateButtonWidget = function MwRcfiltersUiChangesLimitWidget( controller, model, config ) {
17 config = config || {};
20 ChangesLimitAndDateButtonWidget.super.call( this, config );
22 this.controller = controller;
25 this.$overlay = config.$overlay || this.$element;
28 this.limitGroupModel = null;
29 this.groupByPageItemModel = null;
30 this.daysGroupModel = null;
32 this.model.connect( this, {
33 initialize: 'onModelInitialize'
37 .addClass( 'mw-rcfilters-ui-changesLimitAndDateButtonWidget' );
42 OO.inheritClass( ChangesLimitAndDateButtonWidget, OO.ui.Widget );
45 * Respond to model initialize event
47 ChangesLimitAndDateButtonWidget.prototype.onModelInitialize = function () {
48 const displayGroupModel = this.model.getGroup( 'display' );
50 this.limitGroupModel = this.model.getGroup( 'limit' );
51 this.groupByPageItemModel = displayGroupModel.getItemByParamName( 'enhanced' );
52 this.daysGroupModel = this.model.getGroup( 'days' );
54 // HACK: We need the model to be ready before we populate the button
55 // and the widget, because we require the filter items for the
56 // limit and their events. This addition is only done after the
57 // model is initialized.
58 // Note: This will be fixed soon!
59 if ( this.limitGroupModel && this.daysGroupModel ) {
60 this.changesLimitPopupWidget = new ChangesLimitPopupWidget(
62 this.groupByPageItemModel
65 const datePopupWidget = new DatePopupWidget(
68 label: mw.msg( 'rcfilters-date-popup-title' )
72 const selectedItem = this.limitGroupModel.findSelectedItems()[ 0 ];
73 const currentValue = ( selectedItem && selectedItem.getLabel() ) ||
74 mw.language.convertNumber( this.limitGroupModel.getDefaultParamValue() );
76 this.button = new OO.ui.PopupButtonWidget( {
79 label: mw.msg( 'rcfilters-limit-and-date-label', currentValue ),
80 $overlay: this.$overlay,
86 $autoCloseIgnore: this.$overlay,
87 $content: $( '<div>' ).append(
88 // TODO: Merge ChangesLimitPopupWidget with DatePopupWidget into one common widget
89 this.changesLimitPopupWidget.$element,
90 datePopupWidget.$element
95 this.button.popup.connect( this, { ready: 'onPopupInitialized' } );
96 this.button.popup.connect( this, { closing: 'onPopupClosing' } );
97 this.button.popup.$element.attr( 'aria-label',
98 mw.msg( 'rcfilters-limit-and-date-popup-dialog-aria-label' )
100 this.updateButtonLabel();
103 this.limitGroupModel.connect( this, { update: 'updateButtonLabel' } );
104 this.daysGroupModel.connect( this, { update: 'updateButtonLabel' } );
105 this.changesLimitPopupWidget.connect( this, {
106 limit: 'onPopupLimit',
107 groupByPage: 'onPopupGroupByPage',
108 groupByPageUserClick: 'onPopupGroupByPageUserClick'
110 datePopupWidget.connect( this, { days: 'onPopupDays' } );
112 this.$element.append( this.button.$element );
117 * Respond to popup initialized event
120 ChangesLimitAndDateButtonWidget.prototype.onPopupInitialized = function () {
121 this.changesLimitPopupWidget.$element.find( '*[tabindex]' ).first().trigger( 'focus' );
125 * Respond to popup closing event
128 ChangesLimitAndDateButtonWidget.prototype.onPopupClosing = function () {
129 this.button.$button.trigger( 'focus' );
133 * Respond to popup limit change event
135 * @param {string} filterName Chosen filter name
137 ChangesLimitAndDateButtonWidget.prototype.onPopupLimit = function ( filterName ) {
138 const item = this.limitGroupModel.getItemByName( filterName );
140 this.controller.toggleFilterSelect( filterName, true );
141 this.controller.updateLimitDefault( item.getParamName() );
142 this.button.popup.toggle( false );
146 * Respond to popup limit change event
148 * @param {boolean} isGrouped The result set is grouped by page
150 ChangesLimitAndDateButtonWidget.prototype.onPopupGroupByPage = function ( isGrouped ) {
151 this.controller.toggleFilterSelect( this.groupByPageItemModel.getName(), isGrouped );
152 this.button.popup.toggle( false );
156 * Respond to popup request to save the group by page setting in preferences
158 * @param {boolean} isSelected The state of the group by page checkbox
160 ChangesLimitAndDateButtonWidget.prototype.onPopupGroupByPageUserClick = function ( isSelected ) {
161 this.controller.updateGroupByPageDefault( isSelected );
165 * Respond to popup limit change event
167 * @param {string} filterName Chosen filter name
169 ChangesLimitAndDateButtonWidget.prototype.onPopupDays = function ( filterName ) {
170 const item = this.daysGroupModel.getItemByName( filterName );
172 this.controller.toggleFilterSelect( filterName, true );
173 this.controller.updateDaysDefault( item.getParamName() );
174 this.button.popup.toggle( false );
178 * Respond to limit choose event
180 ChangesLimitAndDateButtonWidget.prototype.updateButtonLabel = function () {
181 const limit = this.limitGroupModel.findSelectedItems()[ 0 ],
182 label = limit && limit.getLabel(),
183 days = this.daysGroupModel.findSelectedItems()[ 0 ];
186 if ( label && days ) {
187 const message = mw.msg( 'rcfilters-limit-and-date-label', label,
189 Number( days.getParamName() ) < 1 ?
190 'rcfilters-days-show-hours' :
191 'rcfilters-days-show-days',
195 this.button.setLabel( message );
199 module.exports = ChangesLimitAndDateButtonWidget;