2018-11-12 13:56:38 +00:00
|
|
|
( function () {
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Sub group list widget.
|
|
|
|
* This widget contains a list of notifications from a single source
|
|
|
|
* in a cross-wiki notifications group.
|
|
|
|
*
|
|
|
|
* @param {mw.echo.Controller} controller Notifications controller
|
|
|
|
* @param {mw.echo.dm.SortedList} listModel Notifications list model for this source
|
|
|
|
* @param {Object} config Configuration object
|
|
|
|
* @cfg {boolean} [showTitle=false] Show the title of this group
|
2016-05-19 20:55:21 +00:00
|
|
|
* @cfg {boolean} [showMarkAllRead=false] Show a mark all read button for this group
|
Fix fade-in/out animation in sorting
The fade in/out animation is asynchronous. This means that if we are
sorting multiple items one after the other, by the time the item faded
out, it will be reinserted back into the wrong position, breaking the
sorting.
This also broke the promise of OO.SortedEmitterList whereby all its items
are always in order.
The way to fix this was to force a better synchronization with the item
order while we hide and show the item in its new place. To do that,
a new widget is created as a fake clone of the old one, in the original
position of the old one. The original item is then reinserted (while hidden)
to the proper location -- preserving order. The fake clone is then faded
out, and the real item is then faded in.
For this to work properly, the cloned item had to preserve some of the
original item's information, like timestamp, foreigness and id. However,
since both the real item and the fake new clone have the same details,
the clone fakes its ID by adding a fraction to it - promising that the
fallback in case of equal timestamps (which happens on the real and
cloned items) will still resolve with some decision about the placement
of the items rather than (falsely but understandably) decide they are
both the same.
Since this whole animation is somewhat of a hack, the list now has a
configuration parameter to turn the animation on.
The animation is on in the popups, but off in the special page.
Bug: T141419
Change-Id: Ic7c35e5ddefc51bf7fde497eab36414b4dddcd9e
2016-07-29 23:35:29 +00:00
|
|
|
* @cfg {boolean} [animateSorting=false] Animate the sorting of items
|
2016-04-10 13:31:02 +00:00
|
|
|
* @cfg {jQuery} [$overlay] A jQuery element functioning as an overlay
|
|
|
|
* for popups.
|
|
|
|
*/
|
|
|
|
mw.echo.ui.SubGroupListWidget = function MwEchoUiSubGroupListWidget( controller, listModel, config ) {
|
2018-03-25 13:47:46 +00:00
|
|
|
this.$header = $( '<div>' )
|
|
|
|
.addClass( 'mw-echo-ui-subGroupListWidget-header' );
|
2016-04-10 13:31:02 +00:00
|
|
|
|
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
this.controller = controller;
|
|
|
|
this.model = listModel;
|
|
|
|
|
|
|
|
// Parent constructor
|
2018-05-22 14:56:46 +00:00
|
|
|
mw.echo.ui.SubGroupListWidget.super.call( this, $.extend( { data: this.getSource() }, config ) );
|
2016-04-10 13:31:02 +00:00
|
|
|
|
|
|
|
this.showTitle = !!config.showTitle;
|
2016-05-19 20:55:21 +00:00
|
|
|
this.showMarkAllRead = !!config.showMarkAllRead;
|
2016-04-10 13:31:02 +00:00
|
|
|
this.$overlay = config.$overlay || this.$element;
|
|
|
|
|
|
|
|
this.listWidget = new mw.echo.ui.SortedListWidget(
|
|
|
|
// Sorting callback
|
Fix fade-in/out animation in sorting
The fade in/out animation is asynchronous. This means that if we are
sorting multiple items one after the other, by the time the item faded
out, it will be reinserted back into the wrong position, breaking the
sorting.
This also broke the promise of OO.SortedEmitterList whereby all its items
are always in order.
The way to fix this was to force a better synchronization with the item
order while we hide and show the item in its new place. To do that,
a new widget is created as a fake clone of the old one, in the original
position of the old one. The original item is then reinserted (while hidden)
to the proper location -- preserving order. The fake clone is then faded
out, and the real item is then faded in.
For this to work properly, the cloned item had to preserve some of the
original item's information, like timestamp, foreigness and id. However,
since both the real item and the fake new clone have the same details,
the clone fakes its ID by adding a fraction to it - promising that the
fallback in case of equal timestamps (which happens on the real and
cloned items) will still resolve with some decision about the placement
of the items rather than (falsely but understandably) decide they are
both the same.
Since this whole animation is somewhat of a hack, the list now has a
configuration parameter to turn the animation on.
The animation is on in the popups, but off in the special page.
Bug: T141419
Change-Id: Ic7c35e5ddefc51bf7fde497eab36414b4dddcd9e
2016-07-29 23:35:29 +00:00
|
|
|
config.sortingCallback || function ( a, b ) {
|
2016-04-10 13:31:02 +00:00
|
|
|
// Reverse sorting
|
2016-07-22 18:59:10 +00:00
|
|
|
if ( b.getTimestamp() < a.getTimestamp() ) {
|
|
|
|
return -1;
|
|
|
|
} else if ( b.getTimestamp() > a.getTimestamp() ) {
|
|
|
|
return 1;
|
2016-04-10 13:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback on IDs
|
|
|
|
return b.getId() - a.getId();
|
|
|
|
},
|
|
|
|
// Config
|
Fix fade-in/out animation in sorting
The fade in/out animation is asynchronous. This means that if we are
sorting multiple items one after the other, by the time the item faded
out, it will be reinserted back into the wrong position, breaking the
sorting.
This also broke the promise of OO.SortedEmitterList whereby all its items
are always in order.
The way to fix this was to force a better synchronization with the item
order while we hide and show the item in its new place. To do that,
a new widget is created as a fake clone of the old one, in the original
position of the old one. The original item is then reinserted (while hidden)
to the proper location -- preserving order. The fake clone is then faded
out, and the real item is then faded in.
For this to work properly, the cloned item had to preserve some of the
original item's information, like timestamp, foreigness and id. However,
since both the real item and the fake new clone have the same details,
the clone fakes its ID by adding a fraction to it - promising that the
fallback in case of equal timestamps (which happens on the real and
cloned items) will still resolve with some decision about the placement
of the items rather than (falsely but understandably) decide they are
both the same.
Since this whole animation is somewhat of a hack, the list now has a
configuration parameter to turn the animation on.
The animation is on in the popups, but off in the special page.
Bug: T141419
Change-Id: Ic7c35e5ddefc51bf7fde497eab36414b4dddcd9e
2016-07-29 23:35:29 +00:00
|
|
|
{
|
|
|
|
$overlay: this.$overlay,
|
|
|
|
animated: config.animateSorting
|
|
|
|
}
|
2016-04-10 13:31:02 +00:00
|
|
|
);
|
|
|
|
|
2024-03-05 22:39:09 +00:00
|
|
|
var sourceURL = this.model.getSourceURL() ?
|
2016-04-10 13:31:02 +00:00
|
|
|
this.model.getSourceURL().replace( '$1', 'Special:Notifications' ) :
|
|
|
|
null;
|
2016-05-19 20:55:21 +00:00
|
|
|
if ( sourceURL ) {
|
|
|
|
this.title = new OO.ui.ButtonWidget( {
|
|
|
|
framed: false,
|
2018-03-25 13:47:46 +00:00
|
|
|
classes: [
|
|
|
|
'mw-echo-ui-subGroupListWidget-header-row-title',
|
|
|
|
'mw-echo-ui-subGroupListWidget-header-row-cell'
|
|
|
|
],
|
2016-05-19 20:55:21 +00:00
|
|
|
href: sourceURL
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
this.title = new OO.ui.LabelWidget( {
|
2018-03-25 13:47:46 +00:00
|
|
|
classes: [
|
|
|
|
'mw-echo-ui-subGroupListWidget-header-row-title',
|
|
|
|
'mw-echo-ui-subGroupListWidget-header-row-cell'
|
|
|
|
]
|
2016-05-19 20:55:21 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
if ( this.model.getTitle() ) {
|
|
|
|
this.title.setLabel( this.model.getTitle() );
|
|
|
|
}
|
|
|
|
this.title.toggle( this.showTitle );
|
|
|
|
|
2016-05-19 20:55:21 +00:00
|
|
|
// Mark all as read button
|
|
|
|
this.markAllReadButton = new OO.ui.ButtonWidget( {
|
|
|
|
framed: true,
|
2018-03-22 21:21:06 +00:00
|
|
|
icon: 'checkAll',
|
2016-03-16 22:47:20 +00:00
|
|
|
label: mw.msg( 'echo-specialpage-section-markread' ),
|
2018-03-25 13:47:46 +00:00
|
|
|
classes: [
|
|
|
|
'mw-echo-ui-subGroupListWidget-header-row-markAllReadButton',
|
|
|
|
'mw-echo-ui-subGroupListWidget-header-row-cell'
|
|
|
|
]
|
2016-05-19 20:55:21 +00:00
|
|
|
} );
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
// Events
|
|
|
|
this.model.connect( this, {
|
2016-05-19 20:55:21 +00:00
|
|
|
// Cross-wiki items can be discarded when marked as read.
|
|
|
|
// We need to differentiate this explicit action from the
|
|
|
|
// action of 'remove' because 'remove' is also used when
|
|
|
|
// an item is resorted by OO.SortedEmitterWidget before
|
|
|
|
// it is re-added again
|
|
|
|
discard: 'onModelDiscardItems',
|
|
|
|
// Update all items
|
|
|
|
update: 'resetItemsFromModel'
|
2016-04-10 13:31:02 +00:00
|
|
|
} );
|
2016-05-19 20:55:21 +00:00
|
|
|
this.model.connect( this, { itemUpdate: 'toggleMarkAllReadButton' } );
|
2016-03-16 22:47:20 +00:00
|
|
|
this.markAllReadButton.connect( this, { click: 'onMarkAllReadButtonClick' } );
|
2016-04-10 13:31:02 +00:00
|
|
|
|
2016-05-19 20:55:21 +00:00
|
|
|
// Initialize
|
|
|
|
this.toggleMarkAllReadButton();
|
2016-06-07 20:08:16 +00:00
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
this.$element
|
|
|
|
.addClass( 'mw-echo-ui-subGroupListWidget' )
|
|
|
|
.append(
|
2018-03-25 13:47:46 +00:00
|
|
|
this.$header.append(
|
2016-05-19 20:55:21 +00:00
|
|
|
$( '<div>' )
|
|
|
|
.addClass( 'mw-echo-ui-subGroupListWidget-header-row' )
|
|
|
|
.append(
|
|
|
|
this.title.$element,
|
|
|
|
this.markAllReadButton.$element
|
|
|
|
)
|
|
|
|
),
|
2016-04-10 13:31:02 +00:00
|
|
|
this.listWidget.$element
|
|
|
|
);
|
2018-03-25 13:47:46 +00:00
|
|
|
|
2019-04-03 22:57:13 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
2018-03-25 13:47:46 +00:00
|
|
|
this.$pageContentText = $( '#mw-content-text' );
|
2019-02-06 01:42:53 +00:00
|
|
|
$( window ).on( 'resize', this.resizeHeader.bind( this ) );
|
2018-03-25 13:47:46 +00:00
|
|
|
|
|
|
|
// Resize the header after the stack finishes loading
|
|
|
|
// so the widget is attached
|
|
|
|
setTimeout( this.resizeHeader.bind( this ), 0 );
|
2016-04-10 13:31:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Initialization */
|
|
|
|
|
|
|
|
OO.inheritClass( mw.echo.ui.SubGroupListWidget, OO.ui.Widget );
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2018-03-25 13:47:46 +00:00
|
|
|
/**
|
|
|
|
* Respond to window resize event
|
|
|
|
*/
|
|
|
|
mw.echo.ui.SubGroupListWidget.prototype.resizeHeader = function () {
|
|
|
|
var contentWidth = this.$pageContentText.width(),
|
|
|
|
screenTooNarrow = this.$header.width() > contentWidth;
|
|
|
|
|
|
|
|
// Screen too narrow, put the button under the date
|
|
|
|
this.title.$element.toggleClass(
|
|
|
|
'mw-echo-ui-subGroupListWidget-header-row-cell',
|
|
|
|
!screenTooNarrow
|
|
|
|
);
|
|
|
|
this.markAllReadButton.$element.toggleClass(
|
|
|
|
'mw-echo-ui-subGroupListWidget-header-row-cell',
|
|
|
|
!screenTooNarrow
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy the widget and disconnect events
|
|
|
|
*/
|
|
|
|
mw.echo.ui.SubGroupListWidget.prototype.destroy = function () {
|
|
|
|
$( window ).off( 'resize' );
|
|
|
|
};
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
2016-05-19 20:55:21 +00:00
|
|
|
* Toggle the visibility of the mark all read button for this group
|
|
|
|
* based on whether there are unread notifications
|
|
|
|
*/
|
|
|
|
mw.echo.ui.SubGroupListWidget.prototype.toggleMarkAllReadButton = function () {
|
2016-06-07 20:08:16 +00:00
|
|
|
this.markAllReadButton.toggle( this.showMarkAllRead && this.hasUnread() );
|
2016-05-19 20:55:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to 'mark all as read' button click
|
|
|
|
*/
|
|
|
|
mw.echo.ui.SubGroupListWidget.prototype.onMarkAllReadButtonClick = function () {
|
2016-06-08 23:53:20 +00:00
|
|
|
this.controller.markEntireListModelRead( this.model.getName() );
|
2016-05-19 20:55:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether this sub group list has any unread notifications
|
|
|
|
*
|
|
|
|
* @return {boolean} Sub group has unread notifications
|
|
|
|
*/
|
|
|
|
mw.echo.ui.SubGroupListWidget.prototype.hasUnread = function () {
|
|
|
|
var isUnread = function ( item ) {
|
|
|
|
return !item.isRead();
|
|
|
|
},
|
|
|
|
items = this.model.getItems();
|
|
|
|
|
|
|
|
return items.some( isUnread );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the items and rebuild them according to the model.
|
2016-04-10 13:31:02 +00:00
|
|
|
*
|
2016-05-19 20:55:21 +00:00
|
|
|
* @param {mw.echo.dm.NotificationItem[]} [items] Item models that are added.
|
|
|
|
* If this is empty, the widget will request all the items from the model.
|
2016-04-10 13:31:02 +00:00
|
|
|
*/
|
2016-05-19 20:55:21 +00:00
|
|
|
mw.echo.ui.SubGroupListWidget.prototype.resetItemsFromModel = function ( items ) {
|
2024-03-05 22:39:09 +00:00
|
|
|
var itemWidgets = [],
|
2016-09-26 17:08:43 +00:00
|
|
|
$elements = $();
|
2016-04-10 13:31:02 +00:00
|
|
|
|
2016-05-19 20:55:21 +00:00
|
|
|
items = items || this.model.getItems();
|
|
|
|
|
2024-03-05 22:39:09 +00:00
|
|
|
for ( var i = 0; i < items.length; i++ ) {
|
|
|
|
var widget = new mw.echo.ui.SingleNotificationItemWidget(
|
2016-09-26 17:08:43 +00:00
|
|
|
this.controller,
|
|
|
|
items[ i ],
|
|
|
|
{
|
|
|
|
$overlay: this.$overlay,
|
|
|
|
bundle: items[ i ].isBundled()
|
|
|
|
}
|
2016-04-10 13:31:02 +00:00
|
|
|
);
|
2016-09-26 17:08:43 +00:00
|
|
|
itemWidgets.push( widget );
|
|
|
|
$elements = $elements.add( widget.$element );
|
2016-04-10 13:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the current items if any exist
|
|
|
|
this.getListWidget().clearItems();
|
2016-09-26 17:08:43 +00:00
|
|
|
|
|
|
|
// fire render hook
|
|
|
|
mw.hook( 'ext.echo.notifications.beforeRender' ).fire( this.$element, $elements );
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
// Add the new items
|
|
|
|
this.getListWidget().addItems( itemWidgets );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-05-19 20:55:21 +00:00
|
|
|
* Respond to model remove event. This may happen when an item
|
2016-04-10 13:31:02 +00:00
|
|
|
* is marked as read.
|
|
|
|
*
|
2016-05-19 20:55:21 +00:00
|
|
|
* @param {mw.echo.dm.NotificationItem[]} items Notification item models
|
2016-04-10 13:31:02 +00:00
|
|
|
*/
|
2016-05-19 20:55:21 +00:00
|
|
|
mw.echo.ui.SubGroupListWidget.prototype.onModelDiscardItems = function ( items ) {
|
2024-03-05 22:39:09 +00:00
|
|
|
var itemWidgets = [];
|
2016-05-19 20:55:21 +00:00
|
|
|
|
2024-03-05 22:39:09 +00:00
|
|
|
for ( var i = 0; i < items.length; i++ ) {
|
2016-05-19 20:55:21 +00:00
|
|
|
itemWidgets.push( this.listWidget.getItemFromId( items[ i ].getId() ) );
|
|
|
|
}
|
|
|
|
this.listWidget.removeItems( itemWidgets );
|
2016-04-10 13:31:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the associated list widget. This is useful to specifically
|
|
|
|
* add and/or remove items from the list.
|
|
|
|
*
|
|
|
|
* @return {mw.echo.ui.SortedListWidget} List widget
|
|
|
|
*/
|
|
|
|
mw.echo.ui.SubGroupListWidget.prototype.getListWidget = function () {
|
|
|
|
return this.listWidget;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the timestamp for the list
|
|
|
|
*
|
|
|
|
* @return {number} Timestamp
|
|
|
|
*/
|
|
|
|
mw.echo.ui.SubGroupListWidget.prototype.getTimestamp = function () {
|
2016-06-21 13:23:24 +00:00
|
|
|
return this.model.getTimestamp();
|
2016-04-10 13:31:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle the visibility of the title
|
|
|
|
*
|
|
|
|
* @param {boolean} show Show the title
|
|
|
|
*/
|
|
|
|
mw.echo.ui.SubGroupListWidget.prototype.toggleTitle = function ( show ) {
|
|
|
|
show = show !== undefined ? show : !this.showTitle;
|
|
|
|
|
|
|
|
if ( this.showTitle !== show ) {
|
|
|
|
this.showTitle = show;
|
|
|
|
this.title.toggle( this.showTitle );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a the source of this list.
|
|
|
|
*
|
|
|
|
* @return {string} Group source
|
|
|
|
*/
|
|
|
|
mw.echo.ui.SubGroupListWidget.prototype.getSource = function () {
|
|
|
|
return this.model.getSource();
|
|
|
|
};
|
|
|
|
|
2016-05-19 20:55:21 +00:00
|
|
|
/**
|
|
|
|
* Get an array of IDs of all of the items in this group
|
|
|
|
*
|
|
|
|
* @return {number[]} Array of item IDs
|
|
|
|
*/
|
|
|
|
mw.echo.ui.SubGroupListWidget.prototype.getAllItemIDs = function () {
|
|
|
|
return this.model.getAllItemIds();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an array of IDs of all of the items in this group that
|
|
|
|
* correspond to a specific type
|
|
|
|
*
|
|
|
|
* @param {string} type Item type
|
|
|
|
* @return {number[]} Array of item IDs
|
|
|
|
*/
|
|
|
|
mw.echo.ui.SubGroupListWidget.prototype.getAllItemIDsByType = function ( type ) {
|
|
|
|
return this.model.getAllItemIdsByType( type );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether this group is foreign
|
|
|
|
*
|
|
|
|
* @return {boolean} This group is foreign
|
|
|
|
*/
|
|
|
|
mw.echo.ui.SubGroupListWidget.prototype.isForeign = function () {
|
|
|
|
return this.model.isForeign();
|
|
|
|
};
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
2016-05-31 22:32:16 +00:00
|
|
|
* Get the group id, which is represented by its model symbolic name.
|
2016-04-10 13:31:02 +00:00
|
|
|
* This is meant for sorting callbacks that fallback on
|
|
|
|
* sorting by IDs.
|
|
|
|
*
|
|
|
|
* @return {string} Group source
|
|
|
|
*/
|
|
|
|
mw.echo.ui.SubGroupListWidget.prototype.getId = function () {
|
2016-05-31 22:32:16 +00:00
|
|
|
return this.model.getName();
|
2016-04-10 13:31:02 +00:00
|
|
|
};
|
2018-11-12 13:56:38 +00:00
|
|
|
}() );
|