Skip to content

Commit

Permalink
Use the same routine for node details and serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
melmothx committed Feb 5, 2024
1 parent fef9b6d commit b508d62
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 52 deletions.
35 changes: 2 additions & 33 deletions lib/AmuseWikiFarm/Schema/Result/Node.pm
Original file line number Diff line number Diff line change
Expand Up @@ -473,45 +473,14 @@ sub serialize {
$out{'title_' . $lang} = $desc->title_muse;
$out{'body_' . $lang} = $desc->body_muse;
}
my @attached = map { $_->full_uri } (
$self->aggregation_series->sorted->all,
$self->aggregations->sorted->all,
$self->categories->sorted->all,
$self->titles->sorted_by_title->all,
);
my @attached = map { $_->{uri} } $self->linked_pages;
$out{attached_uris} = join("\n", @attached);
return \%out;
}

sub linked_pages {
my $self = shift;
my @out;
my %icons = (
author => 'address-book-o',
topic => 'tag',
series => 'archive',
aggregations => 'book',
special => 'file-text-o',
text => 'file-text-o',
);
push @out, map { +{ label => encode_entities($_->aggregation_series_name),
type => "series",
uri => $_->full_uri } } $self->aggregation_series->sorted;
push @out, map { +{ label => encode_entities($_->final_name),
type => "aggregation",
uri => $_->full_uri } } $self->aggregations->sorted;
# these are already escaped
push @out, map { +{ label => $_->name,
type => $_->type,
uri => $_->full_uri } } $self->categories->active_only->sorted;
push @out, map { +{ label => $_->author_title,
type => $_->f_class,
uri => $_->full_uri } } $self->titles->sorted->published_all;
Dlog_debug { "linked pages: $_" } \@out;
foreach my $i (@out) {
$i->{icon} = $icons{$i->{type}} || 'tags';
}
return @out;
return $self->site->nodes->linked_pages_for_node($self->node_id);
}

sub children_pages {
Expand Down
61 changes: 42 additions & 19 deletions lib/AmuseWikiFarm/Schema/ResultSet/Node.pm
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,27 @@ sub as_tree {
return \@out;
}

sub _render_node {
my ($source, $id, $depth, %opts) = @_;
$depth ||= 0;
log_debug { "Rendering $id $depth" };
my $node = $source->{$id};
my $root_indent = ' ' x $depth;
my $indent = $root_indent . ' ';
my $html = "\n" . $root_indent . "<div>\n";
$html .= $indent . '<div>';
my $class= $opts{class} || 'amw-label-node';
$html .= sprintf('<div class="%s"><a href="%s">%s</a></div>',
$class,
'/node/' . $node->{full_path},
$node->{title_html});
$html .= "</div>\n";
sub linked_pages_for_node {
my ($self, $nid) = @_;
my $me = $self->current_source_alias;
my $src = $self->search({ "$me.node_id" => $nid })->as_tree_source;
my @list;
if (my $node = $src->{$nid}) {
foreach my $obj (@{_unroll_linked_object($node)}) {
push @list, {
label => $obj->[0],
uri => $obj->[1],
icon => $obj->[2],
};
}
}
Dlog_debug { "Linked pages: $_" } \@list;
return @list;
}

sub _unroll_linked_object {
my ($node) = @_;
my @list;
my %icons = (
author => 'address-book-o',
topic => 'tag',
Expand All @@ -175,7 +181,6 @@ sub _render_node {
special => 'file-text-o',
text => 'file-text-o',
);
my @list;
# here we need to sort.
foreach my $series (@{$node->{node_aggregation_series} || []}) {
if (my $s = $series->{aggregation_series}) {
Expand Down Expand Up @@ -214,8 +219,8 @@ sub _render_node {
if (my $t = $title->{title}) {
if ($t->{status} eq 'published') {
my $full_uri = $t->{f_class} eq 'text'
? "/library/$title->{uri}"
: "/special/$title->{uri}";
? "/library/$t->{uri}"
: "/special/$t->{uri}";
push @list, [ $t->{title},
$full_uri,
$icons{$t->{f_class}},
Expand All @@ -224,6 +229,25 @@ sub _render_node {
}
}
}
return [ sort { $a->[3] <=> $b->[3] } @list ];
}

sub _render_node {
my ($source, $id, $depth, %opts) = @_;
$depth ||= 0;
log_debug { "Rendering $id $depth" };
my $node = $source->{$id};
my $root_indent = ' ' x $depth;
my $indent = $root_indent . ' ';
my $html = "\n" . $root_indent . "<div>\n";
$html .= $indent . '<div>';
my $class= $opts{class} || 'amw-label-node';
$html .= sprintf('<div class="%s"><a href="%s">%s</a></div>',
$class,
'/node/' . $node->{full_path},
$node->{title_html});
$html .= "</div>\n";
my @list = @{_unroll_linked_object($node)};
Dlog_debug { "My linked pages: $_" } \@list;
if (@list) {
$html .= join("",
Expand All @@ -232,7 +256,6 @@ sub _render_node {
$_->[2],
$_->[1],
$_->[0]) . "\n" }
sort { $a->[3] <=> $b->[3] }
@list),
$indent . "</ul>\n");
}
Expand Down

0 comments on commit b508d62

Please sign in to comment.