summaryrefslogtreecommitdiff
blob: 1183aee2c8d83046445689ffa84641e25f588749 (plain)
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
165
( function ( mw ) {
	/**
	 * Cross-wiki notification item model. Contains a list of sources,
	 * that each contain a list of notification items from that source.
	 *
	 * @class
	 * @extends mw.echo.dm.NotificationItem
	 *
	 * @constructor
	 * @param {number} id Notification id
	 * @param {Object} [config] Configuration object
	 * @cfg {number} count The initial anticipated count of notifications through all
	 *  of the sources.
	 */
	mw.echo.dm.CrossWikiNotificationItem = function MwEchoDmCrossWikiNotificationItem( id, config ) {
		config = config || {};

		mw.echo.dm.CrossWikiNotificationItem.parent.call( this, id, config );

		this.foreign = true;
		this.source = null;
		this.count = config.count || 0;
		this.modelName = config.modelName || 'xwiki';

		this.list = new mw.echo.dm.NotificationGroupsList();

		this.list.connect( this, { discard: 'onListDiscard' } );
	};

	OO.inheritClass( mw.echo.dm.CrossWikiNotificationItem, mw.echo.dm.NotificationItem );

	/* Events */

	/**
	 * @event discard
	 * @param {string} name The symbolic name for the list model that was discarded
	 *
	 * A sub list has been discarded
	 */

	/* Methods */

	/**
	 * Respond to list being removed from the cross-wiki bundle.
	 *
	 * @param {mw.echo.dm.NotificationGroupsList} sourceModel The source model that was removed
	 * @fires discard
	 */
	mw.echo.dm.CrossWikiNotificationItem.prototype.onListDiscard = function ( sourceModel ) {
		this.emit( 'discard', sourceModel.getName() );
	};

	/**
	 * Get the list of sources
	 *
	 * @return {mw.echo.dm.NotificationGroupsList} List of sources
	 */
	mw.echo.dm.CrossWikiNotificationItem.prototype.getList = function () {
		return this.list;
	};

	/**
	 * Get an array of source names that are in the cross-wiki list
	 *
	 * @return {string[]} Source names
	 */
	mw.echo.dm.CrossWikiNotificationItem.prototype.getSourceNames = function () {
		var i,
			sourceNames = [],
			sourceLists = this.list.getItems();

		for ( i = 0; i < sourceLists.length; i++ ) {
			sourceNames.push( sourceLists[ i ].getName() );
		}

		return sourceNames;
	};

	/**
	 * Get a specific item from the list by its source name
	 *
	 * @param {string} sourceName Source name
	 * @return {mw.echo.dm.NotificationGroupsList} Source item
	 */
	mw.echo.dm.CrossWikiNotificationItem.prototype.getItemBySource = function ( sourceName ) {
		return this.list.getGroupByName( sourceName );
	};

	/**
	 * Get expected item count from all sources
	 *
	 * @return {number} Item count
	 */
	mw.echo.dm.CrossWikiNotificationItem.prototype.getCount = function () {
		return this.count;
	};

	/**
	 * Check if there are unseen items in any of the cross wiki source lists.
	 * This method is required for all models that are managed by the
	 * mw.echo.dm.ModelManager.
	 *
	 * @return {boolean} There are unseen items
	 */
	mw.echo.dm.CrossWikiNotificationItem.prototype.hasUnseen = function () {
		var i, j, items,
			sourceLists = this.getList().getItems();

		for ( i = 0; i < sourceLists.length; i++ ) {
			items = sourceLists[ i ].getItems();
			for ( j = 0; j < items.length; j++ ) {
				if ( !items[ j ].isSeen() ) {
					return true;
				}
			}
		}

		return false;
	};

	/**
	 * Set all notifications in all groups to seen
	 *
	 * @param {number} timestamp New seen timestamp
	 */
	mw.echo.dm.CrossWikiNotificationItem.prototype.updateSeenState = function ( timestamp ) {
		this.getList().getItems().forEach( function ( source ) {
			source.getItems().forEach( function ( notification ) {
				notification.toggleSeen(
					notification.isRead() || notification.getTimestamp() < timestamp
				);
			} );
		} );
	};

	/**
	 * Get all items in the cross wiki notification bundle
	 *
	 * @return {mw.echo.dm.NotificationItem[]} All items across all sources
	 */
	mw.echo.dm.CrossWikiNotificationItem.prototype.getItems = function () {
		var notifications = [];
		this.list.getItems().forEach( function ( sourceList ) {
			notifications = notifications.concat( sourceList.getItems() );
		} );

		return notifications;
	};

	/**
	 * This item is a group.
	 * This method is required for all models that are managed by the
	 * mw.echo.dm.ModelManager.
	 *
	 * @return {boolean} This item is a group
	 */
	mw.echo.dm.CrossWikiNotificationItem.prototype.isGroup = function () {
		return true;
	};

	mw.echo.dm.CrossWikiNotificationItem.prototype.isEmpty = function () {
		return this.getList().isEmpty();
	};

}( mediaWiki ) );