summaryrefslogtreecommitdiff
blob: 0f9503ee8febfcd94a6caa37100cff711ba35e25 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
( function ( mw, $ ) {
	/**
	 * Sub group list widget.
	 * This widget contains a list of notifications from a single source
	 * in a cross-wiki notifications group.
	 *
	 * @param {mw.echo.Controller} controller Notifications controller
	 * @param {mw.echo.dm.SortedList} listModel Notifications list model for this source
	 * @param {Object} config Configuration object
	 * @cfg {boolean} [showTitle=false] Show the title of this group
	 * @cfg {boolean} [showMarkAllRead=false] Show a mark all read button for this group
	 * @cfg {boolean} [animateSorting=false] Animate the sorting of items
	 * @cfg {jQuery} [$overlay] A jQuery element functioning as an overlay
	 *  for popups.
	 */
	mw.echo.ui.SubGroupListWidget = function MwEchoUiSubGroupListWidget( controller, listModel, config ) {
		var sourceURL;

		this.$header = $( '<div>' )
			.addClass( 'mw-echo-ui-subGroupListWidget-header' );

		config = config || {};

		this.controller = controller;
		this.model = listModel;

		// Parent constructor
		mw.echo.ui.SubGroupListWidget.super.call( this, $.extend( { data: this.getSource() }, config ) );

		this.showTitle = !!config.showTitle;
		this.showMarkAllRead = !!config.showMarkAllRead;
		this.$overlay = config.$overlay || this.$element;

		this.listWidget = new mw.echo.ui.SortedListWidget(
			// Sorting callback
			config.sortingCallback || function ( a, b ) {
				// Reverse sorting
				if ( b.getTimestamp() < a.getTimestamp() ) {
					return -1;
				} else if ( b.getTimestamp() > a.getTimestamp() ) {
					return 1;
				}

				// Fallback on IDs
				return b.getId() - a.getId();
			},
			// Config
			{
				$overlay: this.$overlay,
				animated: config.animateSorting
			}
		);

		sourceURL = this.model.getSourceURL() ?
			this.model.getSourceURL().replace( '$1', 'Special:Notifications' ) :
			null;
		if ( sourceURL ) {
			this.title = new OO.ui.ButtonWidget( {
				framed: false,
				classes: [
					'mw-echo-ui-subGroupListWidget-header-row-title',
					'mw-echo-ui-subGroupListWidget-header-row-cell'
				],
				href: sourceURL
			} );
		} else {
			this.title = new OO.ui.LabelWidget( {
				classes: [
					'mw-echo-ui-subGroupListWidget-header-row-title',
					'mw-echo-ui-subGroupListWidget-header-row-cell'
				]
			} );
		}

		if ( this.model.getTitle() ) {
			this.title.setLabel( this.model.getTitle() );
		}
		this.title.toggle( this.showTitle );

		// Mark all as read button
		this.markAllReadButton = new OO.ui.ButtonWidget( {
			framed: true,
			icon: 'checkAll',
			label: mw.msg( 'echo-specialpage-section-markread' ),
			classes: [
				'mw-echo-ui-subGroupListWidget-header-row-markAllReadButton',
				'mw-echo-ui-subGroupListWidget-header-row-cell'
			]
		} );

		// Events
		this.model.connect( this, {
			// Cross-wiki items can be discarded when marked as read.
			// We need to differentiate this explicit action from the
			// action of 'remove' because 'remove' is also used when
			// an item is resorted by OO.SortedEmitterWidget before
			// it is re-added again
			discard: 'onModelDiscardItems',
			// Update all items
			update: 'resetItemsFromModel'
		} );
		this.model.connect( this, { itemUpdate: 'toggleMarkAllReadButton' } );
		this.markAllReadButton.connect( this, { click: 'onMarkAllReadButtonClick' } );

		// Initialize
		this.toggleMarkAllReadButton();

		this.$element
			.addClass( 'mw-echo-ui-subGroupListWidget' )
			.append(
				this.$header.append(
					$( '<div>' )
						.addClass( 'mw-echo-ui-subGroupListWidget-header-row' )
						.append(
							this.title.$element,
							this.markAllReadButton.$element
						)
				),
				this.listWidget.$element
			);

		this.$pageContentText = $( '#mw-content-text' );
		$( window ).resize( this.resizeHeader.bind( this ) );

		// Resize the header after the stack finishes loading
		// so the widget is attached
		setTimeout( this.resizeHeader.bind( this ), 0 );
	};

	/* Initialization */

	OO.inheritClass( mw.echo.ui.SubGroupListWidget, OO.ui.Widget );

	/* Methods */

	/**
	 * Respond to window resize event
	 */
	mw.echo.ui.SubGroupListWidget.prototype.resizeHeader = function () {
		var contentWidth = this.$pageContentText.width(),
			screenTooNarrow = this.$header.width() > contentWidth;

		// Screen too narrow, put the button under the date
		this.title.$element.toggleClass(
			'mw-echo-ui-subGroupListWidget-header-row-cell',
			!screenTooNarrow
		);
		this.markAllReadButton.$element.toggleClass(
			'mw-echo-ui-subGroupListWidget-header-row-cell',
			!screenTooNarrow
		);
	};

	/**
	 * Destroy the widget and disconnect events
	 */
	mw.echo.ui.SubGroupListWidget.prototype.destroy = function () {
		$( window ).off( 'resize' );
	};

	/**
	 * Toggle the visibility of the mark all read button for this group
	 * based on whether there are unread notifications
	 */
	mw.echo.ui.SubGroupListWidget.prototype.toggleMarkAllReadButton = function () {
		this.markAllReadButton.toggle( this.showMarkAllRead && this.hasUnread() );
	};

	/**
	 * Respond to 'mark all as read' button click
	 */
	mw.echo.ui.SubGroupListWidget.prototype.onMarkAllReadButtonClick = function () {
		this.controller.markEntireListModelRead( this.model.getName() );
	};

	/**
	 * Check whether this sub group list has any unread notifications
	 *
	 * @return {boolean} Sub group has unread notifications
	 */
	mw.echo.ui.SubGroupListWidget.prototype.hasUnread = function () {
		var isUnread = function ( item ) {
				return !item.isRead();
			},
			items = this.model.getItems();

		return items.some( isUnread );
	};

	/**
	 * Reset the items and rebuild them according to the model.
	 *
	 * @param {mw.echo.dm.NotificationItem[]} [items] Item models that are added.
	 *  If this is empty, the widget will request all the items from the model.
	 */
	mw.echo.ui.SubGroupListWidget.prototype.resetItemsFromModel = function ( items ) {
		var i, widget,
			itemWidgets = [],
			$elements = $();

		items = items || this.model.getItems();

		for ( i = 0; i < items.length; i++ ) {
			widget = new mw.echo.ui.SingleNotificationItemWidget(
				this.controller,
				items[ i ],
				{
					$overlay: this.$overlay,
					bundle: items[ i ].isBundled()
				}
			);
			itemWidgets.push( widget );
			$elements = $elements.add( widget.$element );
		}

		// Clear the current items if any exist
		this.getListWidget().clearItems();

		// fire render hook
		mw.hook( 'ext.echo.notifications.beforeRender' ).fire( this.$element, $elements );

		// Add the new items
		this.getListWidget().addItems( itemWidgets );
	};

	/**
	 * Respond to model remove event. This may happen when an item
	 * is marked as read.
	 *
	 * @param {mw.echo.dm.NotificationItem[]} items Notification item models
	 */
	mw.echo.ui.SubGroupListWidget.prototype.onModelDiscardItems = function ( items ) {
		var i,
			itemWidgets = [];

		for ( i = 0; i < items.length; i++ ) {
			itemWidgets.push( this.listWidget.getItemFromId( items[ i ].getId() ) );
		}
		this.listWidget.removeItems( itemWidgets );
	};

	/**
	 * Get the associated list widget. This is useful to specifically
	 * add and/or remove items from the list.
	 *
	 * @return {mw.echo.ui.SortedListWidget} List widget
	 */
	mw.echo.ui.SubGroupListWidget.prototype.getListWidget = function () {
		return this.listWidget;
	};

	/**
	 * Get the timestamp for the list
	 *
	 * @return {number} Timestamp
	 */
	mw.echo.ui.SubGroupListWidget.prototype.getTimestamp = function () {
		return this.model.getTimestamp();
	};

	/**
	 * Toggle the visibility of the title
	 *
	 * @param {boolean} show Show the title
	 */
	mw.echo.ui.SubGroupListWidget.prototype.toggleTitle = function ( show ) {
		show = show !== undefined ? show : !this.showTitle;

		if ( this.showTitle !== show ) {
			this.showTitle = show;
			this.title.toggle( this.showTitle );
		}
	};

	/**
	 * Get a the source of this list.
	 *
	 * @return {string} Group source
	 */
	mw.echo.ui.SubGroupListWidget.prototype.getSource = function () {
		return this.model.getSource();
	};

	/**
	 * Get an array of IDs of all of the items in this group
	 *
	 * @return {number[]} Array of item IDs
	 */
	mw.echo.ui.SubGroupListWidget.prototype.getAllItemIDs = function () {
		return this.model.getAllItemIds();
	};

	/**
	 * Get an array of IDs of all of the items in this group that
	 * correspond to a specific type
	 *
	 * @param {string} type Item type
	 * @return {number[]} Array of item IDs
	 */
	mw.echo.ui.SubGroupListWidget.prototype.getAllItemIDsByType = function ( type ) {
		return this.model.getAllItemIdsByType( type );
	};

	/**
	 * Check whether this group is foreign
	 *
	 * @return {boolean} This group is foreign
	 */
	mw.echo.ui.SubGroupListWidget.prototype.isForeign = function () {
		return this.model.isForeign();
	};

	/**
	 * Get the group id, which is represented by its model symbolic name.
	 * This is meant for sorting callbacks that fallback on
	 * sorting by IDs.
	 *
	 * @return {string} Group source
	 */
	mw.echo.ui.SubGroupListWidget.prototype.getId = function () {
		return this.model.getName();
	};
}( mediaWiki, jQuery ) );