mediawiki-extensions-Echo/modules/ooui/mw.echo.ui.NotificationsWidget.js

154 lines
3.9 KiB
JavaScript
Raw Normal View History

Split alerts and messages in Echo Split the notifications into 'alert' and 'message' badget with two different flyouts. Also clean up styling and module behavior. ** Depends on ooui change Id4bbe14ba0bf6c for footers in popups. ** Depends on ooui change Ie93e4d6ed5637c for fixing a bug in inverted icons. ** MobileFrontend must also be updated to support the new modules in this patch I168f485d6e54cb4067 In this change: * Split notifcations into alert and messages and display those in two different badges. * Create two separate flyout/popups for each category with their notifications. * Create a view-model to control notification state and emit events for both the popup and the badge to intercept and react to. * Clean up module load and distribution: * Create an ext.echo.ui module for javascript-ui support and ooui widgets. * Create an ext.echo.nojs module that unifies all base classes that are needed for both nojs and js support, that the js version builds upon. * Create a separate ext.echo.logger module as a singleton that can be called to perform all logging. * Clean up style uses * Move the special page LESS file into nojs module so all styles load properly even in nojs mode. * Transfer some of the styling from JS to LESS for consistency. * Make the 'read more' button load already with the styles it needs to look like a button, since its behavior is similar in nojs and js vesions, but before its classes were applied only by the js, making it inconsistent and also making its appearance 'jump' from a link to a button. * Delete and clean up all old and unused files. * Moved 'Help.png' icon from modules/overlay to modules/icons for later use. Bug: T108190 Change-Id: I55f440ed9f64c46817f620328a6bb522d44c9ca9
2015-08-13 00:54:16 +00:00
( function ( mw, $ ) {
/**
* Notification widget for echo popup.
*
* @class
* @extends OO.ui.Widget
*
* @constructor
* @param {mw.echo.dm.NotificationsModel} model Notifications view model
* @param {Object} [config] Configuration object
* @cfg {boolean} [markReadWhenSeen=false] State whether the notifications are all
* marked as read when they are seen.
*/
mw.echo.ui.NotificationsWidget = function MwEchoUiNotificationsWidget( model, config ) {
config = config || {};
this.model = model;
this.markReadWhenSeen = !!config.markReadWhenSeen;
// Parent constructor
mw.echo.ui.NotificationsWidget.parent.call( this, config );
// Dummy 'loading' option widget
this.loadingOptionWidget = new OO.ui.OptionWidget( {
data: null,
classes: [ 'mw-echo-ui-notificationsWidget-loadingOption' ]
} );
this.addItems( [ this.loadingOptionWidget ] );
// Events
this.model.connect( this, {
add: 'onModelNotificationAdd',
remove: 'onModelNotificationRemove',
clear: 'onModelNotificationClear'
} );
this.connect( this, {
choose: 'onNotificationChoose'
} );
this.$element
.addClass( 'mw-echo-ui-notificationsWidget' );
};
/* Initialization */
OO.inheritClass( mw.echo.ui.NotificationsWidget, OO.ui.SelectWidget );
/* Methods */
/**
* Respond to model add event
*
* @param {mw.echo.dm.NotificationItem[]} Added notification items
*/
mw.echo.ui.NotificationsWidget.prototype.onModelNotificationAdd = function ( notificationItems, index ) {
var i, len, widget,
$elements = $(),
optionWidgets = [];
for ( i = 0, len = notificationItems.length; i < len; i++ ) {
widget = new mw.echo.ui.NotificationOptionWidget(
notificationItems[i],
{
markReadWhenSeen: this.markReadWhenSeen
}
);
optionWidgets.push( widget );
// Collect the elements for the hook firing
$elements = $elements.add( widget.$element );
}
// Fire hook for gadgets to update the option list
mw.hook( 'ext.echo.overlay.beforeShowingOverlay' ).fire( $elements );
// Remove dummy option
this.removeItems( [ this.loadingOptionWidget ] );
this.addItems( optionWidgets, index );
};
/**
* Respond to model add event
*
* @param {mw.echo.dm.NotificationItem[]} Removed notification items
*/
mw.echo.ui.NotificationsWidget.prototype.onModelNotificationClear = function () {
var i, len,
items = this.getItems();
// Destroy all the widgets and their events
for ( i = 0, len = items.length; i < len; i++ ) {
if ( typeof items[i].destroy === 'function' ) {
// Destroy if destroyable
items[i].destroy();
}
}
this.clearItems();
// Add dummy option
this.addItems( [ this.loadingOptionWidget ] );
};
/**
* Respond to model add event
*
* @param {mw.echo.dm.NotificationItem[]} Removed notification items
*/
mw.echo.ui.NotificationsWidget.prototype.onModelNotificationRemove = function ( notificationItems ) {
var i, len, widget, items,
removalWidgets = [];
for ( i = 0, len = notificationItems.length; i < len; i++ ) {
widget = this.getItemById( notificationItems[i].getId() );
if ( widget && typeof widget.destroy === 'function' ) {
// Destroy all widgets that can be destroyed
widget.destroy();
}
removalWidgets.push( widget );
}
this.removeItems( removalWidgets );
items = this.getItems();
if ( !items.length ) {
// Add dummy option
this.addItems( [ this.loadingOptionWidget ] );
}
};
/**
* Respond to choosing a notification option
*
* @param {mw.echo.ui.NotificationOptionWidget} item Notification option
*/
mw.echo.ui.NotificationsWidget.prototype.onNotificationChoose = function ( item ) {
var link;
if ( !item ) {
return;
}
link = item.getPrimaryLink();
if ( link ) {
// Log the clickthrough
mw.echo.logger.logInteraction( 'notification-link-click', item.getData(), this.type );
// Follow the link
window.location.href = link;
}
};
} )( mediaWiki, jQuery );