/*global window:false */ ( function ( $, mw ) { 'use strict'; // backwards compatibility <= MW 1.21 var getUrl = mw.util.getUrl || mw.util.wikiGetlink, 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._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 * @param integer id of a notification to mark as read * @return integer */ getNumberUnread: function() { return this._totalUnread; }, /** * Mark all existing notifications as read * @method * @param integer id of a notification to mark as read * @return jQuery.Deferred */ markAsRead: function( id ) { var self = this, data; // only need to mark as read if there is unread item if ( this.unread.length ) { data = { action: 'echomarkread', token: mw.user.tokens.get( 'editToken' ), 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.post( 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 string tabName the tab * @param object notifications as returned by the api of notification items * @return jQuery element */ _buildList: function( notifications ) { var self = this, $container = $( '