thread.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import React from 'react';
  2. import { Link } from 'react-router'; // jshint ignore:line
  3. import ReadIcon from 'misago/components/threads-list/read-icon'; // jshint ignore:line
  4. import ThreadOptions from 'misago/components/threads-list/thread-options'; // jshint ignore:line
  5. import escapeHtml from 'misago/utils/escape-html';
  6. const LAST_POSTER_URL = '<a href="%(url)s" class="poster-title">%(user)s</a>';
  7. const LAST_POSTER_SPAN = '<span class="poster-title">%(user)s</span>';
  8. const LAST_REPLY_URL = '<a href="%(url)s" class="last-title" title="%(absolute)s">%(relative)s</a>';
  9. export class Category extends React.Component {
  10. getClassName() {
  11. if (this.props.category.css_class) {
  12. return 'thread-category thread-category-' + this.props.category.css_class;
  13. } else {
  14. return 'thread-category';
  15. }
  16. }
  17. getUrl() {
  18. return this.props.category.absolute_url + this.props.list.path;
  19. }
  20. render() {
  21. /* jshint ignore:start */
  22. return <Link to={this.getUrl()} className={this.getClassName()}>
  23. {this.props.category.name}
  24. </Link>;
  25. /* jshint ignore:end */
  26. }
  27. }
  28. export default class extends React.Component {
  29. constructor(props) {
  30. super(props);
  31. this.state = {
  32. isSelected: false
  33. };
  34. }
  35. getNewLabel() {
  36. if (!this.props.thread.is_read) {
  37. /* jshint ignore:start */
  38. return <li className="thread-new-posts"
  39. title={gettext("Go to first unread post")}>
  40. <a href={this.props.thread.new_post_url}>
  41. <span className="material-icon">
  42. comment
  43. </span>
  44. <span>
  45. {gettext("New posts")}
  46. </span>
  47. </a>
  48. </li>;
  49. /* jshint ignore:end */
  50. } else {
  51. return null;
  52. }
  53. }
  54. getPinnedLabel() {
  55. if (this.props.thread.weight === 2) {
  56. /* jshint ignore:start */
  57. return <li className="thread-pinned-globally">
  58. <span className="material-icon">
  59. bookmark_border
  60. </span>
  61. <span>
  62. {gettext("Pinned globally")}
  63. </span>
  64. </li>;
  65. /* jshint ignore:end */
  66. } else if (this.props.thread.weight === 1) {
  67. /* jshint ignore:start */
  68. return <li className="thread-pinned-locally">
  69. <span className="material-icon">
  70. bookmark_border
  71. </span>
  72. <span>
  73. {gettext("Pinned locally")}
  74. </span>
  75. </li>;
  76. /* jshint ignore:end */
  77. } else {
  78. return null;
  79. }
  80. }
  81. getClosedLabel() {
  82. if (this.props.thread.is_closed) {
  83. /* jshint ignore:start */
  84. return <li className="thread-closed">
  85. <span className="material-icon">
  86. lock_outline
  87. </span>
  88. <span>
  89. {gettext("Closed")}
  90. </span>
  91. </li>;
  92. /* jshint ignore:end */
  93. } else {
  94. return null;
  95. }
  96. }
  97. getPath() {
  98. let top = this.props.categories[this.props.thread.top_category];
  99. let bottom = this.props.categories[this.props.thread.category];
  100. if (top && bottom && top.id !== bottom.id) {
  101. /* jshint ignore:start */
  102. return <li className="thread-path">
  103. <Category category={top} list={this.props.list} />
  104. <span className="path-separator material-icon">
  105. arrow_forward
  106. </span>
  107. <Category category={bottom} list={this.props.list} />
  108. </li>;
  109. /* jshint ignore:end */
  110. } else if (top || bottom) {
  111. /* jshint ignore:start */
  112. return <li className="thread-path">
  113. <Category category={top || bottom} list={this.props.list} />
  114. </li>;
  115. /* jshint ignore:end */
  116. } else {
  117. return null;
  118. }
  119. }
  120. getRepliesCount() {
  121. /* jshint ignore:start */
  122. let message = ngettext(
  123. "%(replies)s reply",
  124. "%(replies)s replies",
  125. this.props.thread.replies);
  126. return <li className="thread-replies">
  127. <a href={this.props.thread.absolute_url}>
  128. <span className="material-icon">
  129. forum
  130. </span>
  131. <span>
  132. {interpolate(message, {
  133. replies: this.props.thread.replies,
  134. }, true)}
  135. </span>
  136. </a>
  137. </li>;
  138. /* jshint ignore:end */
  139. }
  140. getLastReplyDate() {
  141. return interpolate(LAST_REPLY_URL, {
  142. url: escapeHtml(this.props.thread.last_post_url),
  143. absolute: escapeHtml(this.props.thread.last_post_on.format('LLL')),
  144. relative: escapeHtml(this.props.thread.last_post_on.fromNow())
  145. }, true);
  146. }
  147. getLastPoster() {
  148. if (this.props.thread.last_poster_url) {
  149. return interpolate(LAST_POSTER_URL, {
  150. url: escapeHtml(this.props.thread.last_poster_url),
  151. user: escapeHtml(this.props.thread.last_poster_name)
  152. }, true);
  153. } else {
  154. return interpolate(LAST_POSTER_SPAN, {
  155. user: escapeHtml(this.props.thread.last_poster_name)
  156. }, true);
  157. }
  158. }
  159. getLastReply() {
  160. /* jshint ignore:start */
  161. return <li className="thread-last-reply"
  162. dangerouslySetInnerHTML={{__html: interpolate(
  163. escapeHtml(gettext("last reply by %(user)s %(date)s")), {
  164. date: this.getLastReplyDate(),
  165. user: this.getLastPoster()
  166. }, true)}} />;
  167. /* jshint ignore:end */
  168. }
  169. getOptions() {
  170. if (this.props.user.id) {
  171. /* jshint ignore:start */
  172. return <ThreadOptions thread={this.props.thread}
  173. selectThread={this.props.selectThread}
  174. isSelected={this.props.isSelected} />;
  175. /* jshint ignore:end */
  176. } else {
  177. return null;
  178. }
  179. }
  180. getClassName() {
  181. if (this.props.thread.is_read) {
  182. if (this.props.isBusy) {
  183. return 'list-group-item thread-read thread-busy';
  184. } else if (this.props.isSelected) {
  185. return 'list-group-item thread-read thread-selected';
  186. } else {
  187. return 'list-group-item thread-read';
  188. }
  189. } else {
  190. if (this.props.isBusy) {
  191. return 'list-group-item thread-new thread-busy';
  192. } else if (this.props.isSelected) {
  193. return 'list-group-item thread-new thread-selected';
  194. } else {
  195. return 'list-group-item thread-new';
  196. }
  197. }
  198. }
  199. render () {
  200. /* jshint ignore:start */
  201. return <li className={this.getClassName()}>
  202. <ReadIcon thread={this.props.thread} />
  203. <div className="thread-main">
  204. <a href={this.props.thread.absolute_url} className="item-title thread-title">
  205. {this.props.thread.title}
  206. </a>
  207. <ul className="list-inline">
  208. {this.getNewLabel()}
  209. {this.getPinnedLabel()}
  210. {this.getClosedLabel()}
  211. {this.getPath()}
  212. {this.getRepliesCount()}
  213. {this.getLastReply()}
  214. </ul>
  215. </div>
  216. {this.getOptions()}
  217. </li>;
  218. /* jshint ignore:end */
  219. }
  220. }