247 lines
5.5 KiB
JavaScript
Raw Normal View History

2017-04-11 11:34:59 +02:00
/**
*
* Pagination
*
*/
import React from 'react';
import styles from './styles.scss';
class Pagination extends React.Component { // eslint-disable-line react/prefer-stateless-function
constructor(props) {
super(props);
this.onGoPreviousPageClicked = this.onGoPreviousPageClicked.bind(this);
this.onGoNextPageClicked = this.onGoNextPageClicked.bind(this);
2017-04-11 18:22:15 +02:00
this.onGoFirstPageClicked = this.onGoFirstPageClicked.bind(this);
this.onGoLastPageClicked = this.onGoLastPageClicked.bind(this);
2017-04-11 11:34:59 +02:00
}
/**
* Check if the current page is the first one or not
*
* @returns {boolean}
*/
isFirstPage() {
return this.props.currentPage === 1;
}
/**
2017-04-11 18:22:15 +02:00
* Check if the previous links dots
* should be displayed or not
2017-04-11 11:34:59 +02:00
*
* @returns {boolean}
*/
needPreviousLinksDots() {
2017-04-11 18:22:15 +02:00
return this.props.currentPage > 3;
2017-04-11 11:34:59 +02:00
}
2017-04-11 18:22:15 +02:00
/**
* Check if the after links dots
* should be displayed or not
*
* @returns {boolean}
*/
2017-04-11 11:34:59 +02:00
needAfterLinksDots() {
2017-04-11 18:22:15 +02:00
return this.props.currentPage < this.getLastPageNumber() - 1;
2017-04-11 11:34:59 +02:00
}
/**
* Return the last page number
*
* @returns {number}
*/
2017-04-11 18:22:15 +02:00
getLastPageNumber() {
2017-04-11 17:35:40 +02:00
return Math.ceil(this.props.count / this.props.limit);
2017-04-11 11:34:59 +02:00
}
/**
* Check if the current page is the last one
*
* @returns {boolean}
*/
isLastPage() {
2017-04-11 18:22:15 +02:00
return this.props.currentPage === this.getLastPageNumber();
2017-04-11 11:34:59 +02:00
}
/**
2017-04-11 18:22:15 +02:00
* Triggered on previous page click
2017-04-11 11:34:59 +02:00
*
2017-04-11 18:22:15 +02:00
* Prevent link default behavior and go to previous page
2017-04-11 11:34:59 +02:00
*
* @param e {Object} Click event
*/
onGoPreviousPageClicked(e) {
e.preventDefault();
if (!this.isFirstPage()) {
2017-04-11 11:53:00 +02:00
this.props.changePage(this.props.currentPage - 1);
2017-04-11 11:34:59 +02:00
}
}
/**
2017-04-11 18:22:15 +02:00
* Triggered on next page click
2017-04-11 11:34:59 +02:00
*
2017-04-11 18:22:15 +02:00
* Prevent link default behavior and go to next page
2017-04-11 11:34:59 +02:00
*
* @param e {Object} Click event
*/
onGoNextPageClicked(e) {
e.preventDefault();
if (!this.isLastPage()) {
2017-04-11 11:53:00 +02:00
this.props.changePage(this.props.currentPage + 1);
2017-04-11 11:34:59 +02:00
}
}
2017-04-11 18:22:15 +02:00
/**
* Triggered on dots click
*
* Simply prevent link default behavior
*
* @param e {Object} Click event
*/
onDotsClicked(e) {
e.preventDefault();
}
/**
* Triggered on first page click
*
* Prevent link default behavior and go to first page
*
* @param e {Object} Click event
*/
onGoFirstPageClicked(e) {
e.preventDefault();
this.props.changePage(1);
}
/**
* Triggered on last page click
*
* Prevent link default behavior and go to last page
*
* @param e {Object} Click event
*/
onGoLastPageClicked(e) {
e.preventDefault();
this.props.changePage(this.getLastPageNumber());
}
2017-04-11 11:34:59 +02:00
render() {
// Init variables
const linksOptions = [];
// Add active page link
linksOptions.push({
value: this.props.currentPage,
isActive: true,
onClick: (e) => {
e.preventDefault();
},
});
// Add previous page link
if (!this.isFirstPage()) {
linksOptions.unshift({
value: this.props.currentPage - 1,
isActive: false,
onClick: this.onGoPreviousPageClicked,
});
}
// Add next page link
2017-05-03 17:32:38 +02:00
if (!this.isLastPage() && !this.isFirstPage()) {
2017-04-11 11:34:59 +02:00
linksOptions.push({
value: this.props.currentPage + 1,
isActive: false,
onClick: this.onGoNextPageClicked,
});
}
2017-04-11 18:22:15 +02:00
// Add previous link dots and first page link
2017-04-11 11:34:59 +02:00
if (this.needPreviousLinksDots()) {
2017-04-11 18:22:15 +02:00
linksOptions.unshift({
value: '...',
isActive: false,
onClick: this.onDotsClicked,
});
linksOptions.unshift({
value: 1,
isActive: false,
onClick: this.onGoFirstPageClicked,
});
2017-04-11 11:34:59 +02:00
}
2017-04-11 18:22:15 +02:00
// Add next link dots and last page link
2017-04-11 11:34:59 +02:00
if (this.needAfterLinksDots()) {
2017-04-11 18:22:15 +02:00
linksOptions.push({
value: '...',
isActive: false,
onClick: this.onDotsClicked,
});
linksOptions.push({
value: this.getLastPageNumber(),
isActive: false,
onClick: this.onGoLastPageClicked,
});
2017-04-11 11:34:59 +02:00
}
// Generate links
2017-04-11 18:22:15 +02:00
const links = linksOptions.map((linksOption, i) => (
<li className={`${styles.navLi} ${linksOption.isActive && styles.navLiActive}`} key={i}>
2017-04-11 11:34:59 +02:00
<a href disabled={linksOption.isActive} onClick={linksOption.onClick}>
{linksOption.value}
</a>
</li>
)
);
return (
<div className={styles.pagination}>
<a href
className={`
${styles.paginationNavigator}
${styles.paginationNavigatorPrevious}
${this.isFirstPage() && styles.paginationNavigatorDisabled}
`}
onClick={this.onGoPreviousPageClicked}
disabled={this.isFirstPage()}>
<i className="ion ion-chevron-left"></i>
</a>
<div className={styles.separator}></div>
<nav className={styles.nav}>
<ul className={styles.navUl}>
{links}
</ul>
</nav>
<a href
className={`
${styles.paginationNavigator}
${styles.paginationNavigatorNext}
${this.isLastPage() && styles.paginationNavigatorDisabled}
`}
onClick={this.onGoNextPageClicked}
disabled={this.isLastPage()}>
<i className="ion ion-chevron-right"></i>
</a>
</div>
);
}
}
2017-04-11 19:20:33 +02:00
Pagination.propTypes = {
limit: React.PropTypes.number,
currentPage: React.PropTypes.number,
changePage: React.PropTypes.func,
count: React.PropTypes.oneOfType([
React.PropTypes.number,
React.PropTypes.bool,
]),
};
2017-04-11 11:34:59 +02:00
export default Pagination;