Catalyst-Plugin-Navigation

 view release on metacpan or  search on metacpan

lib/CatalystX/NavigationMenu.pm  view on Meta::CPAN

package CatalystX::NavigationMenu;

use strict;
use warnings;

use Moose;
use CatalystX::NavigationMenuItem;
use namespace::autoclean;

=head1 NAME

CatalystX::NavigationMenu

=head1 SYNOPSIS

	my $nm = CatalystX::NavigationMenu->new();
	$nm->popupate($c);

	my $menu = $nm->get_navigation($c, {level => 0});

=head1 DESCRIPTION

CatalystX::NavigationMenu provides a menu object to be used when creating and
managing menus in Catalyst based on attribute data. For details of the Catalyst
attributes see the L(Catalyst::Plugin::Navigation) documentation.

=cut 

has items => (
	traits => ['Array'],
	is => 'rw',
	isa => 'ArrayRef[CatalystX::NavigationMenuItem]',
	default => sub{[]},
	handles    => {
		all_items    => 'elements',
		insert_item  => 'push',
		shift_item   => 'shift',
		find_item    => 'first',
		count_items  => 'count',
		has_items    => 'is_empty',
		sort_items   => 'sort',
	},
);

=head1 METHODS

=head2 populate($c)

Populates the menu based on the controllers found in the Catalyst object.

=cut

sub populate {
	my ($self, $c) = @_;

	my $dispatcher = $c->dispatcher;
	foreach my $c_name ($c->controllers(qr//)) {
		my $controller = $c->controller($c_name);
		my @actions = $dispatcher->get_containers($controller->action_namespace($c));
		$c->log->debug("Looking at Controller $c_name for navigation entries") if $c->debug;

		foreach my $ac (@actions) {
			my $acts = $ac->actions;
			foreach my $key (keys(%$acts)) {
				my $action = $acts->{$key};
				if ($action->attributes->{Menu}) {
					# And get the menu to insert it into the right location.
					$c->log->debug("Adding action item for path: " . ($action->namespace || '') . '/' . ($action->name || '') . " with parent: " . 
						($action->attributes->{MenuParent}->[0] || '') ) if $c->debug;
					$self->add_action($action);
				}
			}
		}
	}
}

=head2 add_action($action)



( run in 3.113 seconds using v1.01-cache-2.11-cpan-364913b4093 )