1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
( function ( mw ) {
/**
* 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
mw.echo.dm.NotificationGroupsList.super.call( this );
// Sorting callback
this.setSortingCallback( function ( a, b ) {
// Reverse sorting
if ( b.getTimestamp() < a.getTimestamp() ) {
return -1;
} else if ( b.getTimestamp() > a.getTimestamp() ) {
return 1;
}
// Fallback on Source
return b.getName() - a.getName();
} );
this.foreign = !!config.foreign;
this.groups = {};
this.aggregate( { discard: 'groupDiscardItem' } );
this.connect( this, { groupDiscardItem: 'onGroupDiscardItem' } );
};
/* Initialization */
OO.inheritClass( mw.echo.dm.NotificationGroupsList, mw.echo.dm.SortedList );
/* Events */
/**
* @event discard
*
* A group was permanently removed
*/
/* Methods */
/**
* Handle a discard event from any list.
* This means that one of the sources has discarded an item.
*
* @param {mw.echo.dm.NotificationsList} groupList List source model for the item
*/
mw.echo.dm.NotificationGroupsList.prototype.onGroupDiscardItem = function ( groupList ) {
// Check if the list has anything at all
if ( groupList.isEmpty() ) {
// Remove it
this.removeGroup( groupList.getName() );
}
};
/**
* 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,
name: groupSource,
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 ) {
var group = this.getGroupByName( groupSource );
if ( group ) {
group.addItems( groupItems );
}
};
/**
* Remove a group from the list. This is an easier to use alias
* to 'removeItems()' method.
*
* 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
*/
mw.echo.dm.NotificationGroupsList.prototype.removeGroup = function ( groupName ) {
var group = this.getGroupByName( groupName );
if ( group ) {
this.removeItems( group );
this.emit( 'discard', group );
}
};
/**
* Get a group by its source identifier.
*
* @param {string} groupName Group name
* @return {mw.echo.dm.NotificationsList|null} Requested group, null if none was found.
*/
mw.echo.dm.NotificationGroupsList.prototype.getGroupByName = function ( groupName ) {
var i,
items = this.getItems();
for ( i = 0; i < items.length; i++ ) {
if ( items[ i ].getName() === groupName ) {
return items[ i ];
}
}
return null;
};
}( mediaWiki ) );
|