Debian Codegraph @

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

raw symbols SCIP
1 ( function () {
2 /**
3 * An inbox-type widget that encompases a dated notifications list with pagination
4 *
5 * @class
6 * @extends OO.ui.Widget
7 * @mixes OO.ui.mixin.PendingElement
8 *
9 * @constructor
10 * @param {mw.echo.Controller} controller Echo controller
11 * @param {mw.echo.dm.ModelManager} manager Model manager
12 * @param {Object} [config] Configuration object
13 * @param {number} [config.limit=25] Limit the number of notifications per page
14 * @param {string} [config.prefLink] Link to preferences page
15 * @param {jQuery} [config.$overlay] An overlay for the popup menus
16 */
17 mw.echo.ui.NotificationsInboxWidget = function MwEchoUiNotificationsInboxWidget( controller, manager, config ) {
18 config = config || {};
19
20 // Parent constructor
21 mw.echo.ui.NotificationsInboxWidget.super.call( this, config );
22 // Mixin constructors
23 OO.ui.mixin.PendingElement.call( this, config );
24
25 this.controller = controller;
26 this.manager = manager;
27
28 this.$overlay = config.$overlay || this.$element;
29 this.limit = config.limit || 25;
30
31 this.error = false;
32
33 // A notice or error message widget
34 this.noticeMessageWidget = new OO.ui.LabelWidget( {
35 classes: [ 'mw-echo-ui-notificationsInboxWidget-notice' ]
36 } );
37
38 // Notifications list
39 this.datedListWidget = new mw.echo.ui.DatedNotificationsWidget(
40 this.controller,
41 this.manager,
42 {
43 $overlay: this.$overlay,
44 animateSorting: false
45 }
46 );
47 this.setPendingElement( this.datedListWidget.$element );
48
49 // Pagination
50 this.topPaginationWidget = new mw.echo.ui.PaginationWidget(
51 this.manager.getPaginationModel(),
52 {
53 itemsPerPage: this.limit
54 }
55 );
56 this.bottomPaginationWidget = new mw.echo.ui.PaginationWidget(
57 this.manager.getPaginationModel(),
58 {
59 itemsPerPage: this.limit
60 }
61 );
62
63 // Settings menu
64 this.settingsMenu = new mw.echo.ui.SpecialHelpMenuWidget(
65 this.manager,
66 {
67 framed: true,
68 prefLink: config.prefLink,
69 $overlay: this.$overlay
70 }
71 );
72
73 // Filter by read state
74 this.readStateSelectWidget = new mw.echo.ui.ReadStateButtonSelectWidget();
75
76 // Sidebar filters
77 this.xwikiUnreadWidget = new mw.echo.ui.CrossWikiUnreadFilterWidget(
78 this.controller,
79 this.manager.getFiltersModel()
80 );
81
82 // Events
83 this.readStateSelectWidget.connect( this, { filter: 'onReadStateFilter' } );
84 this.xwikiUnreadWidget.connect( this, { filter: 'onSourcePageFilter' } );
85 this.manager.connect( this, {
86 modelItemUpdate: 'updatePaginationLabels',
87 localCountChange: 'updatePaginationLabels'
88 } );
89 this.manager.getFiltersModel().connect( this, { update: 'updateReadStateSelectWidget' } );
90 this.manager.getPaginationModel().connect( this, { update: 'updatePaginationLabels' } );
91 this.topPaginationWidget.connect( this, { change: 'populateNotifications' } );
92 this.bottomPaginationWidget.connect( this, { change: 'populateNotifications' } );
93 this.settingsMenu.connect( this, { markAllRead: 'onSettingsMarkAllRead' } );
94
95 this.topPaginationWidget.setDisabled( true );
96 this.bottomPaginationWidget.setDisabled( true );
97
98 this.$topToolbar =
99 $( '<div>' )
100 .addClass( 'mw-echo-ui-notificationsInboxWidget-main-toolbar-top' )
101 .append(
102 $( '<div>' )
103 .addClass( 'mw-echo-ui-notificationsInboxWidget-row' )
104 .append(
105 $( '<div>' )
106 .addClass( 'mw-echo-ui-notificationsInboxWidget-cell-placeholder' )
107 .append(
108 $( '<div>' )
109 .addClass( 'mw-echo-ui-notificationsInboxWidget-main-toolbar-readState' )
110 .addClass( 'mw-echo-ui-notificationsInboxWidget-cell' )
111 .append( this.readStateSelectWidget.$element )
112 ),
113 $( '<div>' )
114 .addClass( 'mw-echo-ui-notificationsInboxWidget-main-toolbar-pagination' )
115 .addClass( 'mw-echo-ui-notificationsInboxWidget-cell' )
116 .append( this.topPaginationWidget.$element ),
117 mw.user.isTemp() ? '' : $( '<div>' )
118 .addClass( 'mw-echo-ui-notificationsInboxWidget-main-toolbar-settings' )
119 .addClass( 'mw-echo-ui-notificationsInboxWidget-cell' )
120 .append( this.settingsMenu.$element )
121 )
122 );
123
124 this.$toolbarWrapper =
125 $( '<div>' )
126 .addClass( 'mw-echo-ui-notificationsInboxWidget-toolbarWrapper' )
127 .append( this.$topToolbar );
128
129 const $sidebar = $( '<div>' )
130 .addClass( 'mw-echo-ui-notificationsInboxWidget-sidebar' )
131 .append( this.xwikiUnreadWidget.$element );
132
133 const $main = $( '<div>' )
134 .addClass( 'mw-echo-ui-notificationsInboxWidget-main' )
135 .append(
136 this.$toolbarWrapper,
137 this.noticeMessageWidget.$element,
138 this.datedListWidget.$element
139 );
140
141 this.$element
142 .addClass( 'mw-echo-ui-notificationsInboxWidget' )
143 .append(
144 $( '<div>' )
145 .addClass( 'mw-echo-ui-notificationsInboxWidget-row' )
146 .append(
147 $sidebar
148 .addClass( 'mw-echo-ui-notificationsInboxWidget-cell' ),
149 $main
150 .addClass( 'mw-echo-ui-notificationsInboxWidget-cell' )
151 )
152 );
153
154 this.updateReadStateSelectWidget();
155 this.xwikiUnreadWidget.populateSources();
156 this.populateNotifications();
157
158 };
159
160 /* Initialization */
161
162 OO.inheritClass( mw.echo.ui.NotificationsInboxWidget, OO.ui.Widget );
163 OO.mixinClass( mw.echo.ui.NotificationsInboxWidget, OO.ui.mixin.PendingElement );
164
165 /* Methods */
166
167 /**
168 * Respond to filters update
169 */
170 mw.echo.ui.NotificationsInboxWidget.prototype.updateReadStateSelectWidget = function () {
171 this.readStateSelectWidget
172 .findItemFromData( this.manager.getFiltersModel().getReadState() )
173 .setSelected( true );
174 };
175
176 /**
177 * Update pagination messages
178 */
179 mw.echo.ui.NotificationsInboxWidget.prototype.updatePaginationLabels = function () {
180 this.resetMessageLabel();
181 // Update the pagination label
182 this.topPaginationWidget.updateLabel();
183 this.bottomPaginationWidget.updateLabel();
184 };
185
186 /**
187 * Respond to mark all read for selected wiki
188 */
189 mw.echo.ui.NotificationsInboxWidget.prototype.onSettingsMarkAllRead = function () {
190 this.pushPending();
191 this.controller.markAllRead()
192 .always( this.popPending.bind( this ) );
193 };
194
195 /**
196 * Respond to unread page filter
197 *
198 * @param {string} source Source symbolic name
199 * @param {string} page Page name
200 */
201 mw.echo.ui.NotificationsInboxWidget.prototype.onSourcePageFilter = function ( source, page ) {
202 this.controller.setFilter( 'sourcePage', source, page );
203 this.populateNotifications();
204 };
205
206 /**
207 * Respond to read state filter event
208 *
209 * @param {string} readState Read state 'all', 'read' or 'unread'
210 */
211 mw.echo.ui.NotificationsInboxWidget.prototype.onReadStateFilter = function ( readState ) {
212 this.controller.setFilter( 'readState', readState );
213 this.populateNotifications();
214 };
215
216 /**
217 * Populate the notifications list
218 *
219 * @param {string} [direction] Direction to fetch from. 'prev' for previous page
220 * or 'next' for the next page. If not given, the first page of results will be fetched.
221 * @return {jQuery.Promise} A promise that is resolved when the results
222 * have been fetched.
223 */
224 mw.echo.ui.NotificationsInboxWidget.prototype.populateNotifications = function ( direction ) {
225 let fetchPromise;
226 if ( direction === 'prev' ) {
227 fetchPromise = this.controller.fetchPrevPageByDate();
228 } else if ( direction === 'next' ) {
229 fetchPromise = this.controller.fetchNextPageByDate();
230 } else {
231 fetchPromise = this.controller.fetchFirstPageByDate();
232 }
233
234 this.pushPending();
235 this.error = false;
236 return fetchPromise
237 .then(
238 // Success
239 () => {
240 // Fire initialization hook
241 mw.hook( 'ext.echo.special.onInitialize' ).fire( this.controller.manager.getTypeString(), this.controller );
242
243 this.popPending();
244 // Update seen time
245 this.controller.updateSeenTime();
246 },
247 // Failure
248 ( errObj ) => {
249 let msg;
250 if ( errObj.errCode === 'notlogin-required' ) {
251 // Login required message
252 msg = mw.msg( 'echo-notification-loginrequired' );
253 } else {
254 // Generic API failure message
255 msg = mw.msg( 'echo-api-failure' );
256 }
257 this.error = true;
258 this.noticeMessageWidget.setLabel( msg );
259 this.displayMessage( true );
260 }
261 )
262 .always( this.popPending.bind( this ) );
263 };
264
265 /**
266 * Extend the pushPending method to disable UI elements
267 */
268 mw.echo.ui.NotificationsInboxWidget.prototype.pushPending = function () {
269 this.noticeMessageWidget.toggle( false );
270 this.topPaginationWidget.setDisabled( true );
271 this.bottomPaginationWidget.setDisabled( true );
272
273 // Mixin method
274 OO.ui.mixin.PendingElement.prototype.pushPending.call( this );
275 };
276
277 /**
278 * Extend the popPending method to enable UI elements
279 */
280 mw.echo.ui.NotificationsInboxWidget.prototype.popPending = function () {
281 if ( !this.error ) {
282 this.resetMessageLabel();
283 }
284
285 this.topPaginationWidget.setDisabled( false );
286 this.bottomPaginationWidget.setDisabled( false );
287
288 // Mixin method
289 OO.ui.mixin.PendingElement.prototype.popPending.call( this );
290 };
291
292 /**
293 * Reset the text of the error message that displays in place of the list
294 * in case the list is empty.
295 */
296 mw.echo.ui.NotificationsInboxWidget.prototype.resetMessageLabel = function () {
297 const count = this.manager.getPaginationModel().getCurrentPageItemCount();
298
299 if ( count === 0 ) {
300 const label = this.manager.getFiltersModel().getReadState() === 'all' ?
301 mw.msg( 'echo-notification-placeholder' ) :
302 mw.msg( 'echo-notification-placeholder-filters' );
303
304 this.noticeMessageWidget.setLabel( label );
305 }
306
307 this.displayMessage( count === 0 );
308 };
309
310 /**
311 * Display the error/notice message instead of the notifications list or vise versa.
312 *
313 * @private
314 * @param {boolean} displayMessage Display error message
315 */
316 mw.echo.ui.NotificationsInboxWidget.prototype.displayMessage = function ( displayMessage ) {
317 this.noticeMessageWidget.toggle( displayMessage );
318 this.datedListWidget.toggle( !displayMessage );
319 };
320
321 }() );