summaryrefslogtreecommitdiff
blob: 5006714552d7789203f87b36ae5c9f40eac44eea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
( function () {
	/**
	 * Notification badge button widget for echo popup.
	 *
	 * @class
	 * @extends OO.ui.ButtonWidget
	 *
	 * @constructor
	 * @param {mw.echo.Controller} controller Echo notifications controller
	 * @param {mw.echo.dm.ModelManager} manager Model manager
	 * @param {Object} links Links object, containing 'notifications' and 'preferences' URLs
	 * @param {Object} config Configuration object
	 * @cfg {string|string[]} [type='message'] The type or array of types of
	 *  notifications that are in this model. They can be 'alert', 'message' or
	 *  an array of both. Defaults to 'message'
	 * @cfg {number} [numItems=0] The number of items that are in the button display
	 * @cfg {string} [convertedNumber] A converted version of the initial count
	 * @cfg {string} [badgeLabel=0] The initial label for the badge. This is the
	 *  formatted version of the number of items in the badge.
	 * @cfg {boolean} [hasUnseen=false] Whether there are unseen items
	 * @cfg {number} [popupWidth=450] The width of the popup
	 * @cfg {string} [badgeIcon] Icon to use for the popup header
	 * @cfg {string} [href] URL the badge links to
	 * @cfg {jQuery} [$overlay] A jQuery element functioning as an overlay
	 *  for popups.
	 */
	mw.echo.ui.NotificationBadgeWidget = function MwEchoUiNotificationBadgeButtonPopupWidget( controller, manager, links, config ) {
		var buttonFlags, allNotificationsButton, preferencesButton, footerButtonGroupWidget, $footer,
			adjustedTypeString;

		config = config || {};

		// Parent constructor
		mw.echo.ui.NotificationBadgeWidget.super.call( this, config );

		// Mixin constructors
		OO.ui.mixin.PendingElement.call( this, config );

		this.$overlay = config.$overlay || this.$element;
		// Create a menu overlay
		this.$menuOverlay = $( '<div>' )
			.addClass( 'mw-echo-ui-NotificationBadgeWidget-overlay-menu' );
		this.$overlay.append( this.$menuOverlay );

		// Controller
		this.controller = controller;
		this.manager = manager;

		adjustedTypeString = this.controller.getTypeString() === 'message' ? 'notice' : this.controller.getTypeString();

		// Properties
		this.types = this.manager.getTypes();

		this.numItems = config.numItems || 0;
		this.hasRunFirstTime = false;

		buttonFlags = [];
		if ( config.hasUnseen ) {
			buttonFlags.push( 'unseen' );
		}

		this.badgeButton = new mw.echo.ui.BadgeLinkWidget( {
			convertedNumber: config.convertedNumber,
			type: this.manager.getTypeString(),
			numItems: this.numItems,
			flags: buttonFlags,
			// The following messages can be used here:
			// * tooltip-pt-notifications-alert
			// * tooltip-pt-notifications-notice
			title: mw.msg( 'tooltip-pt-notifications-' + adjustedTypeString ),
			href: config.href
		} );

		// Notifications list widget
		this.notificationsWidget = new mw.echo.ui.NotificationsListWidget(
			this.controller,
			this.manager,
			{
				type: this.types,
				$overlay: this.$menuOverlay,
				animated: true
			}
		);

		// Footer
		allNotificationsButton = new OO.ui.ButtonWidget( {
			icon: 'next',
			label: mw.msg( 'echo-overlay-link' ),
			href: links.notifications,
			classes: [ 'mw-echo-ui-notificationBadgeButtonPopupWidget-footer-allnotifs' ]
		} );
		allNotificationsButton.$element.children().first().removeAttr( 'role' );

		preferencesButton = new OO.ui.ButtonWidget( {
			icon: 'settings',
			label: mw.msg( 'mypreferences' ),
			href: links.preferences,
			classes: [ 'mw-echo-ui-notificationBadgeButtonPopupWidget-footer-preferences' ]
		} );
		preferencesButton.$element.children().first().removeAttr( 'role' );

		footerButtonGroupWidget = new OO.ui.ButtonGroupWidget( {
			items: [ allNotificationsButton, preferencesButton ],
			classes: [ 'mw-echo-ui-notificationBadgeButtonPopupWidget-footer-buttons' ]
		} );
		$footer = $( '<div>' )
			.addClass( 'mw-echo-ui-notificationBadgeButtonPopupWidget-footer' )
			.append( footerButtonGroupWidget.$element );

		this.popup = new OO.ui.PopupWidget( {
			$content: this.notificationsWidget.$element,
			$footer: $footer,
			width: config.popupWidth || 500,
			hideWhenOutOfView: false,
			autoFlip: false,
			autoClose: true,
			containerPadding: 20,
			$floatableContainer: this.$element,
			// Also ignore clicks from the nested action menu items, that
			// actually exist in the overlay
			$autoCloseIgnore: this.$element.add( this.$menuOverlay ),
			head: true,
			// The following messages can be used here:
			// * echo-notification-alert-text-only
			// * echo-notification-notice-text-only
			label: mw.msg(
				'echo-notification-' + adjustedTypeString +
				'-text-only'
			),
			classes: [ 'mw-echo-ui-notificationBadgeButtonPopupWidget-popup' ]
		} );
		// Append the popup to the overlay
		this.$overlay.append( this.popup.$element );

		// HACK: Add an icon to the popup head label
		this.popupHeadIcon = new OO.ui.IconWidget( { icon: config.badgeIcon } );
		this.popup.$head.prepend( this.popupHeadIcon.$element );

		this.setPendingElement( this.popup.$head );

		// Mark all as read button
		this.markAllReadButton = new OO.ui.ButtonWidget( {
			framed: false,
			label: mw.msg( 'echo-mark-all-as-read' ),
			classes: [ 'mw-echo-ui-notificationsWidget-markAllReadButton' ]
		} );

		// Hide the close button
		this.popup.closeButton.toggle( false );
		// Add the 'mark all as read' button to the header
		this.popup.$head.append( this.markAllReadButton.$element );
		this.markAllReadButton.toggle( false );

		// Events
		this.markAllReadButton.connect( this, { click: 'onMarkAllReadButtonClick' } );
		this.manager.connect( this, {
			update: 'updateBadge'
		} );
		this.manager.getSeenTimeModel().connect( this, { update: 'onSeenTimeModelUpdate' } );
		this.manager.getUnreadCounter().connect( this, { countChange: 'updateBadge' } );
		this.popup.connect( this, { toggle: 'onPopupToggle' } );
		this.badgeButton.connect( this, {
			click: 'onBadgeButtonClick'
		} );
		this.notificationsWidget.connect( this, { modified: 'onNotificationsListModified' } );

		this.$element
			.prop( 'id', 'pt-notifications-' + adjustedTypeString )
			// The following classes can be used here:
			// * mw-echo-ui-notificationBadgeButtonPopupWidget-alert
			// * mw-echo-ui-notificationBadgeButtonPopupWidget-message
			.addClass(
				'mw-echo-ui-notificationBadgeButtonPopupWidget ' +
				'mw-echo-ui-notificationBadgeButtonPopupWidget-' + adjustedTypeString
			)
			.append( this.badgeButton.$element );
	};

	/* Initialization */

	OO.inheritClass( mw.echo.ui.NotificationBadgeWidget, OO.ui.Widget );
	OO.mixinClass( mw.echo.ui.NotificationBadgeWidget, OO.ui.mixin.PendingElement );

	/* Static properties */

	mw.echo.ui.NotificationBadgeWidget.static.tagName = 'li';

	/* Events */

	/**
	 * @event allRead
	 * All notifications were marked as read
	 */

	/**
	 * @event finishLoading
	 * Notifications have successfully finished being processed and are fully loaded
	 */

	/* Methods */

	/**
	 * Respond to list widget modified event.
	 *
	 * This means the list's actual DOM was modified and we should make sure
	 * that the popup resizes itself.
	 */
	mw.echo.ui.NotificationBadgeWidget.prototype.onNotificationsListModified = function () {
		this.popup.clip();
	};

	/**
	 * Respond to badge button click
	 */
	mw.echo.ui.NotificationBadgeWidget.prototype.onBadgeButtonClick = function () {
		this.popup.toggle();
	};

	/**
	 * Respond to SeenTime model update event
	 */
	mw.echo.ui.NotificationBadgeWidget.prototype.onSeenTimeModelUpdate = function () {
		this.updateBadgeSeenState( false );
	};

	/**
	 * Update the badge style to match whether it contains unseen notifications.
	 *
	 * @param {boolean} [hasUnseen=false] There are unseen notifications
	 */
	mw.echo.ui.NotificationBadgeWidget.prototype.updateBadgeSeenState = function ( hasUnseen ) {
		hasUnseen = hasUnseen === undefined ? false : !!hasUnseen;

		this.badgeButton.setFlags( { unseen: !!hasUnseen } );
	};

	/**
	 * Update the badge state and label based on changes to the model
	 */
	mw.echo.ui.NotificationBadgeWidget.prototype.updateBadge = function () {
		var unreadCount, cappedUnreadCount, badgeLabel;

		unreadCount = this.manager.getUnreadCounter().getCount();
		cappedUnreadCount = this.manager.getUnreadCounter().getCappedNotificationCount( unreadCount );
		badgeLabel = mw.message( 'echo-badge-count', mw.language.convertNumber( cappedUnreadCount ) ).text();

		this.badgeButton.setLabel( badgeLabel );
		this.badgeButton.setCount( unreadCount, badgeLabel );
		// Update seen state only if the counter is 0
		// so we don't run into inconsistencies and have an unseen state
		// for the badge with 0 unread notifications
		if ( unreadCount === 0 ) {
			this.updateBadgeSeenState( false );
		}

		// Check if we need to display the 'mark all unread' button
		this.markAllReadButton.toggle( this.manager.hasLocalUnread() );
	};

	/**
	 * Respond to 'mark all as read' button click
	 */
	mw.echo.ui.NotificationBadgeWidget.prototype.onMarkAllReadButtonClick = function () {
		// Log the click action
		mw.echo.logger.logInteraction(
			mw.echo.Logger.static.actions.markAllReadClick,
			mw.echo.Logger.static.context.popup,
			null, // Event id isn't relevant
			this.manager.getTypeString() // The type of the list
		);

		this.controller.markLocalNotificationsRead();
	};

	/**
	 * Extend the response to button click so we can also update the notification list.
	 *
	 * @param {boolean} isVisible The popup is visible
	 * @fires finishLoading
	 */
	mw.echo.ui.NotificationBadgeWidget.prototype.onPopupToggle = function ( isVisible ) {
		var widget = this;

		if ( this.promiseRunning ) {
			return;
		}

		if ( !isVisible ) {
			widget.notificationsWidget.resetInitiallyUnseenItems();
			return;
		}

		// Log the click event
		mw.echo.logger.logInteraction(
			'ui-badge-link-click',
			mw.echo.Logger.static.context.badge,
			null,
			this.controller.getTypeString()
		);

		if ( this.hasRunFirstTime ) {
			// HACK: Clippable doesn't resize the clippable area when
			// it calculates the new size. Since the popup contents changed
			// and the popup is "empty" now, we need to manually set its
			// size to 1px so the clip calculations will resize it properly.
			// See bug report: https://phabricator.wikimedia.org/T110759
			this.popup.$clippable.css( 'height', '1px' );
			this.popup.clip();
		}

		this.pushPending();
		this.markAllReadButton.toggle( false );
		this.promiseRunning = true;

		// Always populate on popup open. The model and widget should handle
		// the case where the promise is already underway.
		this.controller.fetchLocalNotifications( this.hasRunFirstTime )
			.then(
				// Success
				function () {
					if ( widget.popup.isVisible() ) {
						// Fire initialization hook
						mw.hook( 'ext.echo.popup.onInitialize' ).fire( widget.manager.getTypeString(), widget.controller );

						// Update seen time
						return widget.controller.updateSeenTime();
					}
				},
				// Failure
				function ( errorObj ) {
					if ( errorObj.errCode === 'notlogin-required' ) {
						// Login required message
						widget.notificationsWidget.resetLoadingOption( mw.msg( 'echo-notification-loginrequired' ) );
					} else {
						// Generic API failure message
						widget.notificationsWidget.resetLoadingOption( mw.msg( 'echo-api-failure' ) );
					}
				}
			)
			.then( this.emit.bind( this, 'finishLoading' ) )
			.always( function () {
				widget.popup.clip();
				// Pop pending
				widget.popPending();
				widget.promiseRunning = false;
			} );
		this.hasRunFirstTime = true;
	};
}() );