2018-11-12 13:56:38 +00:00
|
|
|
( function () {
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Notifications list widget.
|
|
|
|
* All of its items must be of the mw.echo.ui.NotificationItem type.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends mw.echo.ui.SortedListWidget
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {mw.echo.Controller} controller Echo notifications controller
|
|
|
|
* @param {mw.echo.dm.ModelManager} manager Model manager
|
|
|
|
* @param {Object} [config] Configuration object
|
|
|
|
* marked as read when they are seen.
|
2024-05-27 01:43:21 +00:00
|
|
|
* @param {string} [config.timestamp=0] A timestamp representing the latest item in
|
2018-05-22 14:57:23 +00:00
|
|
|
* the list.
|
2024-05-27 01:43:21 +00:00
|
|
|
* @param {jQuery} [config.$overlay] A jQuery element functioning as an overlay
|
2016-04-10 13:31:02 +00:00
|
|
|
* for popups.
|
|
|
|
*/
|
|
|
|
mw.echo.ui.NotificationsListWidget = function MwEchoUiNotificationsListWidget( controller, manager, config ) {
|
|
|
|
config = config || {};
|
|
|
|
// Parent constructor
|
2018-05-22 14:56:46 +00:00
|
|
|
mw.echo.ui.NotificationsListWidget.super.call(
|
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
|
|
|
this,
|
|
|
|
// Sorting callback
|
|
|
|
function ( a, b ) {
|
|
|
|
if ( !a.isRead() && b.isRead() ) {
|
|
|
|
return -1; // Unread items are always above read items
|
|
|
|
} else if ( a.isRead() && !b.isRead() ) {
|
|
|
|
return 1;
|
|
|
|
} else if ( !a.isForeign() && b.isForeign() ) {
|
|
|
|
return -1;
|
|
|
|
} else if ( a.isForeign() && !b.isForeign() ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reverse sorting
|
|
|
|
if ( b.getTimestamp() < a.getTimestamp() ) {
|
|
|
|
return -1;
|
|
|
|
} else if ( b.getTimestamp() > a.getTimestamp() ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback on IDs
|
|
|
|
return b.getId() - a.getId();
|
|
|
|
},
|
|
|
|
config
|
|
|
|
);
|
2016-04-10 13:31:02 +00:00
|
|
|
|
|
|
|
// Initialize models
|
|
|
|
this.controller = controller;
|
|
|
|
this.manager = manager;
|
|
|
|
this.models = {};
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.$overlay = config.$overlay || this.$element;
|
|
|
|
this.timestamp = config.timestamp || 0;
|
|
|
|
|
|
|
|
// Dummy 'loading' option widget
|
|
|
|
this.loadingOptionWidget = new mw.echo.ui.PlaceholderItemWidget();
|
|
|
|
|
|
|
|
this.resetLoadingOption();
|
|
|
|
|
|
|
|
this.manager.connect( this, {
|
|
|
|
update: 'resetDataFromModel',
|
2016-07-14 00:03:57 +00:00
|
|
|
discard: 'onModelManagerDiscard'
|
2016-04-10 13:31:02 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
this.$element
|
|
|
|
.addClass( 'mw-echo-ui-notificationsListWidget' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Initialization */
|
|
|
|
|
|
|
|
OO.inheritClass( mw.echo.ui.NotificationsListWidget, mw.echo.ui.SortedListWidget );
|
|
|
|
|
2017-03-07 22:35:42 +00:00
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The content of this list has changed.
|
|
|
|
* This event is to state that not only has the content changed
|
|
|
|
* but the actual DOM has been manipulated.
|
2024-04-30 14:21:54 +00:00
|
|
|
*
|
|
|
|
* @event mw.echo.ui.NotificationsListWidget#modified
|
2017-03-07 22:35:42 +00:00
|
|
|
*/
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2016-07-14 00:03:57 +00:00
|
|
|
mw.echo.ui.NotificationsListWidget.prototype.onModelManagerDiscard = function ( modelName ) {
|
2024-03-05 22:39:09 +00:00
|
|
|
var items = this.getItems();
|
2016-04-10 13:31:02 +00:00
|
|
|
|
|
|
|
// For the moment, this is only relevant for xwiki bundles.
|
|
|
|
// Local single items will not get their entire model removed, but
|
|
|
|
// local bundles may - when that happens, the condition below should
|
|
|
|
// also deal with local bundles and removing them specifically
|
|
|
|
if ( modelName === 'xwiki' ) {
|
2024-03-05 22:39:09 +00:00
|
|
|
for ( var i = 0; i < items.length; i++ ) {
|
2016-04-10 13:31:02 +00:00
|
|
|
if ( items[ i ] instanceof mw.echo.ui.CrossWikiNotificationItemWidget ) {
|
|
|
|
this.removeItems( [ items[ i ] ] );
|
2016-06-28 19:35:20 +00:00
|
|
|
this.checkForEmptyNotificationsList();
|
2016-04-10 13:31:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-07 22:35:42 +00:00
|
|
|
|
|
|
|
this.emit( 'modified' );
|
2016-04-10 13:31:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to model manager update event.
|
|
|
|
* This event means we are repopulating the entire list and the
|
|
|
|
* associated models within it.
|
|
|
|
*
|
|
|
|
* @param {Object} models Object of new models to populate the
|
|
|
|
* list.
|
2024-04-30 14:21:54 +00:00
|
|
|
* @fires mw.echo.ui.NotificationsListWidget#modified
|
2016-04-10 13:31:02 +00:00
|
|
|
*/
|
|
|
|
mw.echo.ui.NotificationsListWidget.prototype.resetDataFromModel = function ( models ) {
|
2024-03-05 22:39:09 +00:00
|
|
|
var itemWidgets = [],
|
2016-09-26 17:08:43 +00:00
|
|
|
$elements = $();
|
2016-04-10 13:31:02 +00:00
|
|
|
|
2024-03-05 22:39:09 +00:00
|
|
|
var modelId;
|
2016-04-10 13:31:02 +00:00
|
|
|
// Detach all attached models
|
|
|
|
for ( modelId in this.models ) {
|
|
|
|
this.detachModel( modelId );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach and process new models
|
|
|
|
for ( modelId in models ) {
|
2024-03-05 22:39:09 +00:00
|
|
|
var model = models[ modelId ];
|
2016-04-10 13:31:02 +00:00
|
|
|
this.attachModel( modelId, model );
|
|
|
|
|
2024-03-05 22:39:09 +00:00
|
|
|
var widget;
|
2016-04-10 13:31:02 +00:00
|
|
|
// Build widgets based on the data in the model
|
|
|
|
if ( model.isGroup() ) {
|
2016-06-07 20:08:16 +00:00
|
|
|
if ( model.isForeign() ) {
|
|
|
|
// One Widget to Rule Them All
|
2016-09-26 17:08:43 +00:00
|
|
|
widget = new mw.echo.ui.CrossWikiNotificationItemWidget(
|
2016-06-07 20:08:16 +00:00
|
|
|
this.controller,
|
|
|
|
model,
|
|
|
|
{
|
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,
|
|
|
|
animateSorting: this.animated
|
2016-06-07 20:08:16 +00:00
|
|
|
}
|
2016-09-26 17:08:43 +00:00
|
|
|
);
|
2016-06-07 20:08:16 +00:00
|
|
|
} else {
|
|
|
|
// local bundle
|
2016-09-26 17:08:43 +00:00
|
|
|
widget = new mw.echo.ui.BundleNotificationItemWidget(
|
2016-06-07 20:08:16 +00:00
|
|
|
this.controller,
|
|
|
|
model,
|
|
|
|
{
|
|
|
|
$overlay: this.$overlay,
|
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
|
|
|
bundle: false,
|
|
|
|
animateSorting: this.animated
|
2016-06-07 20:08:16 +00:00
|
|
|
}
|
2016-09-26 17:08:43 +00:00
|
|
|
);
|
2016-06-07 20:08:16 +00:00
|
|
|
}
|
2016-09-26 17:08:43 +00:00
|
|
|
itemWidgets.push( widget );
|
|
|
|
$elements = $elements.add( widget.$element );
|
2016-04-10 13:31:02 +00:00
|
|
|
} else {
|
2024-03-05 22:39:09 +00:00
|
|
|
var subItems = model.getItems();
|
2016-04-10 13:31:02 +00:00
|
|
|
// Separate widgets per item
|
2024-03-05 22:39:09 +00:00
|
|
|
for ( var i = 0; i < subItems.length; i++ ) {
|
|
|
|
var subItem = subItems[ i ];
|
2016-09-26 17:08:43 +00:00
|
|
|
widget = new mw.echo.ui.SingleNotificationItemWidget(
|
2016-04-10 13:31:02 +00:00
|
|
|
this.controller,
|
2016-06-07 20:08:16 +00:00
|
|
|
subItem,
|
2016-04-10 13:31:02 +00:00
|
|
|
{
|
|
|
|
$overlay: this.$overlay,
|
|
|
|
bundle: false
|
|
|
|
}
|
2016-09-26 17:08:43 +00:00
|
|
|
);
|
|
|
|
itemWidgets.push( widget );
|
|
|
|
$elements = $elements.add( widget.$element );
|
2016-04-10 13:31:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the current items and re-add the new item widgets
|
|
|
|
this.clearItems();
|
2016-09-26 17:08:43 +00:00
|
|
|
|
|
|
|
// fire render hook
|
|
|
|
mw.hook( 'ext.echo.notifications.beforeRender' ).fire( this.$element, $elements );
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
this.addItems( itemWidgets );
|
2016-05-29 20:49:48 +00:00
|
|
|
|
2016-06-28 19:35:20 +00:00
|
|
|
this.checkForEmptyNotificationsList();
|
2017-03-07 22:35:42 +00:00
|
|
|
|
|
|
|
this.emit( 'modified' );
|
2016-04-10 13:31:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach a model to the widget
|
|
|
|
*
|
|
|
|
* @param {string} modelId Symbolic name for the model
|
|
|
|
* @param {mw.echo.dm.SortedList} model Notifications list model
|
|
|
|
*/
|
|
|
|
mw.echo.ui.NotificationsListWidget.prototype.attachModel = function ( modelId, model ) {
|
|
|
|
this.models[ modelId ] = model;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detach a model from the widget
|
|
|
|
*
|
|
|
|
* @param {string} modelId Notifications list model
|
|
|
|
*/
|
|
|
|
mw.echo.ui.NotificationsListWidget.prototype.detachModel = function ( modelId ) {
|
|
|
|
this.models[ modelId ].disconnect( this );
|
|
|
|
delete this.models[ modelId ];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the loading 'dummy' option widget
|
|
|
|
*
|
|
|
|
* @param {string} [label] Label for the option widget
|
|
|
|
* @param {string} [link] Link for the option widget
|
2024-04-30 14:21:54 +00:00
|
|
|
* @fires mw.echo.ui.NotificationsListWidget#modified
|
2016-04-10 13:31:02 +00:00
|
|
|
*/
|
|
|
|
mw.echo.ui.NotificationsListWidget.prototype.resetLoadingOption = function ( label, link ) {
|
|
|
|
this.loadingOptionWidget.setLabel( label || '' );
|
|
|
|
this.loadingOptionWidget.setLink( link || '' );
|
|
|
|
if ( this.isEmpty() ) {
|
|
|
|
this.addItems( [ this.loadingOptionWidget ] );
|
|
|
|
}
|
2017-03-07 22:35:42 +00:00
|
|
|
this.emit( 'modified' );
|
2016-04-10 13:31:02 +00:00
|
|
|
};
|
|
|
|
|
2016-06-28 19:35:20 +00:00
|
|
|
/**
|
|
|
|
* Check if the list of notifications is empty and udpate the placeholder
|
|
|
|
* widget as needed.
|
|
|
|
*/
|
|
|
|
mw.echo.ui.NotificationsListWidget.prototype.checkForEmptyNotificationsList = function () {
|
|
|
|
this.resetLoadingOption( this.isEmpty() ? mw.msg( 'echo-notification-placeholder' ) : '' );
|
|
|
|
};
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Reset the 'initiallyUnseen' state of all items
|
|
|
|
*/
|
|
|
|
mw.echo.ui.NotificationsListWidget.prototype.resetInitiallyUnseenItems = function () {
|
2024-03-05 22:39:09 +00:00
|
|
|
var itemWidgets = this.getItems();
|
2016-04-10 13:31:02 +00:00
|
|
|
|
2024-03-05 22:39:09 +00:00
|
|
|
for ( var i = 0; i < itemWidgets.length; i++ ) {
|
2016-05-29 20:49:48 +00:00
|
|
|
itemWidgets[ i ].resetInitiallyUnseen();
|
2016-04-10 13:31:02 +00:00
|
|
|
}
|
|
|
|
};
|
2018-11-12 13:56:38 +00:00
|
|
|
}() );
|