1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00

153 lines
4.0 KiB
React
Raw Normal View History

2018-07-20 11:32:48 +02:00
import React from 'react'
import {connect} from 'react-redux'
import {Link} from 'react-router'
2018-07-20 11:32:48 +02:00
const PageLink = function(props) {
const linkPage = parseInt(props.page);
2018-07-23 12:27:37 +02:00
const label = props.label || (linkPage + 1);
if (props.disabled) {
return <span>{label}</span>;
}
2018-07-20 11:32:48 +02:00
let pr = props.pageReceived;
let pf = props.pageFiltered;
let pn = props.pageNotExported;
// This here can be surely more elegant.
switch(props.anchor) {
case "routes-received":
pr = linkPage;
break;
case "routes-filtered":
pf = linkPage;
break;
case "routes-not-exported":
pn = linkPage;
break;
}
const search = `?pr=${pr}&pf=${pf}&pn=${pn}`;
const hash = `#${props.anchor}`;
const linkTo = {
pathname: props.routing.pathname,
2018-07-20 11:32:48 +02:00
hash: hash,
search: search,
2018-07-20 11:32:48 +02:00
};
2018-07-23 12:27:37 +02:00
2018-07-20 11:32:48 +02:00
return (
2018-07-23 12:27:37 +02:00
<Link to={linkTo}>{label}</Link>
2018-07-20 11:32:48 +02:00
);
}
2018-07-20 11:32:48 +02:00
class RoutesPaginatorView extends React.Component {
render() {
if (this.props.totalPages <= 1) {
return null; // Nothing to paginate
}
2018-07-20 11:32:48 +02:00
const pageLinks = Array.from(Array(this.props.totalPages), (_, i) => {
2018-07-23 12:27:37 +02:00
let className = "";
if (i == this.props.page) {
className = "active";
}
2018-07-20 11:32:48 +02:00
return (
2018-07-23 12:27:37 +02:00
<li key={i} className={className}>
<PageLink page={i}
routing={this.props.routing}
anchor={this.props.anchor}
pageReceived={this.props.pageReceived}
pageFiltered={this.props.pageFiltered}
pageNotExported={this.props.pageNotExported} />
2018-07-20 11:32:48 +02:00
</li>
);
});
2018-07-23 12:27:37 +02:00
let prevLinkClass = "";
if (this.props.page == 0) {
prevLinkClass = "disabled";
}
let nextLinkClass = "";
if (this.props.page + 1 == this.props.totalPages) {
nextLinkClass = "disabled";
}
2018-07-20 11:32:48 +02:00
return (
<nav aria-label="Routes Pagination">
<ul className="pagination">
2018-07-23 12:27:37 +02:00
<li className={prevLinkClass}>
<PageLink page={this.props.page - 1}
label="&laquo;"
disabled={this.props.page == 0}
routing={this.props.routing}
anchor={this.props.anchor}
pageReceived={this.props.pageReceived}
pageFiltered={this.props.pageFiltered}
pageNotExported={this.props.pageNotExported} />
2018-07-20 11:32:48 +02:00
</li>
{pageLinks}
2018-07-23 12:27:37 +02:00
<li className={nextLinkClass}>
<PageLink page={this.props.page + 1}
disabled={this.props.page + 1 == this.props.totalPages}
label="&raquo;"
routing={this.props.routing}
anchor={this.props.anchor}
pageReceived={this.props.pageReceived}
pageFiltered={this.props.pageFiltered}
pageNotExported={this.props.pageNotExported} />
</li>
2018-07-20 11:32:48 +02:00
</ul>
</nav>
);
}
}
export const RoutesPaginator = connect(
2018-07-20 11:32:48 +02:00
(state) => ({
pageReceived: state.routes.receivedPage,
pageFiltered: state.routes.filteredPage,
pageNotExported: state.routes.notExportedPage,
routing: state.routing.locationBeforeTransitions
})
)(RoutesPaginatorView);
export class RoutesPaginationInfo extends React.Component {
render() {
const totalResults = this.props.totalResults;
const perPage = this.props.pageSize;
const start = this.props.page * perPage + 1;
const end = Math.min(start + perPage - 1, totalResults);
if (this.props.totalPages == 1) {
let routes = "route";
if (totalResults > 1) {
routes = "routes";
}
return (
<div className="routes-pagination-info pull-right">
Showing <b>all</b> of <b>{totalResults}</b> {routes}
</div>
);
}
return (
<div className="routes-pagination-info pull-right">
Showing <b>{start} - {end}</b> of <b>{totalResults}</b> total routes
</div>
);
}
}