Debian Codegraph @

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

raw symbols SCIP
1 ( function () {
2 /**
3 * Notification badge button widget for echo popup.
4 *
5 * @class
6 * @extends OO.ui.ButtonWidget
7 *
8 * @constructor
9 * @param {mw.echo.Controller} controller Echo notifications controller
10 * @param {mw.echo.dm.ModelManager} manager Model manager
11 * @param {Object} links Links object, containing 'notifications' and 'preferences' URLs
12 * @param {Object} config Configuration object
13 * @param {string|string[]} [config.type='message'] The type or array of types of
14 * notifications that are in this model. They can be 'alert', 'message' or
15 * an array of both. Defaults to 'message'
16 * @param {number} [config.numItems=0] The number of items that are in the button display
17 * @param {string} [config.convertedNumber] A converted version of the initial count
18 * @param {string} [config.badgeLabel=0] The initial label for the badge. This is the
19 * formatted version of the number of items in the badge.
20 * @param {boolean} [config.hasUnseen=false] Whether there are unseen items
21 * @param {number} [config.popupWidth=450] The width of the popup
22 * @param {string} [config.badgeIcon] Icon to use for the popup header
23 * @param {string} [config.href] URL the badge links to
24 * @param {jQuery} [config.$overlay] A jQuery element functioning as an overlay
25 * for popups.
26 */
27 mw.echo.ui.NotificationBadgeWidget = function MwEchoUiNotificationBadgeButtonPopupWidget( controller, manager, links, config ) {
28 config = config || {};
29
30 // Parent constructor
31 mw.echo.ui.NotificationBadgeWidget.super.call( this, config );
32
33 // Mixin constructors
34 OO.ui.mixin.PendingElement.call( this, config );
35
36 this.$overlay = config.$overlay || this.$element;
37 // Create a menu overlay
38 this.$menuOverlay = $( '<div>' )
39 .addClass( 'mw-echo-ui-NotificationBadgeWidget-overlay-menu' );
40 this.$overlay.append( this.$menuOverlay );
41
42 // Controller
43 this.controller = controller;
44 this.manager = manager;
45
46 const adjustedTypeString = this.controller.getTypeString() === 'message' ? 'notice' : this.controller.getTypeString();
47
48 // Properties
49 this.types = this.manager.getTypes();
50
51 this.numItems = config.numItems || 0;
52 this.hasRunFirstTime = false;
53
54 const buttonFlags = [];
55 if ( config.hasUnseen ) {
56 buttonFlags.push( 'unseen' );
57 }
58
59 this.badgeButton = new mw.echo.ui.BadgeLinkWidget( {
60 convertedNumber: config.convertedNumber,
61 type: this.manager.getTypeString(),
62 numItems: this.numItems,
63 flags: buttonFlags,
64 // The following messages can be used here:
65 // * tooltip-pt-notifications-alert
66 // * tooltip-pt-notifications-notice
67 title: mw.msg( 'tooltip-pt-notifications-' + adjustedTypeString ),
68 href: config.href
69 } );
70
71 // Notifications list widget
72 this.notificationsWidget = new mw.echo.ui.NotificationsListWidget(
73 this.controller,
74 this.manager,
75 {
76 type: this.types,
77 $overlay: this.$menuOverlay,
78 animated: true
79 }
80 );
81
82 // Footer
83 const allNotificationsButton = new OO.ui.ButtonWidget( {
84 icon: 'next',
85 label: mw.msg( 'echo-overlay-link' ),
86 href: links.notifications,
87 classes: [ 'mw-echo-ui-notificationBadgeButtonPopupWidget-footer-allnotifs' ]
88 } );
89 allNotificationsButton.$element.children().first().removeAttr( 'role' );
90
91 const preferencesButton = new OO.ui.ButtonWidget( {
92 icon: 'settings',
93 label: mw.msg( 'mypreferences' ),
94 href: links.preferences,
95 classes: [ 'mw-echo-ui-notificationBadgeButtonPopupWidget-footer-preferences' ]
96 } );
97 preferencesButton.$element.children().first().removeAttr( 'role' );
98
99 const footerItems = [ allNotificationsButton ];
100 if ( !mw.user.isTemp() ) {
101 footerItems.push( preferencesButton );
102 }
103 const footerButtonGroupWidget = new OO.ui.ButtonGroupWidget( {
104 items: footerItems,
105 classes: [ 'mw-echo-ui-notificationBadgeButtonPopupWidget-footer-buttons' ]
106 } );
107 const $footer = $( '<div>' )
108 .addClass( 'mw-echo-ui-notificationBadgeButtonPopupWidget-footer' )
109 .append( footerButtonGroupWidget.$element );
110
111 const screenWidth = $( window ).width();
112 // FIXME 639 is @max-width-breakpoint-mobile value in wikimedia-ui-base.less,
113 // should be updated with aproppriate JS exported Codex token once available, T366622
114 const maxWidthBreakPoint = 639;
115 const isUnderBreakpointMobile = screenWidth < maxWidthBreakPoint;
116 const mql = window.matchMedia( `(max-width: ${ maxWidthBreakPoint }px)` );
117 const matchMedia = function ( event ) {
118 if ( event.matches ) {
119 this.popup.containerPadding = 0;
120 } else {
121 this.popup.containerPadding = 20;
122 }
123 };
124 this.popup = new OO.ui.PopupWidget( {
125 $content: this.notificationsWidget.$element,
126 $footer: $footer,
127 width: config.popupWidth || 500,
128 hideWhenOutOfView: false,
129 autoFlip: false,
130 autoClose: true,
131 containerPadding: isUnderBreakpointMobile ? 0 : 20,
132 $floatableContainer: this.$element,
133 // Also ignore clicks from the nested action menu items, that
134 // actually exist in the overlay
135 $autoCloseIgnore: this.$element.add( this.$menuOverlay ),
136 head: true,
137 // The following messages can be used here:
138 // * echo-notification-alert-text-only
139 // * echo-notification-notice-text-only
140 label: mw.msg(
141 'echo-notification-' + adjustedTypeString +
142 '-text-only'
143 ),
144 classes: [ 'mw-echo-ui-notificationBadgeButtonPopupWidget-popup' ]
145 } );
146 mql.addEventListener( 'change', matchMedia.bind( this ) );
147 // Append the popup to the overlay
148 this.$overlay.append( this.popup.$element );
149
150 // HACK: Add an icon to the popup head label
151 this.popupHeadIcon = new OO.ui.IconWidget( { icon: config.badgeIcon } );
152 this.popup.$head.prepend( this.popupHeadIcon.$element );
153
154 this.setPendingElement( this.popup.$head );
155
156 // Mark all as read button
157 this.markAllReadLabel = mw.msg( 'echo-mark-all-as-read', config.convertedNumber );
158 this.markAllReadButton = new OO.ui.ButtonWidget( {
159 framed: false,
160 label: this.markAllReadLabel,
161 classes: [ 'mw-echo-ui-notificationsWidget-markAllReadButton' ]
162 } );
163
164 // Hide the close button
165 this.popup.closeButton.toggle( false );
166 // Add the 'mark all as read' button to the header
167 this.popup.$head.append( this.markAllReadButton.$element );
168 this.markAllReadButton.toggle( false );
169
170 // Events
171 this.markAllReadButton.connect( this, { click: 'onMarkAllReadButtonClick' } );
172 this.manager.connect( this, {
173 update: 'updateBadge'
174 } );
175 this.manager.getSeenTimeModel().connect( this, { update: 'onSeenTimeModelUpdate' } );
176 this.manager.getUnreadCounter().connect( this, { countChange: 'updateBadge' } );
177 this.popup.connect( this, { toggle: 'onPopupToggle' } );
178 this.badgeButton.connect( this, {
179 click: 'onBadgeButtonClick'
180 } );
181 this.notificationsWidget.connect( this, { modified: 'onNotificationsListModified' } );
182
183 this.$element
184 .prop( 'id', 'pt-notifications-' + adjustedTypeString )
185 // The following classes can be used here:
186 // * mw-echo-ui-notificationBadgeButtonPopupWidget-alert
187 // * mw-echo-ui-notificationBadgeButtonPopupWidget-message
188 .addClass(
189 'mw-echo-ui-notificationBadgeButtonPopupWidget ' +
190 'mw-echo-ui-notificationBadgeButtonPopupWidget-' + adjustedTypeString
191 )
192 .append( this.badgeButton.$element );
193 mw.hook( 'ext.echo.NotificationBadgeWidget.onInitialize' ).fire( this );
194 };
195
196 /* Initialization */
197
198 OO.inheritClass( mw.echo.ui.NotificationBadgeWidget, OO.ui.Widget );
199 OO.mixinClass( mw.echo.ui.NotificationBadgeWidget, OO.ui.mixin.PendingElement );
200
201 /* Static properties */
202
203 mw.echo.ui.NotificationBadgeWidget.static.tagName = 'li';
204
205 /* Events */
206
207 /**
208 * All notifications were marked as read
209 *
210 * @event mw.echo.ui.NotificationBadgeWidget#allRead
211 */
212
213 /**
214 * Notifications have successfully finished being processed and are fully loaded
215 *
216 * @event mw.echo.ui.NotificationBadgeWidget#finishLoading
217 */
218
219 /* Methods */
220
221 /**
222 * Respond to list widget modified event.
223 *
224 * This means the list's actual DOM was modified and we should make sure
225 * that the popup resizes itself.
226 */
227 mw.echo.ui.NotificationBadgeWidget.prototype.onNotificationsListModified = function () {
228 this.popup.clip();
229 };
230
231 /**
232 * Respond to badge button click
233 */
234 mw.echo.ui.NotificationBadgeWidget.prototype.onBadgeButtonClick = function () {
235 this.popup.toggle();
236 };
237
238 /**
239 * Respond to SeenTime model update event
240 */
241 mw.echo.ui.NotificationBadgeWidget.prototype.onSeenTimeModelUpdate = function () {
242 this.updateBadgeSeenState( false );
243 };
244
245 /**
246 * Update the badge style to match whether it contains unseen notifications.
247 *
248 * @param {boolean} [hasUnseen=false] There are unseen notifications
249 */
250 mw.echo.ui.NotificationBadgeWidget.prototype.updateBadgeSeenState = function ( hasUnseen ) {
251 hasUnseen = hasUnseen === undefined ? false : !!hasUnseen;
252
253 this.badgeButton.setFlags( { unseen: !!hasUnseen } );
254 };
255
256 /**
257 * Update the badge state and label based on changes to the model
258 */
259 mw.echo.ui.NotificationBadgeWidget.prototype.updateBadge = function () {
260 const unreadCount = this.manager.getUnreadCounter().getCount();
261 const cappedUnreadCount = this.manager.getUnreadCounter().getCappedNotificationCount( unreadCount );
262 const convertedCount = mw.language.convertNumber( cappedUnreadCount );
263 const badgeLabel = mw.msg( 'echo-badge-count', convertedCount );
264 this.markAllReadLabel = mw.msg( 'echo-mark-all-as-read', convertedCount );
265 this.markAllReadButton.setLabel( this.markAllReadLabel );
266
267 this.badgeButton.setLabel( badgeLabel );
268 this.badgeButton.setCount( unreadCount, badgeLabel );
269 // Update seen state only if the counter is 0
270 // so we don't run into inconsistencies and have an unseen state
271 // for the badge with 0 unread notifications
272 if ( unreadCount === 0 ) {
273 this.updateBadgeSeenState( false );
274 }
275
276 // Check if we need to display the 'mark all unread' button
277 this.markAllReadButton.toggle( this.manager.hasLocalUnread() );
278 };
279
280 /**
281 * Respond to 'mark all as read' button click
282 */
283 mw.echo.ui.NotificationBadgeWidget.prototype.onMarkAllReadButtonClick = function () {
284 this.controller.markLocalNotificationsRead();
285 };
286
287 /**
288 * Extend the response to button click so we can also update the notification list.
289 *
290 * @param {boolean} isVisible The popup is visible
291 * @fires mw.echo.ui.NotificationBadgeWidget#finishLoading
292 */
293 mw.echo.ui.NotificationBadgeWidget.prototype.onPopupToggle = function ( isVisible ) {
294 if ( this.promiseRunning ) {
295 return;
296 }
297
298 if ( !isVisible ) {
299 this.notificationsWidget.resetInitiallyUnseenItems();
300 return;
301 }
302
303 if ( this.hasRunFirstTime ) {
304 // HACK: Clippable doesn't resize the clippable area when
305 // it calculates the new size. Since the popup contents changed
306 // and the popup is "empty" now, we need to manually set its
307 // size to 1px so the clip calculations will resize it properly.
308 // See bug report: https://phabricator.wikimedia.org/T110759
309 this.popup.$clippable.css( 'height', '1px' );
310 this.popup.clip();
311 }
312
313 this.pushPending();
314 this.markAllReadButton.toggle( false );
315 this.promiseRunning = true;
316
317 // Always populate on popup open. The model and widget should handle
318 // the case where the promise is already underway.
319 this.controller.fetchLocalNotifications( this.hasRunFirstTime )
320 .then(
321 // Success
322 () => {
323 if ( this.popup.isVisible() ) {
324 // Fire initialization hook
325 mw.hook( 'ext.echo.popup.onInitialize' ).fire( this.manager.getTypeString(), this.controller );
326
327 // Update seen time
328 return this.controller.updateSeenTime();
329 }
330 },
331 // Failure
332 ( errorObj ) => {
333 if ( errorObj.errCode === 'notlogin-required' ) {
334 // Login required message
335 this.notificationsWidget.resetLoadingOption( mw.msg( 'echo-notification-loginrequired' ) );
336 } else {
337 // Generic API failure message
338 this.notificationsWidget.resetLoadingOption( mw.msg( 'echo-api-failure' ) );
339 }
340 }
341 )
342 .then( this.emit.bind( this, 'finishLoading' ) )
343 .always( () => {
344 this.popup.clip();
345 // Pop pending
346 this.popPending();
347 this.promiseRunning = false;
348 } );
349 this.hasRunFirstTime = true;
350 };
351 }() );