CatalystX-Menu-Tree
view release on metacpan or search on metacpan
lib/CatalystX/Menu/Tree.pm view on Meta::CPAN
$self->_build_tree;
return $self;
}
=head1 INTERNAL METHODS
=head2 C<_build_tree()>
Create and store a reference to a hash containing a tree of menu data.
=cut
sub _build_tree {
my ($self) = @_;
my $c = $self->{context};
$self->_get_actions;
my $actions = $self->{action_hash};
my @data;
my $menpattr = $self->{menupath_attr};
my $mentattr = $self->{menutitle_attr};
for my $namespace (keys %$actions) {
for my $name (keys %{$actions->{$namespace}}) {
my $action = $actions->{$namespace}{$name};
my %data = (
menupath => $action->attributes->{$menpattr}[0],
uri => $c->uri_for($action),
);
if ($mentattr) {
$data{menutitle} = $action->attributes->{$mentattr}[0];
}
push @data, { %data };
}
}
# mix in any nodes the user wants to add
if ($self->{add_nodes}) {
for my $node (@{$self->{add_nodes}}) {
push @data, $node;
}
}
my @sorted =
sort { $b->[0] cmp $a->[0] }
map { $_->[0] =~ s!^/!!; $_ }
map { [ $_->{menupath}, $_ ] }
@data;
my %tree;
for my $obj (@sorted) {
my $mpath = $obj->[1]{menupath};
my $mtitle = $obj->[1]{menutitle} || '';
$mpath =~ s!^/!!;
my $uri = $obj->[1]{uri};
my @path = split m!/!, $mpath;
my $str = join ', ' => @path;
my $node = pop @path;
my $ref = \%tree;
while (@path) {
my $key = shift @path;
if (exists $ref->{$key}) {
unless (exists $ref->{$key}{children}) {
$ref->{$key}{children} = {};
}
$ref = $ref->{$key}{children};
}
else {
$ref->{$key} = {
children => {},
};
$ref = $ref->{$key}{children};
}
}
# this addresses the case where a top level node is added
# with add_node to attach a URI or title to a label
if (exists $ref->{$node}) {
$ref->{$node}{uri} = $uri;
if ($mtitle) {
$ref->{$node}{menutitle} = $mtitle;
}
}
else {
my %data = (
children => {},
uri => $uri,
);
if ($mtitle) {
$data{menutitle} = $mtitle;
}
$ref->{$node} = { %data };
}
}
$self->{tree} = {%tree};
}
=head2 C<_get_actions()>
Build a hash of Catalyst::Action objects.
=cut
sub _get_actions {
my ($self) = @_;
my $c = $self->{context};
my $d = $c->dispatcher;
my @namespace;
my @container;
my %actionhash;
my @controller =
map {$c->controller($_)}
( run in 0.493 second using v1.01-cache-2.11-cpan-71847e10f99 )