summaryrefslogtreecommitdiff
blob: fdfedde7e55102ae5ce5ac8f36a879139a175d71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
( function ( $, mw ) {
	/**
	 * 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
	 * @cfg {number} [itemsPerPage=25] Number of items per page
	 * @cfg {number} [showFirstButton=true] Show a button that allows the user
	 *  to go back to the first page.
	 * @cfg {number} [showFirstButtonAfter=2] Pick the number of pages that it
	 *  takes to show the button that takes the user back to the first set
	 *  of results.
	 * @cfg {string} [startButtonLabel] The label used for the start button
	 */
	mw.echo.ui.PaginationWidget = function MwEchoUiPaginationWidget( paginationModel, config ) {
		config = config || {};

		// Parent constructor
		mw.echo.ui.PaginationWidget.super.call( this, config );

		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 */

	/**
	 * @event change
	 * @param {string} direction Direction of movement 'prev',
	 * 'next' or 'start'
	 *
	 * Pagination changed
	 */

	/* Methods */

	/**
	 * Respond to dir select widget choose event
	 *
	 * @param {OO.ui.ButtonOptionWidget} item Chosen button
	 * @fires change
	 */
	mw.echo.ui.PaginationWidget.prototype.onDirSelectWidgetChoose = function ( item ) {
		var dir = item && item.getData();

		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 () {
		this.dirSelectWidget.findItemFromData( 'prev' )
			.setDisabled( this.isDisabled() || !this.model.hasPrevPage() );
		this.dirSelectWidget.findItemFromData( 'next' )
			.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() );
	};

	// eslint-disable-next-line valid-jsdoc
	/**
	 * Set the 'disabled' state of the widget.
	 *
	 * @param {boolean} disabled Disable widget
	 * @chainable
	 */
	mw.echo.ui.PaginationWidget.prototype.setDisabled = function ( disabled ) {
		// Parent method
		mw.echo.ui.PaginationWidget.super.prototype.setDisabled.call( this, disabled );

		if (
			this.dirSelectWidget &&
			this.startButton &&
			this.labelWidget
		) {
			this.updateWidgetState();
		}

		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 () {
		var label,
			itemsInPage = this.model.getCurrentPageItemCount(),
			firstNotifNum = this.model.getCurrPageIndex() * this.itemsPerPage,
			lastNotifNum = firstNotifNum + itemsInPage;

		if ( itemsInPage === 0 ) {
			label = '';
		} else if ( !this.model.hasPrevPage() && !this.model.hasNextPage() ) {
			label = mw.msg(
				'echo-specialpage-pagination-numnotifications',
				mw.language.convertNumber( itemsInPage )
			);
		} else {
			label = mw.msg(
				'echo-specialpage-pagination-range',
				mw.language.convertNumber( firstNotifNum + 1 ),
				mw.language.convertNumber( lastNotifNum )
			);
		}

		this.labelWidget.setLabel( label );
	};
}( jQuery, mediaWiki ) );