2018-11-12 13:56:38 +00:00
|
|
|
( function () {
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Notifications list data structure.
|
|
|
|
*
|
|
|
|
* This contains the list of mw.echo.dm.NotificationItem items
|
|
|
|
* in the specified order and reflects when the list has changed.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends mw.echo.dm.SortedList
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} config Configuration options
|
2024-05-27 01:43:21 +00:00
|
|
|
* @param {Function} [config.sortingCallback] A function defining the sorting order
|
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
|
|
|
* of items in this list.
|
2024-05-27 01:43:21 +00:00
|
|
|
* @param {string} [config.title] An optional title for this notifications list
|
|
|
|
* @param {string} [config.name='local'] Symbolic name for this list
|
|
|
|
* @param {string} [config.source='local'] Symbolic name for the source of this list.
|
2016-06-08 23:53:20 +00:00
|
|
|
* This is used mainly for recognizing where API actions should be by the
|
|
|
|
* controller.
|
2024-05-27 01:43:21 +00:00
|
|
|
* @param {string} [config.sourceURL] The URL for the article base of the remote
|
2016-04-10 13:31:02 +00:00
|
|
|
* group or wiki
|
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.
|
2016-04-10 13:31:02 +00:00
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsList = function MwEchoDmNotificationsList( config ) {
|
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
// Parent constructor
|
2018-05-22 14:56:46 +00:00
|
|
|
mw.echo.dm.NotificationsList.super.call( this );
|
2016-04-10 13:31:02 +00:00
|
|
|
|
2016-06-08 23:53:20 +00:00
|
|
|
this.name = config.name || 'local';
|
2016-04-10 13:31:02 +00:00
|
|
|
this.source = config.source || 'local';
|
2016-09-16 00:05:04 +00:00
|
|
|
this.sourceURL = config.sourceURL || '';
|
2016-04-10 13:31:02 +00:00
|
|
|
this.title = config.title || '';
|
|
|
|
this.fallbackTimestamp = config.timestamp || 0;
|
|
|
|
|
|
|
|
// Sorting callback
|
2024-06-03 12:26:18 +00:00
|
|
|
this.setSortingCallback( config.sortingCallback || ( ( a, b ) => {
|
2016-04-10 13:31:02 +00:00
|
|
|
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
|
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.getId() - a.getId();
|
2024-06-03 12:26:18 +00:00
|
|
|
} ) );
|
2016-07-06 21:11:10 +00:00
|
|
|
|
|
|
|
// Events
|
|
|
|
this.aggregate( { update: 'itemUpdate' } );
|
2016-04-10 13:31:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Initialization */
|
|
|
|
OO.inheritClass( mw.echo.dm.NotificationsList, mw.echo.dm.SortedList );
|
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The list has been updated
|
2024-04-30 14:21:54 +00:00
|
|
|
*
|
|
|
|
* @event mw.echo.dm.NotificationsList#update
|
|
|
|
* @param {mw.echo.dm.NotificationItem[]} items Current items in the list
|
2016-04-10 13:31:02 +00:00
|
|
|
*/
|
|
|
|
|
2016-07-06 21:11:10 +00:00
|
|
|
/**
|
|
|
|
* An item in the list has been updated
|
2024-04-30 14:21:54 +00:00
|
|
|
*
|
|
|
|
* @event mw.echo.dm.NotificationsList#itemUpdate
|
|
|
|
* @param {mw.echo.dm.NotificationItem} item Item that has changed
|
2016-07-06 21:11:10 +00:00
|
|
|
*/
|
|
|
|
|
2016-07-14 00:03:57 +00:00
|
|
|
/**
|
|
|
|
* An item was discarded
|
2024-04-30 14:21:54 +00:00
|
|
|
*
|
|
|
|
* @event mw.echo.dm.NotificationsList#discard
|
|
|
|
* @param {mw.echo.dm.NotificationItem} item Item that was discarded
|
2016-07-14 00:03:57 +00:00
|
|
|
*/
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the items in this list
|
|
|
|
*
|
2016-05-19 20:55:21 +00:00
|
|
|
* @param {mw.echo.dm.NotificationItem[]} items Items to insert into the list
|
2024-04-30 14:21:54 +00:00
|
|
|
* @fires mw.echo.dm.NotificationsList#update
|
2016-04-10 13:31:02 +00:00
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsList.prototype.setItems = function ( items ) {
|
|
|
|
this.clearItems();
|
|
|
|
this.addItems( items );
|
|
|
|
this.emit( 'update', this.getItems() );
|
|
|
|
};
|
|
|
|
|
2016-05-19 20:55:21 +00:00
|
|
|
/**
|
|
|
|
* Discard items from the list.
|
|
|
|
*
|
|
|
|
* This is a more precise operation than 'removeItems' because when
|
|
|
|
* the list is resorting the position of a single item, it removes
|
|
|
|
* the item and reinserts it, which makes the 'remove' event unhelpful
|
|
|
|
* to differentiate between actually discarding items, and only
|
|
|
|
* temporarily moving them.
|
|
|
|
*
|
|
|
|
* @param {mw.echo.dm.NotificationItem[]} items Items to insert into the list
|
2024-04-30 14:21:54 +00:00
|
|
|
* @fires mw.echo.dm.NotificationsList#discard
|
2016-05-19 20:55:21 +00:00
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsList.prototype.discardItems = function ( items ) {
|
|
|
|
this.removeItems( items );
|
|
|
|
this.emit( 'discard', items );
|
|
|
|
};
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Get an array of all items' IDs.
|
|
|
|
*
|
|
|
|
* @return {number[]} Item IDs
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsList.prototype.getAllItemIds = function () {
|
2024-06-03 12:22:48 +00:00
|
|
|
const idArray = [],
|
2016-04-10 13:31:02 +00:00
|
|
|
items = this.getItems();
|
|
|
|
|
2024-06-03 12:22:48 +00:00
|
|
|
for ( let i = 0; i < items.length; i++ ) {
|
2016-04-10 13:31:02 +00:00
|
|
|
idArray.push( items[ i ].getId() );
|
|
|
|
}
|
|
|
|
|
|
|
|
return idArray;
|
|
|
|
};
|
|
|
|
|
2016-03-16 22:47:20 +00:00
|
|
|
/**
|
|
|
|
* Get an array of all items' IDs for a given type
|
|
|
|
*
|
|
|
|
* @param {string} type Notification type
|
|
|
|
* @return {number[]} Item IDs
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsList.prototype.getAllItemIdsByType = function ( type ) {
|
2024-06-03 12:22:48 +00:00
|
|
|
const idArray = [],
|
2016-03-16 22:47:20 +00:00
|
|
|
items = this.getItems();
|
|
|
|
|
2024-06-03 12:22:48 +00:00
|
|
|
for ( let i = 0; i < items.length; i++ ) {
|
2016-03-16 22:47:20 +00:00
|
|
|
if ( items[ i ].getType() === type ) {
|
|
|
|
idArray.push( items[ i ].getId() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return idArray;
|
|
|
|
};
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Get the title associated with this list.
|
|
|
|
*
|
|
|
|
* @return {string} List title
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsList.prototype.getTitle = function () {
|
|
|
|
return this.title;
|
|
|
|
};
|
|
|
|
|
2016-06-08 23:53:20 +00:00
|
|
|
/**
|
|
|
|
* Get the name associated with this list.
|
|
|
|
*
|
|
|
|
* @return {string} List name
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsList.prototype.getName = function () {
|
|
|
|
return this.name;
|
|
|
|
};
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Get the source associated with this list.
|
|
|
|
*
|
|
|
|
* @return {string} List source
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsList.prototype.getSource = function () {
|
|
|
|
return this.source;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the source article url associated with this list.
|
|
|
|
*
|
|
|
|
* @return {string} List source article url
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsList.prototype.getSourceURL = function () {
|
|
|
|
return this.sourceURL;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the timestamp of the list by taking the latest notification
|
|
|
|
* timestamp.
|
|
|
|
*
|
|
|
|
* @return {string} Latest timestamp
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsList.prototype.getTimestamp = function () {
|
2024-06-03 12:22:48 +00:00
|
|
|
const items = this.getItems();
|
2016-04-10 13:31:02 +00:00
|
|
|
|
|
|
|
return (
|
2017-10-17 12:53:08 +00:00
|
|
|
// In the cases where we want a single timestamp for a
|
|
|
|
// group, the group is usually all unread, which makes
|
|
|
|
// the first item its newest
|
2016-04-10 13:31:02 +00:00
|
|
|
items.length > 0 ?
|
|
|
|
items[ 0 ].getTimestamp() :
|
|
|
|
this.fallbackTimestamp
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all items that match the given IDs.
|
|
|
|
*
|
|
|
|
* @param {number[]} ids An array of item IDs
|
|
|
|
* @return {mw.echo.dm.NotificationItem[]} An array of matching items
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsList.prototype.findByIds = function ( ids ) {
|
2024-06-03 12:26:18 +00:00
|
|
|
return this.getItems().filter( ( item ) => ids.indexOf( item.getId() ) !== -1 );
|
2016-04-10 13:31:02 +00:00
|
|
|
};
|
|
|
|
|
2016-05-31 00:12:31 +00:00
|
|
|
/**
|
|
|
|
* A general method to get the number of notifications in this list
|
|
|
|
*
|
|
|
|
* @return {number} Item count
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsList.prototype.getCount = function () {
|
|
|
|
return this.getItemCount();
|
|
|
|
};
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Check if there are unseen items in this list
|
|
|
|
*
|
|
|
|
* @return {boolean} There are unseen items in the list
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsList.prototype.hasUnseen = function () {
|
2024-06-03 12:22:48 +00:00
|
|
|
const isItemUnseen = function ( item ) {
|
2016-04-10 13:31:02 +00:00
|
|
|
return !item.isSeen();
|
|
|
|
},
|
|
|
|
items = this.getItems();
|
|
|
|
|
|
|
|
return !!items.some( isItemUnseen );
|
|
|
|
};
|
|
|
|
|
2016-09-13 23:18:29 +00:00
|
|
|
/**
|
|
|
|
* Set all notifications to seen
|
|
|
|
*
|
|
|
|
* @param {string} timestamp New seen timestamp
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsList.prototype.updateSeenState = function ( timestamp ) {
|
2024-06-03 12:26:18 +00:00
|
|
|
this.getItems().forEach( ( notification ) => {
|
2016-09-13 23:18:29 +00:00
|
|
|
notification.toggleSeen(
|
|
|
|
notification.isRead() || notification.getTimestamp() < timestamp
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsList.prototype.isGroup = function () {
|
|
|
|
return false;
|
|
|
|
};
|
2016-05-19 20:55:21 +00:00
|
|
|
|
2016-06-07 20:08:16 +00:00
|
|
|
mw.echo.dm.NotificationsList.prototype.isForeign = function () {
|
|
|
|
return this.getSource() !== 'local';
|
|
|
|
};
|
|
|
|
|
2018-11-12 13:56:38 +00:00
|
|
|
}() );
|