support for plugins for navbar items

This commit is contained in:
Oliver Gorwits
2013-02-09 16:52:01 +00:00
parent 367bd6f858
commit eafa039bbd
7 changed files with 78 additions and 5 deletions

View File

@@ -13,7 +13,26 @@ use App::Netdisco::Web::AuthN;
use App::Netdisco::Web::Search;
use App::Netdisco::Web::Device;
use App::Netdisco::Web::PortControl;
use App::Netdisco::Web::Inventory;
sub _load_web_plugins {
my $plugin_list = shift;
foreach my $plugin (@$plugin_list) {
$plugin = 'App::Netdisco::Web::Plugin::'. $plugin
unless $plugin =~ m/^\+/;
$plugin =~ s/^\+//;
eval "require $plugin";
}
}
if (setting('web_plugins') and ref [] eq ref setting('web_plugins')) {
_load_web_plugins( setting('web_plugins') );
}
if (setting('extra_web_plugins') and ref [] eq ref setting('extra_web_plugins')) {
_load_web_plugins( setting('extra_web_plugins') );
}
hook 'before_template' => sub {
my $tokens = shift;

View File

@@ -0,0 +1,27 @@
package App::Netdisco::Web::Plugin;
use Dancer ':syntax';
use Dancer::Plugin;
set('navbar_items' => []);
register 'register_navbar_item' => sub {
my ($self, $config) = plugin_args(@_);
die "bad config to register_navbar_item\n"
unless length $config->{id}
and length $config->{path}
and length $config->{label};
foreach my $item (@{ setting('navbar_items') }) {
if ($item->{id} eq $config->{id}) {
$item = $config;
return;
}
}
push @{ setting('navbar_items') }, $config;
};
register_plugin;
true;

View File

@@ -1,8 +1,16 @@
package App::Netdisco::Web::Inventory;
package App::Netdisco::Web::Plugin::Inventory;
use Dancer ':syntax';
use Dancer::Plugin::DBIC;
use App::Netdisco::Web::Plugin;
register_navbar_item({
id => 'inventory',
path => '/inventory',
label => 'Inventory',
});
get '/inventory' => sub {
my $models = schema('netdisco')->resultset('Device')->get_models();
my $releases = schema('netdisco')->resultset('Device')->get_releases();