Debian Codegraph @

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

raw symbols SCIP
1 ( function () {
2 /**
3 * Notifications list data structure.
4 *
5 * This contains the list of mw.echo.dm.NotificationItem items
6 * in the specified order and reflects when the list has changed.
7 *
8 * @class
9 * @extends mw.echo.dm.SortedList
10 *
11 * @constructor
12 * @param {Object} config Configuration options
13 * @param {Function} [config.sortingCallback] A function defining the sorting order
14 * of items in this list.
15 * @param {string} [config.title] An optional title for this notifications list
16 * @param {string} [config.name='local'] Symbolic name for this list
17 * @param {string} [config.source='local'] Symbolic name for the source of this list.
18 * This is used mainly for recognizing where API actions should be by the
19 * controller.
20 * @param {string} [config.sourceURL] The URL for the article base of the remote
21 * group or wiki
22 * @param {string} [config.timestamp=0] A timestamp representing the latest item in
23 * the list.
24 */
25 mw.echo.dm.NotificationsList = function MwEchoDmNotificationsList( config ) {
26 config = config || {};
27
28 // Parent constructor
29 mw.echo.dm.NotificationsList.super.call( this );
30
31 this.name = config.name || 'local';
32 this.source = config.source || 'local';
33 this.sourceURL = config.sourceURL || '';
34 this.title = config.title || '';
35 this.fallbackTimestamp = config.timestamp || 0;
36
37 // Sorting callback
38 this.setSortingCallback( config.sortingCallback || ( ( a, b ) => {
39 if ( !a.isRead() && b.isRead() ) {
40 return -1; // Unread items are always above read items
41 } else if ( a.isRead() && !b.isRead() ) {
42 return 1;
43 } else if ( !a.isForeign() && b.isForeign() ) {
44 return -1;
45 } else if ( a.isForeign() && !b.isForeign() ) {
46 return 1;
47 }
48
49 // Reverse sorting
50 if ( b.getTimestamp() < a.getTimestamp() ) {
51 return -1;
52 } else if ( b.getTimestamp() > a.getTimestamp() ) {
53 return 1;
54 }
55
56 // Fallback on IDs
57 return b.getId() - a.getId();
58 } ) );
59
60 // Events
61 this.aggregate( { update: 'itemUpdate' } );
62 };
63
64 /* Initialization */
65 OO.inheritClass( mw.echo.dm.NotificationsList, mw.echo.dm.SortedList );
66
67 /* Events */
68
69 /**
70 * The list has been updated
71 *
72 * @event mw.echo.dm.NotificationsList#update
73 * @param {mw.echo.dm.NotificationItem[]} items Current items in the list
74 */
75
76 /**
77 * An item in the list has been updated
78 *
79 * @event mw.echo.dm.NotificationsList#itemUpdate
80 * @param {mw.echo.dm.NotificationItem} item Item that has changed
81 */
82
83 /**
84 * An item was discarded
85 *
86 * @event mw.echo.dm.NotificationsList#discard
87 * @param {mw.echo.dm.NotificationItem} item Item that was discarded
88 */
89
90 /* Methods */
91
92 /**
93 * Set the items in this list
94 *
95 * @param {mw.echo.dm.NotificationItem[]} items Items to insert into the list
96 * @fires mw.echo.dm.NotificationsList#update
97 */
98 mw.echo.dm.NotificationsList.prototype.setItems = function ( items ) {
99 this.clearItems();
100 this.addItems( items );
101 this.emit( 'update', this.getItems() );
102 };
103
104 /**
105 * Discard items from the list.
106 *
107 * This is a more precise operation than 'removeItems' because when
108 * the list is resorting the position of a single item, it removes
109 * the item and reinserts it, which makes the 'remove' event unhelpful
110 * to differentiate between actually discarding items, and only
111 * temporarily moving them.
112 *
113 * @param {mw.echo.dm.NotificationItem[]} items Items to insert into the list
114 * @fires mw.echo.dm.NotificationsList#discard
115 */
116 mw.echo.dm.NotificationsList.prototype.discardItems = function ( items ) {
117 this.removeItems( items );
118 this.emit( 'discard', items );
119 };
120
121 /**
122 * Get an array of all items' IDs.
123 *
124 * @return {number[]} Item IDs
125 */
126 mw.echo.dm.NotificationsList.prototype.getAllItemIds = function () {
127 const idArray = [],
128 items = this.getItems();
129
130 for ( let i = 0; i < items.length; i++ ) {
131 idArray.push( items[ i ].getId() );
132 }
133
134 return idArray;
135 };
136
137 /**
138 * Get an array of all items' IDs for a given type
139 *
140 * @param {string} type Notification type
141 * @return {number[]} Item IDs
142 */
143 mw.echo.dm.NotificationsList.prototype.getAllItemIdsByType = function ( type ) {
144 const idArray = [],
145 items = this.getItems();
146
147 for ( let i = 0; i < items.length; i++ ) {
148 if ( items[ i ].getType() === type ) {
149 idArray.push( items[ i ].getId() );
150 }
151 }
152
153 return idArray;
154 };
155
156 /**
157 * Get the title associated with this list.
158 *
159 * @return {string} List title
160 */
161 mw.echo.dm.NotificationsList.prototype.getTitle = function () {
162 return this.title;
163 };
164
165 /**
166 * Get the name associated with this list.
167 *
168 * @return {string} List name
169 */
170 mw.echo.dm.NotificationsList.prototype.getName = function () {
171 return this.name;
172 };
173
174 /**
175 * Get the source associated with this list.
176 *
177 * @return {string} List source
178 */
179 mw.echo.dm.NotificationsList.prototype.getSource = function () {
180 return this.source;
181 };
182
183 /**
184 * Get the source article url associated with this list.
185 *
186 * @return {string} List source article url
187 */
188 mw.echo.dm.NotificationsList.prototype.getSourceURL = function () {
189 return this.sourceURL;
190 };
191
192 /**
193 * Get the timestamp of the list by taking the latest notification
194 * timestamp.
195 *
196 * @return {string} Latest timestamp
197 */
198 mw.echo.dm.NotificationsList.prototype.getTimestamp = function () {
199 const items = this.getItems();
200
201 return (
202 // In the cases where we want a single timestamp for a
203 // group, the group is usually all unread, which makes
204 // the first item its newest
205 items.length > 0 ?
206 items[ 0 ].getTimestamp() :
207 this.fallbackTimestamp
208 );
209 };
210
211 /**
212 * Find all items that match the given IDs.
213 *
214 * @param {number[]} ids An array of item IDs
215 * @return {mw.echo.dm.NotificationItem[]} An array of matching items
216 */
217 mw.echo.dm.NotificationsList.prototype.findByIds = function ( ids ) {
218 return this.getItems().filter( ( item ) => ids.indexOf( item.getId() ) !== -1 );
219 };
220
221 /**
222 * A general method to get the number of notifications in this list
223 *
224 * @return {number} Item count
225 */
226 mw.echo.dm.NotificationsList.prototype.getCount = function () {
227 return this.getItemCount();
228 };
229
230 /**
231 * Check if there are unseen items in this list
232 *
233 * @return {boolean} There are unseen items in the list
234 */
235 mw.echo.dm.NotificationsList.prototype.hasUnseen = function () {
236 return this.getItems().some( ( item ) => !item.isSeen() );
237 };
238
239 /**
240 * Set all notifications to seen
241 *
242 * @param {string} timestamp New seen timestamp
243 */
244 mw.echo.dm.NotificationsList.prototype.updateSeenState = function ( timestamp ) {
245 this.getItems().forEach( ( notification ) => {
246 notification.toggleSeen(
247 notification.isRead() || notification.getTimestamp() < timestamp
248 );
249 } );
250 };
251
252 /**
253 * @inheritdoc
254 */
255 mw.echo.dm.NotificationsList.prototype.isGroup = function () {
256 return false;
257 };
258
259 mw.echo.dm.NotificationsList.prototype.isForeign = function () {
260 return this.getSource() !== 'local';
261 };
262
263 }() );