2018-11-12 13:56:38 +00:00
|
|
|
( function () {
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Cross-wiki notification item widget.
|
|
|
|
* This widget is expandable and displays groups of
|
|
|
|
* notification item lists by their sources.
|
|
|
|
*
|
|
|
|
* TODO: When we have local bundles (without groups of lists)
|
|
|
|
* we can separate the "expand" functionality and UI to another mixin
|
|
|
|
* so we can use it with both widgets.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends mw.echo.ui.NotificationItemWidget
|
|
|
|
* @mixins OO.ui.mixin.PendingElement
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {mw.echo.Controller} controller Echo notifications controller
|
|
|
|
* @param {mw.echo.dm.CrossWikiNotificationItem} model Notification group model
|
|
|
|
* @param {Object} [config] Configuration object
|
Fix fade-in/out animation in sorting
The fade in/out animation is asynchronous. This means that if we are
sorting multiple items one after the other, by the time the item faded
out, it will be reinserted back into the wrong position, breaking the
sorting.
This also broke the promise of OO.SortedEmitterList whereby all its items
are always in order.
The way to fix this was to force a better synchronization with the item
order while we hide and show the item in its new place. To do that,
a new widget is created as a fake clone of the old one, in the original
position of the old one. The original item is then reinserted (while hidden)
to the proper location -- preserving order. The fake clone is then faded
out, and the real item is then faded in.
For this to work properly, the cloned item had to preserve some of the
original item's information, like timestamp, foreigness and id. However,
since both the real item and the fake new clone have the same details,
the clone fakes its ID by adding a fraction to it - promising that the
fallback in case of equal timestamps (which happens on the real and
cloned items) will still resolve with some decision about the placement
of the items rather than (falsely but understandably) decide they are
both the same.
Since this whole animation is somewhat of a hack, the list now has a
configuration parameter to turn the animation on.
The animation is on in the popups, but off in the special page.
Bug: T141419
Change-Id: Ic7c35e5ddefc51bf7fde497eab36414b4dddcd9e
2016-07-29 23:35:29 +00:00
|
|
|
* @cfg {boolean} [animateSorting=false] Animate the sorting of items
|
2016-04-10 13:31:02 +00:00
|
|
|
*/
|
|
|
|
mw.echo.ui.CrossWikiNotificationItemWidget = function MwEchoUiCrossWikiNotificationItemWidget( controller, model, config ) {
|
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
// Parent constructor
|
2018-05-22 14:56:46 +00:00
|
|
|
mw.echo.ui.CrossWikiNotificationItemWidget.super.call( this, controller, model, config );
|
2016-04-10 13:31:02 +00:00
|
|
|
// Mixin constructors
|
|
|
|
OO.ui.mixin.PendingElement.call( this, config );
|
|
|
|
|
|
|
|
// In cross-wiki groups we only have 'mark as read'
|
|
|
|
this.toggleMarkAsReadButtons( true );
|
|
|
|
|
|
|
|
this.listWidget = new mw.echo.ui.SortedListWidget(
|
|
|
|
// Sorting callback
|
|
|
|
function ( a, b ) {
|
|
|
|
// Define the sorting order.
|
|
|
|
// This will go by the lists' timestamp, which in turn
|
|
|
|
// take the latest timestamp in their items
|
2016-07-22 18:59:10 +00:00
|
|
|
if ( b.getTimestamp() < a.getTimestamp() ) {
|
|
|
|
return -1;
|
|
|
|
} else if ( b.getTimestamp() > a.getTimestamp() ) {
|
|
|
|
return 1;
|
2016-04-10 13:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback on IDs
|
|
|
|
return b.getSource() - a.getSource();
|
|
|
|
},
|
|
|
|
// Config
|
|
|
|
{
|
|
|
|
classes: [ 'mw-echo-ui-crossWikiNotificationItemWidget-group' ],
|
|
|
|
timestamp: this.getTimestamp(),
|
Fix fade-in/out animation in sorting
The fade in/out animation is asynchronous. This means that if we are
sorting multiple items one after the other, by the time the item faded
out, it will be reinserted back into the wrong position, breaking the
sorting.
This also broke the promise of OO.SortedEmitterList whereby all its items
are always in order.
The way to fix this was to force a better synchronization with the item
order while we hide and show the item in its new place. To do that,
a new widget is created as a fake clone of the old one, in the original
position of the old one. The original item is then reinserted (while hidden)
to the proper location -- preserving order. The fake clone is then faded
out, and the real item is then faded in.
For this to work properly, the cloned item had to preserve some of the
original item's information, like timestamp, foreigness and id. However,
since both the real item and the fake new clone have the same details,
the clone fakes its ID by adding a fraction to it - promising that the
fallback in case of equal timestamps (which happens on the real and
cloned items) will still resolve with some decision about the placement
of the items rather than (falsely but understandably) decide they are
both the same.
Since this whole animation is somewhat of a hack, the list now has a
configuration parameter to turn the animation on.
The animation is on in the popups, but off in the special page.
Bug: T141419
Change-Id: Ic7c35e5ddefc51bf7fde497eab36414b4dddcd9e
2016-07-29 23:35:29 +00:00
|
|
|
$overlay: this.$overlay,
|
|
|
|
animated: config.animateSorting
|
2016-04-10 13:31:02 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
this.listWidget.$element
|
|
|
|
// We have to manually set the display to 'none' here because
|
|
|
|
// otherwise the 'slideUp' and 'slideDown' jQuery effects don't
|
|
|
|
// work
|
|
|
|
.css( 'display', 'none' );
|
|
|
|
this.setPendingElement( this.listWidget.$element );
|
|
|
|
|
|
|
|
this.errorWidget = new mw.echo.ui.PlaceholderItemWidget();
|
|
|
|
this.errorWidget.toggle( false );
|
|
|
|
|
|
|
|
// Initialize closed
|
|
|
|
this.showTitles = true;
|
|
|
|
this.expanded = false;
|
|
|
|
this.fetchedOnce = false;
|
|
|
|
|
|
|
|
// Add "expand" button
|
|
|
|
this.toggleExpandButton = new OO.ui.ButtonOptionWidget( {
|
|
|
|
icon: 'expand',
|
|
|
|
framed: false,
|
|
|
|
classes: [ 'mw-echo-ui-notificationItemWidget-content-actions-button' ]
|
|
|
|
} );
|
|
|
|
this.updateExpandButton();
|
|
|
|
this.actionsButtonSelectWidget.addItems( [ this.toggleExpandButton ] );
|
|
|
|
|
|
|
|
// Events
|
2016-07-14 00:03:57 +00:00
|
|
|
this.model.connect( this, { discard: 'onModelDiscard' } );
|
2016-04-10 13:31:02 +00:00
|
|
|
this.toggleExpandButton.connect( this, { click: 'expand' } );
|
|
|
|
this.$content.on( 'click', this.expand.bind( this ) );
|
|
|
|
|
|
|
|
// TODO: Handle cases where the group became empty or a case where the group only has 1 item left.
|
|
|
|
// Note: Right now this code works primarily for cross-wiki notifications. These act differently
|
|
|
|
// than local bundles. Cross-wiki notifications, when they "lose" their items for being read, they
|
|
|
|
// vanish from the list. Unlike them, the plan for local bundles is that read sub-items go outside
|
|
|
|
// the bundle and become their own items in the general notificationsWidget, and when the local bundle
|
|
|
|
// has 1 notification left, the group will actually transform into that last notification item.
|
|
|
|
// We don't listen to the empty event right now, because the entire item is deleted in cross-wiki
|
|
|
|
// notifications. When we work on local bundles, we will have to add that event listener per item.
|
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.populateFromModel();
|
|
|
|
this.toggleExpanded( false );
|
|
|
|
this.toggleRead( false );
|
|
|
|
this.toggleTitles( true );
|
|
|
|
this.$element
|
|
|
|
.addClass( 'mw-echo-ui-crossWikiNotificationItemWidget' )
|
|
|
|
.append(
|
|
|
|
$( '<div>' )
|
|
|
|
.addClass( 'mw-echo-ui-crossWikiNotificationItemWidget-separator' ),
|
|
|
|
this.listWidget.$element,
|
|
|
|
this.errorWidget.$element
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Initialization */
|
|
|
|
|
|
|
|
OO.inheritClass( mw.echo.ui.CrossWikiNotificationItemWidget, mw.echo.ui.NotificationItemWidget );
|
|
|
|
OO.mixinClass( mw.echo.ui.CrossWikiNotificationItemWidget, OO.ui.mixin.PendingElement );
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to model removing source group
|
|
|
|
*
|
2016-07-14 00:03:57 +00:00
|
|
|
* @param {string} groupName Symbolic name of the group
|
2016-04-10 13:31:02 +00:00
|
|
|
*/
|
2016-07-14 00:03:57 +00:00
|
|
|
mw.echo.ui.CrossWikiNotificationItemWidget.prototype.onModelDiscard = function ( groupName ) {
|
2016-04-10 13:31:02 +00:00
|
|
|
var list = this.getList(),
|
2016-07-14 00:03:57 +00:00
|
|
|
group = list.getItemFromId( groupName );
|
2016-04-10 13:31:02 +00:00
|
|
|
|
|
|
|
list.removeItems( [ group ] );
|
|
|
|
|
|
|
|
if ( list.isEmpty() ) {
|
|
|
|
this.controller.removeCrossWikiItem();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-03 18:47:13 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
mw.echo.ui.CrossWikiNotificationItemWidget.prototype.markRead = function () {
|
|
|
|
// Cross wiki notification is always only marked as read, never as
|
|
|
|
// unread. The original parameter is unneeded
|
|
|
|
this.controller.markEntireCrossWikiItemAsRead();
|
|
|
|
};
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Populate the items in this widget according to the data
|
|
|
|
* in the model
|
|
|
|
*/
|
|
|
|
mw.echo.ui.CrossWikiNotificationItemWidget.prototype.populateFromModel = function () {
|
|
|
|
var i,
|
|
|
|
groupWidgets = [],
|
|
|
|
groups = this.model.getList().getItems();
|
|
|
|
|
|
|
|
for ( i = 0; i < groups.length; i++ ) {
|
|
|
|
// Create SubGroup widgets
|
|
|
|
groupWidgets.push(
|
|
|
|
new mw.echo.ui.SubGroupListWidget(
|
|
|
|
this.controller,
|
|
|
|
groups[ i ],
|
|
|
|
{
|
|
|
|
$overlay: this.$overlay,
|
|
|
|
showTitle: this.showTitles
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
this.getList().addItems( groupWidgets );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle the visibility of the titles
|
|
|
|
*
|
|
|
|
* @param {boolean} [show] Show titles
|
|
|
|
*/
|
|
|
|
mw.echo.ui.CrossWikiNotificationItemWidget.prototype.toggleTitles = function ( show ) {
|
|
|
|
var i,
|
|
|
|
items = this.getList().getItems();
|
|
|
|
|
|
|
|
show = show === undefined ? !this.showTitles : show;
|
|
|
|
|
|
|
|
if ( this.showTitles !== show ) {
|
|
|
|
this.showTitles = show;
|
|
|
|
for ( i = 0; i < items.length; i++ ) {
|
|
|
|
items[ i ].toggleTitle( show );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the titles should be shown and toggle them
|
|
|
|
* in each of the group lists.
|
|
|
|
*/
|
|
|
|
mw.echo.ui.CrossWikiNotificationItemWidget.prototype.checkShowTitles = function () {
|
|
|
|
this.toggleTitles( this.getList().getItemCount() > 1 );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle the visibility of the notification item list and the placeholder/error widget.
|
|
|
|
*
|
|
|
|
* @param {boolean} showList Show the list
|
|
|
|
*/
|
|
|
|
mw.echo.ui.CrossWikiNotificationItemWidget.prototype.toggleListDisplay = function ( showList ) {
|
|
|
|
this.errorWidget.toggle( !showList );
|
|
|
|
this.listWidget.toggle( showList );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show an error message
|
|
|
|
*
|
|
|
|
* @param {string} label Error label
|
|
|
|
* @param {string} link Error link
|
|
|
|
*/
|
|
|
|
mw.echo.ui.CrossWikiNotificationItemWidget.prototype.showErrorMessage = function ( label, link ) {
|
|
|
|
this.errorWidget.setLabel( label || '' );
|
|
|
|
this.errorWidget.setLink( link || '' );
|
|
|
|
|
|
|
|
this.toggleListDisplay( false );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expand the group and fetch the list of notifications.
|
|
|
|
* Only fetch the first time we expand.
|
|
|
|
*/
|
|
|
|
mw.echo.ui.CrossWikiNotificationItemWidget.prototype.expand = function () {
|
|
|
|
var widget = this;
|
|
|
|
|
|
|
|
this.toggleExpanded( !this.expanded );
|
|
|
|
this.updateExpandButton();
|
|
|
|
|
|
|
|
if ( !this.expanded ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !this.fetchedOnce ) {
|
|
|
|
// Expand
|
|
|
|
this.pushPending();
|
|
|
|
this.toggleListDisplay( true );
|
|
|
|
// Query all sources
|
|
|
|
this.controller.fetchCrossWikiNotifications()
|
2018-02-10 10:43:11 +00:00
|
|
|
.catch(
|
2016-04-10 13:31:02 +00:00
|
|
|
function ( result ) {
|
|
|
|
var loginPageTitle = mw.Title.newFromText( 'Special:UserLogin' );
|
|
|
|
// If failure, check if the failure is due to login
|
|
|
|
// so we can display a more comprehensive error
|
|
|
|
// message in that case
|
|
|
|
if ( result.errCode === 'notlogin-required' ) {
|
|
|
|
// Login error
|
|
|
|
widget.showErrorMessage(
|
|
|
|
mw.message( 'echo-notification-popup-loginrequired' ),
|
|
|
|
// Set the option link to the login page
|
|
|
|
loginPageTitle.getUrl()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// General error
|
2016-05-26 20:43:14 +00:00
|
|
|
widget.showErrorMessage( mw.msg( 'echo-api-failure' ) );
|
2016-04-10 13:31:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.always( this.popPending.bind( this ) );
|
|
|
|
|
|
|
|
// Only run the fetch notifications action once
|
|
|
|
this.fetchedOnce = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle the expand/collapsed state of this group widget
|
|
|
|
*
|
|
|
|
* @param {boolean} show Show the widget expanded
|
|
|
|
*/
|
|
|
|
mw.echo.ui.CrossWikiNotificationItemWidget.prototype.toggleExpanded = function ( show ) {
|
|
|
|
this.expanded = show !== undefined ? !!show : !this.expanded;
|
|
|
|
|
2016-09-01 18:34:31 +00:00
|
|
|
this.$element.toggleClass( 'mw-echo-ui-crossWikiNotificationItemWidget-expanded', this.expanded );
|
|
|
|
|
|
|
|
if ( this.expanded ) {
|
2019-02-06 01:42:53 +00:00
|
|
|
// FIXME: Use CSS transition
|
2019-04-03 22:57:13 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-slide
|
2016-04-10 13:31:02 +00:00
|
|
|
this.getList().$element.slideDown();
|
|
|
|
} else {
|
2019-04-03 22:57:13 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-slide
|
2016-04-10 13:31:02 +00:00
|
|
|
this.getList().$element.slideUp();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the expand button label
|
|
|
|
*/
|
|
|
|
mw.echo.ui.CrossWikiNotificationItemWidget.prototype.updateExpandButton = function () {
|
|
|
|
var type = this.model.getType();
|
|
|
|
|
|
|
|
this.toggleExpandButton.setLabel(
|
|
|
|
this.expanded ?
|
|
|
|
mw.msg( 'notification-link-text-collapse-all' ) :
|
|
|
|
// Messages that appear here are:
|
2019-11-15 17:26:44 +00:00
|
|
|
// * notification-link-text-expand-alert-count
|
|
|
|
// * notification-link-text-expand-notice-count
|
2016-07-07 22:05:56 +00:00
|
|
|
mw.msg(
|
|
|
|
'notification-link-text-expand-' +
|
|
|
|
( type === 'message' ? 'notice' : type ) +
|
|
|
|
'-count',
|
2016-10-04 10:16:38 +00:00
|
|
|
mw.language.convertNumber( this.model.getCount() )
|
2016-07-07 22:05:56 +00:00
|
|
|
)
|
2016-04-10 13:31:02 +00:00
|
|
|
);
|
|
|
|
this.toggleExpandButton.setIcon(
|
|
|
|
this.expanded ?
|
|
|
|
'collapse' :
|
|
|
|
'expand'
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the list widget contained in this item
|
|
|
|
*
|
|
|
|
* @return {mw.echo.ui.SortedListWidget} List widget
|
|
|
|
*/
|
|
|
|
mw.echo.ui.CrossWikiNotificationItemWidget.prototype.getList = function () {
|
|
|
|
return this.listWidget;
|
|
|
|
};
|
2018-11-12 13:56:38 +00:00
|
|
|
}() );
|