Debian Codegraph @

extensions/Echo/modules/ui/mw.echo.ui.SpecialHelpMenuWidget.js

raw symbols SCIP
1 ( function () {
2 /**
3 * Widget for the settings menu in the Special:Notifications page
4 *
5 * @param {mw.echo.dm.ModelManager} manager Model manager
6 * @param {Object} config Configuration object
7 * @param {string} [config.prefLink] Link to preferences page
8 */
9 mw.echo.ui.SpecialHelpMenuWidget = function MwEchoUiSpecialHelpMenuWidget( manager, config ) {
10 config = config || {};
11
12 // Parent constructor
13 mw.echo.ui.SpecialHelpMenuWidget.super.call( this, Object.assign( {
14 icon: 'settings',
15 label: mw.msg( 'echo-specialpage-special-help-menu-widget-aria-label' ),
16 indicator: 'down',
17 invisibleLabel: true,
18 menu: {
19 classes: [ 'mw-echo-ui-specialHelpMenuWidget-menu' ],
20 horizontalPosition: 'end',
21 width: 'auto'
22 }
23 }, config ) );
24
25 this.manager = manager;
26
27 this.markAllReadOption = new OO.ui.MenuOptionWidget( {
28 icon: 'checkAll',
29 label: this.getMarkAllReadOptionLabel(
30 this.manager.getPaginationModel().getCurrentPageItemCount()
31 ),
32 data: 'markAllRead'
33 } );
34 this.markAllReadOption.toggle( false );
35
36 this.menu.addItems( [ this.markAllReadOption ] );
37 if ( config.prefLink ) {
38 this.menu.addItems( [
39 // Preferences link
40 new OO.ui.MenuOptionWidget( {
41 // Use link for accessibility
42 $element: $( '<a>' ).attr( 'href', config.prefLink ),
43 icon: 'settings',
44 label: mw.msg( 'mypreferences' ),
45 data: { href: config.prefLink }
46 } )
47 ] );
48 }
49
50 // Events
51 this.manager.connect( this, {
52 localCountChange: 'onLocalCountChange'
53 } );
54 this.manager.getFiltersModel().getSourcePagesModel().connect( this, { update: 'onSourcePageUpdate' } );
55 this.menu.connect( this, { choose: 'onMenuChoose' } );
56
57 this.$element.addClass( 'mw-echo-ui-specialHelpMenuWidget' );
58 };
59
60 /* Initialization */
61
62 OO.inheritClass( mw.echo.ui.SpecialHelpMenuWidget, OO.ui.ButtonMenuSelectWidget );
63
64 /* Events */
65
66 /**
67 * Mark all notifications as read in the selected wiki
68 *
69 * @event mw.echo.ui.SpecialHelpMenuWidget#markAllRead
70 */
71
72 /* Methods */
73
74 /**
75 * Respond to source page change
76 */
77 mw.echo.ui.SpecialHelpMenuWidget.prototype.onSourcePageUpdate = function () {
78 const sourcePagesModel = this.manager.getFiltersModel().getSourcePagesModel(),
79 source = sourcePagesModel.getCurrentSource(),
80 sourcePages = sourcePagesModel.getSourcePages( source ),
81 currentPage = sourcePagesModel.getCurrentPage(),
82 currentCount = currentPage ?
83 sourcePages[ currentPage ].count :
84 sourcePagesModel.getSourceTotalCount( source );
85
86 this.markAllReadOption.setLabel( this.getMarkAllReadOptionLabel( currentCount ) );
87 };
88
89 /**
90 * Respond to local counter update event
91 *
92 * @param {number} count New count
93 */
94 mw.echo.ui.SpecialHelpMenuWidget.prototype.onLocalCountChange = function ( count ) {
95 this.markAllReadOption.setLabel( this.getMarkAllReadOptionLabel( count ) );
96 this.markAllReadOption.toggle( count > 0 );
97 };
98
99 /**
100 * Handle menu choose events
101 *
102 * @param {OO.ui.MenuOptionWidget} item Chosen item
103 * @fires mw.echo.ui.SpecialHelpMenuWidget#markAllRead
104 */
105 mw.echo.ui.SpecialHelpMenuWidget.prototype.onMenuChoose = function ( item ) {
106 const data = item.getData();
107 if ( data.href ) {
108 location.href = data.href;
109 } else if ( data === 'markAllRead' ) {
110 this.emit( 'markAllRead' );
111 }
112 };
113
114 /**
115 * Build the button label
116 *
117 * @param {number} count Number of unread notifications
118 * @return {string} Mark all read button label
119 */
120 mw.echo.ui.SpecialHelpMenuWidget.prototype.getMarkAllReadOptionLabel = function ( count ) {
121 const pageModel = this.manager.getFiltersModel().getSourcePagesModel(),
122 source = pageModel.getCurrentSource(),
123 sourceTitle = pageModel.getSourceTitle( source );
124
125 return sourceTitle ?
126 mw.msg( 'echo-mark-wiki-as-read', sourceTitle, count ) :
127 mw.msg( 'echo-mark-all-as-read', count );
128 };
129
130 }() );