2018-11-12 13:56:38 +00:00
|
|
|
( function () {
|
2016-05-31 22:32:16 +00:00
|
|
|
/**
|
|
|
|
* A filter for cross-wiki unread notifications
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends OO.ui.Widget
|
2024-04-30 14:21:54 +00:00
|
|
|
* @mixes OO.ui.mixin.PendingElement
|
2016-05-31 22:32:16 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {mw.echo.Controller} controller Echo controller
|
|
|
|
* @param {mw.echo.dm.FiltersModel} filtersModel Filter model
|
|
|
|
* @param {Object} [config] Configuration object
|
|
|
|
*/
|
|
|
|
mw.echo.ui.CrossWikiUnreadFilterWidget = function MwEchoUiCrossWikiUnreadFilterWidget( controller, filtersModel, config ) {
|
|
|
|
config = config || {};
|
|
|
|
|
2018-05-22 14:56:46 +00:00
|
|
|
// Parent constructor
|
|
|
|
mw.echo.ui.CrossWikiUnreadFilterWidget.super.call( this,
|
2016-05-31 22:32:16 +00:00
|
|
|
// Sorting callback
|
|
|
|
function ( a, b ) {
|
|
|
|
var diff;
|
|
|
|
|
|
|
|
// Local source is always first
|
2016-07-07 19:20:39 +00:00
|
|
|
if ( a.getSource() === 'local' ) {
|
2016-05-31 22:32:16 +00:00
|
|
|
return -1;
|
2016-07-07 19:20:39 +00:00
|
|
|
} else if ( b.getSource() === 'local' ) {
|
2016-05-31 22:32:16 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
diff = Number( b.getTotalCount() ) - Number( a.getTotalCount() );
|
|
|
|
if ( diff !== 0 ) {
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback on Source
|
|
|
|
return b.getSource() - a.getSource();
|
|
|
|
},
|
|
|
|
// Config
|
|
|
|
config
|
|
|
|
);
|
|
|
|
// Mixin
|
|
|
|
OO.ui.mixin.PendingElement.call( this, config );
|
|
|
|
|
|
|
|
this.controller = controller;
|
|
|
|
this.model = filtersModel;
|
|
|
|
this.previousPageSelected = null;
|
|
|
|
|
2024-03-05 22:39:09 +00:00
|
|
|
var titleWidget = new OO.ui.LabelWidget( {
|
2016-05-31 22:32:16 +00:00
|
|
|
classes: [ 'mw-echo-ui-crossWikiUnreadFilterWidget-title' ],
|
|
|
|
label: mw.msg( 'echo-specialpage-pagefilters-title' )
|
|
|
|
} );
|
2024-03-05 22:39:09 +00:00
|
|
|
var subtitleWidget = new OO.ui.LabelWidget( {
|
2016-05-31 22:32:16 +00:00
|
|
|
classes: [ 'mw-echo-ui-crossWikiUnreadFilterWidget-subtitle' ],
|
|
|
|
label: mw.msg( 'echo-specialpage-pagefilters-subtitle' )
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Events
|
|
|
|
this.aggregate( { choose: 'pageFilterChoose' } );
|
|
|
|
this.connect( this, { pageFilterChoose: 'onPageFilterChoose' } );
|
|
|
|
|
|
|
|
this.$element
|
|
|
|
.addClass( 'mw-echo-ui-crossWikiUnreadFilterWidget' )
|
|
|
|
.append(
|
|
|
|
titleWidget.$element,
|
|
|
|
subtitleWidget.$element,
|
|
|
|
this.$group
|
|
|
|
.addClass( 'mw-echo-ui-crossWikiUnreadFilterWidget-group' )
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Initialization */
|
|
|
|
|
|
|
|
OO.inheritClass( mw.echo.ui.CrossWikiUnreadFilterWidget, mw.echo.ui.SortedListWidget );
|
|
|
|
OO.mixinClass( mw.echo.ui.CrossWikiUnreadFilterWidget, OO.ui.mixin.PendingElement );
|
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
2024-04-30 14:21:54 +00:00
|
|
|
* A source page filter was chosen
|
|
|
|
*
|
|
|
|
* @event mw.echo.ui.CrossWikiUnreadFilterWidget#filter
|
2016-05-31 22:32:16 +00:00
|
|
|
* @param {string} source Source symbolic name
|
|
|
|
* @param {number} [pageId] Chosen page ID
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to choose event in one of the page filter widgets
|
|
|
|
*
|
|
|
|
* @param {mw.echo.ui.PageFilterWidget} widget The widget the event originated from
|
|
|
|
* @param {mw.echo.ui.PageNotificationsOptionWidget} item The chosen item
|
2024-04-30 14:21:54 +00:00
|
|
|
* @fires mw.echo.ui.CrossWikiUnreadFilterWidget#filter
|
2016-05-31 22:32:16 +00:00
|
|
|
*/
|
|
|
|
mw.echo.ui.CrossWikiUnreadFilterWidget.prototype.onPageFilterChoose = function ( widget, item ) {
|
|
|
|
var source = widget.getSource(),
|
2016-06-23 13:51:05 +00:00
|
|
|
page = item && item.getData();
|
2016-05-31 22:32:16 +00:00
|
|
|
|
|
|
|
if ( item ) {
|
|
|
|
this.setItemSelected( item );
|
|
|
|
// Emit a choice
|
2016-06-23 13:51:05 +00:00
|
|
|
this.emit( 'filter', source, page );
|
2016-05-31 22:32:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the selected item
|
|
|
|
*
|
|
|
|
* @param {mw.echo.ui.PageNotificationsOptionWidget} item Item to select
|
|
|
|
*/
|
|
|
|
mw.echo.ui.CrossWikiUnreadFilterWidget.prototype.setItemSelected = function ( item ) {
|
|
|
|
// Unselect the previous item
|
|
|
|
if ( this.previousPageSelected ) {
|
|
|
|
this.previousPageSelected.setSelected( false );
|
|
|
|
}
|
|
|
|
item.setSelected( true );
|
|
|
|
this.previousPageSelected = item;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate the sources
|
|
|
|
*/
|
|
|
|
mw.echo.ui.CrossWikiUnreadFilterWidget.prototype.populateSources = function () {
|
|
|
|
this.pushPending();
|
|
|
|
this.controller.fetchUnreadPagesByWiki()
|
|
|
|
.then( this.populateDataFromModel.bind( this ) )
|
|
|
|
.always( this.popPending.bind( this ) );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate the widget from the model data
|
|
|
|
*/
|
|
|
|
mw.echo.ui.CrossWikiUnreadFilterWidget.prototype.populateDataFromModel = function () {
|
2024-03-05 22:39:09 +00:00
|
|
|
var widgets = [],
|
2016-05-31 22:32:16 +00:00
|
|
|
sourcePageModel = this.model.getSourcePagesModel(),
|
|
|
|
selectedSource = sourcePageModel.getCurrentSource(),
|
|
|
|
selectedPage = sourcePageModel.getCurrentPage(),
|
|
|
|
sources = sourcePageModel.getSourcesArray();
|
|
|
|
|
2024-03-05 22:39:09 +00:00
|
|
|
for ( var i = 0; i < sources.length; i++ ) {
|
|
|
|
var source = sources[ i ];
|
|
|
|
var widget = new mw.echo.ui.PageFilterWidget(
|
2016-05-31 22:32:16 +00:00
|
|
|
sourcePageModel,
|
|
|
|
source,
|
|
|
|
{
|
|
|
|
title: sourcePageModel.getSourceTitle( source ),
|
|
|
|
unreadCount: sourcePageModel.getSourceTotalCount( source ),
|
|
|
|
initialSelection: this.previousPageSelected && this.previousPageSelected.getData()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
widgets.push( widget );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.clearItems();
|
|
|
|
this.addItems( widgets );
|
|
|
|
|
|
|
|
// Select the current source
|
2024-03-05 22:39:09 +00:00
|
|
|
var selectedWidget = this.findItemFromData( selectedSource );
|
|
|
|
var item;
|
2016-05-31 22:32:16 +00:00
|
|
|
if ( selectedPage ) {
|
|
|
|
// Select a specific page
|
2017-12-14 08:05:54 +00:00
|
|
|
item = selectedWidget.findItemFromData( selectedPage );
|
2016-05-31 22:32:16 +00:00
|
|
|
} else {
|
|
|
|
// The wiki title is selected
|
|
|
|
item = selectedWidget.getTitleItem();
|
|
|
|
}
|
|
|
|
this.setItemSelected( item );
|
|
|
|
};
|
|
|
|
|
2018-11-12 13:56:38 +00:00
|
|
|
}() );
|