2016-07-07 23:23:18 +02:00
|
|
|
import React, { Component, PropTypes } from "react";
|
|
|
|
import { Link, withRouter } from "react-router";
|
|
|
|
|
|
|
|
export class Pagination extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.buildLinkTo.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
computePaginationBounds(currentPage, nPages, maxNumberPagesShown=5) {
|
|
|
|
// Taken from http://stackoverflow.com/a/8608998/2626416
|
|
|
|
var lowerLimit = currentPage;
|
|
|
|
var upperLimit = currentPage;
|
|
|
|
|
|
|
|
for (var b = 1; b < maxNumberPagesShown && b < nPages;) {
|
|
|
|
if (lowerLimit > 1 ) {
|
|
|
|
lowerLimit--;
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
if (b < maxNumberPagesShown && upperLimit < nPages) {
|
|
|
|
upperLimit++;
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
lowerLimit: lowerLimit,
|
|
|
|
upperLimit: upperLimit + 1 // +1 to ease iteration in for with <
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
buildLinkTo(pageNumber) {
|
|
|
|
return {
|
|
|
|
pathname: this.props.location.pathname,
|
|
|
|
query: Object.assign({}, this.props.location.query, { page: pageNumber })
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
goToPage() {
|
|
|
|
const pageNumber = parseInt(this.refs.pageInput.value);
|
2016-07-26 13:21:37 +02:00
|
|
|
$(this.refs.paginationModal).modal("hide");
|
|
|
|
if (pageNumber) {
|
|
|
|
this.props.router.push(this.buildLinkTo(pageNumber));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dotsOnClick() {
|
|
|
|
$(this.refs.paginationModal).modal();
|
|
|
|
}
|
|
|
|
|
|
|
|
dotsOnKeyDown(ev) {
|
|
|
|
ev.preventDefault;
|
|
|
|
const code = ev.keyCode || ev.which;
|
|
|
|
if (code == 13 || code == 32) { // Enter or Space key
|
|
|
|
this.dotsOnClick(); // Fire same event as onClick
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelModalBox() {
|
|
|
|
$(this.refs.paginationModal).modal("hide");
|
2016-07-07 23:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { lowerLimit, upperLimit } = this.computePaginationBounds(this.props.currentPage, this.props.nPages);
|
|
|
|
var pagesButton = [];
|
|
|
|
var key = 0; // key increment to ensure correct ordering
|
|
|
|
if (lowerLimit > 1) {
|
|
|
|
// Push first page
|
|
|
|
pagesButton.push(
|
|
|
|
<li className="page-item" key={key}>
|
2016-07-26 13:21:37 +02:00
|
|
|
<Link className="page-link" title="Go to page 1" to={this.buildLinkTo(1)}><span className="sr-only">Go to page </span>1</Link>
|
2016-07-07 23:23:18 +02:00
|
|
|
</li>
|
|
|
|
);
|
|
|
|
key++;
|
|
|
|
if (lowerLimit > 2) {
|
|
|
|
// Eventually push "…"
|
|
|
|
pagesButton.push(
|
|
|
|
<li className="page-item" key={key}>
|
2016-07-26 13:21:37 +02:00
|
|
|
<span tabIndex="0" role="button" onKeyDown={this.dotsOnKeyDown.bind(this)} onClick={this.dotsOnClick.bind(this)}>…</span>
|
2016-07-07 23:23:18 +02:00
|
|
|
</li>
|
|
|
|
);
|
|
|
|
key++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var i = 0;
|
|
|
|
for (i = lowerLimit; i < upperLimit; i++) {
|
|
|
|
var className = "page-item";
|
2016-07-26 13:21:37 +02:00
|
|
|
var currentSpan = null;
|
2016-07-07 23:23:18 +02:00
|
|
|
if (this.props.currentPage == i) {
|
|
|
|
className += " active";
|
2016-07-26 13:21:37 +02:00
|
|
|
currentSpan = <span className="sr-only">(current)</span>;
|
2016-07-07 23:23:18 +02:00
|
|
|
}
|
2016-07-26 13:21:37 +02:00
|
|
|
const title = "Go to page " + i;
|
2016-07-07 23:23:18 +02:00
|
|
|
pagesButton.push(
|
|
|
|
<li className={className} key={key}>
|
2016-07-26 13:21:37 +02:00
|
|
|
<Link className="page-link" title={title} to={this.buildLinkTo(i)}><span className="sr-only">Go to page </span>{i} {currentSpan}</Link>
|
2016-07-07 23:23:18 +02:00
|
|
|
</li>
|
|
|
|
);
|
|
|
|
key++;
|
|
|
|
}
|
|
|
|
if (i < this.props.nPages) {
|
|
|
|
if (i < this.props.nPages - 1) {
|
|
|
|
// Eventually push "…"
|
|
|
|
pagesButton.push(
|
|
|
|
<li className="page-item" key={key}>
|
2016-07-26 13:21:37 +02:00
|
|
|
<span tabIndex="0" role="button" onKeyDown={this.dotsOnKeyDown.bind(this)} onClick={this.dotsOnClick.bind(this)}>…</span>
|
2016-07-07 23:23:18 +02:00
|
|
|
</li>
|
|
|
|
);
|
|
|
|
key++;
|
|
|
|
}
|
2016-07-26 13:21:37 +02:00
|
|
|
const title = "Go to page " + this.props.nPages;
|
2016-07-07 23:23:18 +02:00
|
|
|
// Push last page
|
|
|
|
pagesButton.push(
|
|
|
|
<li className="page-item" key={key}>
|
2016-07-26 13:21:37 +02:00
|
|
|
<Link className="page-link" title={title} to={this.buildLinkTo(this.props.nPages)}><span className="sr-only">Go to page </span>{this.props.nPages}</Link>
|
2016-07-07 23:23:18 +02:00
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (pagesButton.length > 1) {
|
|
|
|
return (
|
|
|
|
<div>
|
2016-07-26 13:21:37 +02:00
|
|
|
<nav className="pagination-nav" aria-label="Page navigation">
|
2016-07-07 23:23:18 +02:00
|
|
|
<ul className="pagination">
|
|
|
|
{ pagesButton }
|
|
|
|
</ul>
|
|
|
|
</nav>
|
2016-07-26 13:21:37 +02:00
|
|
|
<div className="modal fade" ref="paginationModal" tabIndex="-1" role="dialog" aria-labelledby="paginationModalLabel">
|
|
|
|
<div className="modal-dialog" role="document">
|
2016-07-07 23:23:18 +02:00
|
|
|
<div className="modal-content">
|
|
|
|
<div className="modal-header">
|
2016-07-26 13:21:37 +02:00
|
|
|
<button type="button" className="close" data-dismiss="modal" aria-label="Close">×</button>
|
|
|
|
<h4 className="modal-title" id="paginationModalLabel">Page to go to?</h4>
|
2016-07-07 23:23:18 +02:00
|
|
|
</div>
|
|
|
|
<div className="modal-body">
|
|
|
|
<form>
|
2016-07-26 13:21:37 +02:00
|
|
|
<input className="form-control" autoComplete="off" type="number" ref="pageInput" aria-label="Page number to go to" />
|
2016-07-07 23:23:18 +02:00
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
<div className="modal-footer">
|
2016-07-26 13:21:37 +02:00
|
|
|
<button type="button" className="btn btn-default" onClick={this.cancelModalBox.bind(this)}>Cancel</button>
|
2016-07-07 23:23:18 +02:00
|
|
|
<button type="button" className="btn btn-primary" onClick={this.goToPage.bind(this)}>OK</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Pagination.propTypes = {
|
|
|
|
currentPage: PropTypes.number.isRequired,
|
|
|
|
location: PropTypes.object.isRequired,
|
|
|
|
nPages: PropTypes.number.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
export default withRouter(Pagination);
|