/
PageList.js
182 lines (158 loc) · 4.51 KB
/
PageList.js
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
180
181
182
var _ = require('lodash');
var React = require('react');
var classNames = require('classnames');
var PageButton = require('./PageButton');
/**
* Create a list of page selectors
* @class PageList
*
* @memberOf VisualizationComponents.Paginator
* @see {@link VisualizationComponents.Paginator}
*
* @type {*|Function}
*/
var PageList = React.createClass({
propTypes: {
currentPage : React.PropTypes.number,
pageCount : React.PropTypes.number.isRequired,
selectPageCallback : React.PropTypes.func.isRequired,
previousPageCallback : React.PropTypes.func,
nextPageCallback : React.PropTypes.func,
range : React.PropTypes.number
},
/**
* Sets the initial state of the PageList
* @memberOf VisualizationComponents.Paginator.PageList
*
* @return {object}
*/
getInitialState: function () {
return {
currentPage: this.props.currentPage
};
},
/**
* Parent callback for page selection
* @memberOf VisualizationComponents.Paginator.PageList
*
* @param {number} pageNumber The page to select
*
* @return {null}
*/
selectPage: function (pageNumber) {
this.props.selectPageCallback(pageNumber);
},
/**
* Build a list of page buttons based on page count
* @memberOf VisualizationComponents.Paginator.PageList
*
* @returns {Array.<jsx>}
*/
buildPageList: function () {
var selectedPage = this.props.currentPage,
range = this.props.range || 3,
min = this.props.currentPage - range >= 1 ? this.props.currentPage - range : 1,
max = this.props.currentPage + range <= this.props.pageCount ? this.props.currentPage + range : this.props.pageCount + 1,
self = this;
return _.range(min, max).map(function(pageNumber, index) {
var classes = classNames({
active: pageNumber === selectedPage
});
return (
<PageButton key={index} pageNumber={pageNumber} classes={classes} selectPageCallback={self.selectPage} />
);
});
},
/**
* Parent callback to select the previous page
* @memberOf VisualizationComponents.Paginator.PageList
*
* @param {event} e The click event triggering this function
*
* @return {null}
*/
previousPage: function (e) {
e.preventDefault();
if (this.props.currentPage === 1) {
return;
}
this.props.previousPageCallback();
},
/**
* Parent callback to select the next page
* @memberOf VisualizationComponents.Paginator.PageList
*
* @param {event} e The click event triggering this function
*
* @return {null}
*/
nextPage: function (e) {
e.preventDefault();
if (this.props.currentPage === this.props.pageCount) {
return;
}
this.props.nextPageCallback();
},
/**
* Select the first page
* @memberOf VisualizationComponents.Paginator.PageList
*
* @param {event} e The click event triggering this function
*
* @return {null}
*/
firstPage: function (e) {
e.preventDefault();
this.selectPage(1);
},
/**
* Select the last page
* @memberOf VisualizationComponents.Paginator.PageList
*
* @param {event} e The click event triggering this function
*
* @return {null}
*/
lastPage: function (e) {
e.preventDefault();
this.selectPage(this.props.pageCount);
},
/**
* Renders the list of pages to the page
* @memberOf VisualizationComponents.Paginator.PageList
*
* @return {jsx}
*/
render: function () {
// Build previous button's classes
var previousClasses = classNames({
previous : true,
disabled : this.props.currentPage === 1
});
// Build next button's classes
var nextClasses = classNames({
next : true,
disabled : this.props.currentPage === this.props.pageCount
});
return (
<div className="dataTables_paginate paging_simple_numbers" id="dataTables-example_paginate">
<ul className="pagination">
<li className={previousClasses} tabIndex="0" id="dataTables-example_previous">
<a href="#" onClick={this.firstPage} className="icon-angle-double-left" ></a>
</li>
<li className={previousClasses} tabIndex="0" id="dataTables-example_previous">
<a href="#" onClick={this.previousPage} className="icon-chevron-left" ></a>
</li>
{this.buildPageList()}
<li className={nextClasses} tabIndex="0" id="dataTables-example_next">
<a href="#" onClick={this.nextPage} className="icon-chevron-right"></a>
</li>
<li className={nextClasses} tabIndex="0" id="dataTables-example_next">
<a href="#" onClick={this.lastPage} className="icon-angle-double-right"></a>
</li>
</ul>
</div>
);
}
});
module.exports = PageList;