/*global window:false */ ( function ( $, mw ) { 'use strict'; var useLang = mw.config.get( 'wgUserLanguage' ); function EchoOverlay( apiResultNotifications ) { this.api = mw.echo.overlay.api; // set internal properties this.tabs = []; this._buildOverlay( apiResultNotifications ); } function EchoOverlayTab( options, notifications ) { this.api = mw.echo.overlay.api; this.markOnView = options.markOnView; this.markAsReadCallback = options.markAsReadCallback; this.name = options.name; this.unread = []; this._totalUnread = notifications[this.name].rawcount; this._setLastUnreadNotificationTime( notifications ); this._buildList( notifications[this.name] ); } EchoOverlayTab.prototype = { /* @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 */ getUnreadIds: function () { return this.unread; }, /** * Get a count the number of all unread notifications of this type * @method * @return {integer} */ getNumberUnread: function () { return this._totalUnread; }, /** * 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 */ getLastUnreadNotificationTime: function () { return this._lastUnreadNotificationTime || false; }, /** * Set the timestamp of the last unread notification * @method * @param {object} notifications List of notifications */ _setLastUnreadNotificationTime: function ( notifications ) { 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; } } ); }, /** * Mark all existing notifications as read * @method * @param integer id of a notification to mark as read * @return jQuery.Deferred */ markAsRead: function ( id ) { var data, self = this; // only need to mark as read if there is unread item if ( this.unread.length ) { data = { action: 'echomarkread', uselang: useLang }; if ( id ) { // If id is given mark that as read otherwise use all unread messages data.list = id; } else { data.sections = this.name; } return this.api.postWithToken( 'edit', data ).then( function ( result ) { return result.query.echomarkread; } ).done( function ( result ) { // reset internal state of unread messages if ( id ) { if ( self.unread.indexOf( id ) > -1 ) { self.unread.splice( self.unread.indexOf( id ), 1 ); } } else { self.unread = []; } // update the count self._totalUnread = result[self.name].rawcount; self.markAsReadCallback( result, id ); } ); } else { return new $.Deferred(); } }, /** * Builds an Echo notifications list * @method * @param {object} notifications as returned by the api of notification items * @return jQuery element */ _buildList: function ( notifications ) { var self = this, $container = $( '