From 576ef86caf71447fe6be8a8471edb0186dc4e64e Mon Sep 17 00:00:00 2001 From: Deimos Date: Mon, 3 Sep 2018 17:00:58 -0600 Subject: [PATCH] Add count to collapsed groups of multiple comments --- tildes/scss/modules/_comment.scss | 10 ++++++++++ tildes/tildes/models/comment/comment_tree.py | 19 +++++++++++++++---- .../intercooler/comment_contents.jinja2 | 2 +- .../tildes/templates/macros/comments.jinja2 | 4 ++++ 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/tildes/scss/modules/_comment.scss b/tildes/scss/modules/_comment.scss index 9d23e59..b519c5e 100644 --- a/tildes/scss/modules/_comment.scss +++ b/tildes/scss/modules/_comment.scss @@ -49,6 +49,12 @@ text-overflow: ellipsis; } +.comment-branch-counter { + display: none; + + margin-right: 0.4rem; +} + .comment-nav-link { font-size: 0.6rem; margin-left: 0.4rem; @@ -137,6 +143,10 @@ display: none; } + .comment-branch-counter { + display: inline-block; + } + .comment-excerpt { display: inline-block; } diff --git a/tildes/tildes/models/comment/comment_tree.py b/tildes/tildes/models/comment/comment_tree.py index cdf77e8..7d95a3c 100644 --- a/tildes/tildes/models/comment/comment_tree.py +++ b/tildes/tildes/models/comment/comment_tree.py @@ -18,6 +18,7 @@ class CommentTree: - `has_visible_descendant`: whether the comment has any visible descendants (if not, it can be pruned from the tree) - `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: @@ -34,10 +35,6 @@ class CommentTree: 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() # 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._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: """Build the initial tree from the flat list of Comments.""" for comment in self.comments: diff --git a/tildes/tildes/templates/intercooler/comment_contents.jinja2 b/tildes/tildes/templates/intercooler/comment_contents.jinja2 index 65eb1ee..ee7cbad 100644 --- a/tildes/tildes/templates/intercooler/comment_contents.jinja2 +++ b/tildes/tildes/templates/intercooler/comment_contents.jinja2 @@ -1,3 +1,3 @@ {% from 'macros/comments.jinja2' import render_comment_contents with context %} -{{ render_comment_contents(comment) }} +{{ render_comment_contents(comment, is_individual_comment=True) }} diff --git a/tildes/tildes/templates/macros/comments.jinja2 b/tildes/tildes/templates/macros/comments.jinja2 index ae58a80..8913aea 100644 --- a/tildes/tildes/templates/macros/comments.jinja2 +++ b/tildes/tildes/templates/macros/comments.jinja2 @@ -39,6 +39,10 @@ {{ "+" if comment.collapsed_state in ("full", "individual") else "−" }} + {% if not is_individual_comment and comment.num_children > 0 %} +
[{{ comment.num_children + 1 }}]
+ {% endif %} + {% if request.has_permission('view', comment) %} {{ username_linked(comment.user.username) }}