Debian Codegraph @

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

raw symbols SCIP
1 ( function () {
2 /**
3 * A notifications list organized and separated by dates
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} modelManager Model manager
12 * @param {Object} [config] Configuration object
13 * @param {boolean} [config.animateSorting=false] Animate the sorting of items
14 * @param {jQuery} [config.$overlay] An overlay for the popup menus
15 */
16 mw.echo.ui.DatedNotificationsWidget = function MwEchoUiDatedNotificationsListWidget( controller, modelManager, config ) {
17 config = config || {};
18
19 // Parent constructor
20 mw.echo.ui.DatedNotificationsWidget.super.call( this, config );
21 // Mixin constructors
22 OO.ui.mixin.PendingElement.call( this, config );
23
24 this.manager = modelManager;
25 this.controller = controller;
26 this.models = {};
27
28 this.$overlay = config.$overlay || this.$element;
29 this.animateSorting = !!config.animateSorting;
30
31 this.listWidget = new mw.echo.ui.SortedListWidget(
32 // Sorting callback
33 ( ( a, b ) => {
34 // Reverse sorting
35 if ( b.getTimestamp() < a.getTimestamp() ) {
36 return -1;
37 } else if ( b.getTimestamp() > a.getTimestamp() ) {
38 return 1;
39 }
40 } ),
41 // Config
42 {
43 classes: [ 'mw-echo-ui-datedNotificationsWidget-group' ],
44 $overlay: this.$overlay,
45 animated: false
46 }
47 );
48
49 // Events
50 this.manager.connect( this, {
51 update: 'populateFromModel',
52 discard: 'onManagerDiscardModel'
53 } );
54
55 this.$element
56 .addClass( 'mw-echo-ui-datedNotificationsWidget' )
57 .append( this.listWidget.$element );
58
59 // Initialization
60 this.populateFromModel();
61 };
62 /* Initialization */
63
64 OO.inheritClass( mw.echo.ui.DatedNotificationsWidget, OO.ui.Widget );
65 OO.mixinClass( mw.echo.ui.DatedNotificationsWidget, OO.ui.mixin.PendingElement );
66
67 mw.echo.ui.DatedNotificationsWidget.prototype.onManagerDiscardModel = function ( modelId ) {
68 const model = this.models[ modelId ],
69 list = this.getList();
70
71 if ( model ) {
72 const group = list.getItemFromId( model.getName() );
73 list.removeItems( [ group ] );
74 }
75 };
76 /**
77 * Respond to model removing source group
78 *
79 * @param {string} source Symbolic name of the source group
80 */
81 mw.echo.ui.DatedNotificationsWidget.prototype.onModelRemoveSource = function ( source ) {
82 const list = this.getList(),
83 group = list.getItemFromId( source );
84
85 list.removeItems( [ group ] );
86 };
87
88 /**
89 * Respond to model manager update event.
90 * This event means we are repopulating the entire list and the
91 * associated models within it.
92 *
93 * @param {Object} models List models, indexed by ID
94 */
95 mw.echo.ui.DatedNotificationsWidget.prototype.populateFromModel = function ( models ) {
96 const groupWidgets = [];
97
98 // Detach all attached models
99 for ( const modelId in this.models ) {
100 this.detachModel( modelId );
101 }
102
103 for ( const model in models ) {
104 // Create SubGroup widgets
105 const subgroupWidget = new mw.echo.ui.DatedSubGroupListWidget(
106 this.controller,
107 models[ model ],
108 {
109 showTitle: true,
110 showMarkAllRead: true,
111 $overlay: this.$overlay,
112 animated: this.animateSorting
113 }
114 );
115 this.attachModel( model, models[ model ] );
116
117 subgroupWidget.resetItemsFromModel();
118 groupWidgets.push( subgroupWidget );
119 }
120
121 this.getList().getItems().forEach( ( widget ) => {
122 // Destroy all available widgets
123 widget.destroy();
124 } );
125
126 // Reset the list and re-add the items
127 this.getList().clearItems();
128 this.getList().addItems( groupWidgets );
129 };
130
131 /**
132 * Attach a model to the widget
133 *
134 * @param {string} modelId Symbolic name for the model
135 * @param {mw.echo.dm.SortedList} model Notifications list model
136 */
137 mw.echo.ui.DatedNotificationsWidget.prototype.attachModel = function ( modelId, model ) {
138 this.models[ modelId ] = model;
139 };
140
141 /**
142 * Detach a model from the widget
143 *
144 * @param {string} modelId Notifications list model
145 */
146 mw.echo.ui.DatedNotificationsWidget.prototype.detachModel = function ( modelId ) {
147 this.models[ modelId ].disconnect( this );
148 delete this.models[ modelId ];
149 };
150
151 /**
152 * Get the list widget contained in this item
153 *
154 * @return {mw.echo.ui.SortedListWidget} List widget
155 */
156 mw.echo.ui.DatedNotificationsWidget.prototype.getList = function () {
157 return this.listWidget;
158 };
159
160 /**
161 * Get the number of all notifications in all sections of the widget
162 *
163 * @return {number} The number of all notifications
164 */
165 mw.echo.ui.DatedNotificationsWidget.prototype.getAllNotificationCount = function () {
166 const groups = this.getList().getItems();
167
168 let count = 0;
169 for ( let i = 0; i < groups.length; i++ ) {
170 count += groups[ i ].getListWidget().getItemCount();
171 }
172
173 return count;
174 };
175
176 }() );