Browse Source

Add count to collapsed groups of multiple comments

merge-requests/37/head
Deimos 6 years ago
parent
commit
576ef86caf
  1. 10
      tildes/scss/modules/_comment.scss
  2. 19
      tildes/tildes/models/comment/comment_tree.py
  3. 2
      tildes/tildes/templates/intercooler/comment_contents.jinja2
  4. 4
      tildes/tildes/templates/macros/comments.jinja2

10
tildes/scss/modules/_comment.scss

@ -49,6 +49,12 @@
text-overflow: ellipsis; text-overflow: ellipsis;
} }
.comment-branch-counter {
display: none;
margin-right: 0.4rem;
}
.comment-nav-link { .comment-nav-link {
font-size: 0.6rem; font-size: 0.6rem;
margin-left: 0.4rem; margin-left: 0.4rem;
@ -137,6 +143,10 @@
display: none; display: none;
} }
.comment-branch-counter {
display: inline-block;
}
.comment-excerpt { .comment-excerpt {
display: inline-block; display: inline-block;
} }

19
tildes/tildes/models/comment/comment_tree.py

@ -18,6 +18,7 @@ class CommentTree:
- `has_visible_descendant`: whether the comment has any visible descendants (if - `has_visible_descendant`: whether the comment has any visible descendants (if
not, it can be pruned from the tree) not, it can be pruned from the tree)
- `collapsed_state`: whether to display this comment in a collapsed state - `collapsed_state`: whether to display this comment in a collapsed state
- `num_children`: how many (visible) children the comment has
""" """
def __init__(self, comments: Sequence[Comment], sort: CommentSortOption) -> None: def __init__(self, comments: Sequence[Comment], sort: CommentSortOption) -> None:
@ -34,10 +35,6 @@ class CommentTree:
self.comments_by_id = {comment.comment_id: comment for comment in comments} self.comments_by_id = {comment.comment_id: comment for comment in comments}
# if there aren't any comments, we can just bail out here
if not self.comments:
return
self._build_tree() self._build_tree()
# The method of building the tree already sorts it by posting time, so there's # The method of building the tree already sorts it by posting time, so there's
@ -50,6 +47,20 @@ class CommentTree:
self.tree = self._prune_empty_branches(self.tree) self.tree = self._prune_empty_branches(self.tree)
self._count_children()
def _count_children(self) -> None:
"""Set the num_children attr for all comments."""
# work backwards through comments so that all children will have their count
# done first, and we can just sum all the counts from the replies
for comment in reversed(self.comments):
comment.num_children = 0
for reply in comment.replies:
comment.num_children += reply.num_children
if not (reply.is_deleted or reply.is_removed):
comment.num_children += 1
def _build_tree(self) -> None: def _build_tree(self) -> None:
"""Build the initial tree from the flat list of Comments.""" """Build the initial tree from the flat list of Comments."""
for comment in self.comments: for comment in self.comments:

2
tildes/tildes/templates/intercooler/comment_contents.jinja2

@ -1,3 +1,3 @@
{% from 'macros/comments.jinja2' import render_comment_contents with context %} {% from 'macros/comments.jinja2' import render_comment_contents with context %}
{{ render_comment_contents(comment) }}
{{ render_comment_contents(comment, is_individual_comment=True) }}

4
tildes/tildes/templates/macros/comments.jinja2

@ -39,6 +39,10 @@
{{ "+" if comment.collapsed_state in ("full", "individual") else "−" }} {{ "+" if comment.collapsed_state in ("full", "individual") else "−" }}
</button> </button>
{% if not is_individual_comment and comment.num_children > 0 %}
<div class="comment-branch-counter">[{{ comment.num_children + 1 }}]</div>
{% endif %}
{% if request.has_permission('view', comment) %} {% if request.has_permission('view', comment) %}
{{ username_linked(comment.user.username) }} {{ username_linked(comment.user.username) }}

Loading…
Cancel
Save