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
|
|
|
/**
|
2016-04-10 13:31:02 +00:00
|
|
|
* A base widget for displaying notification items.
|
2015-08-13 00:54:16 +00:00
|
|
|
*
|
|
|
|
* @class
|
2016-04-10 13:31:02 +00:00
|
|
|
* @extends OO.ui.Widget
|
2015-08-13 00:54:16 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2016-04-10 13:31:02 +00:00
|
|
|
* @param {mw.echo.Controller} controller Echo controller
|
2016-03-25 18:52:59 +00:00
|
|
|
* @param {mw.echo.dm.NotificationItem} model Notification item model
|
2016-04-10 13:31:02 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
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
|
|
|
*/
|
2016-04-10 13:31:02 +00:00
|
|
|
mw.echo.ui.NotificationItemWidget = function MwEchoUiNotificationItemWidget( controller, model, config ) {
|
2016-02-27 01:19:06 +00:00
|
|
|
var i, secondaryUrls, urlObj, linkButton, $icon, isInsideMenu, echoMoment,
|
2016-04-10 13:31:02 +00:00
|
|
|
$message = $( '<div>' ).addClass( 'mw-echo-ui-notificationItemWidget-content-message' );
|
2015-10-16 23:18:25 +00:00
|
|
|
|
2015-08-13 00:54:16 +00:00
|
|
|
config = config || {};
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
// Parent
|
2015-10-16 23:18:25 +00:00
|
|
|
mw.echo.ui.NotificationItemWidget.parent.call( this, $.extend( { data: model.getId() }, config ) );
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
this.controller = controller;
|
2015-08-13 00:54:16 +00:00
|
|
|
this.model = model;
|
2016-04-10 13:31:02 +00:00
|
|
|
|
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
|
|
|
} );
|
|
|
|
|
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-05-09 22:00:18 +00:00
|
|
|
prioritized: false
|
2016-01-21 19:29:21 +00:00
|
|
|
} );
|
2016-03-04 22:44:22 +00:00
|
|
|
this.menuPopupButtonWidget.getMenu().addItems( [ this.toggleReadSecondaryButton ] );
|
|
|
|
|
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
|
2016-04-10 13:31:02 +00:00
|
|
|
.addClass( 'mw-echo-ui-notificationItemWidget' )
|
2015-10-16 23:18:25 +00:00
|
|
|
.toggleClass( 'mw-echo-ui-notificationItemWidget-initiallyUnseen', !this.model.isSeen() && !this.bundle )
|
2016-05-31 01:08:25 +00:00
|
|
|
.toggleClass( 'mw-echo-ui-notificationItemWidget-bundled', this.bundle );
|
2015-08-13 00:54:16 +00:00
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
// Wrap the entire item with primary url
|
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() )
|
2016-04-10 13:31:02 +00:00
|
|
|
.on( 'click', this.onPrimaryLinkClick.bind( this ) )
|
2015-10-16 23:18:25 +00:00
|
|
|
);
|
|
|
|
}
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
OO.inheritClass( mw.echo.ui.NotificationItemWidget, OO.ui.Widget );
|
2015-08-13 00:54:16 +00:00
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Respond to primary link click.
|
|
|
|
* Override this in the descendents.
|
|
|
|
*
|
|
|
|
* @return {boolean} true
|
|
|
|
*/
|
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.onPrimaryLinkClick = function () {
|
|
|
|
return true;
|
|
|
|
};
|
2015-08-13 00:54:16 +00:00
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Get the notification link
|
|
|
|
*
|
|
|
|
* @return {string} Notification link
|
|
|
|
*/
|
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.getPrimaryUrl = function () {
|
|
|
|
return this.model.getPrimaryUrl();
|
|
|
|
};
|
2015-10-16 23:18:25 +00:00
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Get the item id
|
|
|
|
*
|
|
|
|
* @return {number} Notification id
|
|
|
|
*/
|
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.getTimestamp = function () {
|
|
|
|
return this.model.getTimestamp();
|
|
|
|
};
|
2015-10-16 23:18:25 +00:00
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Get the notification Id
|
|
|
|
*
|
|
|
|
* @return {number} Notification id
|
|
|
|
*/
|
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.getId = function () {
|
|
|
|
return this.model.getId();
|
|
|
|
};
|
2015-08-13 00:54:16 +00:00
|
|
|
|
|
|
|
/**
|
2016-04-10 13:31:02 +00:00
|
|
|
* Check whether this item is seen.
|
2015-08-13 00:54:16 +00:00
|
|
|
*
|
2016-04-10 13:31:02 +00:00
|
|
|
* @return {boolean} Item is seen
|
2015-08-13 00:54:16 +00:00
|
|
|
*/
|
2016-04-10 13:31:02 +00:00
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.isSeen = function () {
|
|
|
|
return this.model.isSeen();
|
|
|
|
};
|
2015-08-13 00:54:16 +00:00
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Check whether this item is read.
|
|
|
|
*
|
|
|
|
* @return {boolean} Item is read
|
|
|
|
*/
|
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.isRead = function () {
|
|
|
|
return this.model.isRead();
|
|
|
|
};
|
2015-08-13 00:54:16 +00:00
|
|
|
|
|
|
|
/**
|
2016-04-10 13:31:02 +00:00
|
|
|
* Check whether this item is foreign.
|
|
|
|
*
|
|
|
|
* @return {boolean} Item is foreign
|
2015-08-13 00:54:16 +00:00
|
|
|
*/
|
2016-04-10 13:31:02 +00:00
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.isForeign = function () {
|
|
|
|
return this.model.isForeign();
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
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' ) {
|
2016-04-10 13:31:02 +00:00
|
|
|
this.markRead( !this.model.isRead() );
|
2016-01-21 19:29:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Mark this notification as read
|
|
|
|
*
|
|
|
|
* @param {boolean} [isRead=true] Notification is marked as read
|
|
|
|
*/
|
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.markRead = function ( isRead ) {
|
|
|
|
isRead = isRead !== undefined ? isRead : true;
|
|
|
|
|
|
|
|
this.controller.markSingleItemRead( this.model.getId(), this.model.getSource(), this.model.isForeign(), !!isRead );
|
|
|
|
};
|
|
|
|
|
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-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
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-04-10 13:31:02 +00:00
|
|
|
* Get the model associated with this widget.
|
2015-08-13 00:54:16 +00:00
|
|
|
*/
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-04-10 13:31:02 +00:00
|
|
|
* Disconnect events when widget is destroyed.
|
2015-08-13 00:54:16 +00:00
|
|
|
*/
|
2016-04-10 13:31:02 +00:00
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.destroy = function () {
|
|
|
|
this.model.disconnect( this );
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-04-10 13:31:02 +00:00
|
|
|
* Remove the 'initiallyUnseen' class, which was only used for the
|
|
|
|
* unseen animation when the user has first seen it.
|
2015-08-13 00:54:16 +00:00
|
|
|
*/
|
2016-04-10 13:31:02 +00:00
|
|
|
mw.echo.ui.NotificationItemWidget.prototype.resetInitiallyUnseen = function () {
|
|
|
|
this.$element.removeClass( 'mw-echo-ui-notificationItemWidget-initiallyUnseen' );
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} )( mediaWiki, jQuery );
|