User:Mooeypoo/NotificationsTest.js

/** * This is a test gadget to update notification count without loading all of * Echo's robust scripts. This can be adapted to be done periodically or used * as a basis of another widget. */ $( function {	var echoApi = null,		currentCount = 0;

// Load using existing Echo message for the badge + Echo's API processors mw.loader.using( [ 'mediawiki.api.messages', 'mediawiki.jqueryMsg', 'ext.echo.api' ] ) .then( function {			// This loads the required i18n message (see https://www.mediawiki.org/wiki/Manual:Messages_API)			return new mw.Api.loadMessagesIfMissing( [ 'echo-badge-count' ] );		} ) .then( function {			echoApi = new mw.echo.api.EchoApi;			// Use the EchoApi to fetch unread count for alert			echoApi.fetchUnreadCount( 'local', 'alert' )				.then( function ( alertCount ) { // Update the badge updateBadgeCount( 'alert', alertCount ); // For debugging/testing purposes - output to the console console.log( 'updated alert badge', alertCount ) } );

// Use the EchoApi to fetch unread count for message echoApi.fetchUnreadCount( 'local', 'message' ) .then( function ( msgCount ) {					// Update the badge					updateBadgeCount( 'message', msgCount );

// For debugging/testing purposes - output to the console console.log( 'updated message badge', msgCount ) } );		} );

function updateBadgeCount( type, count ) { var $badge = $( '#pt-notifications-' + ( type === 'alert' ? 'alert' : 'notice' ) + ' a' ); label = mw.msg( 'echo-badge-count', mw.language.convertNumber( count ) );

if ( !$badge.length ) { return; }

$badge .toggleClass( 'mw-echo-notifications-badge-all-read', !count ) .toggleClass( 'mw-echo-notifications-badge-long-label', label.length > 2 ) .attr( 'data-counter-num', count ) .attr( 'data-counter-text', label );

if ( currentCount !== count ) { currentCount = count;

// Fire badge count change hook mw.hook( 'ext.echo.badge.countChange' ).fire( type, currentCount, label ); }	} } );