2018-11-12 13:56:38 +00:00
|
|
|
( function () {
|
2016-05-27 20:39:15 +00:00
|
|
|
/**
|
|
|
|
* A pagination widget allowing the user to go forward, backwards,
|
|
|
|
* and after a couple of pages, go back to home.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends OO.ui.Widget
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {mw.echo.dm.PaginationModel} paginationModel Pagination model
|
|
|
|
* @param {Object} [config] Configuration object
|
2024-05-27 01:43:21 +00:00
|
|
|
* @param {number} [config.itemsPerPage=25] Number of items per page
|
|
|
|
* @param {number} [config.showFirstButton=true] Show a button that allows the user
|
2016-05-27 20:39:15 +00:00
|
|
|
* to go back to the first page.
|
2024-05-27 01:43:21 +00:00
|
|
|
* @param {number} [config.showFirstButtonAfter=2] Pick the number of pages that it
|
2016-05-27 20:39:15 +00:00
|
|
|
* takes to show the button that takes the user back to the first set
|
|
|
|
* of results.
|
2024-05-27 01:43:21 +00:00
|
|
|
* @param {string} [config.startButtonLabel] The label used for the start button
|
2016-05-27 20:39:15 +00:00
|
|
|
*/
|
|
|
|
mw.echo.ui.PaginationWidget = function MwEchoUiPaginationWidget( paginationModel, config ) {
|
|
|
|
config = config || {};
|
|
|
|
|
2018-05-22 14:56:46 +00:00
|
|
|
// Parent constructor
|
|
|
|
mw.echo.ui.PaginationWidget.super.call( this, config );
|
2016-05-27 20:39:15 +00:00
|
|
|
|
|
|
|
this.model = paginationModel;
|
|
|
|
|
|
|
|
this.showFirstButton = config.showFirstButton === undefined ? true : !!config.showFirstButton;
|
|
|
|
this.showFirstButtonAfter = config.showFirstButtonAfter || 2;
|
|
|
|
this.itemsPerPage = config.itemsPerPage || 25;
|
|
|
|
|
|
|
|
// Pagination elements
|
|
|
|
this.labelWidget = new OO.ui.LabelWidget( {
|
|
|
|
classes: [ 'mw-echo-ui-paginationWidget-label' ]
|
|
|
|
} );
|
|
|
|
|
|
|
|
this.startButton = new OO.ui.ButtonWidget( {
|
|
|
|
classes: [ 'mw-echo-ui-paginationWidget-start' ],
|
|
|
|
label: config.startButtonLabel || mw.msg( 'notification-timestamp-today' ),
|
|
|
|
data: 'start'
|
|
|
|
} );
|
|
|
|
|
|
|
|
this.dirSelectWidget = new OO.ui.ButtonSelectWidget( {
|
|
|
|
classes: [ 'mw-echo-ui-paginationWidget-direction' ],
|
|
|
|
items: [
|
|
|
|
new OO.ui.ButtonOptionWidget( {
|
|
|
|
icon: 'previous',
|
|
|
|
data: 'prev'
|
|
|
|
} ),
|
|
|
|
new OO.ui.ButtonOptionWidget( {
|
|
|
|
icon: 'next',
|
|
|
|
data: 'next'
|
|
|
|
} )
|
|
|
|
]
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Events
|
|
|
|
this.startButton.connect( this, { click: [ 'emit', 'change', 'start' ] } );
|
|
|
|
this.dirSelectWidget.connect( this, { choose: 'onDirSelectWidgetChoose' } );
|
|
|
|
this.model.connect( this, { update: 'updateWidgetState' } );
|
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.updateWidgetState();
|
|
|
|
this.$element
|
|
|
|
.addClass( 'mw-echo-ui-paginationWidget' )
|
|
|
|
.append(
|
|
|
|
$( '<div>' )
|
|
|
|
.addClass( 'mw-echo-ui-paginationWidget-row' )
|
|
|
|
.append(
|
|
|
|
this.labelWidget.$element,
|
|
|
|
this.startButton.$element,
|
|
|
|
this.dirSelectWidget.$element
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Initialization */
|
|
|
|
|
|
|
|
OO.inheritClass( mw.echo.ui.PaginationWidget, OO.ui.Widget );
|
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
2024-04-30 14:21:54 +00:00
|
|
|
* Pagination changed
|
|
|
|
*
|
|
|
|
* @event mw.echo.ui.PaginationWidget#change
|
2016-05-27 20:39:15 +00:00
|
|
|
* @param {string} direction Direction of movement 'prev',
|
|
|
|
* 'next' or 'start'
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to dir select widget choose event
|
|
|
|
*
|
|
|
|
* @param {OO.ui.ButtonOptionWidget} item Chosen button
|
2024-04-30 14:21:54 +00:00
|
|
|
* @fires mw.echo.ui.PaginationWidget#change
|
2016-05-27 20:39:15 +00:00
|
|
|
*/
|
|
|
|
mw.echo.ui.PaginationWidget.prototype.onDirSelectWidgetChoose = function ( item ) {
|
2024-06-03 12:22:48 +00:00
|
|
|
const dir = item && item.getData();
|
2016-05-27 20:39:15 +00:00
|
|
|
|
|
|
|
if ( dir ) {
|
|
|
|
this.emit( 'change', dir );
|
|
|
|
item.setSelected( false );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the state - disabled and visibility - of the sub widgets.
|
|
|
|
*/
|
|
|
|
mw.echo.ui.PaginationWidget.prototype.updateWidgetState = function () {
|
2017-12-14 08:05:54 +00:00
|
|
|
this.dirSelectWidget.findItemFromData( 'prev' )
|
2016-05-31 00:12:31 +00:00
|
|
|
.setDisabled( this.isDisabled() || !this.model.hasPrevPage() );
|
2017-12-14 08:05:54 +00:00
|
|
|
this.dirSelectWidget.findItemFromData( 'next' )
|
2016-05-31 00:12:31 +00:00
|
|
|
.setDisabled( this.isDisabled() || !this.model.hasNextPage() );
|
|
|
|
|
|
|
|
this.startButton.toggle(
|
|
|
|
!this.isDisabled() &&
|
|
|
|
this.model.getCurrPageIndex() >= this.showFirstButtonAfter
|
|
|
|
);
|
|
|
|
|
|
|
|
// Only show pagination buttons if there's anywhere to go
|
|
|
|
this.dirSelectWidget.toggle( this.model.hasPrevPage() || this.model.hasNextPage() );
|
|
|
|
|
|
|
|
// Update label text and visibility
|
|
|
|
this.updateLabel();
|
|
|
|
this.labelWidget.toggle( !this.isDisabled() );
|
2016-05-27 20:39:15 +00:00
|
|
|
};
|
2016-11-18 21:16:43 +00:00
|
|
|
|
2016-05-27 20:39:15 +00:00
|
|
|
/**
|
|
|
|
* Set the 'disabled' state of the widget.
|
|
|
|
*
|
|
|
|
* @param {boolean} disabled Disable widget
|
2016-11-18 21:16:43 +00:00
|
|
|
* @chainable
|
2024-04-30 14:21:54 +00:00
|
|
|
* @return {mw.echo.ui.PaginationWidget}
|
2016-05-27 20:39:15 +00:00
|
|
|
*/
|
|
|
|
mw.echo.ui.PaginationWidget.prototype.setDisabled = function ( disabled ) {
|
2018-05-22 14:56:46 +00:00
|
|
|
// Parent method
|
|
|
|
mw.echo.ui.PaginationWidget.super.prototype.setDisabled.call( this, disabled );
|
2016-05-27 20:39:15 +00:00
|
|
|
|
2016-05-31 00:12:31 +00:00
|
|
|
if (
|
|
|
|
this.dirSelectWidget &&
|
|
|
|
this.startButton &&
|
|
|
|
this.labelWidget
|
|
|
|
) {
|
|
|
|
this.updateWidgetState();
|
|
|
|
}
|
2016-05-27 20:39:15 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the pagination label according to the page number, the amount of notifications
|
|
|
|
* per page, and the number of notifications on the current page.
|
|
|
|
*/
|
|
|
|
mw.echo.ui.PaginationWidget.prototype.updateLabel = function () {
|
2024-06-03 12:22:48 +00:00
|
|
|
const itemsInPage = this.model.getCurrentPageItemCount(),
|
2016-05-31 00:12:31 +00:00
|
|
|
firstNotifNum = this.model.getCurrPageIndex() * this.itemsPerPage,
|
|
|
|
lastNotifNum = firstNotifNum + itemsInPage;
|
|
|
|
|
2024-06-03 12:22:48 +00:00
|
|
|
let label;
|
2016-05-31 00:12:31 +00:00
|
|
|
if ( itemsInPage === 0 ) {
|
|
|
|
label = '';
|
|
|
|
} else if ( !this.model.hasPrevPage() && !this.model.hasNextPage() ) {
|
2016-07-09 07:50:45 +00:00
|
|
|
label = mw.msg(
|
|
|
|
'echo-specialpage-pagination-numnotifications',
|
|
|
|
mw.language.convertNumber( itemsInPage )
|
|
|
|
);
|
2016-05-31 00:12:31 +00:00
|
|
|
} else {
|
2016-07-09 07:50:45 +00:00
|
|
|
label = mw.msg(
|
|
|
|
'echo-specialpage-pagination-range',
|
|
|
|
mw.language.convertNumber( firstNotifNum + 1 ),
|
|
|
|
mw.language.convertNumber( lastNotifNum )
|
|
|
|
);
|
2016-05-31 00:12:31 +00:00
|
|
|
}
|
2016-05-27 20:39:15 +00:00
|
|
|
|
|
|
|
this.labelWidget.setLabel( label );
|
|
|
|
};
|
2018-11-12 13:56:38 +00:00
|
|
|
}() );
|