1 | 'use strict';
|
2 |
|
3 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
4 |
|
5 | var React = require('react');
|
6 | var React__default = _interopDefault(React);
|
7 | var PropTypes = _interopDefault(require('prop-types'));
|
8 |
|
9 | function _classCallCheck(instance, Constructor) {
|
10 | if (!(instance instanceof Constructor)) {
|
11 | throw new TypeError("Cannot call a class as a function");
|
12 | }
|
13 | }
|
14 |
|
15 | function _defineProperties(target, props) {
|
16 | for (var i = 0; i < props.length; i++) {
|
17 | var descriptor = props[i];
|
18 | descriptor.enumerable = descriptor.enumerable || false;
|
19 | descriptor.configurable = true;
|
20 | if ("value" in descriptor) descriptor.writable = true;
|
21 | Object.defineProperty(target, descriptor.key, descriptor);
|
22 | }
|
23 | }
|
24 |
|
25 | function _createClass(Constructor, protoProps, staticProps) {
|
26 | if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
27 | if (staticProps) _defineProperties(Constructor, staticProps);
|
28 | return Constructor;
|
29 | }
|
30 |
|
31 | function _defineProperty(obj, key, value) {
|
32 | if (key in obj) {
|
33 | Object.defineProperty(obj, key, {
|
34 | value: value,
|
35 | enumerable: true,
|
36 | configurable: true,
|
37 | writable: true
|
38 | });
|
39 | } else {
|
40 | obj[key] = value;
|
41 | }
|
42 |
|
43 | return obj;
|
44 | }
|
45 |
|
46 | function _objectSpread(target) {
|
47 | for (var i = 1; i < arguments.length; i++) {
|
48 | var source = arguments[i] != null ? arguments[i] : {};
|
49 | var ownKeys = Object.keys(source);
|
50 |
|
51 | if (typeof Object.getOwnPropertySymbols === 'function') {
|
52 | ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
|
53 | return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
54 | }));
|
55 | }
|
56 |
|
57 | ownKeys.forEach(function (key) {
|
58 | _defineProperty(target, key, source[key]);
|
59 | });
|
60 | }
|
61 |
|
62 | return target;
|
63 | }
|
64 |
|
65 | function _inherits(subClass, superClass) {
|
66 | if (typeof superClass !== "function" && superClass !== null) {
|
67 | throw new TypeError("Super expression must either be null or a function");
|
68 | }
|
69 |
|
70 | subClass.prototype = Object.create(superClass && superClass.prototype, {
|
71 | constructor: {
|
72 | value: subClass,
|
73 | writable: true,
|
74 | configurable: true
|
75 | }
|
76 | });
|
77 | if (superClass) _setPrototypeOf(subClass, superClass);
|
78 | }
|
79 |
|
80 | function _getPrototypeOf(o) {
|
81 | _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
|
82 | return o.__proto__ || Object.getPrototypeOf(o);
|
83 | };
|
84 | return _getPrototypeOf(o);
|
85 | }
|
86 |
|
87 | function _setPrototypeOf(o, p) {
|
88 | _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
|
89 | o.__proto__ = p;
|
90 | return o;
|
91 | };
|
92 |
|
93 | return _setPrototypeOf(o, p);
|
94 | }
|
95 |
|
96 | function _objectWithoutPropertiesLoose(source, excluded) {
|
97 | if (source == null) return {};
|
98 | var target = {};
|
99 | var sourceKeys = Object.keys(source);
|
100 | var key, i;
|
101 |
|
102 | for (i = 0; i < sourceKeys.length; i++) {
|
103 | key = sourceKeys[i];
|
104 | if (excluded.indexOf(key) >= 0) continue;
|
105 | target[key] = source[key];
|
106 | }
|
107 |
|
108 | return target;
|
109 | }
|
110 |
|
111 | function _objectWithoutProperties(source, excluded) {
|
112 | if (source == null) return {};
|
113 |
|
114 | var target = _objectWithoutPropertiesLoose(source, excluded);
|
115 |
|
116 | var key, i;
|
117 |
|
118 | if (Object.getOwnPropertySymbols) {
|
119 | var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
120 |
|
121 | for (i = 0; i < sourceSymbolKeys.length; i++) {
|
122 | key = sourceSymbolKeys[i];
|
123 | if (excluded.indexOf(key) >= 0) continue;
|
124 | if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
125 | target[key] = source[key];
|
126 | }
|
127 | }
|
128 |
|
129 | return target;
|
130 | }
|
131 |
|
132 | function _assertThisInitialized(self) {
|
133 | if (self === void 0) {
|
134 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
135 | }
|
136 |
|
137 | return self;
|
138 | }
|
139 |
|
140 | function _possibleConstructorReturn(self, call) {
|
141 | if (call && (typeof call === "object" || typeof call === "function")) {
|
142 | return call;
|
143 | }
|
144 |
|
145 | return _assertThisInitialized(self);
|
146 | }
|
147 |
|
148 | function _toConsumableArray(arr) {
|
149 | return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
|
150 | }
|
151 |
|
152 | function _arrayWithoutHoles(arr) {
|
153 | if (Array.isArray(arr)) {
|
154 | for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
|
155 |
|
156 | return arr2;
|
157 | }
|
158 | }
|
159 |
|
160 | function _iterableToArray(iter) {
|
161 | if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
|
162 | }
|
163 |
|
164 | function _nonIterableSpread() {
|
165 | throw new TypeError("Invalid attempt to spread non-iterable instance");
|
166 | }
|
167 |
|
168 | function getRange(start, end) {
|
169 | return _toConsumableArray(Array(end - start + 1)).map(function (_, i) {
|
170 | return start + i;
|
171 | });
|
172 | }
|
173 | function getPageInfo(_ref) {
|
174 | var limit = _ref.limit,
|
175 | pageCount = _ref.pageCount,
|
176 | total = _ref.total,
|
177 | page = _ref.page;
|
178 | var totalPages = Math.ceil(total / limit);
|
179 | var totalResults = parseInt(total, 10);
|
180 | var currentPage = parseInt(page, 10);
|
181 |
|
182 | if (currentPage < 1) {
|
183 | currentPage = 1;
|
184 | }
|
185 |
|
186 | if (currentPage > totalPages) {
|
187 | currentPage = totalPages;
|
188 | }
|
189 |
|
190 | var firstPage = Math.max(1, currentPage - Math.floor(pageCount / 2));
|
191 | var lastPage = Math.min(totalPages, currentPage + Math.floor(pageCount / 2));
|
192 |
|
193 | if (lastPage - firstPage + 1 < pageCount) {
|
194 | if (currentPage < totalPages / 2) {
|
195 | lastPage = Math.min(totalPages, lastPage + (pageCount - (lastPage - firstPage)));
|
196 | } else {
|
197 | firstPage = Math.max(1, firstPage - (pageCount - (lastPage - firstPage)));
|
198 | }
|
199 | }
|
200 |
|
201 | if (lastPage - firstPage + 1 > pageCount) {
|
202 | if (currentPage > totalPages / 2) {
|
203 |
|
204 | firstPage++;
|
205 | } else {
|
206 |
|
207 | lastPage--;
|
208 | }
|
209 | }
|
210 |
|
211 | var firstResult = limit * (currentPage - 1);
|
212 | var lastResult = limit * currentPage - 1;
|
213 | return {
|
214 | totalPages: totalPages,
|
215 | pages: Math.min(lastPage - firstPage + 1, totalPages),
|
216 | currentPage: currentPage,
|
217 | firstPage: firstPage,
|
218 | lastPage: lastPage,
|
219 | previousPage: currentPage - 1,
|
220 | nextPage: currentPage + 1,
|
221 | hasPreviousPage: currentPage > 1,
|
222 | hasNextPage: currentPage < totalPages,
|
223 | totalResults: totalResults,
|
224 | results: Math.min(lastResult - firstResult + 1, totalResults),
|
225 | firstResult: firstResult,
|
226 | lastResult: lastResult
|
227 | };
|
228 | }
|
229 |
|
230 | var Pagination =
|
231 |
|
232 | function (_Component) {
|
233 | _inherits(Pagination, _Component);
|
234 |
|
235 | function Pagination() {
|
236 | var _this;
|
237 |
|
238 | _classCallCheck(this, Pagination);
|
239 |
|
240 | _this = _possibleConstructorReturn(this, _getPrototypeOf(Pagination).call(this));
|
241 |
|
242 | _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_getPageItemProps", function (props) {
|
243 | var pageValue = props.pageValue,
|
244 | handlePageChange = props.onPageChange,
|
245 | rest = _objectWithoutProperties(props, ["pageValue", "onPageChange"]);
|
246 |
|
247 | var onPageChange = function onPageChange(e) {
|
248 | if (typeof handlePageChange === 'function') {
|
249 | handlePageChange(pageValue, e);
|
250 | }
|
251 |
|
252 | _this.setState({
|
253 | currentPage: pageValue
|
254 | });
|
255 | };
|
256 |
|
257 | return _objectSpread({
|
258 | onClick: onPageChange
|
259 | }, rest);
|
260 | });
|
261 |
|
262 | _this.state = {
|
263 | currentPage: 0
|
264 | };
|
265 | return _this;
|
266 | }
|
267 |
|
268 | _createClass(Pagination, [{
|
269 | key: "componentWillMount",
|
270 | value: function componentWillMount() {
|
271 | if (this.props.currentPage) {
|
272 | this.setState({
|
273 | currentPage: parseInt(this.props.currentPage, 10)
|
274 | });
|
275 | }
|
276 | }
|
277 | }, {
|
278 | key: "componentWillReceiveProps",
|
279 | value: function componentWillReceiveProps(nextProps) {
|
280 | if (nextProps.currentPage !== this.props.currentPage) {
|
281 | this.setState({
|
282 | currentPage: parseInt(nextProps.currentPage, 10)
|
283 | });
|
284 | }
|
285 | }
|
286 | }, {
|
287 | key: "render",
|
288 | value: function render() {
|
289 | var _this$props = this.props,
|
290 | total = _this$props.total,
|
291 | limit = _this$props.limit,
|
292 | pageCount = _this$props.pageCount;
|
293 | var currentPage = this.state.currentPage;
|
294 | var pageInfo = getPageInfo({
|
295 | limit: limit,
|
296 | pageCount: pageCount,
|
297 | total: total,
|
298 | page: currentPage
|
299 | });
|
300 | var firstPage = pageInfo.firstPage,
|
301 | lastPage = pageInfo.lastPage,
|
302 | hasNextPage = pageInfo.hasNextPage,
|
303 | hasPreviousPage = pageInfo.hasPreviousPage,
|
304 | previousPage = pageInfo.previousPage,
|
305 | nextPage = pageInfo.nextPage,
|
306 | totalPages = pageInfo.totalPages;
|
307 | var pages = total ? getRange(firstPage, lastPage) : [];
|
308 | return React__default.createElement("div", null, this.props.children({
|
309 | pages: pages,
|
310 | previousPage: previousPage,
|
311 | nextPage: nextPage,
|
312 | totalPages: totalPages,
|
313 | currentPage: currentPage,
|
314 | hasNextPage: hasNextPage,
|
315 | hasPreviousPage: hasPreviousPage,
|
316 | getPageItemProps: this._getPageItemProps
|
317 | }));
|
318 | }
|
319 | }]);
|
320 |
|
321 | return Pagination;
|
322 | }(React.Component);
|
323 |
|
324 | Pagination.propTypes = {
|
325 | total: PropTypes.number.isRequired,
|
326 | limit: PropTypes.number,
|
327 | pageCount: PropTypes.number,
|
328 | currentPage: PropTypes.number,
|
329 | pageValue: PropTypes.number,
|
330 | children: PropTypes.func.isRequired,
|
331 | onPageChange: PropTypes.func
|
332 | };
|
333 | Pagination.defaultProps = {
|
334 | limit: 10,
|
335 | pageCount: 5,
|
336 | currentPage: 0,
|
337 | pageValue: 0
|
338 | };
|
339 |
|
340 | module.exports = Pagination;
|