mediawiki-extensions-Echo/modules/ui/mw.echo.ui.SpecialHelpMenuWidget.js
Jon Harald Søby 60748b0b25 Add PLURAL support for "mark all as read" buttons
Pass a relevant number of unread notifications to the messages
'echo-mark-all-as-read' and 'echo-mark-wiki-as-read' each time
they are used, in order to facilitate the use of PLURAL in
the messages. This allows translators to say e.g. "mark both as
read" when there are two unread notifications, for instance.

Bug: T321462
Change-Id: Ida65be2d7f2663d9802f71c7ea1eb588db8cd254
2023-04-29 01:31:14 +02:00

139 lines
4.1 KiB
JavaScript

( function () {
/**
* Widget for the settings menu in the Special:Notifications page
*
* @param {mw.echo.dm.ModelManager} manager Model manager
* @param {Object} config Configuration object
* @cfg {string} [prefLink] Link to preferences page
*/
mw.echo.ui.SpecialHelpMenuWidget = function MwEchoUiSpecialHelpMenuWidget( manager, config ) {
config = config || {};
// Parent constructor
mw.echo.ui.SpecialHelpMenuWidget.super.call( this, $.extend( {
icon: 'settings',
label: mw.msg( 'echo-specialpage-special-help-menu-widget-aria-label' ),
indicator: 'down',
invisibleLabel: true,
menu: {
classes: [ 'mw-echo-ui-specialHelpMenuWidget-menu' ],
horizontalPosition: 'end',
width: 'auto'
}
}, config ) );
this.manager = manager;
this.markAllReadOption = new OO.ui.MenuOptionWidget( {
icon: 'checkAll',
label: this.getMarkAllReadOptionLabel(
this.manager.getPaginationModel().getCurrentPageItemCount()
),
data: 'markAllRead'
} );
this.markAllReadOption.toggle( false );
this.menu.addItems( [ this.markAllReadOption ] );
if ( config.prefLink ) {
this.menu.addItems( [
// Preferences link
new OO.ui.MenuOptionWidget( {
// Use link for accessibility
$element: $( '<a>' ).attr( 'href', config.prefLink ),
icon: 'settings',
label: mw.msg( 'mypreferences' ),
data: { href: config.prefLink }
} )
] );
}
// Events
this.manager.connect( this, {
localCountChange: 'onLocalCountChange'
} );
this.manager.getFiltersModel().getSourcePagesModel().connect( this, { update: 'onSourcePageUpdate' } );
this.menu.connect( this, { choose: 'onMenuChoose' } );
this.$element.addClass( 'mw-echo-ui-specialHelpMenuWidget' );
};
/* Initialization */
OO.inheritClass( mw.echo.ui.SpecialHelpMenuWidget, OO.ui.ButtonMenuSelectWidget );
/* Events */
/**
* @event markAllRead
*
* Mark all notifications as read in the selected wiki
*/
/* Methods */
/**
* Respond to source page change
*/
mw.echo.ui.SpecialHelpMenuWidget.prototype.onSourcePageUpdate = function () {
var sourcePagesModel = this.manager.getFiltersModel().getSourcePagesModel(),
source = sourcePagesModel.getCurrentSource(),
sourcePages = sourcePagesModel.getSourcePages( source ),
currentPage = sourcePagesModel.getCurrentPage(),
currentCount = currentPage ?
sourcePages[ currentPage ].count :
sourcePagesModel.getSourceTotalCount( source );
this.markAllReadOption.setLabel( this.getMarkAllReadOptionLabel( currentCount ) );
};
/**
* Respond to local counter update event
*
* @param {number} count New count
*/
mw.echo.ui.SpecialHelpMenuWidget.prototype.onLocalCountChange = function ( count ) {
this.markAllReadOption.setLabel( this.getMarkAllReadOptionLabel( count ) );
this.markAllReadOption.toggle( count > 0 );
};
/**
* Handle menu choose events
*
* @param {OO.ui.MenuOptionWidget} item Chosen item
*/
mw.echo.ui.SpecialHelpMenuWidget.prototype.onMenuChoose = function ( item ) {
var data = item.getData();
if ( data.href ) {
location.href = data.href;
} else if ( data === 'markAllRead' ) {
// Log this action
mw.echo.logger.logInteraction(
mw.echo.Logger.static.actions.markAllReadClick,
mw.echo.Logger.static.context.archive,
null, // Notification ID is irrelevant
this.manager.getTypeString(), // The type of the list in general
null, // The Logger has logic to decide whether this is mobile or not
this.manager.getFiltersModel().getSourcePagesModel().getCurrentSource() // Source name
);
this.emit( 'markAllRead' );
}
};
/**
* Build the button label
*
* @param {number} count Number of unread notifications
* @return {string} Mark all read button label
*/
mw.echo.ui.SpecialHelpMenuWidget.prototype.getMarkAllReadOptionLabel = function ( count ) {
var pageModel = this.manager.getFiltersModel().getSourcePagesModel(),
source = pageModel.getCurrentSource(),
sourceTitle = pageModel.getSourceTitle( source );
return sourceTitle ?
mw.msg( 'echo-mark-wiki-as-read', sourceTitle, count ) :
mw.msg( 'echo-mark-all-as-read', count );
};
}() );