Debian Codegraph @

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

raw symbols SCIP
1 ( function () {
2 /**
3 * A pagination widget allowing the user to go forward, backwards,
4 * and after a couple of pages, go back to home.
5 *
6 * @class
7 * @extends OO.ui.Widget
8 *
9 * @constructor
10 * @param {mw.echo.dm.PaginationModel} paginationModel Pagination model
11 * @param {Object} [config] Configuration object
12 * @param {number} [config.itemsPerPage=25] Number of items per page
13 * @param {number} [config.showFirstButton=true] Show a button that allows the user
14 * to go back to the first page.
15 * @param {number} [config.showFirstButtonAfter=2] Pick the number of pages that it
16 * takes to show the button that takes the user back to the first set
17 * of results.
18 * @param {string} [config.startButtonLabel] The label used for the start button
19 */
20 mw.echo.ui.PaginationWidget = function MwEchoUiPaginationWidget( paginationModel, config ) {
21 config = config || {};
22
23 // Parent constructor
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 // Pagination elements
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 // Events
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 // Initialization
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 /* Initialization */
78
79 OO.inheritClass( mw.echo.ui.PaginationWidget, OO.ui.Widget );
80
81 /* Events */
82
83 /**
84 * Pagination changed
85 *
86 * @event mw.echo.ui.PaginationWidget#change
87 * @param {string} direction Direction of movement 'prev',
88 * 'next' or 'start'
89 */
90
91 /* Methods */
92
93 /**
94 * Respond to dir select widget choose event
95 *
96 * @param {OO.ui.ButtonOptionWidget} item Chosen button
97 * @fires mw.echo.ui.PaginationWidget#change
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 * Update the state - disabled and visibility - of the sub widgets.
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 // Only show pagination buttons if there's anywhere to go
123 this.dirSelectWidget.toggle( this.model.hasPrevPage() || this.model.hasNextPage() );
124
125 // Update label text and visibility
126 this.updateLabel();
127 this.labelWidget.toggle( !this.isDisabled() );
128 };
129
130 /**
131 * Set the 'disabled' state of the widget.
132 *
133 * @param {boolean} disabled Disable widget
134 * @chainable
135 * @return {mw.echo.ui.PaginationWidget}
136 */
137 mw.echo.ui.PaginationWidget.prototype.setDisabled = function ( disabled ) {
138 // Parent method
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 * Update the pagination label according to the page number, the amount of notifications
154 * per page, and the number of notifications on the current page.
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 }() );