Home Reference Source

application/components/common/paginator.js

  1. import React from 'react';
  2. import {Link} from 'react-router';
  3. import Request from './../../util/request';
  4. import URI from 'urijs';
  5.  
  6. /**
  7. * Pagination component. Currently, updates the URL for each
  8. * page change. Use the onPaginate parameter to watch for changes.
  9. * Expects pagination property to be API standard.
  10. */
  11. class Paginator extends React.Component {
  12.  
  13. constructor(props) {
  14. super(props);
  15.  
  16. this.onPaginate = this.onPaginate.bind(this);
  17. }
  18.  
  19. onPaginate(...params) {
  20. if (this.props.onPaginate) {
  21. this.props.onPaginate(...params);
  22. }
  23. }
  24.  
  25. render() {
  26. if (!this.props.data.size) {
  27. return (
  28. <ul className="pagination"></ul>
  29. );
  30. }
  31.  
  32. let pages = [];
  33. let url = '';
  34.  
  35. if (this.props.url) {
  36. url = this.props.url;
  37. } else {
  38. url = Request.getPath();
  39. }
  40.  
  41. let previousClass = 'waves-effect';
  42. if (parseInt(this.props.data.get('before')) === parseInt(this.props.data.get('current'))) {
  43. previousClass = 'disabled';
  44. }
  45. let nextClass = '';
  46. if (parseInt(this.props.data.get('current')) === parseInt(this.props.data.get('last'))) {
  47. nextClass = 'disabled';
  48. }
  49.  
  50. const prevQuery = Request.setQuery({'page': this.props.data.get('before')});
  51. const nextQuery = Request.setQuery({'page': this.props.data.get('next')});
  52.  
  53. const previous = (
  54. <li className={previousClass}>
  55. <Link to={url + '?' + prevQuery} onClick={this.onPaginate.bind(this, this.props.data.get('before'))}><i className='fa fa-chevron-left'></i></Link>
  56. </li>
  57. );
  58. const next = (
  59. <li className={nextClass}>
  60. <Link to={url + '?' + nextQuery} onClick={this.onPaginate.bind(this, this.props.data.get('next'))}><i className='fa fa-chevron-right'></i></Link>
  61. </li>
  62. );
  63.  
  64. const total = parseInt(this.props.data.get('total_pages'));
  65. const current = parseInt(this.props.data.get('current'));
  66. if (total <= 8) {
  67. for (let i = 0; i < total; i++) {
  68. const newQuery = Request.setQuery({'page': (i+1)});
  69. pages.push((
  70. <li key={(i+1)} className={((i+1) === current) ? 'active' : 'waves-effect'}>
  71. <Link to={url + '?' + newQuery} onClick={this.onPaginate.bind(this, (i+1))}>{(i+1)}</Link>
  72. </li>
  73. ));
  74. }
  75. } else {
  76. let start = 1;
  77. if (current > 6) {
  78. start = current - 5;
  79. }
  80. let max = start+10;
  81. if (total <= 10) {
  82. max = total;
  83. }
  84. for (let i = start; i < max; i++) {
  85. const newQuery = Request.setQuery({'page': i});
  86. pages.push((
  87. <li key={i} className={(i === current) ? 'active' : 'waves-effect'}>
  88. <Link to={url + '?' + newQuery} onClick={this.onPaginate.bind(this, i)}>{i}</Link>
  89. </li>
  90. ));
  91. }
  92. }
  93.  
  94. return (
  95. <ul className="pagination">
  96. {previous}
  97. {pages}
  98. {next}
  99. </ul>
  100. );
  101.  
  102. }
  103. }
  104.  
  105. export default Paginator;