| 1 |
( function () { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
mw.echo.ui.PaginationWidget = function MwEchoUiPaginationWidget( paginationModel, config ) { |
| 21 |
config = config || {}; |
| 22 |
|
| 23 |
|
| 24 |
mw.echo.ui.PaginationWidget.super.call( this, config ); |
| 25 |
|
| 26 |
this.model = paginationModel; |
| 27 |
|
| 28 |
this.showFirstButton = config.showFirstButton === undefined ? true : !!config.showFirstButton; |
| 29 |
this.showFirstButtonAfter = config.showFirstButtonAfter || 2; |
| 30 |
this.itemsPerPage = config.itemsPerPage || 25; |
| 31 |
|
| 32 |
|
| 33 |
this.labelWidget = new OO.ui.LabelWidget( { |
| 34 |
classes: [ 'mw-echo-ui-paginationWidget-label' ] |
| 35 |
} ); |
| 36 |
|
| 37 |
this.startButton = new OO.ui.ButtonWidget( { |
| 38 |
classes: [ 'mw-echo-ui-paginationWidget-start' ], |
| 39 |
label: config.startButtonLabel || mw.msg( 'notification-timestamp-today' ), |
| 40 |
data: 'start' |
| 41 |
} ); |
| 42 |
|
| 43 |
this.dirSelectWidget = new OO.ui.ButtonSelectWidget( { |
| 44 |
classes: [ 'mw-echo-ui-paginationWidget-direction' ], |
| 45 |
items: [ |
| 46 |
new OO.ui.ButtonOptionWidget( { |
| 47 |
icon: 'previous', |
| 48 |
data: 'prev' |
| 49 |
} ), |
| 50 |
new OO.ui.ButtonOptionWidget( { |
| 51 |
icon: 'next', |
| 52 |
data: 'next' |
| 53 |
} ) |
| 54 |
] |
| 55 |
} ); |
| 56 |
|
| 57 |
|
| 58 |
this.startButton.connect( this, { click: [ 'emit', 'change', 'start' ] } ); |
| 59 |
this.dirSelectWidget.connect( this, { choose: 'onDirSelectWidgetChoose' } ); |
| 60 |
this.model.connect( this, { update: 'updateWidgetState' } ); |
| 61 |
|
| 62 |
|
| 63 |
this.updateWidgetState(); |
| 64 |
this.$element |
| 65 |
.addClass( 'mw-echo-ui-paginationWidget' ) |
| 66 |
.append( |
| 67 |
$( '<div>' ) |
| 68 |
.addClass( 'mw-echo-ui-paginationWidget-row' ) |
| 69 |
.append( |
| 70 |
this.labelWidget.$element, |
| 71 |
this.startButton.$element, |
| 72 |
this.dirSelectWidget.$element |
| 73 |
) |
| 74 |
); |
| 75 |
}; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
OO.inheritClass( mw.echo.ui.PaginationWidget, OO.ui.Widget ); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
mw.echo.ui.PaginationWidget.prototype.onDirSelectWidgetChoose = function ( item ) { |
| 100 |
const dir = item && item.getData(); |
| 101 |
|
| 102 |
if ( dir ) { |
| 103 |
this.emit( 'change', dir ); |
| 104 |
item.setSelected( false ); |
| 105 |
} |
| 106 |
}; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
mw.echo.ui.PaginationWidget.prototype.updateWidgetState = function () { |
| 112 |
this.dirSelectWidget.findItemFromData( 'prev' ) |
| 113 |
.setDisabled( this.isDisabled() || !this.model.hasPrevPage() ); |
| 114 |
this.dirSelectWidget.findItemFromData( 'next' ) |
| 115 |
.setDisabled( this.isDisabled() || !this.model.hasNextPage() ); |
| 116 |
|
| 117 |
this.startButton.toggle( |
| 118 |
!this.isDisabled() && |
| 119 |
this.model.getCurrPageIndex() >= this.showFirstButtonAfter |
| 120 |
); |
| 121 |
|
| 122 |
|
| 123 |
this.dirSelectWidget.toggle( this.model.hasPrevPage() || this.model.hasNextPage() ); |
| 124 |
|
| 125 |
|
| 126 |
this.updateLabel(); |
| 127 |
this.labelWidget.toggle( !this.isDisabled() ); |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
mw.echo.ui.PaginationWidget.prototype.setDisabled = function ( disabled ) { |
| 138 |
|
| 139 |
mw.echo.ui.PaginationWidget.super.prototype.setDisabled.call( this, disabled ); |
| 140 |
|
| 141 |
if ( |
| 142 |
this.dirSelectWidget && |
| 143 |
this.startButton && |
| 144 |
this.labelWidget |
| 145 |
) { |
| 146 |
this.updateWidgetState(); |
| 147 |
} |
| 148 |
|
| 149 |
return this; |
| 150 |
}; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
mw.echo.ui.PaginationWidget.prototype.updateLabel = function () { |
| 157 |
const itemsInPage = this.model.getCurrentPageItemCount(), |
| 158 |
firstNotifNum = this.model.getCurrPageIndex() * this.itemsPerPage, |
| 159 |
lastNotifNum = firstNotifNum + itemsInPage; |
| 160 |
|
| 161 |
let label; |
| 162 |
if ( itemsInPage === 0 ) { |
| 163 |
label = ''; |
| 164 |
} else if ( !this.model.hasPrevPage() && !this.model.hasNextPage() ) { |
| 165 |
label = mw.msg( |
| 166 |
'echo-specialpage-pagination-numnotifications', |
| 167 |
mw.language.convertNumber( itemsInPage ) |
| 168 |
); |
| 169 |
} else { |
| 170 |
label = mw.msg( |
| 171 |
'echo-specialpage-pagination-range', |
| 172 |
mw.language.convertNumber( firstNotifNum + 1 ), |
| 173 |
mw.language.convertNumber( lastNotifNum ) |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
this.labelWidget.setLabel( label ); |
| 178 |
}; |
| 179 |
}() ); |