Debian Codegraph @

extensions/Echo/modules/model/mw.echo.dm.NotificationGroupsList.js

raw symbols SCIP
1 ( function () {
2 /**
3 * Notification groups list data structure.
4 * It contains mw.echo.dm.NotificationsList items
5 *
6 * This contains a list of grouped notifications by source, and
7 * serves as a list of lists for cross-wiki notifications based
8 * on their remote sources and/or wikis.
9 *
10 * @class
11 * @extends mw.echo.dm.SortedList
12 *
13 * @constructor
14 * @param {Object} [config] Configuration options
15 * @param {boolean} [config.foreign] The list contains foreign notifications
16 */
17 mw.echo.dm.NotificationGroupsList = function MwEchoDmNotificationGroupsList( config ) {
18 config = config || {};
19
20 // Parent constructor
21 mw.echo.dm.NotificationGroupsList.super.call( this );
22
23 // Sorting callback
24 this.setSortingCallback( ( a, b ) => {
25 // Reverse sorting
26 if ( b.getTimestamp() < a.getTimestamp() ) {
27 return -1;
28 } else if ( b.getTimestamp() > a.getTimestamp() ) {
29 return 1;
30 }
31
32 // Fallback on Source
33 return b.getName() - a.getName();
34 } );
35
36 this.foreign = !!config.foreign;
37 this.groups = {};
38
39 this.aggregate( { discard: 'groupDiscardItem' } );
40 this.connect( this, { groupDiscardItem: 'onGroupDiscardItem' } );
41 };
42
43 /* Initialization */
44 OO.inheritClass( mw.echo.dm.NotificationGroupsList, mw.echo.dm.SortedList );
45
46 /* Events */
47
48 /**
49 * A group was permanently removed
50 *
51 * @event mw.echo.dm.NotificationGroupsList#discard
52 */
53
54 /* Methods */
55
56 /**
57 * Handle a discard event from any list.
58 * This means that one of the sources has discarded an item.
59 *
60 * @param {mw.echo.dm.NotificationsList} groupList List source model for the item
61 */
62 mw.echo.dm.NotificationGroupsList.prototype.onGroupDiscardItem = function ( groupList ) {
63 // Check if the list has anything at all
64 if ( groupList.isEmpty() ) {
65 // Remove it
66 this.removeGroup( groupList.getName() );
67 }
68 };
69
70 /**
71 * Add a group to the list. This is a more convenient alias to using
72 * addItems()
73 *
74 * @param {string} groupSource Symbolic name for the source of
75 * this group, to be recognized for API operations
76 * @param {Object} sourceData Source data
77 * @param {mw.echo.dm.NotificationItem[]} [groupItems] Optional items to add to this group
78 */
79 mw.echo.dm.NotificationGroupsList.prototype.addGroup = function ( groupSource, sourceData, groupItems ) {
80 const groupListModel = new mw.echo.dm.NotificationsList( {
81 title: sourceData.title,
82 name: groupSource,
83 source: groupSource,
84 sourceURL: sourceData.base,
85 timestamp: sourceData.ts
86 } );
87
88 if ( Array.isArray( groupItems ) && groupItems.length > 0 ) {
89 groupListModel.addItems( groupItems );
90 }
91
92 // Add the group
93 this.addItems( [ groupListModel ] );
94 };
95
96 /**
97 * Get the timestamp of the list by taking the latest list's
98 * timestamp.
99 *
100 * @return {string} Latest timestamp
101 */
102 mw.echo.dm.NotificationGroupsList.prototype.getTimestamp = function () {
103 const items = this.getItems();
104
105 return (
106 items.length > 0 ?
107 items[ 0 ].getTimestamp() :
108 0
109 );
110 };
111
112 /**
113 * Add items to a specific group by its source identifier.
114 *
115 * @param {string} groupSource Source identifier of the group
116 * @param {mw.echo.dm.NotificationItem[]} groupItems Items to add to this group
117 */
118 mw.echo.dm.NotificationGroupsList.prototype.addItemsToGroup = function ( groupSource, groupItems ) {
119 const group = this.getGroupByName( groupSource );
120
121 if ( group ) {
122 group.addItems( groupItems );
123 }
124 };
125 /**
126 * Remove a group from the list. This is an easier to use alias
127 * to 'removeItems()' method.
128 *
129 * Since this is an intentional action, we fire 'discard' event.
130 * The main reason for this is that the event 'remove' is a general
131 * event that is fired both when a user intends on removing an
132 * item and also when an item is temporarily removed to be re-added
133 * for the sake of sorting. To avoid ambiguity, we use 'discard' event.
134 *
135 * @param {string} groupName Group name
136 * @fires mw.echo.dm.NotificationGroupsList#discard
137 */
138 mw.echo.dm.NotificationGroupsList.prototype.removeGroup = function ( groupName ) {
139 const group = this.getGroupByName( groupName );
140
141 if ( group ) {
142 this.removeItems( group );
143 this.emit( 'discard', group );
144 }
145 };
146
147 /**
148 * Get a group by its source identifier.
149 *
150 * @param {string} groupName Group name
151 * @return {mw.echo.dm.NotificationsList|null} Requested group, null if none was found.
152 */
153 mw.echo.dm.NotificationGroupsList.prototype.getGroupByName = function ( groupName ) {
154 const items = this.getItems();
155
156 for ( let i = 0; i < items.length; i++ ) {
157 if ( items[ i ].getName() === groupName ) {
158 return items[ i ];
159 }
160 }
161 return null;
162 };
163 }() );