2018-11-12 13:56:38 +00:00
|
|
|
( function () {
|
2016-03-16 22:47:20 +00:00
|
|
|
/**
|
|
|
|
* A notifications list organized and separated by dates
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends OO.ui.Widget
|
2024-04-30 14:21:54 +00:00
|
|
|
* @mixes OO.ui.mixin.PendingElement
|
2016-03-16 22:47:20 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {mw.echo.Controller} controller Echo controller
|
|
|
|
* @param {mw.echo.dm.ModelManager} modelManager Model manager
|
|
|
|
* @param {Object} [config] Configuration object
|
2024-05-27 01:43:21 +00:00
|
|
|
* @param {boolean} [config.animateSorting=false] Animate the sorting of items
|
|
|
|
* @param {jQuery} [config.$overlay] An overlay for the popup menus
|
2016-03-16 22:47:20 +00:00
|
|
|
*/
|
|
|
|
mw.echo.ui.DatedNotificationsWidget = function MwEchoUiDatedNotificationsListWidget( controller, modelManager, config ) {
|
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
// Parent constructor
|
2018-05-22 14:56:46 +00:00
|
|
|
mw.echo.ui.DatedNotificationsWidget.super.call( this, config );
|
2016-03-16 22:47:20 +00:00
|
|
|
// Mixin constructors
|
|
|
|
OO.ui.mixin.PendingElement.call( this, config );
|
|
|
|
|
|
|
|
this.manager = modelManager;
|
|
|
|
this.controller = controller;
|
|
|
|
this.models = {};
|
|
|
|
|
|
|
|
this.$overlay = config.$overlay || this.$element;
|
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.animateSorting = !!config.animateSorting;
|
2016-03-16 22:47:20 +00:00
|
|
|
|
|
|
|
this.listWidget = new mw.echo.ui.SortedListWidget(
|
|
|
|
// Sorting callback
|
2024-06-03 12:26:18 +00:00
|
|
|
( ( a, b ) => {
|
2016-03-16 22:47:20 +00:00
|
|
|
// Reverse sorting
|
2016-07-22 18:59:10 +00:00
|
|
|
if ( b.getTimestamp() < a.getTimestamp() ) {
|
|
|
|
return -1;
|
|
|
|
} else if ( b.getTimestamp() > a.getTimestamp() ) {
|
|
|
|
return 1;
|
|
|
|
}
|
2024-06-03 12:26:18 +00:00
|
|
|
} ),
|
2016-03-16 22:47:20 +00:00
|
|
|
// Config
|
|
|
|
{
|
|
|
|
classes: [ 'mw-echo-ui-datedNotificationsWidget-group' ],
|
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: false
|
2016-03-16 22:47:20 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Events
|
|
|
|
this.manager.connect( this, {
|
|
|
|
update: 'populateFromModel',
|
2016-07-14 00:03:57 +00:00
|
|
|
discard: 'onManagerDiscardModel'
|
2016-03-16 22:47:20 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
this.$element
|
|
|
|
.addClass( 'mw-echo-ui-datedNotificationsWidget' )
|
|
|
|
.append( this.listWidget.$element );
|
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.populateFromModel();
|
|
|
|
};
|
|
|
|
/* Initialization */
|
|
|
|
|
|
|
|
OO.inheritClass( mw.echo.ui.DatedNotificationsWidget, OO.ui.Widget );
|
|
|
|
OO.mixinClass( mw.echo.ui.DatedNotificationsWidget, OO.ui.mixin.PendingElement );
|
|
|
|
|
2016-07-14 00:03:57 +00:00
|
|
|
mw.echo.ui.DatedNotificationsWidget.prototype.onManagerDiscardModel = function ( modelId ) {
|
2024-06-03 12:22:48 +00:00
|
|
|
const model = this.models[ modelId ],
|
2016-07-14 00:03:57 +00:00
|
|
|
list = this.getList();
|
|
|
|
|
|
|
|
if ( model ) {
|
2024-06-03 12:22:48 +00:00
|
|
|
const group = list.getItemFromId( model.getName() );
|
2016-07-14 00:03:57 +00:00
|
|
|
list.removeItems( [ group ] );
|
|
|
|
}
|
|
|
|
};
|
2016-03-16 22:47:20 +00:00
|
|
|
/**
|
|
|
|
* Respond to model removing source group
|
|
|
|
*
|
|
|
|
* @param {string} source Symbolic name of the source group
|
|
|
|
*/
|
|
|
|
mw.echo.ui.DatedNotificationsWidget.prototype.onModelRemoveSource = function ( source ) {
|
2024-06-03 12:22:48 +00:00
|
|
|
const list = this.getList(),
|
2016-03-16 22:47:20 +00:00
|
|
|
group = list.getItemFromId( source );
|
|
|
|
|
|
|
|
list.removeItems( [ group ] );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to model manager update event.
|
|
|
|
* This event means we are repopulating the entire list and the
|
|
|
|
* associated models within it.
|
2016-11-18 21:16:43 +00:00
|
|
|
*
|
|
|
|
* @param {Object} models List models, indexed by ID
|
2016-03-16 22:47:20 +00:00
|
|
|
*/
|
|
|
|
mw.echo.ui.DatedNotificationsWidget.prototype.populateFromModel = function ( models ) {
|
2024-06-03 12:22:48 +00:00
|
|
|
const groupWidgets = [];
|
2016-03-16 22:47:20 +00:00
|
|
|
|
|
|
|
// Detach all attached models
|
2024-06-03 12:22:48 +00:00
|
|
|
for ( const modelId in this.models ) {
|
2016-03-16 22:47:20 +00:00
|
|
|
this.detachModel( modelId );
|
|
|
|
}
|
|
|
|
|
2024-06-03 12:22:48 +00:00
|
|
|
for ( const model in models ) {
|
2016-03-16 22:47:20 +00:00
|
|
|
// Create SubGroup widgets
|
2024-06-03 12:22:48 +00:00
|
|
|
const subgroupWidget = new mw.echo.ui.DatedSubGroupListWidget(
|
2016-03-16 22:47:20 +00:00
|
|
|
this.controller,
|
|
|
|
models[ model ],
|
|
|
|
{
|
|
|
|
showTitle: true,
|
|
|
|
showMarkAllRead: true,
|
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: this.animateSorting
|
2016-03-16 22:47:20 +00:00
|
|
|
}
|
|
|
|
);
|
2016-07-14 00:03:57 +00:00
|
|
|
this.attachModel( model, models[ model ] );
|
|
|
|
|
2016-03-16 22:47:20 +00:00
|
|
|
subgroupWidget.resetItemsFromModel();
|
|
|
|
groupWidgets.push( subgroupWidget );
|
|
|
|
}
|
|
|
|
|
2024-06-03 12:26:18 +00:00
|
|
|
this.getList().getItems().forEach( ( widget ) => {
|
2018-03-25 13:47:46 +00:00
|
|
|
// Destroy all available widgets
|
|
|
|
widget.destroy();
|
|
|
|
} );
|
|
|
|
|
2016-03-16 22:47:20 +00:00
|
|
|
// Reset the list and re-add the items
|
|
|
|
this.getList().clearItems();
|
|
|
|
this.getList().addItems( groupWidgets );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.DatedNotificationsWidget.prototype.attachModel = function ( modelId, model ) {
|
|
|
|
this.models[ modelId ] = model;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detach a model from the widget
|
|
|
|
*
|
|
|
|
* @param {string} modelId Notifications list model
|
|
|
|
*/
|
|
|
|
mw.echo.ui.DatedNotificationsWidget.prototype.detachModel = function ( modelId ) {
|
|
|
|
this.models[ modelId ].disconnect( this );
|
|
|
|
delete this.models[ modelId ];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the list widget contained in this item
|
|
|
|
*
|
|
|
|
* @return {mw.echo.ui.SortedListWidget} List widget
|
|
|
|
*/
|
|
|
|
mw.echo.ui.DatedNotificationsWidget.prototype.getList = function () {
|
|
|
|
return this.listWidget;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the number of all notifications in all sections of the widget
|
|
|
|
*
|
|
|
|
* @return {number} The number of all notifications
|
|
|
|
*/
|
|
|
|
mw.echo.ui.DatedNotificationsWidget.prototype.getAllNotificationCount = function () {
|
2024-06-03 12:21:23 +00:00
|
|
|
const groups = this.getList().getItems();
|
2016-03-16 22:47:20 +00:00
|
|
|
|
2024-06-03 12:21:23 +00:00
|
|
|
let count = 0;
|
2024-06-03 12:22:48 +00:00
|
|
|
for ( let i = 0; i < groups.length; i++ ) {
|
2016-03-16 22:47:20 +00:00
|
|
|
count += groups[ i ].getListWidget().getItemCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
};
|
|
|
|
|
2018-11-12 13:56:38 +00:00
|
|
|
}() );
|