RecentCreated.jsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import React from 'react';
  2. import Page from '../PageList/Page';
  3. import PropTypes from 'prop-types';
  4. import Pagination from 'react-bootstrap/lib/Pagination';
  5. export default class RecentCreated extends React.Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. pages: [],
  10. activePage: 1,
  11. paginationNumbers: {},
  12. };
  13. this.calculatePagination = this.calculatePagination.bind(this);
  14. }
  15. componentWillMount() {
  16. this.getRecentCreatedList(1);
  17. }
  18. getRecentCreatedList(selectPageNumber) {
  19. const pageId = this.props.pageId;
  20. const userId = this.props.crowi.me;
  21. const limit = this.props.limit;
  22. const offset = (selectPageNumber - 1) * limit;
  23. // pagesList get and pagination calculate
  24. this.props.crowi.apiGet('/pages.recentCreated', { page_id: pageId, user: userId, limit, offset })
  25. .then(res => {
  26. const totalCount = res.totalCount;
  27. const pages = res.pages;
  28. const activePage = selectPageNumber;
  29. // pagiNation calculate function call
  30. const paginationNumbers = this.calculatePagination(limit, totalCount, activePage);
  31. this.setState({
  32. pages,
  33. activePage,
  34. paginationNumbers,
  35. });
  36. });
  37. }
  38. calculatePagination(limit, totalCount, activePage) {
  39. // calc totalPageNumber
  40. const totalPage = Math.floor(totalCount / limit) + (totalCount % limit === 0 ? 0 : 1);
  41. let paginationStart = activePage - 2;
  42. let maxViewPageNum = activePage + 2;
  43. // pagiNation Number area size = 5 , pageNuber calculate in here
  44. // activePage Position calculate ex. 4 5 [6] 7 8 (Page8 over is Max), 3 4 5 [6] 7 (Page7 is Max)
  45. if ( paginationStart < 1 ) {
  46. const diff = 1 - paginationStart;
  47. paginationStart += diff;
  48. maxViewPageNum = Math.min(totalPage, maxViewPageNum + diff);
  49. }
  50. if ( maxViewPageNum > totalPage ) {
  51. const diff = maxViewPageNum - totalPage;
  52. maxViewPageNum -= diff;
  53. paginationStart = Math.max(1, paginationStart - diff);
  54. }
  55. return {
  56. totalPage,
  57. paginationStart,
  58. maxViewPageNum,
  59. };
  60. }
  61. /**
  62. * generate Elements of Page
  63. *
  64. * @param {any} pages Array of pages Model Obj
  65. *
  66. */
  67. generatePageList(pages) {
  68. return pages.map(page => {
  69. return <Page page={page} key={'recent-created:list-view:' + page._id} />;
  70. });
  71. }
  72. /**
  73. * generate Elements of Pagination First Prev
  74. * ex. << < 1 2 3 > >>
  75. * this function set << & <
  76. */
  77. generateFirstPrev(activePage) {
  78. let paginationItems = [];
  79. if (1 != activePage) {
  80. paginationItems.push(
  81. <Pagination.First key="first" onClick={() => this.getRecentCreatedList(1)} />
  82. );
  83. paginationItems.push(
  84. <Pagination.Prev key="prev" onClick={() => this.getRecentCreatedList(this.state.activePage - 1)} />
  85. );
  86. }
  87. else {
  88. paginationItems.push(
  89. <Pagination.First key="first" disabled />
  90. );
  91. paginationItems.push(
  92. <Pagination.Prev key="prev" disabled />
  93. );
  94. }
  95. return paginationItems;
  96. }
  97. /**
  98. * generate Elements of Pagination First Prev
  99. * ex. << < 4 5 6 7 8 > >>, << < 1 2 3 4 > >>
  100. * this function set numbers
  101. */
  102. generatePaginations(activePage, paginationStart, maxViewPageNum) {
  103. let paginationItems = [];
  104. for (let number = paginationStart; number <= maxViewPageNum; number++) {
  105. paginationItems.push(
  106. <Pagination.Item key={number} active={number === activePage} onClick={ () => this.getRecentCreatedList(number)}>{number}</Pagination.Item>
  107. );
  108. }
  109. return paginationItems;
  110. }
  111. /**
  112. * generate Elements of Pagination First Prev
  113. * ex. << < 1 2 3 > >>
  114. * this function set > & >>
  115. */
  116. generateNextLast(activePage, totalPage) {
  117. let paginationItems = [];
  118. if (totalPage != activePage) {
  119. paginationItems.push(
  120. <Pagination.Next key="next" onClick={() => this.getRecentCreatedList(this.state.activePage + 1)} />
  121. );
  122. paginationItems.push(
  123. <Pagination.Last key="last" onClick={() => this.getRecentCreatedList(totalPage)} />
  124. );
  125. }
  126. else {
  127. paginationItems.push(
  128. <Pagination.Next key="next" disabled />
  129. );
  130. paginationItems.push(
  131. <Pagination.Last key="last" disabled />
  132. );
  133. }
  134. return paginationItems;
  135. }
  136. render() {
  137. const pageList = this.generatePageList(this.state.pages);
  138. let paginationItems = [];
  139. const activePage = this.state.activePage;
  140. const totalPage = this.state.paginationNumbers.totalPage;
  141. const paginationStart = this.state.paginationNumbers.paginationStart;
  142. const maxViewPageNum = this.state.paginationNumbers.maxViewPageNum;
  143. const firstPrevItems = this.generateFirstPrev(activePage);
  144. paginationItems.push(firstPrevItems);
  145. const paginations = this.generatePaginations(activePage, paginationStart, maxViewPageNum);
  146. paginationItems.push(paginations);
  147. const nextLastItems = this.generateNextLast(activePage, totalPage);
  148. paginationItems.push(nextLastItems);
  149. return (
  150. <div className="page-list-container-create">
  151. <ul className="page-list-ul page-list-ul-flat">
  152. {pageList}
  153. </ul>
  154. <Pagination bsSize="small">{paginationItems}</Pagination>
  155. </div>
  156. );
  157. }
  158. }
  159. RecentCreated.propTypes = {
  160. pageId: PropTypes.string.isRequired,
  161. crowi: PropTypes.object.isRequired,
  162. limit: PropTypes.number,
  163. };
  164. RecentCreated.defaultProps = {
  165. };