PageHistory.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import React from 'react';
  2. import Icon from './Common/Icon';
  3. import PageRevisionList from './PageHistory/PageRevisionList';
  4. export default class PageHistory extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.crowi = window.crowi; // FIXME
  8. this.state = {
  9. revisions: [],
  10. diffOpened: {},
  11. };
  12. this.getPreviousRevision = this.getPreviousRevision.bind(this);
  13. this.onDiffOpenClicked = this.onDiffOpenClicked.bind(this);
  14. }
  15. componentDidMount() {
  16. const pageId = this.props.pageId;
  17. if (!pageId) {
  18. return ;
  19. }
  20. this.crowi.apiGet('/revisions.ids', {page_id: pageId})
  21. .then(res => {
  22. const rev = res.revisions;
  23. let diffOpened = {};
  24. const lastId = rev.length - 1;
  25. res.revisions.map((revision, i) => {
  26. const user = this.crowi.findUserById(revision.author);
  27. if (user) {
  28. rev[i].author = user;
  29. }
  30. if (i === 0 || i === lastId) {
  31. diffOpened[revision._id] = true;
  32. } else {
  33. diffOpened[revision._id] = false;
  34. }
  35. });
  36. this.setState({
  37. revisions: rev,
  38. diffOpened: diffOpened,
  39. });
  40. // load 0, and last default
  41. if (rev[0]) {
  42. this.fetchPageRevisionBody(rev[0]);
  43. }
  44. if (rev[1]) {
  45. this.fetchPageRevisionBody(rev[1]);
  46. }
  47. if (lastId !== 0 && lastId !== 1 && rev[lastId]) {
  48. this.fetchPageRevisionBody(rev[lastId]);
  49. }
  50. }).catch(err => {
  51. // do nothing
  52. });
  53. }
  54. getPreviousRevision(currentRevision) {
  55. let cursor = null;
  56. for (let revision of this.state.revisions) {
  57. if (cursor && cursor._id == currentRevision._id) {
  58. cursor = revision;
  59. break;
  60. }
  61. cursor = revision;
  62. }
  63. return cursor;
  64. }
  65. onDiffOpenClicked(revision)
  66. {
  67. const diffOpened = this.state.diffOpened,
  68. revisionId = revision._id;
  69. if (diffOpened[revisionId]) {
  70. return ;
  71. }
  72. diffOpened[revisionId] = true;
  73. this.setState({
  74. diffOpened
  75. });
  76. this.fetchPageRevisionBody(revision);
  77. this.fetchPageRevisionBody(this.getPreviousRevision(revision));
  78. }
  79. fetchPageRevisionBody(revision)
  80. {
  81. if (revision.body) {
  82. return ;
  83. }
  84. this.crowi.apiGet('/revisions.get', {revision_id: revision._id})
  85. .then(res => {
  86. if (res.ok) {
  87. this.setState({
  88. revisions: this.state.revisions.map((rev) => {
  89. if (rev._id == res.revision._id) {
  90. return res.revision;
  91. }
  92. return rev;
  93. })
  94. })
  95. }
  96. }).catch(err => {
  97. });
  98. }
  99. render() {
  100. return (
  101. <div>
  102. <h1><Icon name="history" /> History</h1>
  103. <PageRevisionList
  104. revisions={this.state.revisions}
  105. diffOpened={this.state.diffOpened}
  106. getPreviousRevision={this.getPreviousRevision}
  107. onDiffOpenClicked={this.onDiffOpenClicked}
  108. />
  109. </div>
  110. );
  111. }
  112. }
  113. PageHistory.propTypes = {
  114. pageId: React.PropTypes.string,
  115. };
  116. /*
  117. <div class="tab-pane revision-history" id="revision-history">
  118. <div class="revision-history-list">
  119. {% for tt in tree %}
  120. <div class="revision-hisory-outer">
  121. <img src="{{ tt.author|picture }}" class="picture picture-rounded">
  122. <div class="revision-history-main">
  123. <div class="revision-history-author">
  124. <strong>{% if tt.author %}{{ tt.author.username }}{% else %}-{% endif %}</strong>
  125. </div>
  126. <div class="revision-history-comment">
  127. </div>
  128. <div class="revision-history-meta">
  129. {{ tt.createdAt|datetz('Y-m-d H:i:s') }}
  130. <br>
  131. <a href="?revision={{ tt._id.toString() }}"><i class="fa fa-history"></i> {{ t('View this version') }}</a>
  132. <a class="diff-view" data-revision-id="{{ tt._id.toString() }}">
  133. <i id="diff-icon-{{ tt._id.toString() }}" class="fa fa-arrow-circle-right"></i> {{ t('View diff') }}
  134. </a>
  135. <div class="" id="diff-display-{{ tt._id.toString()}}" style="display: none"></div>
  136. </div>
  137. </div>
  138. </div>
  139. {% endfor %}
  140. </div>
  141. */