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-08-06 00:24:34 +00:00
|
|
|
function EchoOverlay( apiResultNotifications ) {
|
|
|
|
this.api = mw.echo.overlay.api;
|
2014-08-06 19:29:17 +00:00
|
|
|
// set internal properties
|
|
|
|
this.tabs = [];
|
2014-08-06 00:24:34 +00:00
|
|
|
this._buildOverlay( apiResultNotifications );
|
|
|
|
}
|
|
|
|
|
2014-08-12 01:54:34 +00:00
|
|
|
function EchoOverlayTab( options, notifications ) {
|
2014-08-06 19:29:17 +00:00
|
|
|
this.api = mw.echo.overlay.api;
|
2014-08-12 01:54:34 +00:00
|
|
|
this.markOnView = options.markOnView;
|
|
|
|
this.markAsReadCallback = options.markAsReadCallback;
|
|
|
|
this.name = options.name;
|
2014-08-06 19:29:17 +00:00
|
|
|
this.unread = [];
|
2014-08-13 00:35:08 +00:00
|
|
|
this._totalUnread = notifications[this.name].rawcount;
|
2015-04-29 12:08:30 +00:00
|
|
|
this._setLastUnreadNotificationTime( notifications );
|
2014-08-12 01:54:34 +00:00
|
|
|
this._buildList( notifications[this.name] );
|
2014-08-06 19:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EchoOverlayTab.prototype = {
|
2014-08-13 00:35:08 +00:00
|
|
|
/* @var integer totalUnread the number of unread notifications in this tab.
|
|
|
|
including those that are not visible. */
|
|
|
|
/**
|
|
|
|
* Return a list of unread and shown ids
|
|
|
|
* @method
|
|
|
|
* @param integer id of a notification to mark as read
|
|
|
|
* @return jQuery.Deferred
|
|
|
|
*/
|
2015-05-04 19:24:37 +00:00
|
|
|
getUnreadIds: function () {
|
2014-08-06 19:29:17 +00:00
|
|
|
return this.unread;
|
|
|
|
},
|
2014-08-13 00:35:08 +00:00
|
|
|
/**
|
|
|
|
* Get a count the number of all unread notifications of this type
|
|
|
|
* @method
|
2015-04-29 12:08:30 +00:00
|
|
|
* @return {integer}
|
2014-08-13 00:35:08 +00:00
|
|
|
*/
|
2015-05-04 19:24:37 +00:00
|
|
|
getNumberUnread: function () {
|
2014-08-13 00:35:08 +00:00
|
|
|
return this._totalUnread;
|
|
|
|
},
|
2015-04-29 12:08:30 +00:00
|
|
|
/**
|
|
|
|
* Returns the timestamp of the last unread notification
|
|
|
|
* @method
|
|
|
|
* @return {string|false} Timestamp of last notification (in TS_MW format), or false if there is none
|
|
|
|
*/
|
2015-05-04 19:24:37 +00:00
|
|
|
getLastUnreadNotificationTime: function () {
|
2015-04-29 12:08:30 +00:00
|
|
|
return this._lastUnreadNotificationTime || false;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Set the timestamp of the last unread notification
|
|
|
|
* @method
|
|
|
|
* @param {object} notifications List of notifications
|
|
|
|
*/
|
2015-05-04 19:24:37 +00:00
|
|
|
_setLastUnreadNotificationTime: function ( notifications ) {
|
2015-04-29 12:08:30 +00:00
|
|
|
var self = this;
|
|
|
|
$.each( notifications[this.name].list, function ( key, data ) {
|
|
|
|
if ( data.read ) {
|
|
|
|
// ignore notifications that have already been read
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
self._lastUnreadNotificationTime === undefined ||
|
|
|
|
self._lastUnreadNotificationTime < data.timestamp.mw
|
|
|
|
) {
|
|
|
|
self._lastUnreadNotificationTime = data.timestamp.mw;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
},
|
2014-08-06 19:29:17 +00:00
|
|
|
/**
|
2014-08-12 17:17:19 +00:00
|
|
|
* Mark all existing notifications as read
|
2014-08-06 19:29:17 +00:00
|
|
|
* @method
|
2014-08-12 17:17:19 +00:00
|
|
|
* @param integer id of a notification to mark as read
|
2014-08-06 19:29:17 +00:00
|
|
|
* @return jQuery.Deferred
|
|
|
|
*/
|
2015-05-04 19:24:37 +00:00
|
|
|
markAsRead: function ( id ) {
|
|
|
|
var data,
|
|
|
|
self = this;
|
2014-08-06 19:29:17 +00:00
|
|
|
// only need to mark as read if there is unread item
|
|
|
|
if ( this.unread.length ) {
|
2014-08-12 01:54:34 +00:00
|
|
|
data = {
|
2014-08-12 15:09:09 +00:00
|
|
|
action: 'echomarkread',
|
2015-06-08 00:30:00 +00:00
|
|
|
uselang: 'user'
|
2014-08-12 01:54:34 +00:00
|
|
|
};
|
2014-08-12 17:17:19 +00:00
|
|
|
if ( id ) {
|
|
|
|
// If id is given mark that as read otherwise use all unread messages
|
|
|
|
data.list = id;
|
|
|
|
} else {
|
2014-08-13 23:11:38 +00:00
|
|
|
data.sections = this.name;
|
2014-08-12 17:17:19 +00:00
|
|
|
}
|
|
|
|
|
2015-06-08 00:24:09 +00:00
|
|
|
return this.api.postWithToken( 'edit', data ).then( function ( result ) {
|
2014-08-12 17:17:19 +00:00
|
|
|
return result.query.echomarkread;
|
2015-05-04 19:24:37 +00:00
|
|
|
} ).done( function ( result ) {
|
2014-08-11 22:19:37 +00:00
|
|
|
// reset internal state of unread messages
|
2014-08-12 17:17:19 +00:00
|
|
|
if ( id ) {
|
|
|
|
if ( self.unread.indexOf( id ) > -1 ) {
|
|
|
|
self.unread.splice( self.unread.indexOf( id ), 1 );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.unread = [];
|
|
|
|
}
|
2014-08-13 00:35:08 +00:00
|
|
|
// update the count
|
|
|
|
self._totalUnread = result[self.name].rawcount;
|
2014-08-12 17:17:19 +00:00
|
|
|
self.markAsReadCallback( result, id );
|
2014-08-06 19:29:17 +00:00
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
return new $.Deferred();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Builds an Echo notifications list
|
|
|
|
* @method
|
2015-04-29 12:08:30 +00:00
|
|
|
* @param {object} notifications as returned by the api of notification items
|
2014-08-06 19:29:17 +00:00
|
|
|
* @return jQuery element
|
|
|
|
*/
|
2015-05-04 19:24:37 +00:00
|
|
|
_buildList: function ( notifications ) {
|
2014-08-06 19:29:17 +00:00
|
|
|
var self = this,
|
2014-08-12 01:54:34 +00:00
|
|
|
$container = $( '<div class="mw-echo-notifications">' )
|
2014-08-06 19:29:17 +00:00
|
|
|
.data( 'tab', this )
|
2014-09-19 01:32:20 +00:00
|
|
|
.css( 'max-height', $( window ).height() - 140 ),
|
2015-04-29 12:08:30 +00:00
|
|
|
$ul = $( '<ul>' ).appendTo( $container ),
|
|
|
|
seenTime = mw.user.options.get( 'echo-seen-time' );
|
2014-08-06 19:29:17 +00:00
|
|
|
|
|
|
|
$.each( notifications.index, function ( index, id ) {
|
|
|
|
var $wrapper,
|
|
|
|
data = notifications.list[id],
|
|
|
|
$li = $( '<li>' )
|
|
|
|
.data( 'details', data )
|
|
|
|
.data( 'id', id )
|
|
|
|
.attr( {
|
|
|
|
'data-notification-category': data.category,
|
|
|
|
'data-notification-event': data.id,
|
|
|
|
'data-notification-type': data.type
|
|
|
|
} )
|
|
|
|
.addClass( 'mw-echo-notification' );
|
|
|
|
|
|
|
|
if ( !data['*'] ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$li.append( data['*'] )
|
|
|
|
.appendTo( $ul );
|
|
|
|
|
2014-08-12 17:17:19 +00:00
|
|
|
if ( !data.read ) {
|
|
|
|
$li.addClass( 'mw-echo-unread' );
|
|
|
|
self.unread.push( id );
|
|
|
|
if ( !self.markOnView ) {
|
|
|
|
$( '<button class="mw-ui-button mw-ui-quiet">×</button>' )
|
2015-05-04 19:24:37 +00:00
|
|
|
.on( 'click', function ( ev ) {
|
2014-08-12 17:17:19 +00:00
|
|
|
ev.preventDefault();
|
|
|
|
self.markAsRead( $( this ).closest( 'li' ).data( 'notification-event' ) );
|
|
|
|
} ).appendTo( $li );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 12:08:30 +00:00
|
|
|
if ( seenTime !== null && data.timestamp.mw > seenTime ) {
|
|
|
|
$li.addClass( 'mw-echo-unseen' );
|
|
|
|
}
|
|
|
|
|
2014-08-06 19:29:17 +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(
|
2015-05-04 19:24:37 +00:00
|
|
|
function () {
|
2015-03-13 16:03:54 +00:00
|
|
|
$( this ).find( '.mw-echo-title a, .mw-echo-notification-footer a' ).removeClass( 'mw-echo-grey-link' );
|
2014-08-06 19:29:17 +00:00
|
|
|
},
|
2015-05-04 19:24:37 +00:00
|
|
|
function () {
|
2015-03-13 16:03:54 +00:00
|
|
|
$( this ).find( '.mw-echo-title a, .mw-echo-notification-footer a' ).addClass( 'mw-echo-grey-link' );
|
2014-08-06 19:29:17 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
// If there is a primary link, make the entire notification clickable.
|
|
|
|
// Yes, it is possible to nest <a> tags via DOM manipulation,
|
|
|
|
// and it works like one would expect.
|
|
|
|
if ( $li.find( '.mw-echo-notification-primary-link' ).length ) {
|
|
|
|
$wrapper = $( '<a>' )
|
|
|
|
.addClass( 'mw-echo-notification-wrapper' )
|
|
|
|
.attr( 'href', $li.find( '.mw-echo-notification-primary-link' ).attr( 'href' ) )
|
2015-05-04 19:24:37 +00:00
|
|
|
.click( function () {
|
2014-08-06 19:29:17 +00:00
|
|
|
if ( mw.echo.clickThroughEnabled ) {
|
|
|
|
// Log the clickthrough
|
|
|
|
mw.echo.logInteraction( 'notification-link-click', 'flyout', +data.id, data.type );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
} else {
|
2015-05-04 19:24:37 +00:00
|
|
|
$wrapper = $( '<div>' ).addClass( 'mw-echo-notification-wrapper' );
|
2014-08-06 19:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$li.wrapInner( $wrapper );
|
|
|
|
|
|
|
|
mw.echo.setupNotificationLogging( $li, 'flyout' );
|
|
|
|
} );
|
2014-08-12 01:54:34 +00:00
|
|
|
|
|
|
|
if ( !this.markOnView && this.unread.length ) {
|
|
|
|
$( '<button class="mw-ui-button mw-ui-quiet">' )
|
|
|
|
.text( mw.msg( 'echo-mark-all-as-read' ) )
|
2015-05-04 19:24:37 +00:00
|
|
|
.on( 'click', function () {
|
2014-08-12 01:54:34 +00:00
|
|
|
var $btn = $( this );
|
2015-05-04 19:24:37 +00:00
|
|
|
self.markAsRead().done( function () {
|
2014-08-12 01:54:34 +00:00
|
|
|
self.$el.find( '.mw-echo-unread' ).removeClass( 'mw-echo-unread' );
|
|
|
|
$btn.remove();
|
|
|
|
} );
|
|
|
|
} )
|
|
|
|
.prependTo( $container );
|
|
|
|
}
|
|
|
|
this.$el = $container;
|
2014-08-06 19:29:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-06 00:24:34 +00:00
|
|
|
EchoOverlay.prototype = {
|
2014-08-06 19:29:17 +00:00
|
|
|
/**
|
|
|
|
* @var array a list of EchoOverlayTabs
|
|
|
|
*/
|
|
|
|
tabs: [],
|
2014-08-06 18:40:47 +00:00
|
|
|
/**
|
|
|
|
* @var object current count status of notification types
|
|
|
|
*/
|
|
|
|
notificationCount: {
|
|
|
|
/* @var integer length of all notifications (both unread and read) that will be visible in the overlay */
|
|
|
|
all: 0,
|
|
|
|
/* @var string a string representation the current number of unread notifications (1, 99, 99+) */
|
|
|
|
unread: '0',
|
|
|
|
/* @var integer the total number of all unread notifications including those not in the overlay */
|
|
|
|
unreadRaw: 0
|
|
|
|
},
|
2014-08-06 19:29:17 +00:00
|
|
|
|
2013-09-25 00:18:02 +00:00
|
|
|
/**
|
2014-08-06 19:29:17 +00:00
|
|
|
* FIXME: This should be pulled out of EchoOverlay and use an EventEmitter.
|
2013-09-25 00:18:02 +00:00
|
|
|
* @param newCount formatted count
|
|
|
|
* @param rawCount unformatted count
|
|
|
|
*/
|
2014-08-06 19:29:17 +00:00
|
|
|
updateBadgeCount: function ( newCount, rawCount ) {
|
|
|
|
var $badge = mw.echo.getBadge();
|
2013-05-15 23:10:49 +00:00
|
|
|
$badge.text( newCount );
|
2013-09-25 00:18:02 +00:00
|
|
|
|
2015-04-29 12:08:30 +00:00
|
|
|
this.notificationCount.unread = newCount;
|
|
|
|
this.notificationCount.unreadRaw = rawCount;
|
|
|
|
mw.hook( 'ext.echo.updateNotificationCount' ).fire( rawCount );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FIXME: This should be pulled out of EchoOverlay and use an EventEmitter.
|
|
|
|
*/
|
|
|
|
updateBadgeColor: function () {
|
|
|
|
var $badge = mw.echo.getBadge(),
|
|
|
|
count = this.notificationCount.unreadRaw,
|
2015-05-06 08:49:07 +00:00
|
|
|
seenTime = mw.user.options.get( 'echo-seen-time' ),
|
2015-04-29 12:08:30 +00:00
|
|
|
seen = true;
|
|
|
|
|
|
|
|
// figure out if unread notifications in all tabs have already been seen
|
|
|
|
$.each( this.tabs, function ( key, tab ) {
|
|
|
|
var time = tab.getLastUnreadNotificationTime();
|
2015-05-06 08:49:07 +00:00
|
|
|
seen = seen && ( time === false || ( seenTime !== null && time < seenTime ) );
|
2015-04-29 12:08:30 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
if ( !seen && count !== '0' && count !== 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
|
|
|
|
2015-05-04 19:24:37 +00:00
|
|
|
_getFooterElement: function () {
|
2014-08-05 23:13:32 +00:00
|
|
|
var $prefLink = $( '#pt-preferences a' ),
|
2014-08-14 21:56:00 +00:00
|
|
|
links = [
|
2015-06-08 00:33:29 +00:00
|
|
|
{ url: mw.util.getUrl( 'Special:Notifications' ), text: mw.msg( 'echo-overlay-link' ),
|
2014-08-14 21:56:00 +00:00
|
|
|
className: 'mw-echo-icon-all' },
|
|
|
|
{ url: $prefLink.attr( 'href' ) + '#mw-prefsection-echo', text: $prefLink.text(),
|
|
|
|
className: 'mw-echo-icon-cog' }
|
|
|
|
],
|
2014-08-14 21:31:13 +00:00
|
|
|
$overlayFooter = $( '<div class="mw-echo-overlay-footer">' );
|
2014-08-05 23:13:32 +00:00
|
|
|
|
2015-05-04 19:24:37 +00:00
|
|
|
$.each( links, function ( i, link ) {
|
2014-08-14 21:56:00 +00:00
|
|
|
$( '<a class="mw-echo-grey-link">' )
|
|
|
|
.attr( 'href', link.url )
|
|
|
|
.addClass( link.className )
|
|
|
|
.text( link.text )
|
|
|
|
.appendTo( $overlayFooter );
|
|
|
|
} );
|
2014-08-05 23:13:32 +00:00
|
|
|
// add link to notifications archive
|
2014-09-18 16:45:10 +00:00
|
|
|
$overlayFooter.find( 'a' ).hover(
|
2015-05-04 19:24:37 +00:00
|
|
|
function () {
|
2014-08-14 21:56:00 +00:00
|
|
|
$( this ).removeClass( 'mw-echo-grey-link' );
|
|
|
|
},
|
2015-05-04 19:24:37 +00:00
|
|
|
function () {
|
2014-08-14 21:56:00 +00:00
|
|
|
$( this ).addClass( 'mw-echo-grey-link' );
|
|
|
|
}
|
2014-08-05 23:13:32 +00:00
|
|
|
);
|
|
|
|
return $overlayFooter;
|
|
|
|
},
|
2014-08-05 23:32:22 +00:00
|
|
|
|
2015-05-04 19:24:37 +00:00
|
|
|
_showTabList: function ( tab ) {
|
2014-08-12 01:54:34 +00:00
|
|
|
var $lists = this.$el.find( '.mw-echo-notifications' ).hide();
|
2014-08-05 23:32:22 +00:00
|
|
|
|
2014-08-13 17:07:32 +00:00
|
|
|
this._activeTab = tab;
|
2015-05-04 19:24:37 +00:00
|
|
|
$lists.each( function () {
|
2014-08-06 19:29:17 +00:00
|
|
|
if ( $( this ).data( 'tab' ).name === tab.name ) {
|
|
|
|
$( this ).show();
|
2014-08-12 01:54:34 +00:00
|
|
|
if ( tab.markOnView ) {
|
|
|
|
tab.markAsRead();
|
|
|
|
}
|
2014-08-05 23:32:22 +00:00
|
|
|
}
|
2014-08-06 19:29:17 +00:00
|
|
|
} );
|
2014-08-05 23:32:22 +00:00
|
|
|
},
|
|
|
|
|
2015-05-04 19:24:37 +00:00
|
|
|
_updateTitleElement: function () {
|
2014-08-11 22:19:37 +00:00
|
|
|
var $header;
|
|
|
|
$header = this.$el.find( '.mw-echo-overlay-title' );
|
|
|
|
this._getTitleElement().insertBefore( $header );
|
|
|
|
$header.remove();
|
|
|
|
},
|
|
|
|
|
2015-05-04 19:24:37 +00:00
|
|
|
_getTabsElement: function () {
|
2014-08-06 19:29:17 +00:00
|
|
|
var $li,
|
2015-05-04 19:24:37 +00:00
|
|
|
$ul = $( '<ul>' ),
|
|
|
|
self = this;
|
2013-08-27 00:55:45 +00:00
|
|
|
|
2015-05-04 19:24:37 +00:00
|
|
|
$.each( this.tabs, function ( i, echoTab ) {
|
2014-08-06 19:29:17 +00:00
|
|
|
var
|
2014-09-09 00:10:33 +00:00
|
|
|
tabName = self.tabs.length > 1 ? echoTab.name : ( echoTab.name + '-text-only' ),
|
2014-08-15 02:01:45 +00:00
|
|
|
// Messages that can be used here:
|
|
|
|
// * echo-notification-alert
|
|
|
|
// * echo-notification-message
|
2014-09-09 00:10:33 +00:00
|
|
|
// * echo-notification-alert-text-only
|
|
|
|
// * echo-notification-message-text-only
|
2014-08-11 22:19:37 +00:00
|
|
|
// @todo: Unread value is inaccurate. If a user has more than mw.echo.overlay.notificationLimit
|
|
|
|
// API change needed
|
2014-09-08 13:14:19 +00:00
|
|
|
label = mw.msg(
|
|
|
|
'echo-notification-' + tabName,
|
|
|
|
mw.language.convertNumber( echoTab.getNumberUnread() )
|
|
|
|
);
|
2013-06-05 23:09:28 +00:00
|
|
|
|
2014-08-06 19:29:17 +00:00
|
|
|
$li = $( '<li>' )
|
2014-08-11 23:40:20 +00:00
|
|
|
.appendTo( $ul );
|
|
|
|
|
2015-03-03 01:16:31 +00:00
|
|
|
$( '<a class="mw-ui-anchor mw-ui-progressive">' )
|
2015-05-04 19:24:37 +00:00
|
|
|
.on( 'click', function () {
|
2014-08-06 19:29:17 +00:00
|
|
|
var $this = $( this );
|
2014-09-08 23:43:13 +00:00
|
|
|
$ul.find( 'a' ).removeClass( 'mw-ui-quiet' ).addClass( 'mw-ui-active' );
|
2015-05-04 19:24:37 +00:00
|
|
|
$this.addClass( 'mw-ui-quiet' ).removeClass( 'mw-ui-active' );
|
2014-08-06 19:29:17 +00:00
|
|
|
self._showTabList( $this.data( 'tab' ) );
|
|
|
|
} )
|
|
|
|
.data( 'tab', echoTab )
|
2014-09-08 23:43:13 +00:00
|
|
|
.addClass( echoTab.name === self._activeTab.name ? 'mw-ui-quiet' : 'mw-ui-active' )
|
2014-08-11 23:40:20 +00:00
|
|
|
.text( label ).appendTo( $li );
|
2014-08-06 19:29:17 +00:00
|
|
|
} );
|
|
|
|
return $ul;
|
|
|
|
},
|
2013-06-05 23:09:28 +00:00
|
|
|
|
2015-05-04 19:24:37 +00:00
|
|
|
getUnreadCount: function () {
|
2014-08-06 19:29:17 +00:00
|
|
|
var count = 0;
|
2015-05-04 19:24:37 +00:00
|
|
|
$.each( this.tabs, function ( i, tab ) {
|
2014-08-13 00:35:08 +00:00
|
|
|
count += tab.getNumberUnread();
|
2014-08-06 19:29:17 +00:00
|
|
|
} );
|
|
|
|
return count;
|
|
|
|
},
|
2013-06-12 23:18:26 +00:00
|
|
|
|
2015-05-04 19:24:37 +00:00
|
|
|
_getTitleElement: function () {
|
2014-08-11 22:19:37 +00:00
|
|
|
var $title = $( '<div>' ).addClass( 'mw-echo-overlay-title' )
|
|
|
|
.append( this._getTabsElement() );
|
2014-08-06 19:29:17 +00:00
|
|
|
return $title;
|
2014-08-06 18:40:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_buildOverlay: function ( notifications ) {
|
2014-08-06 19:29:17 +00:00
|
|
|
var tabs,
|
|
|
|
self = this,
|
2014-08-12 01:54:34 +00:00
|
|
|
options = {
|
2015-05-04 19:24:37 +00:00
|
|
|
markAsReadCallback: function ( data, id ) {
|
2014-08-12 01:54:34 +00:00
|
|
|
self.updateBadgeCount( data.count, data.rawcount );
|
2015-04-29 12:08:30 +00:00
|
|
|
self.updateBadgeColor();
|
2014-08-12 01:54:34 +00:00
|
|
|
self._updateTitleElement();
|
2014-08-12 17:17:19 +00:00
|
|
|
if ( id ) {
|
2015-05-04 19:24:37 +00:00
|
|
|
self.$el.find( '[data-notification-event="' + id + '"]' ).removeClass( 'mw-echo-unread' )
|
2014-08-12 17:17:19 +00:00
|
|
|
.find( 'button' ).remove();
|
|
|
|
}
|
2014-08-12 01:54:34 +00:00
|
|
|
}
|
|
|
|
},
|
2014-08-06 19:29:17 +00:00
|
|
|
$overlay = $( '<div>' ).addClass( 'mw-echo-overlay' );
|
2014-08-06 18:40:47 +00:00
|
|
|
|
2014-08-06 19:29:17 +00:00
|
|
|
this.$el = $overlay;
|
2012-06-08 05:55:01 +00:00
|
|
|
|
2014-08-06 19:29:17 +00:00
|
|
|
if ( notifications.message.index.length ) {
|
2014-09-09 14:41:59 +00:00
|
|
|
tabs = [ { name: 'alert', markOnView: true }, { name: 'message' } ];
|
2014-08-06 19:29:17 +00:00
|
|
|
} else {
|
2014-08-12 01:54:34 +00:00
|
|
|
tabs = [ { name: 'alert', markOnView: true } ];
|
2014-08-06 00:24:34 +00:00
|
|
|
}
|
2013-06-05 23:09:28 +00:00
|
|
|
|
2015-05-04 19:24:37 +00:00
|
|
|
$.each( tabs, function ( i, tabOptions ) {
|
2014-08-12 01:54:34 +00:00
|
|
|
var tab = new EchoOverlayTab( $.extend( tabOptions, options ), notifications );
|
2014-08-06 19:29:17 +00:00
|
|
|
self.$el.append( tab.$el );
|
|
|
|
self.tabs.push( tab );
|
2014-08-12 01:54:34 +00:00
|
|
|
self.notificationCount.all += notifications[tabOptions.name].index.length;
|
2014-08-06 19:29:17 +00:00
|
|
|
} );
|
2014-09-05 23:42:13 +00:00
|
|
|
|
|
|
|
if ( tabs.length === 1 ) {
|
|
|
|
// only one tab exists
|
|
|
|
this._activeTab = this.tabs[0];
|
|
|
|
} else if (
|
|
|
|
notifications.message.rawcount > 0 &&
|
|
|
|
notifications.alert.rawcount === 0
|
|
|
|
) {
|
|
|
|
// if there are new messages and no new alerts show the messages tab
|
2014-09-09 14:41:59 +00:00
|
|
|
this._activeTab = this.tabs[1];
|
2014-09-05 23:42:13 +00:00
|
|
|
} else {
|
|
|
|
// otherwise show the alerts tab
|
2014-09-09 14:41:59 +00:00
|
|
|
this._activeTab = this.tabs[0];
|
2014-09-05 23:42:13 +00:00
|
|
|
}
|
2014-08-06 00:24:34 +00:00
|
|
|
|
2014-08-06 19:29:17 +00:00
|
|
|
$overlay.prepend( this._getTitleElement() );
|
|
|
|
$overlay.append( this._getFooterElement() );
|
2014-09-09 17:20:23 +00:00
|
|
|
// Show the active tab.
|
|
|
|
this._showTabList( this._activeTab );
|
2015-04-29 12:08:30 +00:00
|
|
|
|
|
|
|
this._updateSeenTime();
|
|
|
|
},
|
|
|
|
|
2015-05-04 19:24:37 +00:00
|
|
|
_updateSeenTime: function () {
|
2015-04-29 12:08:30 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// update 'echo-seen-time'
|
|
|
|
return this.api.post( {
|
|
|
|
action: 'echomarkseen',
|
|
|
|
token: mw.user.tokens.get( 'editToken' )
|
|
|
|
} ).done( function ( data ) {
|
|
|
|
// update echo-seen-time value in JS (where it wouldn't
|
|
|
|
// otherwise propagate until page reload)
|
|
|
|
mw.user.options.set( 'echo-seen-time', data.query.echomarkseen.timestamp );
|
|
|
|
|
|
|
|
self.updateBadgeColor();
|
|
|
|
} );
|
2012-06-01 10:57:09 +00:00
|
|
|
}
|
2012-08-01 19:53:05 +00:00
|
|
|
};
|
2014-08-06 00:24:34 +00:00
|
|
|
|
|
|
|
mw.echo.overlay = {
|
|
|
|
/**
|
|
|
|
* @var integer the maximum number of notifications to show in the overlay
|
|
|
|
*/
|
2014-08-14 00:01:35 +00:00
|
|
|
notificationLimit: 25,
|
2014-08-06 00:24:34 +00:00
|
|
|
/**
|
|
|
|
* @var mw.Api
|
|
|
|
*/
|
|
|
|
api: new mw.Api( { ajax: { cache: false } } ),
|
|
|
|
/**
|
2014-08-12 15:09:09 +00:00
|
|
|
* Create an Echo overlay
|
|
|
|
* @return jQuery.Deferred with new EchoOverlay passed in callback
|
2014-08-06 00:24:34 +00:00
|
|
|
*/
|
2015-05-04 19:24:37 +00:00
|
|
|
getNewOverlay: function () {
|
2014-08-06 00:24:34 +00:00
|
|
|
var apiData = {
|
2014-08-14 00:01:35 +00:00
|
|
|
action: 'query',
|
|
|
|
meta: 'notifications',
|
|
|
|
notsections: 'alert|message',
|
2014-08-06 19:29:17 +00:00
|
|
|
notgroupbysection: 1,
|
2014-08-14 00:01:35 +00:00
|
|
|
notmessageunreadfirst: 1,
|
|
|
|
notformat: 'flyout',
|
|
|
|
notlimit: this.notificationLimit,
|
2014-08-12 15:09:09 +00:00
|
|
|
notprop: 'index|list|count',
|
2015-06-08 00:30:00 +00:00
|
|
|
uselang: 'user'
|
2014-08-06 00:24:34 +00:00
|
|
|
};
|
|
|
|
|
2014-08-12 15:09:09 +00:00
|
|
|
return this.api.get( apiData ).then( function ( result ) {
|
|
|
|
return new EchoOverlay( result.query.notifications );
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Builds an overlay element
|
|
|
|
* @method
|
|
|
|
* @param callback a callback which passes the newly created overlay as a parameter
|
|
|
|
*/
|
2015-05-04 19:24:37 +00:00
|
|
|
buildOverlay: function ( callback ) {
|
|
|
|
this.getNewOverlay().done( function ( overlay ) {
|
2014-08-06 00:24:34 +00:00
|
|
|
callback( overlay.$el );
|
|
|
|
} ).fail( function () {
|
|
|
|
window.location.href = $( '#pt-notifications a' ).attr( 'href' );
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
removeOverlay: function () {
|
2014-08-07 21:03:32 +00:00
|
|
|
$( '.mw-echo-overlay' ).fadeOut( 'fast',
|
2014-08-06 00:24:34 +00:00
|
|
|
function () {
|
|
|
|
$( this ).remove();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2012-08-30 16:04:39 +00:00
|
|
|
} )( jQuery, mediaWiki );
|