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';
|
|
|
|
|
2012-08-01 19:53:05 +00:00
|
|
|
mw.echo.overlay = {
|
2012-12-10 05:01:00 +00:00
|
|
|
|
2013-06-12 00:44:01 +00:00
|
|
|
updateCount: function ( newCount ) {
|
2013-05-15 23:10:49 +00:00
|
|
|
var $badge = $( '.mw-echo-notifications-badge' );
|
|
|
|
$badge.text( newCount );
|
|
|
|
// newCount could be '99+' or another string.
|
|
|
|
// Checking for number as well just to be paranoid.
|
|
|
|
if ( newCount !== '0' && newCount !== 0 ) {
|
|
|
|
$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-06-12 00:44:01 +00:00
|
|
|
buildOverlay: function ( callback ) {
|
2013-04-17 10:35:43 +00:00
|
|
|
var notificationLimit,
|
|
|
|
$overlay = $( '<div></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-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-06-05 23:09:28 +00:00
|
|
|
api.get( {
|
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-06-05 23:09:28 +00:00
|
|
|
} ).done( function ( result ) {
|
|
|
|
var notifications = result.query.notifications,
|
|
|
|
unread = [],
|
|
|
|
unreadTotalCount = result.query.notifications.count,
|
|
|
|
$title = $( '<div class="mw-echo-overlay-title"></div>' ),
|
|
|
|
$ul = $( '<ul class="mw-echo-notifications"></ul>' ),
|
|
|
|
titleText = '',
|
|
|
|
overflow = false,
|
|
|
|
$overlayFooter,
|
|
|
|
$markReadButton;
|
|
|
|
|
|
|
|
if ( unreadTotalCount !== undefined ) {
|
|
|
|
mw.echo.overlay.updateCount( unreadTotalCount );
|
|
|
|
}
|
|
|
|
$ul.css( 'max-height', notificationLimit * 95 + 'px' );
|
|
|
|
$.each( notifications.index, function ( index, id ) {
|
|
|
|
var data = notifications.list[id],
|
|
|
|
$li = $( '<li></li>' )
|
|
|
|
.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
|
|
|
|
} )
|
2013-06-05 23:09:28 +00:00
|
|
|
.addClass( 'mw-echo-notification' )
|
|
|
|
.append( data['*'] )
|
|
|
|
.appendTo( $ul );
|
|
|
|
|
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.
|
|
|
|
if ( $li.find( '.mw-echo-notification-primary-link' ).length ) {
|
|
|
|
$li.css( 'cursor', 'pointer' );
|
|
|
|
$li.click( function() {
|
|
|
|
if ( mw.echo.clickThroughEnabled ) {
|
|
|
|
// Log the clickthrough
|
2013-07-02 17:37:31 +00:00
|
|
|
mw.echo.logInteraction( 'notification-link-click', 'flyout', +data.id, data.type );
|
2013-06-12 23:18:26 +00:00
|
|
|
}
|
|
|
|
window.location.href = $li.find( '.mw-echo-notification-primary-link' ).attr( 'href' );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2013-06-05 20:44:06 +00:00
|
|
|
mw.echo.setupNotificationLogging( $li, 'flyout' );
|
|
|
|
|
2013-06-05 23:09:28 +00:00
|
|
|
if ( !data.read ) {
|
|
|
|
$li.addClass( 'mw-echo-unread' );
|
|
|
|
unread.push( id );
|
2013-05-15 23:10:49 +00:00
|
|
|
}
|
2012-06-01 10:57:09 +00:00
|
|
|
|
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 ) {
|
|
|
|
if ( isNaN( unreadTotalCount ) || unreadTotalCount > unread.length ) {
|
|
|
|
titleText = mw.msg( 'echo-overlay-title-overflow', unread.length, unreadTotalCount );
|
|
|
|
overflow = true;
|
2013-05-09 23:11:22 +00:00
|
|
|
} else {
|
2013-06-05 23:09:28 +00:00
|
|
|
titleText = mw.msg( 'echo-overlay-title' );
|
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
|
|
|
$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();
|
2013-07-12 22:46:37 +00:00
|
|
|
api.post( {
|
2013-06-05 23:09:28 +00:00
|
|
|
'action' : 'query',
|
|
|
|
'meta' : 'notifications',
|
|
|
|
'notmarkallread' : true,
|
|
|
|
'notprop' : 'count'
|
|
|
|
} ).done( function ( result ) {
|
|
|
|
if ( result.query.notifications.count !== undefined ) {
|
|
|
|
count = result.query.notifications.count;
|
|
|
|
mw.echo.overlay.updateCount( count );
|
|
|
|
// Reset header to 'Notifications'
|
|
|
|
$( '#mw-echo-overlay-title-text').msg( 'echo-overlay-title' );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
} );
|
2012-06-08 05:55:01 +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.
|
|
|
|
if ( overflow &&
|
|
|
|
!isNaN( unreadTotalCount ) &&
|
|
|
|
unreadTotalCount < mw.echo.overlay.configuration['max-notification-count']
|
|
|
|
) {
|
|
|
|
// Add the 'mark all as read' button to the title area
|
|
|
|
$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' )
|
2013-06-05 23:09:28 +00:00
|
|
|
.attr( 'href', mw.util.wikiGetlink( 'Special:Notifications' ) )
|
|
|
|
.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-07-12 22:46:37 +00:00
|
|
|
api.post( {
|
2013-06-05 23:09:28 +00:00
|
|
|
'action' : 'query',
|
|
|
|
'meta' : 'notifications',
|
|
|
|
'notmarkread' : unread.join( '|' ),
|
|
|
|
'notprop' : 'count'
|
|
|
|
} ).done( function ( result ) {
|
|
|
|
if ( result.query.notifications.count !== undefined ) {
|
|
|
|
count = result.query.notifications.count;
|
|
|
|
mw.echo.overlay.updateCount( count );
|
|
|
|
}
|
|
|
|
} );
|
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-04-17 10:35:43 +00:00
|
|
|
var $target, $overlay;
|
|
|
|
|
2012-06-01 10:57:09 +00:00
|
|
|
e.preventDefault();
|
2013-06-05 20:44:06 +00:00
|
|
|
|
|
|
|
// log the badge click
|
|
|
|
mw.echo.logInteraction( 'ui-badge-link-click' );
|
|
|
|
|
2013-04-17 10:35:43 +00:00
|
|
|
$target = $( e.target );
|
2012-08-01 19:42:06 +00:00
|
|
|
// If the user clicked on the overlay or any child,
|
|
|
|
// ignore the click
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( $target.hasClass( 'mw-echo-overlay' ) ||
|
|
|
|
$target.is( 'mw-echo-overlay *' )
|
2012-08-01 19:42:06 +00:00
|
|
|
) {
|
2012-06-01 10:57:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-17 10:35:43 +00:00
|
|
|
$overlay = $( '.mw-echo-overlay' );
|
2012-06-01 10:57:09 +00:00
|
|
|
|
|
|
|
if ( $overlay.length ) {
|
|
|
|
$overlay.fadeOut( 'fast',
|
2013-06-05 23:09:28 +00:00
|
|
|
function () { $overlay.remove(); }
|
2012-06-01 10:57:09 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2013-04-17 10:35:43 +00:00
|
|
|
|
2012-08-01 19:53:05 +00:00
|
|
|
$overlay = 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' ) );
|
|
|
|
// Create the pokey (aka chevron)
|
2013-06-05 23:09:28 +00:00
|
|
|
$( '.mw-echo-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();
|
2012-06-01 10:57:09 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2013-06-05 23:09:28 +00:00
|
|
|
$( 'body' ).click( function ( e ) {
|
2013-01-10 04:13:08 +00:00
|
|
|
if ( ! $( e.target ).is( '.mw-echo-overlay, .mw-echo-overlay *, .mw-echo-overlay-pokey' ) ) {
|
|
|
|
$( '.mw-echo-overlay, .mw-echo-overlay-pokey' ).fadeOut( 'fast',
|
2013-06-05 23:09:28 +00:00
|
|
|
function () { $( this ).remove(); }
|
2012-06-01 10:57:09 +00:00
|
|
|
);
|
|
|
|
}
|
2012-08-30 16:04:39 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} )( jQuery, mediaWiki );
|