From: hylom Date: Wed, 21 Sep 2016 20:00:13 +0000 (+0900) Subject: Controller::Story: show comments with nested style X-Git-Tag: v0.1.0~298 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=6c9fe16f9021df45f11685b186a5d6ca6300bbff;p=newslash%2Fnewslash.git Controller::Story: show comments with nested style --- diff --git a/dev/newslash_web/css/main/comment.less b/dev/newslash_web/css/main/comment.less index aa3a46b1..1064b085 100644 --- a/dev/newslash_web/css/main/comment.less +++ b/dev/newslash_web/css/main/comment.less @@ -6,12 +6,14 @@ ol { margin: 0; padding: 0; + margin-left: 16px; list-style-type: none; } } .comment-item { padding: 6px 12px; + margin-bottom: 6px; border: 1px solid @border-color-inactive; font-size: 94%; diff --git a/dev/newslash_web/lib/Newslash/Web/Controller/Story.pm b/dev/newslash_web/lib/Newslash/Web/Controller/Story.pm index 7f680fb6..71b70a0e 100644 --- a/dev/newslash_web/lib/Newslash/Web/Controller/Story.pm +++ b/dev/newslash_web/lib/Newslash/Web/Controller/Story.pm @@ -1,5 +1,6 @@ package Newslash::Web::Controller::Story; use Mojo::Base 'Mojolicious::Controller'; +use Data::Dumper; sub story { my $c = shift; @@ -8,18 +9,47 @@ sub story { my $user = {}; if ($session->{token}) { - $user = $c->users->get(token => $session->{token}); + $user = $c->users->get(token => $session->{token}); } my $story = $c->stories->select(sid => $sid); my $discuss = $c->discussions->select(sid => $sid); my $d_id = $discuss->{id}; my $comments = []; if ($d_id) { - $comments = $c->comments->select(discussion_id => $d_id); + $comments = $c->comments->select(discussion_id => $d_id); } + my $roots = $c->_build_comment_tree($comments); + #$c->app->log->debug(Dumper($roots)); + $c->render(user => $user, sid => $sid, story => $story, comments => $comments, root_comments => $roots); +} + +sub _build_comment_tree { + my ($c, $comments) = @_; + my $root = []; + my $comment_of = {}; + + # prerequisite: $comments are sorted with ascendent order by cid + for my $comment (@$comments) { + my $pid = $comment->{pid}; + my $cid = $comment->{cid}; + $comment_of->{$cid} = $comment; + $comment->{children} = []; - $c->render(user => $user, sid => $sid, story => $story, comments => $comments); + if ($pid == 0) { + push @$root, $comment; + next; + } + + my $parent = $comment_of->{$pid}; + if ($parent) { + push @{$parent->{children}}, $comment; + } else { + $c->app->log->warn("(Controller::Story) invalid comment order. cid: $cid, pid: $pid"); + } + } + return $root; } + 1; diff --git a/dev/newslash_web/templates/story/story.html.tt2 b/dev/newslash_web/templates/story/story.html.tt2 index 076baff4..8726dcd1 100644 --- a/dev/newslash_web/templates/story/story.html.tt2 +++ b/dev/newslash_web/templates/story/story.html.tt2 @@ -1,5 +1,17 @@ [% WRAPPER common/layout sidebar=1 %] +[%- BLOCK subcomments -%] +
  • + [%- INCLUDE common/comment comment=item -%] +
      + [%- FOREACH sub_item IN item.children -%] + [%- INCLUDE subcomments item=sub_item -%] + [%- END -%] +
    +
  • +[%- END -%] + +
    [%- INCLUDE common/article hide_more_link = 1 %] @@ -7,9 +19,14 @@
      - [%- FOREACH comment IN comments -%] + [%- FOREACH item IN root_comments -%]
    1. - [% INCLUDE common/comment %] + [%- INCLUDE common/comment comment=item -%] +
        + [%- FOREACH sub_item IN item.children -%] + [%- INCLUDE subcomments item=sub_item -%] + [%- END -%] +
    2. [%- END -%]