RecentCreated.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import Pagination from 'react-bootstrap/lib/Pagination';
  4. export default class RecentCreated extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. pages: [],
  9. limit: 10, // TODO GC-941で対応予定
  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.state.limit;
  22. const offset = (selectPageNumber - 1) * limit;
  23. // pagiNation
  24. this.props.crowi.apiGet('/pages.recentCreated', {page_id: pageId, user: userId, limit: limit, offset: offset, })
  25. .then(res => {
  26. const totalCount = res.pages[0].totalCount;
  27. const activePage = selectPageNumber;
  28. const pages = res.pages[1];
  29. // pageNation 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. let PaginationNumbers = {};
  40. // pagiNation totalPageNumber calculate
  41. let totalPage = totalCount % limit === 0 ? totalCount / limit : Math.floor(totalCount / limit) + 1;
  42. let paginationStart;
  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 ( activePage < 4) {
  46. paginationStart = 1;
  47. }
  48. else if ( activePage <= (totalPage - 2 )) {
  49. paginationStart = activePage- 2;
  50. }
  51. else if ( activePage >= (totalPage -2) ) {
  52. paginationStart = (totalPage > 5)? totalPage -4 : 1;
  53. }
  54. // MaxViewPageNum calculate ex. 4 5 6 7 8 , 1 2 3 4
  55. let MaxViewPageNum = (paginationStart ) + 4;
  56. if (totalPage < paginationStart + 4) {
  57. MaxViewPageNum = totalPage;
  58. }
  59. PaginationNumbers.totalPage = totalPage;
  60. PaginationNumbers.paginationStart = paginationStart;
  61. PaginationNumbers.MaxViewPageNum = MaxViewPageNum;
  62. return PaginationNumbers;
  63. }
  64. /**
  65. * generate Elements of Page
  66. *
  67. * @param {any} pages Array of pages Model Obj
  68. *
  69. */
  70. generatePageList(pages) {
  71. return pages.map((page, i) => {
  72. return (
  73. <li key={i}>
  74. <img src="/images/icons/user.svg" className="picture img-circle" ></img>
  75. <a href={page.path} className="page-list-link" data-path={page.path} data-short-path={page.revision.body}>{decodeURI(page.path)}</a>
  76. </li>
  77. );
  78. });
  79. }
  80. /**
  81. * generate Elements of Pagination First Prev
  82. * ex. << < 1 2 3 > >>
  83. * this function set << & <
  84. */
  85. generateFirstPrev(activePage) {
  86. let paginationItems = [];
  87. if (1 != activePage) {
  88. paginationItems.push(
  89. <Pagination.First key="first" onClick={() => this.getRecentCreatedList(1)} />
  90. );
  91. paginationItems.push(
  92. <Pagination.Prev key="prev" onClick={() => this.getRecentCreatedList(this.state.activePage - 1)} />
  93. );
  94. }
  95. else {
  96. paginationItems.push(
  97. <Pagination.First key="first" disabled />
  98. );
  99. paginationItems.push(
  100. <Pagination.Prev key="prev" disabled />
  101. );
  102. }
  103. return paginationItems;
  104. }
  105. /**
  106. * generate Elements of Pagination First Prev
  107. * ex. << < 4 5 6 7 8 > >>, << < 1 2 3 4 > >>
  108. * this function set numbers
  109. */
  110. generatePaginations(activePage, paginationStart, MaxViewPageNum) {
  111. let paginationItems = [];
  112. for (let number = paginationStart; number <= MaxViewPageNum; number++) {
  113. paginationItems.push(
  114. <Pagination.Item key={number} active={number === activePage} onClick={ () => this.getRecentCreatedList(number)}>{number}</Pagination.Item>
  115. );
  116. }
  117. return paginationItems;
  118. }
  119. /**
  120. * generate Elements of Pagination First Prev
  121. * ex. << < 1 2 3 > >>
  122. * this function set > & >>
  123. */
  124. generateNextLast(activePage, totalPage) {
  125. let paginationItems = [];
  126. if (totalPage != activePage) {
  127. paginationItems.push(
  128. <Pagination.Next key="next" onClick={() => this.getRecentCreatedList(this.state.activePage + 1)} />
  129. );
  130. paginationItems.push(
  131. <Pagination.Last key="last" onClick={() => this.getRecentCreatedList(totalPage)} />
  132. );
  133. }
  134. else {
  135. paginationItems.push(
  136. <Pagination.Next key="next" disabled />
  137. );
  138. paginationItems.push(
  139. <Pagination.Last key="last" disabled />
  140. );
  141. }
  142. return paginationItems;
  143. }
  144. render() {
  145. const pageList = this.generatePageList(this.state.pages);
  146. let paginationItems = [];
  147. let activePage = this.state.activePage;
  148. let totalPage = this.state.PaginationNumbers.totalPage;
  149. let paginationStart = this.state.PaginationNumbers.paginationStart;
  150. let MaxViewPageNum = this.state.PaginationNumbers.MaxViewPageNum;
  151. let firstPrevItems = this.generateFirstPrev(activePage);
  152. paginationItems.push(firstPrevItems);
  153. let paginations = this.generatePaginations(activePage, paginationStart, MaxViewPageNum);
  154. paginationItems.push(paginations);
  155. let nextLastItems = this.generateNextLast(activePage, totalPage);
  156. paginationItems.push(nextLastItems);
  157. return (
  158. <div className="page-list-container-create">
  159. <ul className="page-list-ul page-list-ul-flat">
  160. {pageList}
  161. </ul>
  162. {
  163. <Pagination bsSize="small">{paginationItems}</Pagination>
  164. }
  165. </div>
  166. );
  167. }
  168. }
  169. RecentCreated.propTypes = {
  170. pageId: PropTypes.string.isRequired,
  171. crowi: PropTypes.object.isRequired,
  172. };
  173. RecentCreated.defaultProps = {
  174. };