2015-08-13 00:54:16 +00:00
|
|
|
( function ( mw, $ ) {
|
2015-10-16 23:18:25 +00:00
|
|
|
/*global moment:false */
|
2015-08-13 00:54:16 +00:00
|
|
|
/**
|
|
|
|
* Notification option widget for echo popup.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends OO.ui.OptionWidget
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} [config] Configuration object
|
|
|
|
* @cfg {boolean} [markReadWhenSeen=false] This option is marked as read when it is viewed
|
2015-11-21 01:54:12 +00:00
|
|
|
* @cfg {jQuery} [$overlay] A jQuery element functioning as an overlay
|
|
|
|
* for popups.
|
2015-10-16 23:18:25 +00:00
|
|
|
* @cfg {boolean} [bundle=false] This notification item is part of a bundle.
|
2015-08-13 00:54:16 +00:00
|
|
|
*/
|
2015-11-12 19:37:56 +00:00
|
|
|
mw.echo.ui.NotificationItemWidget = function MwEchoUiNotificationItemWidget( model, config ) {
|
2015-10-16 23:18:25 +00:00
|
|
|
var i, secondaryUrls, urlObj, linkButton, $icon,
|
|
|
|
$content = $( '<div>' ).addClass( 'mw-echo-ui-notificationItemWidget-content' ),
|
|
|
|
$message = $( '<div>' ).addClass( 'mw-echo-ui-notificationItemWidget-content-message' ),
|
|
|
|
widget = this;
|
|
|
|
|
2015-08-13 00:54:16 +00:00
|
|
|
config = config || {};
|
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
// Parent constructor
|
|
|
|
mw.echo.ui.NotificationItemWidget.parent.call( this, $.extend( { data: model.getId() }, config ) );
|
|
|
|
|
2015-08-13 00:54:16 +00:00
|
|
|
this.model = model;
|
2015-11-21 01:54:12 +00:00
|
|
|
this.$overlay = config.$overlay || this.$element;
|
2015-10-16 23:18:25 +00:00
|
|
|
this.bundle = !!config.bundle;
|
2015-08-13 00:54:16 +00:00
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
this.$actions = $( '<div>' )
|
|
|
|
.addClass( 'mw-echo-ui-notificationItemWidget-content-actions' );
|
2015-08-13 00:54:16 +00:00
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
// Mark unread
|
2015-08-13 00:54:16 +00:00
|
|
|
this.markAsReadButton = new OO.ui.ButtonWidget( {
|
|
|
|
icon: 'close',
|
|
|
|
framed: false,
|
2015-11-12 19:37:56 +00:00
|
|
|
classes: [ 'mw-echo-ui-notificationItemWidget-markAsReadButton' ]
|
2015-08-13 00:54:16 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
this.toggleRead( this.model.isRead() );
|
|
|
|
this.toggleSeen( this.model.isSeen() );
|
|
|
|
|
|
|
|
this.markReadWhenSeen = !!config.markReadWhenSeen;
|
2015-09-30 18:36:41 +00:00
|
|
|
this.markAsReadButton.toggle( !this.markReadWhenSeen && !this.model.isRead() );
|
2015-08-13 00:54:16 +00:00
|
|
|
|
|
|
|
// Events
|
|
|
|
this.markAsReadButton.connect( this, { click: 'onMarkAsReadButtonClick' } );
|
|
|
|
this.model.connect( this, {
|
|
|
|
seen: 'toggleSeen',
|
|
|
|
read: 'toggleRead'
|
|
|
|
} );
|
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
// Icon
|
|
|
|
if ( this.model.getIconURL() ) {
|
|
|
|
$icon = $( '<div>' )
|
|
|
|
.addClass( 'mw-echo-ui-notificationItemWidget-icon' )
|
|
|
|
.append( $( '<img>' ).attr( 'src', this.model.getIconURL() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Content
|
|
|
|
$message.append(
|
|
|
|
$( '<div>' )
|
|
|
|
.addClass( 'mw-echo-ui-notificationItemWidget-content-message-header' )
|
|
|
|
.append( this.model.getContentHeader() )
|
|
|
|
);
|
|
|
|
if ( !this.bundle && this.model.getContentBody() ) {
|
|
|
|
$message.append(
|
|
|
|
$( '<div>' )
|
|
|
|
.addClass( 'mw-echo-ui-notificationItemWidget-content-message-body' )
|
|
|
|
.append( this.model.getContentBody() )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actions menu
|
|
|
|
this.actionsButtonSelectWidget = new OO.ui.ButtonSelectWidget( {
|
|
|
|
classes: [ 'mw-echo-ui-notificationItemWidget-content-actions-buttons' ]
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Popup menu
|
|
|
|
this.menuPopupButtonWidget = new mw.echo.ui.ActionMenuPopupWidget( {
|
|
|
|
framed: false,
|
|
|
|
icon: 'ellipsis',
|
|
|
|
$overlay: this.$overlay,
|
|
|
|
menuWidth: 200,
|
|
|
|
classes: [ 'mw-echo-ui-notificationItemWidget-content-actions-menu' ]
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Timestamp
|
|
|
|
this.timestampWidget = new OO.ui.LabelWidget( {
|
|
|
|
classes: [ 'mw-echo-ui-notificationItemWidget-content-actions-timestamp' ],
|
2015-12-22 16:07:30 +00:00
|
|
|
label: moment.utc( this.model.getTimestamp(), 'YYYYMMDDHHmmss' ).fromNow()
|
2015-10-16 23:18:25 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
// Build the actions line
|
2015-12-22 23:16:32 +00:00
|
|
|
if ( this.bundle ) {
|
|
|
|
// In a bundled item, the timestamp should go before the menu
|
|
|
|
this.$actions.append(
|
|
|
|
$( '<div>' )
|
|
|
|
// We are wrapping the actions in a 'row' div so that the
|
|
|
|
// internal pieces are also a table layout
|
|
|
|
.addClass( 'mw-echo-ui-notificationItemWidget-content-actions-row' )
|
|
|
|
.append(
|
|
|
|
this.actionsButtonSelectWidget.$element,
|
|
|
|
this.timestampWidget.$element,
|
|
|
|
this.menuPopupButtonWidget.$element
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.$actions.append(
|
|
|
|
this.actionsButtonSelectWidget.$element,
|
|
|
|
this.menuPopupButtonWidget.$element,
|
|
|
|
this.timestampWidget.$element
|
|
|
|
);
|
|
|
|
}
|
2015-10-16 23:18:25 +00:00
|
|
|
|
|
|
|
// Actions
|
|
|
|
secondaryUrls = this.model.getSecondaryUrls();
|
|
|
|
for ( i = 0; i < secondaryUrls.length; i++ ) {
|
|
|
|
urlObj = secondaryUrls[ i ];
|
|
|
|
|
|
|
|
linkButton = new OO.ui.ButtonOptionWidget( {
|
2016-01-15 02:00:01 +00:00
|
|
|
icon: urlObj.icon || 'next',
|
2015-10-16 23:18:25 +00:00
|
|
|
framed: false,
|
|
|
|
label: urlObj.label,
|
|
|
|
classes: [ 'mw-echo-ui-notificationItemWidget-content-actions-button' ]
|
|
|
|
} );
|
|
|
|
if ( urlObj.url ) {
|
|
|
|
linkButton.$element.find( 'a.oo-ui-buttonElement-button' )
|
|
|
|
// HACK: We need to use ButtonOptionWidgets because both SelectWidgets expect an OptionWidget
|
|
|
|
// However, the optionwidgets do not support href for buttons, because they listen to 'choose'
|
|
|
|
// and 'select' events and do their own thing.
|
|
|
|
// We could solve this by reimplementing/extending the ActionMenuPopupWidget *and* the SelectWidget
|
|
|
|
// or we can do the simpler thing, which is to wrap our buttons with a link.
|
|
|
|
// Since we do the latter in the notifications anyways (see below) this seemed to be the
|
|
|
|
// best course of action.
|
|
|
|
.attr( 'href', urlObj.url );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !this.bundle && urlObj.prioritized !== undefined ) {
|
|
|
|
this.actionsButtonSelectWidget.addItems( [ linkButton ] );
|
|
|
|
} else {
|
|
|
|
this.menuPopupButtonWidget.getMenu().addItems( [ linkButton ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.menuPopupButtonWidget.toggle( !this.menuPopupButtonWidget.getMenu().isEmpty() );
|
|
|
|
|
2015-12-22 23:16:32 +00:00
|
|
|
if ( this.bundle ) {
|
|
|
|
// In a bundle, we have table layout, so the icon is
|
|
|
|
// inserted into the content, and the 'mark as read'
|
|
|
|
// button is not floating, and should be at the end
|
|
|
|
$content.append(
|
|
|
|
$icon,
|
|
|
|
$message,
|
|
|
|
this.$actions,
|
|
|
|
this.markAsReadButton.$element
|
|
|
|
);
|
|
|
|
this.$element.append( $content );
|
|
|
|
} else {
|
|
|
|
$content.append(
|
|
|
|
this.markAsReadButton.$element,
|
|
|
|
$message,
|
|
|
|
this.$actions
|
|
|
|
);
|
|
|
|
this.$element.append( $icon, $content );
|
|
|
|
}
|
|
|
|
|
2015-08-13 00:54:16 +00:00
|
|
|
this.$element
|
2015-11-12 19:37:56 +00:00
|
|
|
.addClass( 'mw-echo-ui-notificationItemWidget mw-echo-ui-notificationItemWidget-' + this.model.getType() )
|
2015-10-16 23:18:25 +00:00
|
|
|
.toggleClass( 'mw-echo-ui-notificationItemWidget-initiallyUnseen', !this.model.isSeen() && !this.bundle )
|
2015-12-22 23:16:32 +00:00
|
|
|
.toggleClass( 'mw-echo-ui-notificationItemWidget-bundle', this.bundle );
|
2015-08-13 00:54:16 +00:00
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
if ( this.model.getPrimaryUrl() ) {
|
|
|
|
this.$element.contents()
|
|
|
|
.wrapAll(
|
|
|
|
// HACK: Wrap the entire item with a link that takes
|
|
|
|
// the user to the primary url. This is not perfect,
|
|
|
|
// but it makes the behavior native to the browser rather
|
|
|
|
// than us listening to click events and opening new
|
|
|
|
// windows.
|
|
|
|
$( '<a>' )
|
|
|
|
.addClass( 'mw-echo-ui-notificationItemWidget-linkWrapper' )
|
|
|
|
.attr( 'href', this.model.getPrimaryUrl() )
|
|
|
|
.on( 'click', function () {
|
|
|
|
// Log notification click
|
|
|
|
|
|
|
|
// TODO: In order to properly log a click of an item that
|
|
|
|
// is part of a bundled cross-wiki notification, we will
|
|
|
|
// need to add 'source' to the logging schema. Otherwise,
|
|
|
|
// the logger will log item ID as if it is local, which
|
|
|
|
// is wrong.
|
|
|
|
mw.echo.logger.logInteraction(
|
|
|
|
mw.echo.Logger.static.actions.notificationClick,
|
|
|
|
mw.echo.Logger.static.context.popup,
|
|
|
|
widget.getModel().getId(),
|
2015-12-23 18:33:59 +00:00
|
|
|
widget.getModel().getCategory(),
|
|
|
|
false,
|
|
|
|
// Source of this notification if it is cross-wiki
|
|
|
|
widget.bundle ? widget.getModel().getSource() : ''
|
2015-10-16 23:18:25 +00:00
|
|
|
);
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK: We have to remove the built-in label. When this
|
|
|
|
// widget is switched to a standalone widget rather than
|
|
|
|
// an OptionWidget we can get rid of this
|
|
|
|
this.$label.detach();
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Initialization */
|
|
|
|
|
2015-11-12 19:37:56 +00:00
|
|
|
OO.inheritClass( mw.echo.ui.NotificationItemWidget, OO.ui.OptionWidget );
|
2015-08-13 00:54:16 +00:00
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
/* Static properties */
|
|
|
|
|
|
|
|
mw.echo.ui.NotificationItemWidget.static.pressable = false;
|
|
|
|
mw.echo.ui.NotificationItemWidget.static.selectable = false;
|
|
|
|
|
2015-08-13 00:54:16 +00:00
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event markAsRead
|
|
|
|
*
|
|
|
|
* Mark this notification as read
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to mark as read button click
|
|
|
|
*/
|
2015-11-12 19:37:56 +00:00
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.onMarkAsReadButtonClick = function () {
|
2015-08-13 00:54:16 +00:00
|
|
|
this.model.toggleRead( true );
|
|
|
|
};
|
2015-09-23 00:15:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the status of the notification without touching its user-controlled status.
|
|
|
|
* For one, remove 'initiallyUnseen' which exists only for the animation to work.
|
|
|
|
* This is called when new notifications are added to the parent widget, having to
|
|
|
|
* reset the 'unseen' status from the old ones.
|
|
|
|
*/
|
2015-11-12 19:37:56 +00:00
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.reset = function () {
|
|
|
|
this.$element.removeClass( 'mw-echo-ui-notificationItemWidget-initiallyUnseen' );
|
2015-09-23 00:15:28 +00:00
|
|
|
};
|
|
|
|
|
2015-08-13 00:54:16 +00:00
|
|
|
/**
|
|
|
|
* Toggle the read state of the widget
|
|
|
|
*
|
|
|
|
* @param {boolean} [read] The current read state. If not given, the state will
|
|
|
|
* become the opposite of its current state.
|
|
|
|
*/
|
2015-11-12 19:37:56 +00:00
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.toggleRead = function ( read ) {
|
2015-08-13 00:54:16 +00:00
|
|
|
this.read = read !== undefined ? read : !this.read;
|
|
|
|
|
2015-11-12 19:37:56 +00:00
|
|
|
this.$element.toggleClass( 'mw-echo-ui-notificationItemWidget-unread', !this.read );
|
2015-08-13 00:54:16 +00:00
|
|
|
this.markAsReadButton.toggle( !this.read );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle the seen state of the widget
|
|
|
|
*
|
|
|
|
* @param {boolean} [seen] The current seen state. If not given, the state will
|
|
|
|
* become the opposite of its current state.
|
|
|
|
*/
|
2015-11-12 19:37:56 +00:00
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.toggleSeen = function ( seen ) {
|
2015-08-13 00:54:16 +00:00
|
|
|
this.seen = seen !== undefined ? seen : !this.seen;
|
|
|
|
|
|
|
|
this.$element
|
2015-11-12 19:37:56 +00:00
|
|
|
.toggleClass( 'mw-echo-ui-notificationItemWidget-unseen', !this.seen );
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the notification link
|
|
|
|
*
|
|
|
|
* @return {string} Notification link
|
|
|
|
*/
|
2015-11-12 19:37:56 +00:00
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.getModel = function () {
|
2015-08-13 00:54:16 +00:00
|
|
|
return this.model;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the notification link
|
|
|
|
*
|
|
|
|
* @return {string} Notification link
|
|
|
|
*/
|
2015-11-12 19:37:56 +00:00
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.getPrimaryUrl = function () {
|
2015-09-02 23:56:51 +00:00
|
|
|
return this.model.getPrimaryUrl();
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect events when widget is destroyed.
|
|
|
|
*/
|
2015-11-12 19:37:56 +00:00
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.destroy = function () {
|
2015-08-13 00:54:16 +00:00
|
|
|
this.model.disconnect( this );
|
|
|
|
};
|
|
|
|
|
|
|
|
} )( mediaWiki, jQuery );
|