mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Echo
synced 2024-11-25 00:05:29 +00:00
623d07011c
The flyout loads no more than 25 notifications from a given source. Using those in-memory notification objects to count how many are currently unread (and update the badge) produces a result of at most 25. This patch extracts the responsibility or counting the unread from the Model/Item/Groupitem structure into a new UnreadNotificationCounter class. It receives estimated updates from other components and synchronizes with the server after markRead/markUnread operations have completed. Bug: T129726 Change-Id: I9af4defc00dd491ed2b355eb4e85073476e08ce7
153 lines
5.1 KiB
JavaScript
153 lines
5.1 KiB
JavaScript
( function ( mw, $ ) {
|
|
/*global moment:false */
|
|
'use strict';
|
|
|
|
mw.echo = mw.echo || {};
|
|
|
|
// Activate ooui
|
|
$( document ).ready( function () {
|
|
var myWidget, echoApi,
|
|
$existingAlertLink = $( '#pt-notifications-alert a' ),
|
|
$existingMessageLink = $( '#pt-notifications-message a' ),
|
|
numAlerts = $existingAlertLink.text(),
|
|
numMessages = $existingMessageLink.text(),
|
|
hasUnseenAlerts = $existingAlertLink.hasClass( 'mw-echo-unseen-notifications' ),
|
|
hasUnseenMessages = $existingMessageLink.hasClass( 'mw-echo-unseen-notifications' ),
|
|
// Store links
|
|
links = {
|
|
notifications: $( '#pt-notifications-alert a' ).attr( 'href' ),
|
|
preferences: $( '#pt-preferences a' ).attr( 'href' ) + '#mw-prefsection-echo'
|
|
};
|
|
|
|
// Respond to click on the notification button and load the UI on demand
|
|
$( '.mw-echo-notification-badge-nojs' ).click( function ( e ) {
|
|
var myType = $( this ).parent().prop( 'id' ) === 'pt-notifications-alert' ? 'alert' : 'message',
|
|
time = mw.now();
|
|
|
|
if ( e.which !== 1 ) {
|
|
return;
|
|
}
|
|
|
|
// Dim the button while we load
|
|
$( this ).addClass( 'mw-echo-notifications-badge-dimmed' );
|
|
|
|
// Fire the notification API requests
|
|
echoApi = new mw.echo.api.EchoApi();
|
|
echoApi.fetchNotifications( myType )
|
|
.then( function ( data ) {
|
|
mw.track( 'timing.MediaWiki.echo.overlay.api', mw.now() - time );
|
|
return data;
|
|
} );
|
|
|
|
// Load the ui
|
|
mw.loader.using( 'ext.echo.ui.desktop', function () {
|
|
var messageNotificationsModel,
|
|
alertNotificationsModel,
|
|
unreadMessageCounter,
|
|
unreadAlertCounter,
|
|
momentOrigLocale = moment.locale(),
|
|
maxNotificationCount = mw.config.get( 'wgEchoMaxNotificationCount' );
|
|
|
|
// Set up new 'short relative time' locale strings for momentjs
|
|
moment.defineLocale( 'echo-shortRelativeTime', {
|
|
relativeTime: function ( number, withoutSuffix, key ) {
|
|
var keymap = {
|
|
s: 'seconds',
|
|
m: 'minutes',
|
|
mm: 'minutes',
|
|
h: 'hours',
|
|
hh: 'hours',
|
|
d: 'days',
|
|
dd: 'days',
|
|
M: 'months',
|
|
MM: 'months',
|
|
y: 'years',
|
|
yy: 'years'
|
|
};
|
|
return mw.msg( 'notification-timestamp-ago-' + keymap[ key ], number );
|
|
} } );
|
|
// Reset back to original locale
|
|
moment.locale( momentOrigLocale );
|
|
|
|
// Overlay
|
|
$( 'body' ).append( mw.echo.ui.$overlay );
|
|
|
|
// Load message button and popup if messages exist
|
|
if ( $existingMessageLink.length ) {
|
|
unreadMessageCounter = new mw.echo.dm.UnreadNotificationCounter( echoApi, 'message', maxNotificationCount );
|
|
messageNotificationsModel = new mw.echo.dm.NotificationsModel(
|
|
echoApi,
|
|
unreadMessageCounter,
|
|
{
|
|
type: 'message'
|
|
}
|
|
);
|
|
mw.echo.ui.messageWidget = new mw.echo.ui.NotificationBadgeWidget( messageNotificationsModel, unreadMessageCounter, {
|
|
markReadWhenSeen: false,
|
|
$overlay: mw.echo.ui.$overlay,
|
|
numItems: numMessages,
|
|
hasUnseen: hasUnseenMessages,
|
|
badgeIcon: 'speechBubbles',
|
|
links: links,
|
|
href: $existingMessageLink.attr( 'href' )
|
|
} );
|
|
// HACK: avoid late debouncedUpdateThemeClasses
|
|
mw.echo.ui.messageWidget.badgeButton.debouncedUpdateThemeClasses();
|
|
// Replace the link button with the ooui button
|
|
$existingMessageLink.parent().replaceWith( mw.echo.ui.messageWidget.$element );
|
|
|
|
mw.echo.ui.messageWidget.getModel().on( 'allTalkRead', function () {
|
|
// If there was a talk page notification, get rid of it
|
|
$( '#pt-mytalk a' )
|
|
.removeClass( 'mw-echo-alert' )
|
|
.text( mw.msg( 'mytalk' ) );
|
|
} );
|
|
}
|
|
// Load alerts popup and button
|
|
unreadAlertCounter = new mw.echo.dm.UnreadNotificationCounter( echoApi, 'alert', maxNotificationCount );
|
|
alertNotificationsModel = new mw.echo.dm.NotificationsModel(
|
|
echoApi,
|
|
unreadAlertCounter,
|
|
{
|
|
type: 'alert'
|
|
}
|
|
);
|
|
mw.echo.ui.alertWidget = new mw.echo.ui.NotificationBadgeWidget( alertNotificationsModel, unreadAlertCounter, {
|
|
markReadWhenSeen: true,
|
|
numItems: numAlerts,
|
|
hasUnseen: hasUnseenAlerts,
|
|
badgeIcon: {
|
|
seen: 'bell',
|
|
unseen: 'bellOn'
|
|
},
|
|
links: links,
|
|
$overlay: mw.echo.ui.$overlay,
|
|
href: $existingAlertLink.attr( 'href' )
|
|
} );
|
|
// HACK: avoid late debouncedUpdateThemeClasses
|
|
mw.echo.ui.alertWidget.badgeButton.debouncedUpdateThemeClasses();
|
|
// Replace the link button with the ooui button
|
|
$existingAlertLink.parent().replaceWith( mw.echo.ui.alertWidget.$element );
|
|
|
|
// HACK: Now that the module loaded, show the popup
|
|
myWidget = myType === 'alert' ? mw.echo.ui.alertWidget : mw.echo.ui.messageWidget;
|
|
myWidget.once( 'finishLoading', function () {
|
|
// Log timing after notifications are shown
|
|
mw.track( 'timing.MediaWiki.echo.overlay', mw.now() - time );
|
|
} );
|
|
myWidget.popup.toggle( true );
|
|
mw.track( 'timing.MediaWiki.echo.overlay.ooui', mw.now() - time );
|
|
} );
|
|
|
|
if ( hasUnseenAlerts || hasUnseenMessages ) {
|
|
// Clicked on the flyout due to having unread notifications
|
|
mw.track( 'counter.MediaWiki.echo.unseen.click' );
|
|
}
|
|
|
|
// Prevent default
|
|
return false;
|
|
} );
|
|
} );
|
|
|
|
} )( mediaWiki, jQuery );
|