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
|
2016-03-25 18:52:59 +00:00
|
|
|
* @param {mw.echo.dm.NotificationItem} model Notification item model
|
2015-08-13 00:54:16 +00:00
|
|
|
* @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 ) {
|
2016-02-27 01:19:06 +00:00
|
|
|
var i, secondaryUrls, urlObj, linkButton, $icon, isInsideMenu, echoMoment,
|
2015-10-16 23:18:25 +00:00
|
|
|
$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
|
|
|
|
2016-02-22 20:32:05 +00:00
|
|
|
this.$content = $( '<div>' ).addClass( 'mw-echo-ui-notificationItemWidget-content' );
|
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
|
|
|
|
2016-04-15 20:30:14 +00:00
|
|
|
// Mark as read
|
2015-08-13 00:54:16 +00:00
|
|
|
this.markAsReadButton = new OO.ui.ButtonWidget( {
|
|
|
|
icon: 'close',
|
|
|
|
framed: false,
|
2016-04-15 20:30:14 +00:00
|
|
|
title: mw.msg( 'echo-notification-markasread-tooltip' ),
|
2015-11-12 19:37:56 +00:00
|
|
|
classes: [ 'mw-echo-ui-notificationItemWidget-markAsReadButton' ]
|
2015-08-13 00:54:16 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
this.markReadWhenSeen = !!config.markReadWhenSeen;
|
|
|
|
|
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() )
|
2016-02-24 05:09:57 +00:00
|
|
|
// dir=auto has a similar effect to wrapping the content in <bdi>, but
|
|
|
|
// makes text-overflow: ellipsis; behave less strangely
|
|
|
|
.attr( 'dir', 'auto' )
|
2015-10-16 23:18:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2016-04-15 20:30:14 +00:00
|
|
|
title: mw.msg( 'echo-notification-more-options-tooltip' ),
|
2015-10-16 23:18:25 +00:00
|
|
|
classes: [ 'mw-echo-ui-notificationItemWidget-content-actions-menu' ]
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Timestamp
|
2016-02-27 01:19:06 +00:00
|
|
|
// We want to use extra-short timestamp strings; we change the locale
|
|
|
|
// to our echo-defined one and use that instead of the normal moment locale
|
|
|
|
echoMoment = moment.utc( this.model.getTimestamp(), 'YYYYMMDDHHmmss' );
|
|
|
|
echoMoment.locale( 'echo-shortRelativeTime' );
|
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
this.timestampWidget = new OO.ui.LabelWidget( {
|
|
|
|
classes: [ 'mw-echo-ui-notificationItemWidget-content-actions-timestamp' ],
|
2016-02-27 01:19:06 +00:00
|
|
|
// Get the time 'fromNow' without the suffix 'ago'
|
|
|
|
label: echoMoment.fromNow( true )
|
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 ];
|
|
|
|
|
2016-01-30 00:27:47 +00:00
|
|
|
isInsideMenu = !this.bundle && urlObj.prioritized !== undefined;
|
|
|
|
|
|
|
|
linkButton = new mw.echo.ui.MenuItemWidget( {
|
2016-01-15 02:00:01 +00:00
|
|
|
icon: urlObj.icon || 'next',
|
2015-10-16 23:18:25 +00:00
|
|
|
label: urlObj.label,
|
2016-02-17 17:27:49 +00:00
|
|
|
tooltip: urlObj.tooltip,
|
2016-01-30 00:27:47 +00:00
|
|
|
description: urlObj.description,
|
|
|
|
url: urlObj.url,
|
|
|
|
prioritized: isInsideMenu
|
2015-10-16 23:18:25 +00:00
|
|
|
} );
|
|
|
|
|
2016-01-30 00:27:47 +00:00
|
|
|
if ( isInsideMenu ) {
|
2015-10-16 23:18:25 +00:00
|
|
|
this.actionsButtonSelectWidget.addItems( [ linkButton ] );
|
|
|
|
} else {
|
|
|
|
this.menuPopupButtonWidget.getMenu().addItems( [ linkButton ] );
|
|
|
|
}
|
|
|
|
}
|
2016-01-21 19:29:21 +00:00
|
|
|
// Add a "mark as read" secondary action
|
2016-03-04 22:44:22 +00:00
|
|
|
this.toggleReadSecondaryButton = new mw.echo.ui.MenuItemWidget( {
|
|
|
|
data: 'toggleRead',
|
2016-02-26 23:16:47 +00:00
|
|
|
prioritized: false,
|
2016-01-21 19:29:21 +00:00
|
|
|
classes: [ 'mw-echo-ui-notificationItemWidget-content-actions-button' ]
|
|
|
|
} );
|
2016-03-04 22:44:22 +00:00
|
|
|
this.menuPopupButtonWidget.getMenu().addItems( [ this.toggleReadSecondaryButton ] );
|
|
|
|
|
2016-01-21 19:29:21 +00:00
|
|
|
// Toggle 'mark as read' functionality
|
|
|
|
this.toggleMarkAsReadButtons( !this.markReadWhenSeen && !this.model.isRead() );
|
2015-10-16 23:18:25 +00:00
|
|
|
|
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
|
2016-02-22 20:32:05 +00:00
|
|
|
this.$content.append(
|
2015-12-22 23:16:32 +00:00
|
|
|
$icon,
|
|
|
|
$message,
|
|
|
|
this.$actions,
|
|
|
|
this.markAsReadButton.$element
|
|
|
|
);
|
2016-02-22 20:32:05 +00:00
|
|
|
this.$element.append( this.$content );
|
2015-12-22 23:16:32 +00:00
|
|
|
} else {
|
2016-02-22 20:32:05 +00:00
|
|
|
this.$content.append(
|
2015-12-22 23:16:32 +00:00
|
|
|
this.markAsReadButton.$element,
|
|
|
|
$message,
|
2016-02-23 01:44:48 +00:00
|
|
|
$( '<div>' )
|
|
|
|
.addClass( 'mw-echo-ui-notificationItemWidget-content-table' )
|
|
|
|
.append( this.$actions )
|
2015-12-22 23:16:32 +00:00
|
|
|
);
|
2016-02-22 20:32:05 +00:00
|
|
|
this.$element.append( $icon, this.$content );
|
2015-12-22 23:16:32 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
);
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-01-21 19:29:21 +00:00
|
|
|
// Events
|
|
|
|
this.markAsReadButton.connect( this, { click: 'onMarkAsReadButtonClick' } );
|
|
|
|
this.menuPopupButtonWidget.getMenu().connect( this, { choose: 'onPopupButtonWidgetChoose' } );
|
|
|
|
this.model.connect( this, {
|
|
|
|
seen: 'toggleSeen',
|
|
|
|
read: 'toggleRead'
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Initialize state
|
|
|
|
this.toggleRead( this.model.isRead() );
|
|
|
|
this.toggleSeen( this.model.isSeen() );
|
|
|
|
|
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
|
|
|
|
2016-01-21 19:29:21 +00:00
|
|
|
/**
|
|
|
|
* Respond to selecting an item from the popup button widget
|
|
|
|
*/
|
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.onPopupButtonWidgetChoose = function ( item ) {
|
|
|
|
var action = item && item.getData();
|
|
|
|
|
2016-03-04 22:44:22 +00:00
|
|
|
if ( action === 'toggleRead' ) {
|
|
|
|
this.model.toggleRead();
|
2016-01-21 19:29:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-03-04 22:44:22 +00:00
|
|
|
* Toggle the function of the 'mark as read' secondary button from 'mark as read' to
|
|
|
|
* 'mark as unread' and update the visibility of the primary 'mark as read' X button.
|
|
|
|
*
|
2016-01-21 19:29:21 +00:00
|
|
|
*
|
|
|
|
* @param {boolean} [show] Show the 'mark as read' buttons
|
2016-03-04 22:44:22 +00:00
|
|
|
* - "false" means that the item is marked as read, whereby we show the user 'mark unread'
|
|
|
|
* and hide the primary 'x' button
|
|
|
|
* - "true" means that the item is marked as unread and we show the user 'mark as read'
|
|
|
|
* primary and secondary buttons.
|
2016-01-21 19:29:21 +00:00
|
|
|
*/
|
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.toggleMarkAsReadButtons = function ( show ) {
|
|
|
|
show = show !== undefined ? show : !this.model.isRead();
|
|
|
|
|
|
|
|
this.markAsReadButton.toggle( show );
|
|
|
|
|
|
|
|
if ( show ) {
|
2016-03-04 22:44:22 +00:00
|
|
|
// Mark read
|
|
|
|
this.toggleReadSecondaryButton.setLabel( mw.msg( 'echo-notification-markasread' ) );
|
|
|
|
this.toggleReadSecondaryButton.setIcon( 'check' );
|
2016-01-21 19:29:21 +00:00
|
|
|
} else {
|
2016-03-04 22:44:22 +00:00
|
|
|
// Mark unread
|
|
|
|
this.toggleReadSecondaryButton.setLabel( mw.msg( 'echo-notification-markasunread' ) );
|
|
|
|
this.toggleReadSecondaryButton.setIcon( 'sun' );
|
2016-01-21 19:29:21 +00:00
|
|
|
}
|
|
|
|
this.menuPopupButtonWidget.toggle( !this.menuPopupButtonWidget.getMenu().isEmpty() );
|
|
|
|
};
|
|
|
|
|
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 );
|
2016-01-21 19:29:21 +00:00
|
|
|
this.toggleMarkAsReadButtons( !this.read );
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 );
|