2015-08-13 00:54:16 +00:00
|
|
|
( function ( mw, $ ) {
|
|
|
|
'use strict';
|
|
|
|
|
2015-09-16 19:29:01 +00:00
|
|
|
mw.echo = mw.echo || {};
|
2015-08-13 00:54:16 +00:00
|
|
|
|
2015-09-15 06:13:51 +00:00
|
|
|
mw.echo.apiCallParams = {
|
|
|
|
action: 'query',
|
|
|
|
meta: 'notifications',
|
|
|
|
// We have to send the API 'groupbysection' otherwise
|
|
|
|
// the 'messageunreadfirst' doesn't do anything.
|
|
|
|
// TODO: Fix the API.
|
|
|
|
notgroupbysection: 1,
|
|
|
|
notmessageunreadfirst: 1,
|
2015-10-16 23:18:25 +00:00
|
|
|
notformat: 'model',
|
2015-09-15 06:13:51 +00:00
|
|
|
notlimit: 25,
|
|
|
|
notprop: 'index|list|count',
|
|
|
|
uselang: mw.config.get( 'wgUserLanguage' )
|
|
|
|
};
|
|
|
|
|
2015-08-13 00:54:16 +00:00
|
|
|
// Activate ooui
|
|
|
|
$( document ).ready( function () {
|
2015-09-15 06:13:51 +00:00
|
|
|
var apiRequest, myWidget,
|
|
|
|
$existingAlertLink = $( '#pt-notifications-alert a' ),
|
2015-09-07 23:54:47 +00:00
|
|
|
$existingMessageLink = $( '#pt-notifications-message a' ),
|
2015-08-13 00:54:16 +00:00
|
|
|
numAlerts = $existingAlertLink.text(),
|
|
|
|
numMessages = $existingMessageLink.text(),
|
2015-09-03 21:24:03 +00:00
|
|
|
hasUnseenAlerts = $existingAlertLink.hasClass( 'mw-echo-unseen-notifications' ),
|
|
|
|
hasUnseenMessages = $existingMessageLink.hasClass( 'mw-echo-unseen-notifications' ),
|
2015-08-13 00:54:16 +00:00
|
|
|
// Store links
|
|
|
|
links = {
|
2015-09-08 15:27:40 +00:00
|
|
|
notifications: $( '#pt-notifications-alert a' ).attr( 'href' ),
|
2015-09-11 15:15:26 +00:00
|
|
|
preferences: $( '#pt-preferences a' ).attr( 'href' ) + '#mw-prefsection-echo'
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
|
|
|
|
2015-09-15 06:13:51 +00:00
|
|
|
// Respond to click on the notification button and load the UI on demand
|
2015-09-24 01:13:37 +00:00
|
|
|
$( '.mw-echo-notification-badge-nojs' ).click( function ( e ) {
|
2015-09-22 18:55:01 +00:00
|
|
|
var myType = $( this ).parent().prop( 'id' ) === 'pt-notifications-alert' ? 'alert' : 'message',
|
|
|
|
time = mw.now();
|
2015-09-24 01:13:37 +00:00
|
|
|
|
|
|
|
if ( e.which !== 1 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-15 06:13:51 +00:00
|
|
|
// Dim the button while we load
|
|
|
|
$( this ).addClass( 'mw-echo-notifications-badge-dimmed' );
|
|
|
|
|
|
|
|
// Fire the notification API requests
|
2015-09-24 21:33:21 +00:00
|
|
|
apiRequest = new mw.Api( { ajax: { cache: false } } ).get( $.extend( { notsections: myType }, mw.echo.apiCallParams ) )
|
2015-11-11 23:22:36 +00:00
|
|
|
.then( function ( data ) {
|
|
|
|
mw.track( 'timing.MediaWiki.echo.overlay.api', mw.now() - time );
|
|
|
|
return data;
|
|
|
|
} );
|
2015-09-15 06:13:51 +00:00
|
|
|
|
|
|
|
// Load the ui
|
|
|
|
mw.loader.using( 'ext.echo.ui', function () {
|
2015-10-26 22:23:22 +00:00
|
|
|
var messageNotificationsModel, alertNotificationsModel;
|
|
|
|
|
2015-11-21 01:54:12 +00:00
|
|
|
// Overlay
|
|
|
|
$( 'body' ).append( mw.echo.ui.$overlay );
|
|
|
|
|
2015-09-15 06:13:51 +00:00
|
|
|
// Load message button and popup if messages exist
|
|
|
|
if ( $existingMessageLink.length ) {
|
2015-10-26 22:23:22 +00:00
|
|
|
messageNotificationsModel = new mw.echo.dm.NotificationsModel(
|
2015-11-11 23:22:36 +00:00
|
|
|
// Create a network handler
|
|
|
|
new mw.echo.dm.NetworkHandler( {
|
2015-10-26 22:23:22 +00:00
|
|
|
type: 'message',
|
|
|
|
baseParams: mw.echo.apiCallParams
|
|
|
|
} ),
|
|
|
|
{
|
|
|
|
type: 'message'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
mw.echo.ui.messageWidget = new mw.echo.ui.NotificationBadgeWidget( messageNotificationsModel, {
|
2015-09-15 06:13:51 +00:00
|
|
|
markReadWhenSeen: false,
|
2015-11-21 01:54:12 +00:00
|
|
|
$overlay: mw.echo.ui.$overlay,
|
2015-09-15 06:13:51 +00:00
|
|
|
numItems: numMessages,
|
|
|
|
hasUnseen: hasUnseenMessages,
|
2015-12-16 23:14:06 +00:00
|
|
|
badgeIcon: 'speechBubbles',
|
2015-09-24 01:13:37 +00:00
|
|
|
links: links,
|
|
|
|
href: $existingMessageLink.attr( 'href' )
|
2015-09-15 06:13:51 +00:00
|
|
|
} );
|
2015-09-17 00:05:52 +00:00
|
|
|
// HACK: avoid late debouncedUpdateThemeClasses
|
|
|
|
mw.echo.ui.messageWidget.badgeButton.debouncedUpdateThemeClasses();
|
2015-09-15 06:13:51 +00:00
|
|
|
// Replace the link button with the ooui button
|
2015-09-17 00:05:52 +00:00
|
|
|
$existingMessageLink.parent().replaceWith( mw.echo.ui.messageWidget.$element );
|
2015-09-17 20:47:23 +00:00
|
|
|
|
2015-10-01 17:56:40 +00:00
|
|
|
mw.echo.ui.messageWidget.getModel().on( 'allTalkRead', function () {
|
2015-09-17 20:47:23 +00:00
|
|
|
// If there was a talk page notification, get rid of it
|
2015-09-22 22:36:14 +00:00
|
|
|
$( '#pt-mytalk a' )
|
|
|
|
.removeClass( 'mw-echo-alert' )
|
|
|
|
.text( mw.msg( 'mytalk' ) );
|
2015-09-17 20:47:23 +00:00
|
|
|
} );
|
2015-09-15 06:13:51 +00:00
|
|
|
}
|
|
|
|
// Load alerts popup and button
|
2015-10-26 22:23:22 +00:00
|
|
|
alertNotificationsModel = new mw.echo.dm.NotificationsModel(
|
2015-11-11 23:22:36 +00:00
|
|
|
// Create a network handler
|
|
|
|
new mw.echo.dm.NetworkHandler( {
|
2015-10-26 22:23:22 +00:00
|
|
|
type: 'alert',
|
|
|
|
baseParams: mw.echo.apiCallParams
|
|
|
|
} ),
|
|
|
|
{
|
|
|
|
type: 'alert'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
mw.echo.ui.alertWidget = new mw.echo.ui.NotificationBadgeWidget( alertNotificationsModel, {
|
2015-09-15 06:13:51 +00:00
|
|
|
markReadWhenSeen: true,
|
|
|
|
numItems: numAlerts,
|
|
|
|
hasUnseen: hasUnseenAlerts,
|
|
|
|
badgeIcon: {
|
|
|
|
seen: 'bell',
|
|
|
|
unseen: 'bellOn'
|
|
|
|
},
|
2015-09-24 01:13:37 +00:00
|
|
|
links: links,
|
2015-10-16 23:18:25 +00:00
|
|
|
$overlay: mw.echo.ui.$overlay,
|
2015-09-24 01:13:37 +00:00
|
|
|
href: $existingAlertLink.attr( 'href' )
|
2015-09-15 06:13:51 +00:00
|
|
|
} );
|
2015-09-17 00:05:52 +00:00
|
|
|
// HACK: avoid late debouncedUpdateThemeClasses
|
|
|
|
mw.echo.ui.alertWidget.badgeButton.debouncedUpdateThemeClasses();
|
2015-09-15 06:13:51 +00:00
|
|
|
// Replace the link button with the ooui button
|
2015-09-17 00:05:52 +00:00
|
|
|
$existingAlertLink.parent().replaceWith( mw.echo.ui.alertWidget.$element );
|
2015-09-15 06:13:51 +00:00
|
|
|
|
|
|
|
// HACK: Now that the module loaded, show the popup
|
|
|
|
myWidget = myType === 'alert' ? mw.echo.ui.alertWidget : mw.echo.ui.messageWidget;
|
2015-09-22 18:55:01 +00:00
|
|
|
myWidget.populateNotifications( apiRequest ).then( function () {
|
|
|
|
// Log timing after notifications are shown
|
2015-09-24 21:33:21 +00:00
|
|
|
// FIXME: The notifications might not be shown yet if the API
|
|
|
|
// request finished before the UI loads, in which case it will
|
|
|
|
// only be shown after the toggle( true ) call below.
|
2015-09-22 18:55:01 +00:00
|
|
|
mw.track( 'timing.MediaWiki.echo.overlay', mw.now() - time );
|
|
|
|
} );
|
2015-09-15 06:13:51 +00:00
|
|
|
myWidget.popup.toggle( true );
|
2015-09-24 21:33:21 +00:00
|
|
|
mw.track( 'timing.MediaWiki.echo.overlay.ooui', mw.now() - time );
|
2015-08-13 00:54:16 +00:00
|
|
|
} );
|
2015-09-15 22:58:13 +00:00
|
|
|
|
|
|
|
if ( hasUnseenAlerts || hasUnseenMessages ) {
|
|
|
|
// Clicked on the flyout due to having unread notifications
|
|
|
|
mw.track( 'counter.MediaWiki.echo.unseen.click' );
|
|
|
|
}
|
|
|
|
|
2015-09-15 06:13:51 +00:00
|
|
|
// Prevent default
|
|
|
|
return false;
|
2015-08-13 00:54:16 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
} )( mediaWiki, jQuery );
|