Debian Codegraph @

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

raw symbols SCIP
1 ( function () {
2 /**
3 * Notifications list widget.
4 * All of its items must be of the mw.echo.ui.NotificationItem type.
5 *
6 * @class
7 * @extends mw.echo.ui.SortedListWidget
8 *
9 * @constructor
10 * @param {mw.echo.Controller} controller Echo notifications controller
11 * @param {mw.echo.dm.ModelManager} manager Model manager
12 * @param {Object} [config] Configuration object
13 * marked as read when they are seen.
14 * @param {string} [config.timestamp=0] A timestamp representing the latest item in
15 * the list.
16 * @param {jQuery} [config.$overlay] A jQuery element functioning as an overlay
17 * for popups.
18 */
19 mw.echo.ui.NotificationsListWidget = function MwEchoUiNotificationsListWidget( controller, manager, config ) {
20 config = config || {};
21 // Parent constructor
22 mw.echo.ui.NotificationsListWidget.super.call(
23 this,
24 // Sorting callback
25 ( a, b ) => {
26 if ( !a.isRead() && b.isRead() ) {
27 return -1; // Unread items are always above read items
28 } else if ( a.isRead() && !b.isRead() ) {
29 return 1;
30 } else if ( !a.isForeign() && b.isForeign() ) {
31 return -1;
32 } else if ( a.isForeign() && !b.isForeign() ) {
33 return 1;
34 }
35
36 // Reverse sorting
37 if ( b.getTimestamp() < a.getTimestamp() ) {
38 return -1;
39 } else if ( b.getTimestamp() > a.getTimestamp() ) {
40 return 1;
41 }
42
43 // Fallback on IDs
44 return b.getId() - a.getId();
45 },
46 config
47 );
48
49 // Initialize models
50 this.controller = controller;
51 this.manager = manager;
52 this.models = {};
53
54 // Properties
55 this.$overlay = config.$overlay || this.$element;
56 this.timestamp = config.timestamp || 0;
57
58 // Dummy 'loading' option widget
59 this.loadingOptionWidget = new mw.echo.ui.PlaceholderItemWidget();
60
61 this.resetLoadingOption();
62
63 this.manager.connect( this, {
64 update: 'resetDataFromModel',
65 discard: 'onModelManagerDiscard'
66 } );
67
68 this.$element
69 .addClass( 'mw-echo-ui-notificationsListWidget' );
70 };
71
72 /* Initialization */
73
74 OO.inheritClass( mw.echo.ui.NotificationsListWidget, mw.echo.ui.SortedListWidget );
75
76 /* Events */
77
78 /**
79 * The content of this list has changed.
80 * This event is to state that not only has the content changed
81 * but the actual DOM has been manipulated.
82 *
83 * @event mw.echo.ui.NotificationsListWidget#modified
84 */
85
86 /* Methods */
87
88 mw.echo.ui.NotificationsListWidget.prototype.onModelManagerDiscard = function ( modelName ) {
89 const items = this.getItems();
90
91 // For the moment, this is only relevant for xwiki bundles.
92 // Local single items will not get their entire model removed, but
93 // local bundles may - when that happens, the condition below should
94 // also deal with local bundles and removing them specifically
95 if ( modelName === 'xwiki' ) {
96 for ( let i = 0; i < items.length; i++ ) {
97 if ( items[ i ] instanceof mw.echo.ui.CrossWikiNotificationItemWidget ) {
98 this.removeItems( [ items[ i ] ] );
99 this.checkForEmptyNotificationsList();
100 return;
101 }
102 }
103 }
104
105 this.emit( 'modified' );
106 };
107
108 /**
109 * Respond to model manager update event.
110 * This event means we are repopulating the entire list and the
111 * associated models within it.
112 *
113 * @param {Object} models Object of new models to populate the
114 * list.
115 * @fires mw.echo.ui.NotificationsListWidget#modified
116 */
117 mw.echo.ui.NotificationsListWidget.prototype.resetDataFromModel = function ( models ) {
118 const itemWidgets = [];
119 let $elements = $();
120
121 // Detach all attached models
122 for ( const modelId in this.models ) {
123 this.detachModel( modelId );
124 }
125
126 // Attach and process new models
127 for ( const modelId in models ) {
128 const model = models[ modelId ];
129 this.attachModel( modelId, model );
130
131 // Build widgets based on the data in the model
132 if ( model.isGroup() ) {
133 let widget;
134 if ( model.isForeign() ) {
135 // One Widget to Rule Them All
136 widget = new mw.echo.ui.CrossWikiNotificationItemWidget(
137 this.controller,
138 model,
139 {
140 $overlay: this.$overlay,
141 animateSorting: this.animated
142 }
143 );
144 } else {
145 // local bundle
146 widget = new mw.echo.ui.BundleNotificationItemWidget(
147 this.controller,
148 model,
149 {
150 $overlay: this.$overlay,
151 bundle: false,
152 animateSorting: this.animated
153 }
154 );
155 }
156 itemWidgets.push( widget );
157 $elements = $elements.add( widget.$element );
158 } else {
159 const subItems = model.getItems();
160 // Separate widgets per item
161 for ( let i = 0; i < subItems.length; i++ ) {
162 const subItem = subItems[ i ];
163 const widget = new mw.echo.ui.SingleNotificationItemWidget(
164 this.controller,
165 subItem,
166 {
167 $overlay: this.$overlay,
168 bundle: false
169 }
170 );
171 itemWidgets.push( widget );
172 $elements = $elements.add( widget.$element );
173 }
174 }
175 }
176
177 // Reset the current items and re-add the new item widgets
178 this.clearItems();
179
180 // fire render hook
181 mw.hook( 'ext.echo.notifications.beforeRender' ).fire( this.$element, $elements );
182
183 this.addItems( itemWidgets );
184
185 this.checkForEmptyNotificationsList();
186
187 this.emit( 'modified' );
188 };
189
190 /**
191 * Attach a model to the widget
192 *
193 * @param {string} modelId Symbolic name for the model
194 * @param {mw.echo.dm.SortedList} model Notifications list model
195 */
196 mw.echo.ui.NotificationsListWidget.prototype.attachModel = function ( modelId, model ) {
197 this.models[ modelId ] = model;
198 };
199
200 /**
201 * Detach a model from the widget
202 *
203 * @param {string} modelId Notifications list model
204 */
205 mw.echo.ui.NotificationsListWidget.prototype.detachModel = function ( modelId ) {
206 this.models[ modelId ].disconnect( this );
207 delete this.models[ modelId ];
208 };
209
210 /**
211 * Reset the loading 'dummy' option widget
212 *
213 * @param {string} [label] Label for the option widget
214 * @param {string} [link] Link for the option widget
215 * @fires mw.echo.ui.NotificationsListWidget#modified
216 */
217 mw.echo.ui.NotificationsListWidget.prototype.resetLoadingOption = function ( label, link ) {
218 this.loadingOptionWidget.setLabel( label || '' );
219 this.loadingOptionWidget.setLink( link || '' );
220 if ( this.isEmpty() ) {
221 this.addItems( [ this.loadingOptionWidget ] );
222 }
223 this.emit( 'modified' );
224 };
225
226 /**
227 * Check if the list of notifications is empty and udpate the placeholder
228 * widget as needed.
229 */
230 mw.echo.ui.NotificationsListWidget.prototype.checkForEmptyNotificationsList = function () {
231 this.resetLoadingOption( this.isEmpty() ? mw.msg( 'echo-notification-placeholder' ) : '' );
232 };
233
234 /**
235 * Reset the 'initiallyUnseen' state of all items
236 */
237 mw.echo.ui.NotificationsListWidget.prototype.resetInitiallyUnseenItems = function () {
238 const itemWidgets = this.getItems();
239
240 for ( let i = 0; i < itemWidgets.length; i++ ) {
241 itemWidgets[ i ].resetInitiallyUnseen();
242 }
243 };
244 }() );