Debian Codegraph @

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

raw symbols SCIP
1 ( function () {
2 /* global moment:false */
3 /**
4 * A base widget for displaying notification items.
5 *
6 * @class
7 * @abstract
8 * @extends OO.ui.Widget
9 *
10 * @constructor
11 * @param {mw.echo.Controller} controller Echo controller
12 * @param {mw.echo.dm.NotificationItem} model Notification item model
13 * @param {Object} [config] Configuration options
14 * @param {jQuery} [config.$overlay] A jQuery element functioning as an overlay
15 * for popups.
16 * @param {boolean} [config.bundle=false] This notification item is part of a bundle.
17 */
18 mw.echo.ui.NotificationItemWidget = function MwEchoUiNotificationItemWidget( controller, model, config ) {
19 config = config || {};
20
21 // Parent constructor
22 mw.echo.ui.NotificationItemWidget.super.call( this, Object.assign( { data: model.getId() }, config ) );
23
24 this.controller = controller;
25 this.model = model;
26
27 this.$overlay = config.$overlay || this.$element;
28 this.bundle = !!config.bundle;
29
30 this.$content = $( '<div>' ).addClass( 'mw-echo-ui-notificationItemWidget-content' );
31 this.$actions = $( '<div>' )
32 .addClass( 'mw-echo-ui-notificationItemWidget-content-actions' );
33
34 // Mark as read
35 this.markAsReadButton = new mw.echo.ui.ToggleReadCircleButtonWidget( {
36 framed: false,
37 classes: [ 'mw-echo-ui-notificationItemWidget-markAsReadButton' ],
38 markAsRead: !this.model.isRead()
39 } );
40
41 let $icon;
42 const iconUrl = this.model.getIconUrl();
43 // Icon
44 if ( iconUrl ) {
45 // Only invert non colored images
46 /* eslint-disable es-x/no-array-prototype-includes */
47 const invertIconClass =
48 iconUrl.includes( 'progressive' ) ||
49 iconUrl.includes( 'constructive' ) ||
50 iconUrl.includes( 'failure' ) ? '' : 'skin-invert';
51 /* eslint-disable-next-line mediawiki/class-doc */
52 $icon = $( '<div>' )
53 .addClass( `mw-echo-ui-notificationItemWidget-icon ${ invertIconClass }` )
54 .append( $( '<img>' ).attr( {
55 src: this.model.getIconUrl(),
56 role: 'presentation',
57 alt: ' '
58 } ) );
59 }
60
61 const $message = $( '<div>' ).addClass( 'mw-echo-ui-notificationItemWidget-content-message' );
62 // Content
63 $message.append(
64 $( '<div>' )
65 .addClass( 'mw-echo-ui-notificationItemWidget-content-message-header-wrapper' )
66 .append(
67 $( '<div>' )
68 .addClass( 'mw-echo-ui-notificationItemWidget-content-message-header' )
69 .append( this.model.getContentHeader() )
70 )
71 );
72 if ( !this.bundle && this.model.getContentBody() ) {
73 $message.append(
74 $( '<div>' )
75 .addClass( 'mw-echo-ui-notificationItemWidget-content-message-body' )
76 .append( this.model.getContentBody() )
77 // dir=auto has a similar effect to wrapping the content in <bdi>, but
78 // makes text-overflow: ellipsis; behave less strangely
79 .attr( 'dir', 'auto' )
80 );
81 }
82
83 // Actions menu
84 this.actionsButtonSelectWidget = new OO.ui.ButtonSelectWidget( {
85 classes: [ 'mw-echo-ui-notificationItemWidget-content-actions-buttons' ],
86 tabIndex: -1
87 } );
88
89 // Popup menu
90 this.menuPopupButtonWidget = new mw.echo.ui.ActionMenuPopupWidget( {
91 framed: false,
92 icon: 'ellipsis',
93 $overlay: this.$overlay,
94 horizontalPosition: this.bundle ? 'end' : 'auto',
95 title: mw.msg( 'echo-notification-more-options-tooltip' ),
96 classes: [ 'mw-echo-ui-notificationItemWidget-content-actions-menu' ]
97 } );
98
99 // Timestamp
100 // We want to use extra-short timestamp strings; we change the locale
101 // to our echo-defined one and use that instead of the normal moment locale
102 const echoMoment = moment.utc( this.model.getTimestamp() );
103 echoMoment.locale( 'echo-shortRelativeTime' );
104 echoMoment.local();
105
106 this.timestampWidget = new OO.ui.LabelWidget( {
107 classes: [ 'mw-echo-ui-notificationItemWidget-content-actions-timestamp' ],
108 // Get the time 'fromNow' without the suffix 'ago'
109 label: echoMoment.fromNow( true )
110 } );
111
112 // Build the actions line
113 if ( this.bundle ) {
114 // In a bundled item, the timestamp should go before the menu
115 this.$actions.append(
116 $( '<div>' )
117 // We are wrapping the actions in a 'row' div so that the
118 // internal pieces are also a table layout
119 .addClass( 'mw-echo-ui-notificationItemWidget-content-actions-row' )
120 .append(
121 this.actionsButtonSelectWidget.$element,
122 this.timestampWidget.$element,
123 this.menuPopupButtonWidget.$element
124 )
125 );
126 } else {
127 this.$actions.append(
128 this.actionsButtonSelectWidget.$element,
129 this.menuPopupButtonWidget.$element,
130 this.timestampWidget.$element
131 );
132 }
133
134 // Actions
135 let outsideMenuItemCounter = 0;
136 const secondaryUrls = this.model.getSecondaryUrls();
137 for ( let i = 0; i < secondaryUrls.length; i++ ) {
138 const urlObj = secondaryUrls[ i ];
139
140 // Items are placed outside the dotdotdot menu if they are
141 // prioritized explicitly, *except* for items inside a bundle
142 // (where all actions are inside the menu) or there are more than
143 // two prioritized actions (all others go into the menu)
144 const isOutsideMenu = !this.bundle &&
145 (
146 (
147 // Make sure we don't have too many prioritized items
148 urlObj.prioritized &&
149 outsideMenuItemCounter < mw.echo.config.maxPrioritizedActions
150 ) ||
151 // If the number of total items are equal to or less than the
152 // maximum allowed, they all go outside the menu
153 // mw.echo.config.maxPrioritizedActions is 2 on desktop and 1 on mobile.
154 secondaryUrls.length <= mw.echo.config.maxPrioritizedActions
155 );
156
157 const linkButton = new mw.echo.ui.MenuItemWidget( {
158 type: urlObj.type,
159 actionData: urlObj.data,
160 icon: urlObj.icon || 'next',
161 label: urlObj.label,
162 tooltip: urlObj.tooltip,
163 description: urlObj.description,
164 url: urlObj.url,
165 prioritized: isOutsideMenu
166 } );
167
168 // Limit to 2 items outside the menu
169 if ( isOutsideMenu ) {
170 this.actionsButtonSelectWidget.addItems( [ linkButton ] );
171 this.actionsButtonSelectWidget.setTabIndex( 0 );
172 outsideMenuItemCounter++;
173 } else {
174 this.menuPopupButtonWidget.getMenu().addItems( [ linkButton ] );
175 }
176 }
177
178 if ( this.bundle ) {
179 // In a bundle, we have table layout, so the icon is
180 // inserted into the content, and the 'mark as read'
181 // button is not floating, and should be at the end
182 this.$content.append(
183 $icon,
184 $message,
185 this.$actions,
186 this.markAsReadButton.$element
187 );
188 this.$element.append( this.$content );
189 } else {
190 this.$content.append(
191 this.markAsReadButton.$element,
192 $message,
193 this.$actions
194 );
195 this.$element.append( $icon, this.$content );
196 }
197
198 // Events
199 this.actionsButtonSelectWidget.connect( this, { choose: 'onPopupButtonWidgetChoose' } );
200 this.menuPopupButtonWidget.getMenu().connect( this, { choose: 'onPopupButtonWidgetChoose' } );
201 this.markAsReadButton.connect( this, { click: 'onMarkAsReadButtonClick' } );
202
203 this.$element
204 .addClass( 'mw-echo-ui-notificationItemWidget' )
205 .toggleClass( 'mw-echo-ui-notificationItemWidget-initiallyUnseen', !this.model.isSeen() && !this.bundle )
206 .toggleClass( 'mw-echo-ui-notificationItemWidget-bundled', this.bundle );
207
208 if ( this.model.getPrimaryUrl() ) {
209 this.$element
210 .attr( 'href', this.model.getPrimaryUrl() )
211 .on( 'click', this.onPrimaryLinkClick.bind( this ) )
212 .on( 'auxclick', this.onPrimaryLinkAuxclick.bind( this ) );
213 }
214 };
215
216 OO.inheritClass( mw.echo.ui.NotificationItemWidget, OO.ui.Widget );
217
218 // Make the whole item a link to get native link behaviour
219 mw.echo.ui.NotificationItemWidget.static.tagName = 'a';
220
221 /**
222 * Respond to primary link click.
223 * Override this in the descendents.
224 *
225 * @return {boolean} true
226 */
227 mw.echo.ui.NotificationItemWidget.prototype.onPrimaryLinkClick = function () {
228 return true;
229 };
230
231 /**
232 * Respond to primary link middle-click by immediately marking as read,
233 * then let the browser open the link in a new tab as normal.
234 * Opening the link would also mark as read, but this way the user gets immediate feedback.
235 *
236 * @param {Event} event
237 *
238 * @return {boolean} true
239 */
240 mw.echo.ui.NotificationItemWidget.prototype.onPrimaryLinkAuxclick = function ( event ) {
241 if ( event.button === 1 ) {
242 this.$element.removeClass( 'mw-echo-ui-notificationItemWidget-initiallyUnseen' );
243 this.markRead( true );
244 }
245 // Don't prevent default action, let the browser take care of opening a new tab.
246 return true;
247 };
248
249 /**
250 * Manage a click on a dynamic secondary link.
251 * We can't know what the link intends us to do in the API, so we trust the 'apiParams'
252 * to tell the controller. When the link is clicked, we will pass the information on
253 * to the controller, which will manage whatever promise and action is needed.
254 *
255 * NOTE: The messages are parsed as HTML. If user-input is expected
256 * please make sure to properly escape it.
257 *
258 * @param {OO.ui.ButtonOptionWidget} item The selected item
259 */
260 mw.echo.ui.NotificationItemWidget.prototype.onPopupButtonWidgetChoose = function ( item ) {
261 if ( !( item instanceof mw.echo.ui.MenuItemWidget ) ) {
262 // Other kinds of items may be added by subclasses
263 return;
264 }
265
266 const actionData = item && item.getActionData(),
267 messages = item && item.getConfirmationMessages();
268
269 // Send to controller
270 item.pushPending();
271 this.controller.performDynamicAction( actionData, this.getModel().getSource() )
272 .then( () => {
273 const $title = $( '<p>' )
274 .addClass( 'mw-echo-ui-notificationItemWidget-notify-title' )
275 .append( $.parseHTML( messages.title ) ),
276 $description = $( '<p>' )
277 .addClass( 'mw-echo-ui-notificationItemWidget-notify-description' )
278 .append( $.parseHTML( messages.description ) );
279
280 // Get rid of the button
281 if ( item.isPrioritized() ) {
282 this.actionsButtonSelectWidget.removeItems( [ item ] );
283 } else {
284 // It's inside the popup menu
285 this.menuPopupButtonWidget.getMenu().removeItems( [ item ] );
286 }
287
288 // Make sure to hide either piece if it is empty
289 $title.toggle( !!$title.text() );
290 $description.toggle( !!$description.text() );
291
292 // Display confirmation
293 const $confirmation = $( '<div>' )
294 .append( $title, $description );
295
296 // Send to mw.notify
297 mw.notify( $confirmation );
298 } );
299 };
300
301 /**
302 * Respond to mark as read button click
303 */
304 mw.echo.ui.NotificationItemWidget.prototype.onMarkAsReadButtonClick = function () {
305 // If we're marking read or unread, the notification was already seen.
306 // Remove the animation class
307 this.$element.removeClass( 'mw-echo-ui-notificationItemWidget-initiallyUnseen' );
308 this.markRead( !this.model.isRead() );
309 };
310
311 /**
312 * Mark this notification as read
313 *
314 * @method
315 * @abstract
316 * @param {boolean} [isRead=true] Notification is marked as read
317 */
318 mw.echo.ui.NotificationItemWidget.prototype.markRead = null;
319
320 /**
321 * Get the notification link
322 *
323 * @return {string} Notification link
324 */
325 mw.echo.ui.NotificationItemWidget.prototype.getPrimaryUrl = function () {
326 return this.model.getPrimaryUrl();
327 };
328
329 /**
330 * Get the item id
331 *
332 * @return {number} Notification id
333 */
334 mw.echo.ui.NotificationItemWidget.prototype.getTimestamp = function () {
335 return this.model.getTimestamp();
336 };
337
338 /**
339 * Get the notification Id
340 *
341 * @return {number} Notification id
342 */
343 mw.echo.ui.NotificationItemWidget.prototype.getId = function () {
344 return this.model.getId();
345 };
346
347 /**
348 * Check whether this item is seen.
349 *
350 * @return {boolean} Item is seen
351 */
352 mw.echo.ui.NotificationItemWidget.prototype.isSeen = function () {
353 return this.model.isSeen();
354 };
355
356 /**
357 * Check whether this item is read.
358 *
359 * @return {boolean} Item is read
360 */
361 mw.echo.ui.NotificationItemWidget.prototype.isRead = function () {
362 return this.model.isRead();
363 };
364
365 /**
366 * Check whether this item is foreign.
367 *
368 * @return {boolean} Item is foreign
369 */
370 mw.echo.ui.NotificationItemWidget.prototype.isForeign = function () {
371 return this.model.isForeign();
372 };
373
374 /**
375 * Toggle the function of the 'mark as read' buttons from 'mark as read' to 'mark as unread'
376 * and vice versa.
377 *
378 * @param {boolean} [showMarkAsRead] Show the 'mark as read' buttons
379 * - "false" means that the item is marked as read, whereby we show the user 'mark unread'
380 * buttons.
381 * - "true" means that the item is marked as unread and we show the user 'mark as read'
382 * buttons
383 */
384 mw.echo.ui.NotificationItemWidget.prototype.toggleMarkAsReadButtons = function ( showMarkAsRead ) {
385 showMarkAsRead = showMarkAsRead !== undefined ? showMarkAsRead : !this.model.isRead();
386
387 this.markAsReadButton.toggleState( showMarkAsRead );
388 this.menuPopupButtonWidget.toggle( !this.menuPopupButtonWidget.getMenu().isEmpty() );
389 };
390
391 /**
392 * Toggle the read state of the widget
393 *
394 * @param {boolean} [read] The current read state. If not given, the state will
395 * become the opposite of its current state.
396 */
397 mw.echo.ui.NotificationItemWidget.prototype.toggleRead = function ( read ) {
398 this.read = read !== undefined ? read : !this.read;
399
400 this.$element.toggleClass( 'mw-echo-ui-notificationItemWidget-unread', !this.read );
401 this.toggleMarkAsReadButtons( !this.read );
402 };
403
404 /**
405 * Toggle the seen state of the widget
406 *
407 * @param {boolean} [seen] The current seen state. If not given, the state will
408 * become the opposite of its current state.
409 */
410 mw.echo.ui.NotificationItemWidget.prototype.toggleSeen = function ( seen ) {
411 this.seen = seen !== undefined ? seen : !this.seen;
412
413 this.$element
414 .toggleClass( 'mw-echo-ui-notificationItemWidget-unseen', !this.seen );
415 };
416
417 /**
418 * Get the model associated with this widget.
419 *
420 * @return {mw.echo.dm.NotificationItem} Item model
421 */
422 mw.echo.ui.NotificationItemWidget.prototype.getModel = function () {
423 return this.model;
424 };
425
426 /**
427 * Disconnect events when widget is destroyed.
428 */
429 mw.echo.ui.NotificationItemWidget.prototype.destroy = function () {
430 this.model.disconnect( this );
431 };
432
433 /**
434 * Remove the 'initiallyUnseen' class, which was only used for the
435 * unseen animation when the user has first seen it.
436 */
437 mw.echo.ui.NotificationItemWidget.prototype.resetInitiallyUnseen = function () {
438 this.$element.removeClass( 'mw-echo-ui-notificationItemWidget-initiallyUnseen' );
439 };
440
441 /**
442 * Declares whether this widget is a cloned fake.
443 *
444 * @return {boolean} false
445 */
446 mw.echo.ui.NotificationItemWidget.prototype.isFake = function () {
447 return false;
448 };
449 }() );