Localisation updates from https://translatewiki.net.
[mediawiki.git] / resources / src / mediawiki.rcfilters / ui / ChangesLimitAndDateButtonWidget.js
blob972a4888d389cd55a57672e4bea4c56d6132a8bd
1 const ChangesLimitPopupWidget = require( './ChangesLimitPopupWidget.js' ),
2         DatePopupWidget = require( './DatePopupWidget.js' );
4 /**
5  * Widget defining the button controlling the popup for the number of results.
6  *
7  * @class mw.rcfilters.ui.ChangesLimitAndDateButtonWidget
8  * @ignore
9  * @extends OO.ui.Widget
10  *
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
15  */
16 const ChangesLimitAndDateButtonWidget = function MwRcfiltersUiChangesLimitWidget( controller, model, config ) {
17         config = config || {};
19         // Parent
20         ChangesLimitAndDateButtonWidget.super.call( this, config );
22         this.controller = controller;
23         this.model = model;
25         this.$overlay = config.$overlay || this.$element;
27         this.button = null;
28         this.limitGroupModel = null;
29         this.groupByPageItemModel = null;
30         this.daysGroupModel = null;
32         this.model.connect( this, {
33                 initialize: 'onModelInitialize'
34         } );
36         this.$element
37                 .addClass( 'mw-rcfilters-ui-changesLimitAndDateButtonWidget' );
40 /* Initialization */
42 OO.inheritClass( ChangesLimitAndDateButtonWidget, OO.ui.Widget );
44 /**
45  * Respond to model initialize event
46  */
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(
61                         this.limitGroupModel,
62                         this.groupByPageItemModel
63                 );
65                 const datePopupWidget = new DatePopupWidget(
66                         this.daysGroupModel,
67                         {
68                                 label: mw.msg( 'rcfilters-date-popup-title' )
69                         }
70                 );
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( {
77                         icon: 'settings',
78                         indicator: 'down',
79                         label: mw.msg( 'rcfilters-limit-and-date-label', currentValue ),
80                         $overlay: this.$overlay,
81                         popup: {
82                                 width: 300,
83                                 padded: false,
84                                 anchor: false,
85                                 align: 'backwards',
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
91                                 )
92                         }
93                 } );
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' )
99                 );
100                 this.updateButtonLabel();
102                 // Events
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'
109                 } );
110                 datePopupWidget.connect( this, { days: 'onPopupDays' } );
112                 this.$element.append( this.button.$element );
113         }
117  * Respond to popup initialized event
119  */
120 ChangesLimitAndDateButtonWidget.prototype.onPopupInitialized = function () {
121         this.changesLimitPopupWidget.$element.find( '*[tabindex]' ).first().trigger( 'focus' );
125  * Respond to popup closing event
127  */
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
136  */
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
149  */
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
159  */
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
168  */
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
179  */
180 ChangesLimitAndDateButtonWidget.prototype.updateButtonLabel = function () {
181         const limit = this.limitGroupModel.findSelectedItems()[ 0 ],
182                 label = limit && limit.getLabel(),
183                 days = this.daysGroupModel.findSelectedItems()[ 0 ];
185         // Update the label
186         if ( label && days ) {
187                 const message = mw.msg( 'rcfilters-limit-and-date-label', label,
188                         mw.msg(
189                                 Number( days.getParamName() ) < 1 ?
190                                         'rcfilters-days-show-hours' :
191                                         'rcfilters-days-show-days',
192                                 days.getLabel()
193                         )
194                 );
195                 this.button.setLabel( message );
196         }
199 module.exports = ChangesLimitAndDateButtonWidget;