forked from AdeleD/react-paginate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PaginationBoxView.js
executable file
·301 lines (249 loc) · 10.7 KB
/
PaginationBoxView.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _PageView = require('./PageView');
var _PageView2 = _interopRequireDefault(_PageView);
var _BreakView = require('./BreakView');
var _BreakView2 = _interopRequireDefault(_BreakView);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var PaginationBoxView = function (_Component) {
_inherits(PaginationBoxView, _Component);
function PaginationBoxView(props) {
_classCallCheck(this, PaginationBoxView);
var _this = _possibleConstructorReturn(this, (PaginationBoxView.__proto__ || Object.getPrototypeOf(PaginationBoxView)).call(this, props));
_this.handlePreviousPage = function (evt) {
var selected = _this.state.selected;
evt.preventDefault ? evt.preventDefault() : evt.returnValue = false;
if (selected > 0) {
_this.handlePageSelected(selected - 1, evt);
}
};
_this.handleNextPage = function (evt) {
var selected = _this.state.selected;
var pageCount = _this.props.pageCount;
evt.preventDefault ? evt.preventDefault() : evt.returnValue = false;
if (selected < pageCount - 1) {
_this.handlePageSelected(selected + 1, evt);
}
};
_this.handlePageSelected = function (selected, evt) {
evt.preventDefault ? evt.preventDefault() : evt.returnValue = false;
if (_this.state.selected === selected) return;
_this.setState({ selected: selected });
// Call the callback with the new selected item:
_this.callCallback(selected);
};
_this.callCallback = function (selectedItem) {
if (typeof _this.props.onPageChange !== "undefined" && typeof _this.props.onPageChange === "function") {
_this.props.onPageChange({ selected: selectedItem });
}
};
_this.pagination = function () {
var items = [];
var _this$props = _this.props,
pageRangeDisplayed = _this$props.pageRangeDisplayed,
pageCount = _this$props.pageCount,
marginPagesDisplayed = _this$props.marginPagesDisplayed,
breakLabel = _this$props.breakLabel,
breakClassName = _this$props.breakClassName;
var selected = _this.state.selected;
if (pageCount <= pageRangeDisplayed) {
for (var index = 0; index < pageCount; index++) {
items.push(_this.getPageElement(index));
}
} else {
var leftSide = pageRangeDisplayed / 2;
var rightSide = pageRangeDisplayed - leftSide;
if (selected > pageCount - pageRangeDisplayed / 2) {
rightSide = pageCount - selected;
leftSide = pageRangeDisplayed - rightSide;
} else if (selected < pageRangeDisplayed / 2) {
leftSide = selected;
rightSide = pageRangeDisplayed - leftSide;
}
var _index = void 0;
var page = void 0;
var breakView = void 0;
var createPageView = function createPageView(index) {
return _this.getPageElement(index);
};
for (_index = 0; _index < pageCount; _index++) {
page = _index + 1;
if (page <= marginPagesDisplayed) {
items.push(createPageView(_index));
continue;
}
if (page > pageCount - marginPagesDisplayed) {
items.push(createPageView(_index));
continue;
}
if (_index >= selected - leftSide && _index <= selected + rightSide) {
items.push(createPageView(_index));
continue;
}
if (breakLabel && items[items.length - 1] !== breakView) {
breakView = _react2.default.createElement(_BreakView2.default, {
key: _index,
breakLabel: breakLabel,
breakClassName: breakClassName
});
items.push(breakView);
}
}
}
return items;
};
_this.state = {
selected: props.initialPage ? props.initialPage : props.forcePage ? props.forcePage : 0
};
return _this;
}
_createClass(PaginationBoxView, [{
key: 'componentDidMount',
value: function componentDidMount() {
var _props = this.props,
initialPage = _props.initialPage,
disableInitialCallback = _props.disableInitialCallback;
// Call the callback with the initialPage item:
if (typeof initialPage !== 'undefined' && !disableInitialCallback) {
this.callCallback(initialPage);
}
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (typeof nextProps.forcePage !== 'undefined' && this.props.forcePage !== nextProps.forcePage) {
this.setState({ selected: nextProps.forcePage });
}
}
}, {
key: 'hrefBuilder',
value: function hrefBuilder(pageIndex) {
var _props2 = this.props,
hrefBuilder = _props2.hrefBuilder,
pageCount = _props2.pageCount;
if (hrefBuilder && pageIndex !== this.state.selected && pageIndex >= 0 && pageIndex < pageCount) {
return hrefBuilder(pageIndex + 1);
}
}
}, {
key: 'getPageElement',
value: function getPageElement(index) {
var selected = this.state.selected;
var _props3 = this.props,
pageClassName = _props3.pageClassName,
pageLinkClassName = _props3.pageLinkClassName,
activeClassName = _props3.activeClassName,
extraAriaContext = _props3.extraAriaContext;
return _react2.default.createElement(_PageView2.default, {
key: index,
onClick: this.handlePageSelected.bind(null, index),
selected: selected === index,
pageClassName: pageClassName,
pageLinkClassName: pageLinkClassName,
activeClassName: activeClassName,
extraAriaContext: extraAriaContext,
href: this.hrefBuilder(index),
page: index + 1 });
}
}, {
key: 'render',
value: function render() {
var _props4 = this.props,
disabledClassName = _props4.disabledClassName,
previousClassName = _props4.previousClassName,
nextClassName = _props4.nextClassName,
pageCount = _props4.pageCount,
containerClassName = _props4.containerClassName,
previousLinkClassName = _props4.previousLinkClassName,
previousLabel = _props4.previousLabel,
nextLinkClassName = _props4.nextLinkClassName,
nextLabel = _props4.nextLabel;
var selected = this.state.selected;
var previousClasses = previousClassName + (selected === 0 ? ' ' + disabledClassName : '');
var nextClasses = nextClassName + (selected === pageCount - 1 ? ' ' + disabledClassName : '');
return _react2.default.createElement(
'ul',
{ className: containerClassName },
_react2.default.createElement(
'li',
{ className: previousClasses },
_react2.default.createElement(
'a',
{ onClick: this.handlePreviousPage,
className: previousLinkClassName,
href: this.hrefBuilder(selected - 1),
tabIndex: '0',
role: 'button',
onKeyPress: this.handlePreviousPage },
previousLabel
)
),
this.pagination(),
_react2.default.createElement(
'li',
{ className: nextClasses },
_react2.default.createElement(
'a',
{ onClick: this.handleNextPage,
className: nextLinkClassName,
href: this.hrefBuilder(selected + 1),
tabIndex: '0',
role: 'button',
onKeyPress: this.handleNextPage },
nextLabel
)
)
);
}
}]);
return PaginationBoxView;
}(_react.Component);
PaginationBoxView.propTypes = {
pageCount: _propTypes2.default.number.isRequired,
pageRangeDisplayed: _propTypes2.default.number.isRequired,
marginPagesDisplayed: _propTypes2.default.number.isRequired,
previousLabel: _propTypes2.default.node,
nextLabel: _propTypes2.default.node,
breakLabel: _propTypes2.default.node,
hrefBuilder: _propTypes2.default.func,
onPageChange: _propTypes2.default.func,
initialPage: _propTypes2.default.number,
forcePage: _propTypes2.default.number,
disableInitialCallback: _propTypes2.default.bool,
containerClassName: _propTypes2.default.string,
pageClassName: _propTypes2.default.string,
pageLinkClassName: _propTypes2.default.string,
activeClassName: _propTypes2.default.string,
previousClassName: _propTypes2.default.string,
nextClassName: _propTypes2.default.string,
previousLinkClassName: _propTypes2.default.string,
nextLinkClassName: _propTypes2.default.string,
disabledClassName: _propTypes2.default.string,
breakClassName: _propTypes2.default.string
};
PaginationBoxView.defaultProps = {
pageCount: 10,
pageRangeDisplayed: 2,
marginPagesDisplayed: 3,
activeClassName: "selected",
previousClassName: "previous",
nextClassName: "next",
previousLabel: "Previous",
nextLabel: "Next",
breakLabel: "...",
disabledClassName: "disabled",
disableInitialCallback: false
};
exports.default = PaginationBoxView;
;
//# sourceMappingURL=PaginationBoxView.js.map