Debian Codegraph @

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

raw symbols SCIP
1 ( function () {
2 /**
3 * Cross-wiki notification item widget.
4 * This widget is expandable and displays groups of
5 * notification item lists by their sources.
6 *
7 * TODO: When we have local bundles (without groups of lists)
8 * we can separate the "expand" functionality and UI to another mixin
9 * so we can use it with both widgets.
10 *
11 * @class
12 * @extends mw.echo.ui.NotificationItemWidget
13 * @mixes OO.ui.mixin.PendingElement
14 *
15 * @constructor
16 * @param {mw.echo.Controller} controller Echo notifications controller
17 * @param {mw.echo.dm.CrossWikiNotificationItem} model Notification group model
18 * @param {Object} [config] Configuration object
19 * @param {boolean} [config.animateSorting=false] Animate the sorting of items
20 */
21 mw.echo.ui.CrossWikiNotificationItemWidget = function MwEchoUiCrossWikiNotificationItemWidget( controller, model, config ) {
22 config = config || {};
23
24 // Parent constructor
25 mw.echo.ui.CrossWikiNotificationItemWidget.super.call( this, controller, model, config );
26 // Mixin constructors
27 OO.ui.mixin.PendingElement.call( this, config );
28
29 // In cross-wiki groups we only have 'mark as read'
30 this.toggleMarkAsReadButtons( true );
31
32 this.listWidget = new mw.echo.ui.SortedListWidget(
33 // Sorting callback
34 ( ( a, b ) => {
35 // Define the sorting order.
36 // This will go by the lists' timestamp, which in turn
37 // take the latest timestamp in their items
38 if ( b.getTimestamp() < a.getTimestamp() ) {
39 return -1;
40 } else if ( b.getTimestamp() > a.getTimestamp() ) {
41 return 1;
42 }
43
44 // Fallback on IDs
45 return b.getSource() - a.getSource();
46 } ),
47 // Config
48 {
49 classes: [ 'mw-echo-ui-crossWikiNotificationItemWidget-group' ],
50 timestamp: this.getTimestamp(),
51 $overlay: this.$overlay,
52 animated: config.animateSorting
53 }
54 );
55
56 this.listWidget.$element
57 // We have to manually set the display to 'none' here because
58 // otherwise the 'slideUp' and 'slideDown' jQuery effects don't
59 // work
60 .css( 'display', 'none' );
61 this.setPendingElement( this.listWidget.$element );
62
63 this.errorWidget = new mw.echo.ui.PlaceholderItemWidget();
64 this.errorWidget.toggle( false );
65
66 // Initialize closed
67 this.showTitles = true;
68 this.expanded = false;
69 this.fetchedOnce = false;
70
71 // Add "expand" button
72 this.toggleExpandButton = new OO.ui.ButtonOptionWidget( {
73 icon: 'expand',
74 framed: false,
75 classes: [ 'mw-echo-ui-notificationItemWidget-content-actions-button' ]
76 } );
77 this.updateExpandButton();
78 this.actionsButtonSelectWidget.addItems( [ this.toggleExpandButton ] );
79
80 // Events
81 this.model.connect( this, { discard: 'onModelDiscard' } );
82 this.toggleExpandButton.connect( this, { click: 'expand' } );
83 this.$content.on( 'click', this.expand.bind( this ) );
84
85 // TODO: Handle cases where the group became empty or a case where the group only has 1 item left.
86 // Note: Right now this code works primarily for cross-wiki notifications. These act differently
87 // than local bundles. Cross-wiki notifications, when they "lose" their items for being read, they
88 // vanish from the list. Unlike them, the plan for local bundles is that read sub-items go outside
89 // the bundle and become their own items in the general notificationsWidget, and when the local bundle
90 // has 1 notification left, the group will actually transform into that last notification item.
91 // We don't listen to the empty event right now, because the entire item is deleted in cross-wiki
92 // notifications. When we work on local bundles, we will have to add that event listener per item.
93
94 // Initialization
95 this.populateFromModel();
96 this.toggleExpanded( false );
97 this.toggleRead( false );
98 this.toggleTitles( true );
99 this.$element
100 .addClass( 'mw-echo-ui-crossWikiNotificationItemWidget' )
101 .append(
102 $( '<div>' )
103 .addClass( 'mw-echo-ui-crossWikiNotificationItemWidget-separator' ),
104 this.listWidget.$element,
105 this.errorWidget.$element
106 );
107 };
108
109 /* Initialization */
110
111 OO.inheritClass( mw.echo.ui.CrossWikiNotificationItemWidget, mw.echo.ui.NotificationItemWidget );
112 OO.mixinClass( mw.echo.ui.CrossWikiNotificationItemWidget, OO.ui.mixin.PendingElement );
113
114 /* Methods */
115
116 /**
117 * Respond to model removing source group
118 *
119 * @param {string} groupName Symbolic name of the group
120 */
121 mw.echo.ui.CrossWikiNotificationItemWidget.prototype.onModelDiscard = function ( groupName ) {
122 const list = this.getList(),
123 group = list.getItemFromId( groupName );
124
125 list.removeItems( [ group ] );
126
127 if ( list.isEmpty() ) {
128 this.controller.removeCrossWikiItem();
129 }
130 };
131
132 /**
133 * @inheritdoc
134 */
135 mw.echo.ui.CrossWikiNotificationItemWidget.prototype.markRead = function () {
136 // Cross wiki notification is always only marked as read, never as
137 // unread. The original parameter is unneeded
138 this.controller.markEntireCrossWikiItemAsRead();
139 };
140
141 /**
142 * Populate the items in this widget according to the data
143 * in the model
144 */
145 mw.echo.ui.CrossWikiNotificationItemWidget.prototype.populateFromModel = function () {
146 const groupWidgets = [],
147 groups = this.model.getList().getItems();
148
149 for ( let i = 0; i < groups.length; i++ ) {
150 // Create SubGroup widgets
151 groupWidgets.push(
152 new mw.echo.ui.SubGroupListWidget(
153 this.controller,
154 groups[ i ],
155 {
156 $overlay: this.$overlay,
157 showTitle: this.showTitles
158 }
159 )
160 );
161 }
162 this.getList().addItems( groupWidgets );
163 };
164
165 /**
166 * Toggle the visibility of the titles
167 *
168 * @param {boolean} [show] Show titles
169 */
170 mw.echo.ui.CrossWikiNotificationItemWidget.prototype.toggleTitles = function ( show ) {
171 const items = this.getList().getItems();
172
173 show = show === undefined ? !this.showTitles : show;
174
175 if ( this.showTitles !== show ) {
176 this.showTitles = show;
177 for ( let i = 0; i < items.length; i++ ) {
178 items[ i ].toggleTitle( show );
179 }
180 }
181 };
182
183 /**
184 * Check whether the titles should be shown and toggle them
185 * in each of the group lists.
186 */
187 mw.echo.ui.CrossWikiNotificationItemWidget.prototype.checkShowTitles = function () {
188 this.toggleTitles( this.getList().getItemCount() > 1 );
189 };
190
191 /**
192 * Toggle the visibility of the notification item list and the placeholder/error widget.
193 *
194 * @param {boolean} showList Show the list
195 */
196 mw.echo.ui.CrossWikiNotificationItemWidget.prototype.toggleListDisplay = function ( showList ) {
197 this.errorWidget.toggle( !showList );
198 this.listWidget.toggle( showList );
199 };
200
201 /**
202 * Show an error message
203 *
204 * @param {string} label Error label
205 * @param {string} link Error link
206 */
207 mw.echo.ui.CrossWikiNotificationItemWidget.prototype.showErrorMessage = function ( label, link ) {
208 this.errorWidget.setLabel( label || '' );
209 this.errorWidget.setLink( link || '' );
210
211 this.toggleListDisplay( false );
212 };
213
214 /**
215 * Expand the group and fetch the list of notifications.
216 * Only fetch the first time we expand.
217 */
218 mw.echo.ui.CrossWikiNotificationItemWidget.prototype.expand = function () {
219 this.toggleExpanded( !this.expanded );
220 this.updateExpandButton();
221
222 if ( !this.expanded ) {
223 return;
224 }
225
226 if ( !this.fetchedOnce ) {
227 // Expand
228 this.pushPending();
229 this.toggleListDisplay( true );
230 // Query all sources
231 this.controller.fetchCrossWikiNotifications()
232 .catch(
233 ( result ) => {
234 const loginPageTitle = mw.Title.newFromText( 'Special:UserLogin' );
235 // If failure, check if the failure is due to login
236 // so we can display a more comprehensive error
237 // message in that case
238 if ( result.errCode === 'notlogin-required' ) {
239 // Login error
240 this.showErrorMessage(
241 mw.message( 'echo-notification-popup-loginrequired' ),
242 // Set the option link to the login page
243 loginPageTitle.getUrl()
244 );
245 } else {
246 // General error
247 this.showErrorMessage( mw.msg( 'echo-api-failure' ) );
248 }
249 }
250 )
251 .always( this.popPending.bind( this ) );
252
253 // Only run the fetch notifications action once
254 this.fetchedOnce = true;
255 }
256 };
257
258 /**
259 * Toggle the expand/collapsed state of this group widget
260 *
261 * @param {boolean} show Show the widget expanded
262 */
263 mw.echo.ui.CrossWikiNotificationItemWidget.prototype.toggleExpanded = function ( show ) {
264 this.expanded = show !== undefined ? !!show : !this.expanded;
265
266 this.$element.toggleClass( 'mw-echo-ui-crossWikiNotificationItemWidget-expanded', this.expanded );
267
268 if ( this.expanded ) {
269 // FIXME: Use CSS transition
270 // eslint-disable-next-line no-jquery/no-slide
271 this.getList().$element.slideDown();
272 } else {
273 // eslint-disable-next-line no-jquery/no-slide
274 this.getList().$element.slideUp();
275 }
276 };
277
278 /**
279 * Update the expand button label
280 */
281 mw.echo.ui.CrossWikiNotificationItemWidget.prototype.updateExpandButton = function () {
282 const type = this.model.getType();
283
284 this.toggleExpandButton.setLabel(
285 this.expanded ?
286 mw.msg( 'notification-link-text-collapse-all' ) :
287 // Messages that appear here are:
288 // * notification-link-text-expand-alert-count
289 // * notification-link-text-expand-notice-count
290 mw.msg(
291 'notification-link-text-expand-' +
292 ( type === 'message' ? 'notice' : type ) +
293 '-count',
294 mw.language.convertNumber( this.model.getCount() )
295 )
296 );
297 this.toggleExpandButton.setIcon(
298 this.expanded ?
299 'collapse' :
300 'expand'
301 );
302 };
303
304 /**
305 * Get the list widget contained in this item
306 *
307 * @return {mw.echo.ui.SortedListWidget} List widget
308 */
309 mw.echo.ui.CrossWikiNotificationItemWidget.prototype.getList = function () {
310 return this.listWidget;
311 };
312 }() );