2018-11-12 13:56:38 +00:00
|
|
|
( function () {
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Notification groups list data structure.
|
|
|
|
* It contains mw.echo.dm.NotificationsList items
|
|
|
|
*
|
|
|
|
* This contains a list of grouped notifications by source, and
|
|
|
|
* serves as a list of lists for cross-wiki notifications based
|
|
|
|
* on their remote sources and/or wikis.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends mw.echo.dm.SortedList
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
* @cfg {boolean} [foreign] The list contains foreign notifications
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationGroupsList = function MwEchoDmNotificationGroupsList( config ) {
|
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
// Parent constructor
|
2018-05-22 14:56:46 +00:00
|
|
|
mw.echo.dm.NotificationGroupsList.super.call( this );
|
2016-04-10 13:31:02 +00:00
|
|
|
|
|
|
|
// Sorting callback
|
|
|
|
this.setSortingCallback( function ( a, b ) {
|
|
|
|
// 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 Source
|
2016-07-14 00:03:57 +00:00
|
|
|
return b.getName() - a.getName();
|
2016-04-10 13:31:02 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
this.foreign = !!config.foreign;
|
|
|
|
this.groups = {};
|
|
|
|
|
2016-07-14 00:03:57 +00:00
|
|
|
this.aggregate( { discard: 'groupDiscardItem' } );
|
|
|
|
this.connect( this, { groupDiscardItem: 'onGroupDiscardItem' } );
|
2016-04-10 13:31:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Initialization */
|
|
|
|
OO.inheritClass( mw.echo.dm.NotificationGroupsList, mw.echo.dm.SortedList );
|
|
|
|
|
2016-07-14 00:03:57 +00:00
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event discard
|
|
|
|
*
|
|
|
|
* A group was permanently removed
|
|
|
|
*/
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
2016-07-14 00:03:57 +00:00
|
|
|
* Handle a discard event from any list.
|
|
|
|
* This means that one of the sources has discarded an item.
|
2016-04-10 13:31:02 +00:00
|
|
|
*
|
|
|
|
* @param {mw.echo.dm.NotificationsList} groupList List source model for the item
|
|
|
|
*/
|
2016-07-14 00:03:57 +00:00
|
|
|
mw.echo.dm.NotificationGroupsList.prototype.onGroupDiscardItem = function ( groupList ) {
|
2016-04-10 13:31:02 +00:00
|
|
|
// Check if the list has anything at all
|
|
|
|
if ( groupList.isEmpty() ) {
|
|
|
|
// Remove it
|
2016-07-14 00:03:57 +00:00
|
|
|
this.removeGroup( groupList.getName() );
|
2016-04-10 13:31:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a group to the list. This is a more convenient alias to using
|
|
|
|
* addItems()
|
|
|
|
*
|
|
|
|
* @param {string} groupSource Symbolic name for the source of
|
|
|
|
* this group, to be recognized for API operations
|
|
|
|
* @param {Object} sourceData Source data
|
|
|
|
* @param {mw.echo.dm.NotificationItem[]} [groupItems] Optional items to add to this group
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationGroupsList.prototype.addGroup = function ( groupSource, sourceData, groupItems ) {
|
|
|
|
var groupListModel = new mw.echo.dm.NotificationsList( {
|
|
|
|
title: sourceData.title,
|
2016-06-08 23:53:20 +00:00
|
|
|
name: groupSource,
|
2016-04-10 13:31:02 +00:00
|
|
|
source: groupSource,
|
|
|
|
sourceURL: sourceData.base,
|
|
|
|
timestamp: sourceData.ts
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( Array.isArray( groupItems ) && groupItems.length > 0 ) {
|
|
|
|
groupListModel.addItems( groupItems );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the group
|
|
|
|
this.addItems( [ groupListModel ] );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the timestamp of the list by taking the latest list's
|
|
|
|
* timestamp.
|
|
|
|
*
|
|
|
|
* @return {string} Latest timestamp
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationGroupsList.prototype.getTimestamp = function () {
|
|
|
|
var items = this.getItems();
|
|
|
|
|
|
|
|
return (
|
|
|
|
items.length > 0 ?
|
|
|
|
items[ 0 ].getTimestamp() :
|
|
|
|
0
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add items to a specific group by its source identifier.
|
|
|
|
*
|
|
|
|
* @param {string} groupSource Source identifier of the group
|
|
|
|
* @param {mw.echo.dm.NotificationItem[]} groupItems Items to add to this group
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationGroupsList.prototype.addItemsToGroup = function ( groupSource, groupItems ) {
|
2016-07-14 00:03:57 +00:00
|
|
|
var group = this.getGroupByName( groupSource );
|
2016-04-10 13:31:02 +00:00
|
|
|
|
|
|
|
if ( group ) {
|
|
|
|
group.addItems( groupItems );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Remove a group from the list. This is an easier to use alias
|
|
|
|
* to 'removeItems()' method.
|
|
|
|
*
|
2016-07-14 00:03:57 +00:00
|
|
|
* Since this is an intentional action, we fire 'discard' event.
|
|
|
|
* The main reason for this is that the event 'remove' is a general
|
|
|
|
* event that is fired both when a user intends on removing an
|
|
|
|
* item and also when an item is temporarily removed to be re-added
|
|
|
|
* for the sake of sorting. To avoid ambiguity, we use 'discard' event.
|
|
|
|
*
|
|
|
|
* @param {string} groupName Group name
|
|
|
|
* @fires discard
|
2016-04-10 13:31:02 +00:00
|
|
|
*/
|
2016-07-14 00:03:57 +00:00
|
|
|
mw.echo.dm.NotificationGroupsList.prototype.removeGroup = function ( groupName ) {
|
|
|
|
var group = this.getGroupByName( groupName );
|
2016-04-10 13:31:02 +00:00
|
|
|
|
|
|
|
if ( group ) {
|
|
|
|
this.removeItems( group );
|
2016-07-14 00:03:57 +00:00
|
|
|
this.emit( 'discard', group );
|
2016-04-10 13:31:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a group by its source identifier.
|
|
|
|
*
|
2016-07-14 00:03:57 +00:00
|
|
|
* @param {string} groupName Group name
|
2016-04-10 13:31:02 +00:00
|
|
|
* @return {mw.echo.dm.NotificationsList|null} Requested group, null if none was found.
|
|
|
|
*/
|
2016-07-14 00:03:57 +00:00
|
|
|
mw.echo.dm.NotificationGroupsList.prototype.getGroupByName = function ( groupName ) {
|
2016-04-10 13:31:02 +00:00
|
|
|
var i,
|
|
|
|
items = this.getItems();
|
|
|
|
|
|
|
|
for ( i = 0; i < items.length; i++ ) {
|
2016-07-14 00:03:57 +00:00
|
|
|
if ( items[ i ].getName() === groupName ) {
|
2016-04-10 13:31:02 +00:00
|
|
|
return items[ i ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
2018-11-12 13:56:38 +00:00
|
|
|
}() );
|