2013-01-03 19:59:42 +00:00
|
|
|
/*global window:false */
|
2013-06-05 23:09:28 +00:00
|
|
|
( function ( $, mw ) {
|
2012-09-26 05:09:43 +00:00
|
|
|
'use strict';
|
|
|
|
|
2014-02-21 13:50:10 +00:00
|
|
|
// backwards compatibility <= MW 1.21
|
|
|
|
var getUrl = mw.util.getUrl || mw.util.wikiGetlink;
|
|
|
|
|
2012-08-01 19:53:05 +00:00
|
|
|
mw.echo.overlay = {
|
2012-12-10 05:01:00 +00:00
|
|
|
|
2013-09-25 00:18:02 +00:00
|
|
|
/**
|
|
|
|
* @param newCount formatted count
|
|
|
|
* @param rawCount unformatted count
|
|
|
|
*/
|
|
|
|
updateCount: function ( newCount, rawCount ) {
|
2013-05-15 23:10:49 +00:00
|
|
|
var $badge = $( '.mw-echo-notifications-badge' );
|
|
|
|
$badge.text( newCount );
|
2013-09-25 00:18:02 +00:00
|
|
|
|
|
|
|
if ( rawCount !== '0' && rawCount !== 0 ) {
|
2013-05-15 23:10:49 +00:00
|
|
|
$badge.addClass( 'mw-echo-unread-notifications' );
|
2012-12-12 02:18:51 +00:00
|
|
|
} else {
|
2013-05-15 23:10:49 +00:00
|
|
|
$badge.removeClass( 'mw-echo-unread-notifications' );
|
2012-12-12 02:18:51 +00:00
|
|
|
}
|
2012-08-01 19:53:05 +00:00
|
|
|
},
|
2012-06-01 10:57:09 +00:00
|
|
|
|
2013-06-12 00:44:01 +00:00
|
|
|
configuration: mw.config.get( 'wgEchoOverlayConfiguration' ),
|
2012-08-01 19:53:05 +00:00
|
|
|
|
2013-11-01 21:38:42 +00:00
|
|
|
removeOverlay: function () {
|
|
|
|
$( '.mw-echo-overlay, .mw-echo-overlay-pokey' ).fadeOut( 'fast',
|
|
|
|
function () { $( this ).remove(); }
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2013-06-12 00:44:01 +00:00
|
|
|
buildOverlay: function ( callback ) {
|
2013-04-17 10:35:43 +00:00
|
|
|
var notificationLimit,
|
2013-11-01 20:56:58 +00:00
|
|
|
$overlay = $( '<div>' ).addClass( 'mw-echo-overlay' ),
|
2012-12-10 05:01:00 +00:00
|
|
|
$prefLink = $( '#pt-preferences a' ),
|
2013-04-17 10:35:43 +00:00
|
|
|
count = 0,
|
2013-08-27 00:55:45 +00:00
|
|
|
apiData,
|
2013-07-12 22:46:37 +00:00
|
|
|
api = new mw.Api( { ajax: { cache: false } } );
|
2013-04-17 10:35:43 +00:00
|
|
|
|
2013-01-03 19:59:42 +00:00
|
|
|
// Set notification limit based on height of the window
|
2013-04-17 10:35:43 +00:00
|
|
|
notificationLimit = Math.floor( ( $( window ).height() - 134 ) / 90 );
|
2013-01-03 19:59:42 +00:00
|
|
|
|
|
|
|
if ( notificationLimit < 1 ) {
|
|
|
|
notificationLimit = 1;
|
2013-04-10 00:43:57 +00:00
|
|
|
} else if ( notificationLimit > 8 ) {
|
|
|
|
notificationLimit = 8;
|
2013-01-03 19:59:42 +00:00
|
|
|
}
|
2012-06-01 10:57:09 +00:00
|
|
|
|
2013-08-27 00:55:45 +00:00
|
|
|
apiData = {
|
2012-06-01 10:57:09 +00:00
|
|
|
'action' : 'query',
|
|
|
|
'meta' : 'notifications',
|
2013-01-07 22:44:58 +00:00
|
|
|
'notformat' : 'flyout',
|
2013-01-03 19:59:42 +00:00
|
|
|
'notlimit' : notificationLimit,
|
2012-12-17 21:58:18 +00:00
|
|
|
'notprop' : 'index|list|count'
|
2013-08-27 00:55:45 +00:00
|
|
|
};
|
|
|
|
|
2013-09-25 00:18:02 +00:00
|
|
|
api.get( mw.echo.desktop.appendUseLang( apiData ) ).done( function ( result ) {
|
2013-06-05 23:09:28 +00:00
|
|
|
var notifications = result.query.notifications,
|
|
|
|
unread = [],
|
|
|
|
unreadTotalCount = result.query.notifications.count,
|
2013-09-25 00:18:02 +00:00
|
|
|
unreadRawTotalCount = result.query.notifications.rawcount,
|
2013-11-01 20:56:58 +00:00
|
|
|
$title = $( '<div>' ).addClass( 'mw-echo-overlay-title' ),
|
|
|
|
$ul = $( '<ul>' ).addClass( 'mw-echo-notifications' ),
|
|
|
|
titleText,
|
|
|
|
overflow,
|
2013-06-05 23:09:28 +00:00
|
|
|
$overlayFooter,
|
|
|
|
$markReadButton;
|
|
|
|
|
|
|
|
if ( unreadTotalCount !== undefined ) {
|
2013-09-25 00:18:02 +00:00
|
|
|
mw.echo.overlay.updateCount( unreadTotalCount, unreadRawTotalCount );
|
2013-06-05 23:09:28 +00:00
|
|
|
}
|
|
|
|
$ul.css( 'max-height', notificationLimit * 95 + 'px' );
|
|
|
|
$.each( notifications.index, function ( index, id ) {
|
2013-08-05 21:10:08 +00:00
|
|
|
var $wrapper,
|
|
|
|
data = notifications.list[id],
|
2013-11-01 20:56:58 +00:00
|
|
|
$li = $( '<li>' )
|
2013-06-05 23:09:28 +00:00
|
|
|
.data( 'details', data )
|
|
|
|
.data( 'id', id )
|
2013-06-05 20:44:06 +00:00
|
|
|
.attr( {
|
|
|
|
'data-notification-category': data.category,
|
|
|
|
'data-notification-event': data.id,
|
|
|
|
'data-notification-type': data.type
|
|
|
|
} )
|
2014-02-05 18:23:54 +00:00
|
|
|
.addClass( 'mw-echo-notification' );
|
2014-02-06 00:53:04 +00:00
|
|
|
|
|
|
|
if ( !data.read ) {
|
|
|
|
$li.addClass( 'mw-echo-unread' );
|
|
|
|
unread.push( id );
|
|
|
|
}
|
|
|
|
|
2014-02-05 18:23:54 +00:00
|
|
|
if ( !data['*'] ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$li.append( data['*'] )
|
|
|
|
.appendTo( $ul );
|
2013-06-05 23:09:28 +00:00
|
|
|
|
2013-06-12 23:18:26 +00:00
|
|
|
// Grey links in the notification title and footer (except on hover)
|
|
|
|
$li.find( '.mw-echo-title a, .mw-echo-notification-footer a' )
|
|
|
|
.addClass( 'mw-echo-grey-link' );
|
|
|
|
$li.hover(
|
|
|
|
function() {
|
|
|
|
$( this ).find( '.mw-echo-title a' ).removeClass( 'mw-echo-grey-link' );
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
$( this ).find( '.mw-echo-title a' ).addClass( 'mw-echo-grey-link' );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
// If there is a primary link, make the entire notification clickable.
|
2013-08-05 21:10:08 +00:00
|
|
|
// Yes, it is possible to nest <a> tags via DOM manipulation,
|
|
|
|
// and it works like one would expect.
|
2013-06-12 23:18:26 +00:00
|
|
|
if ( $li.find( '.mw-echo-notification-primary-link' ).length ) {
|
2013-08-05 21:10:08 +00:00
|
|
|
$wrapper = $( '<a>' )
|
|
|
|
.addClass( 'mw-echo-notification-wrapper' )
|
|
|
|
.attr( 'href', $li.find( '.mw-echo-notification-primary-link' ).attr( 'href' ) )
|
|
|
|
.click( function() {
|
|
|
|
if ( mw.echo.clickThroughEnabled ) {
|
|
|
|
// Log the clickthrough
|
|
|
|
mw.echo.logInteraction( 'notification-link-click', 'flyout', +data.id, data.type );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
$wrapper = $('<div>').addClass( 'mw-echo-notification-wrapper' );
|
2013-06-12 23:18:26 +00:00
|
|
|
}
|
|
|
|
|
2013-08-05 21:10:08 +00:00
|
|
|
$li.wrapInner( $wrapper );
|
|
|
|
|
2013-06-05 20:44:06 +00:00
|
|
|
mw.echo.setupNotificationLogging( $li, 'flyout' );
|
|
|
|
|
2013-06-05 23:09:28 +00:00
|
|
|
// Set up each individual notification with a close box and dismiss
|
|
|
|
// interface if it is dismissable.
|
|
|
|
if ( $li.find( '.mw-echo-dismiss' ).length ) {
|
|
|
|
mw.echo.setUpDismissability( $li );
|
2012-12-17 21:58:18 +00:00
|
|
|
}
|
2013-06-05 23:09:28 +00:00
|
|
|
} );
|
2013-04-17 01:00:21 +00:00
|
|
|
|
2013-06-05 23:09:28 +00:00
|
|
|
if ( notifications.index.length > 0 ) {
|
2013-09-25 00:18:02 +00:00
|
|
|
if ( unreadRawTotalCount > unread.length ) {
|
2013-10-07 10:44:39 +00:00
|
|
|
titleText = mw.msg(
|
|
|
|
'echo-overlay-title-overflow',
|
|
|
|
mw.language.convertNumber( unread.length ),
|
|
|
|
mw.language.convertNumber( unreadTotalCount )
|
|
|
|
);
|
2013-06-05 23:09:28 +00:00
|
|
|
overflow = true;
|
2013-05-09 23:11:22 +00:00
|
|
|
} else {
|
2013-11-01 20:56:58 +00:00
|
|
|
titleText = mw.msg( 'echo-overlay-title' );
|
|
|
|
overflow = false;
|
2013-04-17 01:00:21 +00:00
|
|
|
}
|
2013-06-05 23:09:28 +00:00
|
|
|
} else {
|
|
|
|
titleText = mw.msg( 'echo-none' );
|
|
|
|
}
|
2013-04-25 04:35:04 +00:00
|
|
|
|
2013-06-05 23:09:28 +00:00
|
|
|
// If there are more unread notifications than can fit in the overlay,
|
|
|
|
// but fewer than the maximum count, show the 'mark all as read' button.
|
|
|
|
// The only reason we limit it to the maximum is to prevent expensive
|
|
|
|
// database updates. If the count is more than the maximum, it could
|
|
|
|
// be thousands.
|
2013-09-25 00:18:02 +00:00
|
|
|
if ( overflow && unreadRawTotalCount < mw.echo.overlay.configuration['max-notification-count']
|
2013-06-05 23:09:28 +00:00
|
|
|
) {
|
|
|
|
// Add the 'mark all as read' button to the title area
|
2013-11-01 19:23:05 +00:00
|
|
|
$markReadButton = $( '<button>' )
|
|
|
|
.addClass( 'mw-ui-button' )
|
|
|
|
.attr( 'id', 'mw-echo-mark-read-button' )
|
|
|
|
.text( mw.msg( 'echo-mark-all-as-read' ) )
|
|
|
|
.click( function ( e ) {
|
|
|
|
e.preventDefault();
|
|
|
|
api.post( mw.echo.desktop.appendUseLang( {
|
|
|
|
'action' : 'echomarkread',
|
|
|
|
'all' : true,
|
|
|
|
'token': mw.user.tokens.get( 'editToken' )
|
|
|
|
} ) ).done( function ( result ) {
|
|
|
|
if ( result.query.echomarkread.count !== undefined ) {
|
|
|
|
count = result.query.echomarkread.count;
|
|
|
|
mw.echo.overlay.updateCount( count, result.query.echomarkread.rawcount );
|
|
|
|
// Reset header to 'Notifications'
|
2013-11-01 20:56:58 +00:00
|
|
|
$( '#mw-echo-overlay-title-text' ).html( mw.msg( 'echo-overlay-title' ) );
|
2013-11-01 19:23:05 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
} );
|
2013-06-05 23:09:28 +00:00
|
|
|
$title.append( $markReadButton );
|
|
|
|
}
|
2012-12-22 00:43:41 +00:00
|
|
|
|
2013-06-05 23:09:28 +00:00
|
|
|
// Add the header to the title area
|
|
|
|
$( '<div>' )
|
|
|
|
.attr( 'id', 'mw-echo-overlay-title-text' )
|
|
|
|
.html( titleText )
|
|
|
|
.appendTo( $title );
|
|
|
|
|
|
|
|
// Add help button
|
|
|
|
$( '<a>' )
|
|
|
|
.attr( 'href', mw.config.get( 'wgEchoHelpPage' ) )
|
|
|
|
.attr( 'title', mw.msg( 'echo-more-info' ) )
|
|
|
|
.attr( 'id', 'mw-echo-overlay-moreinfo-link' )
|
|
|
|
.attr( 'target', '_blank' )
|
2013-06-18 15:23:35 +00:00
|
|
|
.click( function () {
|
2013-06-05 20:44:06 +00:00
|
|
|
mw.echo.logInteraction( 'ui-help-click', 'flyout' );
|
|
|
|
} )
|
2013-06-05 23:09:28 +00:00
|
|
|
.appendTo( $title );
|
|
|
|
|
|
|
|
// Insert the title area into the overlay
|
|
|
|
$title.appendTo( $overlay );
|
|
|
|
|
|
|
|
if ( $ul.find( 'li' ).length ) {
|
|
|
|
$ul.appendTo( $overlay );
|
|
|
|
}
|
2012-06-08 05:55:01 +00:00
|
|
|
|
2013-06-05 23:09:28 +00:00
|
|
|
$overlayFooter = $( '<div>' )
|
|
|
|
.attr( 'id', 'mw-echo-overlay-footer' );
|
2012-12-10 05:01:00 +00:00
|
|
|
|
2013-06-05 23:09:28 +00:00
|
|
|
// add link to notifications archive
|
|
|
|
$overlayFooter.append(
|
|
|
|
$( '<a>' )
|
|
|
|
.attr( 'id', 'mw-echo-overlay-link' )
|
2013-06-12 23:18:26 +00:00
|
|
|
.addClass( 'mw-echo-grey-link' )
|
2014-02-21 13:50:10 +00:00
|
|
|
.attr( 'href', getUrl( 'Special:Notifications' ) )
|
2013-06-05 23:09:28 +00:00
|
|
|
.text( mw.msg( 'echo-overlay-link' ) )
|
2013-06-18 15:23:35 +00:00
|
|
|
.click( function () {
|
2013-06-05 20:44:06 +00:00
|
|
|
mw.echo.logInteraction( 'ui-archive-link-click', 'flyout' );
|
|
|
|
} )
|
2013-06-12 23:18:26 +00:00
|
|
|
.hover(
|
|
|
|
function() {
|
|
|
|
$( this ).removeClass( 'mw-echo-grey-link' );
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
$( this ).addClass( 'mw-echo-grey-link' );
|
|
|
|
}
|
|
|
|
)
|
2013-06-05 23:09:28 +00:00
|
|
|
);
|
2012-12-22 00:43:41 +00:00
|
|
|
|
2013-06-05 23:09:28 +00:00
|
|
|
// add link to notification preferences
|
|
|
|
$overlayFooter.append(
|
2013-07-29 19:50:07 +00:00
|
|
|
$( '<a>' )
|
|
|
|
.html( $prefLink.html() )
|
2013-06-05 23:09:28 +00:00
|
|
|
.attr( 'id', 'mw-echo-overlay-pref-link' )
|
2013-06-12 23:18:26 +00:00
|
|
|
.addClass( 'mw-echo-grey-link' )
|
2013-06-05 23:09:28 +00:00
|
|
|
.attr( 'href', $prefLink.attr( 'href' ) + '#mw-prefsection-echo' )
|
2013-06-18 15:23:35 +00:00
|
|
|
.click( function () {
|
2013-06-05 20:44:06 +00:00
|
|
|
mw.echo.logInteraction( 'ui-prefs-click', 'flyout' );
|
|
|
|
} )
|
2013-06-12 23:18:26 +00:00
|
|
|
.hover(
|
|
|
|
function() {
|
|
|
|
$( this ).removeClass( 'mw-echo-grey-link' );
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
$( this ).addClass( 'mw-echo-grey-link' );
|
|
|
|
}
|
|
|
|
)
|
2013-06-05 23:09:28 +00:00
|
|
|
);
|
2012-06-01 10:57:09 +00:00
|
|
|
|
2013-06-05 23:09:28 +00:00
|
|
|
$overlay.append( $overlayFooter );
|
|
|
|
|
|
|
|
callback( $overlay );
|
|
|
|
|
|
|
|
// only need to mark as read if there is unread item
|
|
|
|
if ( unread.length > 0 ) {
|
2013-09-25 00:18:02 +00:00
|
|
|
api.post( mw.echo.desktop.appendUseLang( {
|
2013-09-18 21:10:37 +00:00
|
|
|
'action' : 'echomarkread',
|
|
|
|
'list' : unread.join( '|' ),
|
|
|
|
'token': mw.user.tokens.get( 'editToken' )
|
2013-09-25 00:18:02 +00:00
|
|
|
} ) ).done( function ( result ) {
|
2013-09-18 21:10:37 +00:00
|
|
|
if ( result.query.echomarkread.count !== undefined ) {
|
|
|
|
count = result.query.echomarkread.count;
|
2013-09-25 00:18:02 +00:00
|
|
|
mw.echo.overlay.updateCount( count, result.query.echomarkread.rawcount );
|
2013-06-05 23:09:28 +00:00
|
|
|
}
|
|
|
|
} );
|
2012-06-01 10:57:09 +00:00
|
|
|
}
|
2013-06-05 23:09:28 +00:00
|
|
|
} ).fail( function () {
|
|
|
|
window.location.href = $( '#pt-notifications a' ).attr( 'href' );
|
2012-06-01 10:57:09 +00:00
|
|
|
} );
|
|
|
|
}
|
2012-08-01 19:53:05 +00:00
|
|
|
};
|
|
|
|
|
2013-06-05 23:09:28 +00:00
|
|
|
$( function () {
|
2012-08-30 16:04:39 +00:00
|
|
|
var $link = $( '#pt-notifications a' );
|
2012-08-01 19:53:05 +00:00
|
|
|
if ( ! $link.length ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-06-01 10:57:09 +00:00
|
|
|
|
2013-06-05 23:09:28 +00:00
|
|
|
$link.click( function ( e ) {
|
2013-11-01 21:38:42 +00:00
|
|
|
var $target;
|
2013-04-17 10:35:43 +00:00
|
|
|
|
2013-06-05 20:44:06 +00:00
|
|
|
// log the badge click
|
|
|
|
mw.echo.logInteraction( 'ui-badge-link-click' );
|
|
|
|
|
2013-12-21 13:24:47 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
2013-04-17 10:35:43 +00:00
|
|
|
$target = $( e.target );
|
2013-11-01 21:38:42 +00:00
|
|
|
// If the user clicked on the overlay or any child, ignore the click
|
|
|
|
if ( $target.hasClass( 'mw-echo-overlay' ) || $target.is( '.mw-echo-overlay *' ) ) {
|
2012-06-01 10:57:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-01 21:38:42 +00:00
|
|
|
if ( $( '.mw-echo-overlay' ).length ) {
|
|
|
|
mw.echo.overlay.removeOverlay();
|
2012-06-01 10:57:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-04-17 10:35:43 +00:00
|
|
|
|
2013-11-01 20:56:58 +00:00
|
|
|
mw.echo.overlay.buildOverlay(
|
2013-06-05 23:09:28 +00:00
|
|
|
function ( $overlay ) {
|
2012-06-01 10:57:09 +00:00
|
|
|
$overlay
|
|
|
|
.hide()
|
2013-01-10 04:13:08 +00:00
|
|
|
.appendTo( $( '#pt-notifications' ) );
|
2013-11-01 20:56:58 +00:00
|
|
|
|
2013-01-10 04:13:08 +00:00
|
|
|
// Create the pokey (aka chevron)
|
2013-11-01 20:56:58 +00:00
|
|
|
$overlay.before( $( '<div>' ).addClass( 'mw-echo-overlay-pokey' ) );
|
2013-05-16 03:24:29 +00:00
|
|
|
|
|
|
|
mw.hook( 'ext.echo.overlay.beforeShowingOverlay' ).fire( $overlay );
|
|
|
|
|
2013-01-10 04:13:08 +00:00
|
|
|
// Show the notifications overlay
|
|
|
|
$overlay.show();
|
2013-12-21 13:24:29 +00:00
|
|
|
|
|
|
|
// Make sure the overlay is visible, even if the badge is near the edge of browser window.
|
|
|
|
// 10 is an arbitrarily chosen "close enough" number.
|
|
|
|
// We are careful not to slide out from below the pokey (which is 21px wide) (200-21/2+1 == 189)
|
|
|
|
var
|
|
|
|
offset = $overlay.offset(),
|
|
|
|
width = $overlay.width(),
|
|
|
|
windowWidth = $( window ).width();
|
|
|
|
if ( offset.left < 10 ) {
|
|
|
|
$overlay.css( 'left', '+=' + Math.min( 189, 10 - offset.left ) );
|
|
|
|
} else if ( offset.left + width > windowWidth - 10 ) {
|
|
|
|
$overlay.css( 'left', '-=' + Math.min( 189, ( offset.left + width ) - ( windowWidth - 10 ) ) );
|
|
|
|
}
|
2013-11-01 20:56:58 +00:00
|
|
|
}
|
|
|
|
);
|
2012-06-01 10:57:09 +00:00
|
|
|
} );
|
|
|
|
|
2013-06-05 23:09:28 +00:00
|
|
|
$( 'body' ).click( function ( e ) {
|
2013-11-01 21:38:42 +00:00
|
|
|
if ( ! $( e.target ).is( '.mw-echo-overlay, .mw-echo-overlay *, .mw-echo-overlay-pokey, #pt-notifications a' ) ) {
|
|
|
|
mw.echo.overlay.removeOverlay();
|
2012-06-01 10:57:09 +00:00
|
|
|
}
|
2012-08-30 16:04:39 +00:00
|
|
|
} );
|
2014-04-03 13:46:00 +00:00
|
|
|
|
|
|
|
// Closes the notifications overlay when ESC key pressed
|
|
|
|
$( document ).on( 'keydown', function ( e ) {
|
|
|
|
if ( e.which === 27 ) {
|
|
|
|
mw.echo.overlay.removeOverlay();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2012-08-30 16:04:39 +00:00
|
|
|
} );
|
|
|
|
} )( jQuery, mediaWiki );
|