Debian Codegraph @

extensions/Echo/modules/ui/mw.echo.ui.SubGroupListWidget.js

raw symbols SCIP
1 ( function () {
2 /**
3 * Sub group list widget.
4 * This widget contains a list of notifications from a single source
5 * in a cross-wiki notifications group.
6 *
7 * @param {mw.echo.Controller} controller Notifications controller
8 * @param {mw.echo.dm.SortedList} listModel Notifications list model for this source
9 * @param {Object} config Configuration object
10 * @param {boolean} [config.showTitle=false] Show the title of this group
11 * @param {boolean} [config.showMarkAllRead=false] Show a mark all read button for this group
12 * @param {boolean} [config.animateSorting=false] Animate the sorting of items
13 * @param {jQuery} [config.$overlay] A jQuery element functioning as an overlay
14 * for popups.
15 */
16 mw.echo.ui.SubGroupListWidget = function MwEchoUiSubGroupListWidget( controller, listModel, config ) {
17 this.$header = $( '<div>' )
18 .addClass( 'mw-echo-ui-subGroupListWidget-header' );
19
20 config = config || {};
21
22 this.controller = controller;
23 this.model = listModel;
24
25 // Parent constructor
26 mw.echo.ui.SubGroupListWidget.super.call( this, Object.assign( { data: this.getSource() }, config ) );
27
28 this.showTitle = !!config.showTitle;
29 this.showMarkAllRead = !!config.showMarkAllRead;
30 this.$overlay = config.$overlay || this.$element;
31
32 this.listWidget = new mw.echo.ui.SortedListWidget(
33 // Sorting callback
34 config.sortingCallback || ( ( a, b ) => {
35 // Reverse sorting
36 if ( b.getTimestamp() < a.getTimestamp() ) {
37 return -1;
38 } else if ( b.getTimestamp() > a.getTimestamp() ) {
39 return 1;
40 }
41
42 // Fallback on IDs
43 return b.getId() - a.getId();
44 } ),
45 // Config
46 {
47 $overlay: this.$overlay,
48 animated: config.animateSorting
49 }
50 );
51
52 const sourceURL = this.model.getSourceURL() ?
53 this.model.getSourceURL().replace( '$1', 'Special:Notifications' ) :
54 null;
55 if ( sourceURL ) {
56 this.title = new OO.ui.ButtonWidget( {
57 framed: false,
58 classes: [
59 'mw-echo-ui-subGroupListWidget-header-row-title',
60 'mw-echo-ui-subGroupListWidget-header-row-cell'
61 ],
62 href: sourceURL
63 } );
64 } else {
65 this.title = new OO.ui.LabelWidget( {
66 classes: [
67 'mw-echo-ui-subGroupListWidget-header-row-title',
68 'mw-echo-ui-subGroupListWidget-header-row-cell'
69 ]
70 } );
71 }
72
73 if ( this.model.getTitle() ) {
74 this.title.setLabel( this.model.getTitle() );
75 }
76 this.title.toggle( this.showTitle );
77
78 // Mark all as read button
79 this.markAllReadButton = new OO.ui.ButtonWidget( {
80 framed: true,
81 icon: 'checkAll',
82 label: mw.msg( 'echo-specialpage-section-markread' ),
83 classes: [
84 'mw-echo-ui-subGroupListWidget-header-row-markAllReadButton',
85 'mw-echo-ui-subGroupListWidget-header-row-cell'
86 ]
87 } );
88
89 // Events
90 this.model.connect( this, {
91 // Cross-wiki items can be discarded when marked as read.
92 // We need to differentiate this explicit action from the
93 // action of 'remove' because 'remove' is also used when
94 // an item is resorted by OO.SortedEmitterWidget before
95 // it is re-added again
96 discard: 'onModelDiscardItems',
97 // Update all items
98 update: 'resetItemsFromModel'
99 } );
100 this.model.connect( this, { itemUpdate: 'toggleMarkAllReadButton' } );
101 this.markAllReadButton.connect( this, { click: 'onMarkAllReadButtonClick' } );
102
103 // Initialize
104 this.toggleMarkAllReadButton();
105
106 this.$element
107 .addClass( 'mw-echo-ui-subGroupListWidget' )
108 .append(
109 this.$header.append(
110 $( '<div>' )
111 .addClass( 'mw-echo-ui-subGroupListWidget-header-row' )
112 .append(
113 this.title.$element,
114 this.markAllReadButton.$element
115 )
116 ),
117 this.listWidget.$element
118 );
119
120 // eslint-disable-next-line no-jquery/no-global-selector
121 this.$pageContentText = $( '#mw-content-text' );
122 $( window ).on( 'resize', this.resizeHeader.bind( this ) );
123
124 // Resize the header after the stack finishes loading
125 // so the widget is attached
126 setTimeout( this.resizeHeader.bind( this ), 0 );
127 };
128
129 /* Initialization */
130
131 OO.inheritClass( mw.echo.ui.SubGroupListWidget, OO.ui.Widget );
132
133 /* Methods */
134
135 /**
136 * Respond to window resize event
137 */
138 mw.echo.ui.SubGroupListWidget.prototype.resizeHeader = function () {
139 const contentWidth = this.$pageContentText.width(),
140 screenTooNarrow = this.$header.width() > contentWidth;
141
142 // Screen too narrow, put the button under the date
143 this.title.$element.toggleClass(
144 'mw-echo-ui-subGroupListWidget-header-row-cell',
145 !screenTooNarrow
146 );
147 this.markAllReadButton.$element.toggleClass(
148 'mw-echo-ui-subGroupListWidget-header-row-cell',
149 !screenTooNarrow
150 );
151 };
152
153 /**
154 * Destroy the widget and disconnect events
155 */
156 mw.echo.ui.SubGroupListWidget.prototype.destroy = function () {
157 $( window ).off( 'resize' );
158 };
159
160 /**
161 * Toggle the visibility of the mark all read button for this group
162 * based on whether there are unread notifications
163 */
164 mw.echo.ui.SubGroupListWidget.prototype.toggleMarkAllReadButton = function () {
165 this.markAllReadButton.toggle( this.showMarkAllRead && this.hasUnread() );
166 };
167
168 /**
169 * Respond to 'mark all as read' button click
170 */
171 mw.echo.ui.SubGroupListWidget.prototype.onMarkAllReadButtonClick = function () {
172 this.controller.markEntireListModelRead( this.model.getName() );
173 };
174
175 /**
176 * Check whether this sub group list has any unread notifications
177 *
178 * @return {boolean} Sub group has unread notifications
179 */
180 mw.echo.ui.SubGroupListWidget.prototype.hasUnread = function () {
181 return this.model.getItems().some( ( item ) => !item.isRead() );
182 };
183
184 /**
185 * Reset the items and rebuild them according to the model.
186 *
187 * @param {mw.echo.dm.NotificationItem[]} [items] Item models that are added.
188 * If this is empty, the widget will request all the items from the model.
189 */
190 mw.echo.ui.SubGroupListWidget.prototype.resetItemsFromModel = function ( items ) {
191 const itemWidgets = [];
192 let $elements = $();
193
194 items = items || this.model.getItems();
195
196 for ( let i = 0; i < items.length; i++ ) {
197 const widget = new mw.echo.ui.SingleNotificationItemWidget(
198 this.controller,
199 items[ i ],
200 {
201 $overlay: this.$overlay,
202 bundle: items[ i ].isBundled()
203 }
204 );
205 itemWidgets.push( widget );
206 $elements = $elements.add( widget.$element );
207 }
208
209 // Clear the current items if any exist
210 this.getListWidget().clearItems();
211
212 // fire render hook
213 mw.hook( 'ext.echo.notifications.beforeRender' ).fire( this.$element, $elements );
214
215 // Add the new items
216 this.getListWidget().addItems( itemWidgets );
217 };
218
219 /**
220 * Respond to model remove event. This may happen when an item
221 * is marked as read.
222 *
223 * @param {mw.echo.dm.NotificationItem[]} items Notification item models
224 */
225 mw.echo.ui.SubGroupListWidget.prototype.onModelDiscardItems = function ( items ) {
226 const itemWidgets = [];
227
228 for ( let i = 0; i < items.length; i++ ) {
229 itemWidgets.push( this.listWidget.getItemFromId( items[ i ].getId() ) );
230 }
231 this.listWidget.removeItems( itemWidgets );
232 };
233
234 /**
235 * Get the associated list widget. This is useful to specifically
236 * add and/or remove items from the list.
237 *
238 * @return {mw.echo.ui.SortedListWidget} List widget
239 */
240 mw.echo.ui.SubGroupListWidget.prototype.getListWidget = function () {
241 return this.listWidget;
242 };
243
244 /**
245 * Get the timestamp for the list
246 *
247 * @return {number} Timestamp
248 */
249 mw.echo.ui.SubGroupListWidget.prototype.getTimestamp = function () {
250 return this.model.getTimestamp();
251 };
252
253 /**
254 * Toggle the visibility of the title
255 *
256 * @param {boolean} show Show the title
257 */
258 mw.echo.ui.SubGroupListWidget.prototype.toggleTitle = function ( show ) {
259 show = show !== undefined ? show : !this.showTitle;
260
261 if ( this.showTitle !== show ) {
262 this.showTitle = show;
263 this.title.toggle( this.showTitle );
264 }
265 };
266
267 /**
268 * Get a the source of this list.
269 *
270 * @return {string} Group source
271 */
272 mw.echo.ui.SubGroupListWidget.prototype.getSource = function () {
273 return this.model.getSource();
274 };
275
276 /**
277 * Get an array of IDs of all of the items in this group
278 *
279 * @return {number[]} Array of item IDs
280 */
281 mw.echo.ui.SubGroupListWidget.prototype.getAllItemIDs = function () {
282 return this.model.getAllItemIds();
283 };
284
285 /**
286 * Get an array of IDs of all of the items in this group that
287 * correspond to a specific type
288 *
289 * @param {string} type Item type
290 * @return {number[]} Array of item IDs
291 */
292 mw.echo.ui.SubGroupListWidget.prototype.getAllItemIDsByType = function ( type ) {
293 return this.model.getAllItemIdsByType( type );
294 };
295
296 /**
297 * Check whether this group is foreign
298 *
299 * @return {boolean} This group is foreign
300 */
301 mw.echo.ui.SubGroupListWidget.prototype.isForeign = function () {
302 return this.model.isForeign();
303 };
304
305 /**
306 * Get the group id, which is represented by its model symbolic name.
307 * This is meant for sorting callbacks that fallback on
308 * sorting by IDs.
309 *
310 * @return {string} Group source
311 */
312 mw.echo.ui.SubGroupListWidget.prototype.getId = function () {
313 return this.model.getName();
314 };
315 }() );