Debian Codegraph @

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

raw symbols SCIP
1 ( function () {
2 /**
3 * Bundle notification item widget.
4 * This widget is expandable and displays
5 * inner notifications that constitute the bundle.
6 *
7 * @class
8 * @extends mw.echo.ui.NotificationItemWidget
9 *
10 * @constructor
11 * @param {mw.echo.Controller} controller Echo notifications controller
12 * @param {mw.echo.dm.BundleNotificationItem} model Notification group model
13 * @param {Object} [config] Configuration object
14 * @param {boolean} [config.animateSorting=false] Animate the sorting of items
15 */
16 mw.echo.ui.BundleNotificationItemWidget = function MwEchoUiBundleNotificationItemWidget( controller, model, config ) {
17 config = config || {};
18
19 // Parent constructor
20 mw.echo.ui.BundleNotificationItemWidget.super.call( this, controller, model, config );
21
22 this.toggleMarkAsReadButtons( true );
23
24 this.listWidget = new mw.echo.ui.SortedListWidget(
25 // Sorting callback
26 ( ( a, b ) => {
27 // Reverse sorting
28 if ( b.getTimestamp() < a.getTimestamp() ) {
29 return -1;
30 } else if ( b.getTimestamp() > a.getTimestamp() ) {
31 return 1;
32 }
33
34 // Fallback on IDs
35 return b.getId() - a.getId();
36 } ),
37 // Config
38 {
39 classes: [ 'mw-echo-ui-bundleNotificationItemWidget-group' ],
40 timestamp: this.getTimestamp(),
41 $overlay: this.$overlay,
42 animated: !!config.animateSorting
43 }
44 );
45
46 this.listWidget.$element
47 // We have to manually set the display to 'none' here because
48 // otherwise the 'slideUp' and 'slideDown' jQuery effects don't
49 // work
50 .css( 'display', 'none' );
51
52 // Prevent clicks on the list padding area from activating the primary link
53 this.listWidget.$element.on( 'click', ( e ) => {
54 if ( e.target.closest( 'a' ) === this.$element[ 0 ] ) {
55 e.preventDefault();
56 }
57 } );
58
59 // Initialize closed
60 this.expanded = false;
61
62 // Add "expand" button
63 this.toggleExpandButton = new OO.ui.ButtonOptionWidget( {
64 icon: 'expand',
65 framed: false,
66 classes: [ 'mw-echo-ui-notificationItemWidget-content-actions-button' ]
67 } );
68 this.updateExpandButton();
69 this.actionsButtonSelectWidget.addItems( [ this.toggleExpandButton ], 0 );
70
71 // Events
72 this.toggleExpandButton.connect( this, { click: 'expand' } );
73
74 if ( !this.model.getPrimaryUrl() ) {
75 // If there's no primary link, make sure a click
76 // triggers the 'expand' action
77 this.$content.on( 'click', this.expand.bind( this ) );
78 }
79
80 // Initialization
81 this.populateFromModel();
82 this.toggleExpanded( false );
83 this.toggleRead( false );
84 this.$element
85 .addClass( 'mw-echo-ui-bundleNotificationItemWidget' )
86 .append(
87 $( '<div>' )
88 .addClass( 'mw-echo-ui-bundleNotificationItemWidget-separator' ),
89 this.listWidget.$element
90 );
91
92 // Events
93 this.model.connect( this, { update: 'updateDataFromModel' } );
94
95 // Update read and seen states from the model
96 this.updateDataFromModel();
97 };
98
99 /* Initialization */
100
101 OO.inheritClass( mw.echo.ui.BundleNotificationItemWidget, mw.echo.ui.NotificationItemWidget );
102
103 /* Methods */
104
105 /**
106 * @inheritdoc
107 */
108 mw.echo.ui.BundleNotificationItemWidget.prototype.markRead = function ( isRead ) {
109 this.controller.markEntireListModelRead( this.model.getModelName(), isRead );
110 };
111
112 /**
113 * Populate the items in this widget according to the data
114 * in the model
115 */
116 mw.echo.ui.BundleNotificationItemWidget.prototype.populateFromModel = function () {
117 this.getList().addItems( this.model.getList().getItems().map( ( singleNotifModel ) => new mw.echo.ui.SingleNotificationItemWidget(
118 this.controller,
119 singleNotifModel,
120 {
121 $overlay: this.$overlay,
122 bundle: true
123 }
124 ) ) );
125 };
126
127 /**
128 * Update item state when the item model changes.
129 *
130 * @fires OO.EventEmitter#sortChange
131 */
132 mw.echo.ui.BundleNotificationItemWidget.prototype.updateDataFromModel = function () {
133 this.toggleRead( this.model.isRead() );
134 this.toggleSeen( this.model.isSeen() );
135 this.emit( 'sortChange' );
136 };
137
138 /**
139 * Toggle the visibility of the notification item list and the placeholder/error widget.
140 *
141 * @param {boolean} showList Show the list
142 */
143 mw.echo.ui.BundleNotificationItemWidget.prototype.toggleListDisplay = function ( showList ) {
144 this.listWidget.toggle( showList );
145 };
146
147 /**
148 * Expand the group and fetch the list of notifications.
149 * Only fetch the first time we expand.
150 */
151 mw.echo.ui.BundleNotificationItemWidget.prototype.expand = function () {
152 this.toggleExpanded( !this.expanded );
153 this.updateExpandButton();
154
155 this.$element.toggleClass( 'mw-echo-ui-bundleNotificationItemWidget-expanded', this.expanded );
156
157 if ( !this.expanded ) {
158 return;
159 }
160 };
161
162 /**
163 * Toggle the expand/collapsed state of this group widget
164 *
165 * @param {boolean} show Show the widget expanded
166 */
167 mw.echo.ui.BundleNotificationItemWidget.prototype.toggleExpanded = function ( show ) {
168 this.expanded = show !== undefined ? !!show : !this.expanded;
169
170 if ( show ) {
171 // FIXME: Use CSS transition
172 // eslint-disable-next-line no-jquery/no-slide
173 this.getList().$element.slideDown();
174 } else {
175 // eslint-disable-next-line no-jquery/no-slide
176 this.getList().$element.slideUp();
177 }
178 };
179
180 /**
181 * Update the expand button label
182 */
183 mw.echo.ui.BundleNotificationItemWidget.prototype.updateExpandButton = function () {
184 this.toggleExpandButton.setLabel(
185 this.expanded ?
186 mw.msg( 'notification-link-text-collapse-all' ) :
187 mw.msg( 'notification-link-text-expand-all' )
188 );
189 this.toggleExpandButton.setIcon(
190 this.expanded ?
191 'collapse' :
192 'expand'
193 );
194 // T258706
195 this.toggleExpandButton.setActive( false );
196 };
197
198 /**
199 * Get the list widget contained in this item
200 *
201 * @return {mw.echo.ui.SortedListWidget} List widget
202 */
203 mw.echo.ui.BundleNotificationItemWidget.prototype.getList = function () {
204 return this.listWidget;
205 };
206 }() );