Debian Codegraph @

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

raw symbols SCIP
1 ( function () {
2 /**
3 * Cross-wiki notification item model. Contains a list of sources,
4 * that each contain a list of notification items from that source.
5 *
6 * @class
7 * @extends mw.echo.dm.NotificationItem
8 *
9 * @constructor
10 * @param {number} id Notification id
11 * @param {Object} [config] Configuration object
12 * @param {number} config.count The initial anticipated count of notifications through all
13 * of the sources.
14 */
15 mw.echo.dm.CrossWikiNotificationItem = function MwEchoDmCrossWikiNotificationItem( id, config ) {
16 config = config || {};
17
18 mw.echo.dm.CrossWikiNotificationItem.super.call( this, id, config );
19
20 this.foreign = true;
21 this.source = null;
22 this.count = config.count || 0;
23 this.modelName = config.modelName || 'xwiki';
24
25 this.list = new mw.echo.dm.NotificationGroupsList();
26
27 this.list.connect( this, { discard: 'onListDiscard' } );
28 };
29
30 OO.inheritClass( mw.echo.dm.CrossWikiNotificationItem, mw.echo.dm.NotificationItem );
31
32 /* Events */
33
34 /**
35 * A sub list has been discarded
36 *
37 * @event mw.echo.dm.CrossWikiNotificationItem#discard
38 * @param {string} name The symbolic name for the list model that was discarded
39 */
40
41 /* Methods */
42
43 /**
44 * Respond to list being removed from the cross-wiki bundle.
45 *
46 * @param {mw.echo.dm.NotificationGroupsList} sourceModel The source model that was removed
47 * @fires mw.echo.dm.CrossWikiNotificationItem#discard
48 */
49 mw.echo.dm.CrossWikiNotificationItem.prototype.onListDiscard = function ( sourceModel ) {
50 this.emit( 'discard', sourceModel.getName() );
51 };
52
53 /**
54 * Get the list of sources
55 *
56 * @return {mw.echo.dm.NotificationGroupsList} List of sources
57 */
58 mw.echo.dm.CrossWikiNotificationItem.prototype.getList = function () {
59 return this.list;
60 };
61
62 /**
63 * Get an array of source names that are in the cross-wiki list
64 *
65 * @return {string[]} Source names
66 */
67 mw.echo.dm.CrossWikiNotificationItem.prototype.getSourceNames = function () {
68 const sourceNames = [],
69 sourceLists = this.list.getItems();
70
71 for ( let i = 0; i < sourceLists.length; i++ ) {
72 sourceNames.push( sourceLists[ i ].getName() );
73 }
74
75 return sourceNames;
76 };
77
78 /**
79 * Get a specific item from the list by its source name
80 *
81 * @param {string} sourceName Source name
82 * @return {mw.echo.dm.NotificationGroupsList} Source item
83 */
84 mw.echo.dm.CrossWikiNotificationItem.prototype.getItemBySource = function ( sourceName ) {
85 return this.list.getGroupByName( sourceName );
86 };
87
88 /**
89 * Get expected item count from all sources
90 *
91 * @return {number} Item count
92 */
93 mw.echo.dm.CrossWikiNotificationItem.prototype.getCount = function () {
94 return this.count;
95 };
96
97 /**
98 * Check if there are unseen items in any of the cross wiki source lists.
99 * This method is required for all models that are managed by the
100 * mw.echo.dm.ModelManager.
101 *
102 * @return {boolean} There are unseen items
103 */
104 mw.echo.dm.CrossWikiNotificationItem.prototype.hasUnseen = function () {
105 const sourceLists = this.getList().getItems();
106
107 for ( let i = 0; i < sourceLists.length; i++ ) {
108 const items = sourceLists[ i ].getItems();
109 for ( let j = 0; j < items.length; j++ ) {
110 if ( !items[ j ].isSeen() ) {
111 return true;
112 }
113 }
114 }
115
116 return false;
117 };
118
119 /**
120 * Set all notifications in all groups to seen
121 *
122 * @param {number} timestamp New seen timestamp
123 */
124 mw.echo.dm.CrossWikiNotificationItem.prototype.updateSeenState = function ( timestamp ) {
125 this.getList().getItems().forEach( ( source ) => {
126 source.getItems().forEach( ( notification ) => {
127 notification.toggleSeen(
128 notification.isRead() || notification.getTimestamp() < timestamp
129 );
130 } );
131 } );
132 };
133
134 /**
135 * Get all items in the cross wiki notification bundle
136 *
137 * @return {mw.echo.dm.NotificationItem[]} All items across all sources
138 */
139 mw.echo.dm.CrossWikiNotificationItem.prototype.getItems = function () {
140 let notifications = [];
141 this.list.getItems().forEach( ( sourceList ) => {
142 notifications = notifications.concat( sourceList.getItems() );
143 } );
144
145 return notifications;
146 };
147
148 /**
149 * This item is a group.
150 * This method is required for all models that are managed by the
151 * mw.echo.dm.ModelManager.
152 *
153 * @return {boolean} This item is a group
154 */
155 mw.echo.dm.CrossWikiNotificationItem.prototype.isGroup = function () {
156 return true;
157 };
158
159 mw.echo.dm.CrossWikiNotificationItem.prototype.isEmpty = function () {
160 return this.getList().isEmpty();
161 };
162
163 }() );