clean up the sidebar defaults for search and report

This commit is contained in:
Oliver Gorwits
2017-12-09 23:48:50 +00:00
parent 5768b26e5c
commit 9e91431411
8 changed files with 77 additions and 83 deletions

View File

@@ -71,6 +71,24 @@ Dancer::Session::Cookie::init(session);
# workaround for https://github.com/PerlDancer/Dancer/issues/935
hook after_error_render => sub { setting('layout' => 'main') };
hook 'before' => sub {
my $key = request->path;
if (param('tab') and ($key !~ m/ajax/)) {
$key .= ('/' . param('tab'));
}
$key =~ s|.*/(\w+)/(\w+)$|${1}_${2}|;
vars->{'sidebar_key'} = $key;
return unless param('firstsearch')
and exists setting('sidebar_defaults')->{$key};
# new searches will use these defaults in their sidebars
my %params = %{ setting('sidebar_defaults')->{$key} };
foreach my $p (keys %params) {
params->{$p} = $params{$p} if $params{$p};
}
};
# this hook should be loaded _after_ all plugins
hook 'before_template' => sub {
my $tokens = shift;
@@ -92,9 +110,34 @@ hook 'before_template' => sub {
$tokens->{table_showrecordsmenu} =
to_json( setting('table_showrecordsmenu') );
# linked searches will use these defaults in their sidebars
foreach my $sidebar_key (keys %{ setting('sidebar_defaults') }) {
my ($mode, $report) = ($sidebar_key =~ m/(\w+)_(\w+)/);
if ($mode =~ m/^search$/) {
$tokens->{$sidebar_key} = uri_for("/$mode", {tab => $report});
}
elsif ($mode =~ m/^report$/) {
$tokens->{$sidebar_key} = uri_for("/$mode/$report");
}
if (exists setting('sidebar_defaults')->{$sidebar_key}) {
foreach my $col (keys %{ setting('sidebar_defaults')->{$sidebar_key} }) {
if (var('sidebar_key') eq $sidebar_key) {
$tokens->{$sidebar_key}->query_param($col, params->{$col})
if params->{$col};
}
else {
$tokens->{$sidebar_key}->query_param($col,
setting('sidebar_defaults')->{$sidebar_key}->{$col});
}
}
}
$tokens->{$sidebar_key} = $tokens->{$sidebar_key}->path_query;
}
# fix Plugin Template Variables to be only path+query
$tokens->{$_} = $tokens->{$_}->path_query
for qw/search_node search_device device_ports/;
$tokens->{device_ports} = $tokens->{device_ports}->path_query;
# allow very long lists of ports
$Template::Directive::WHILE_MAX = 10_000;

View File

@@ -16,28 +16,6 @@ register_report(
}
);
hook 'before' => sub {
return
unless (
request->path eq uri_for('/report/moduleinventory')->path
or index( request->path,
uri_for('/ajax/content/report/moduleinventory')->path ) == 0
);
# view settings
var('module_options' => [
{ name => 'fruonly',
label => 'FRU Only',
default => 'on'
},
{ name => 'matchall',
label => 'Match All Options',
default => 'on'
},
]
);
};
hook 'before_template' => sub {
my $tokens = shift;

View File

@@ -17,10 +17,6 @@ get '/report/*' => require_login sub {
elsif ( $tag eq 'moduleinventory' ) {
$class_list = [ schema('netdisco')->resultset('DeviceModule')
->get_distinct_col('class') ];
foreach my $col ( @{ var('module_options') } ) {
params->{$col->{name}} = 'checked' if $col->{default} eq 'on';
}
}
elsif ( $tag eq 'portssid' ) {
$ssid_list = [ schema('netdisco')->resultset('DevicePortSsid')

View File

@@ -8,51 +8,9 @@ use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Util::Web 'sql_match';
use NetAddr::MAC ();
hook 'before' => sub {
# view settings for node options
var('node_options' => [
{ name => 'stamps', label => 'Time Stamps', default => 'on' },
{ name => 'deviceports', label => 'Device Ports', default => 'on' },
]);
# view settings for port options
var('port_options' => [
{ name => 'ethernet', label => 'Ethernet Only', default => 'on' },
]);
# view settings for device options
var('device_options' => [
{ name => 'matchall', label => 'Match All Options', default => 'on' },
]);
return unless param('firstsearch') and
(request->path eq uri_for('/search')->path
or index(request->path, uri_for('/ajax/content/search')->path) == 0);
foreach my $col (@{ var('node_options') },
@{ var('port_options') },
@{ var('device_options') }) {
params->{$col->{name}} = 'checked' if $col->{default} eq 'on';
}
};
hook 'before_template' => sub {
my $tokens = shift;
# new searches will use these defaults in their sidebars
$tokens->{search_node} = uri_for('/search', {tab => 'node'});
$tokens->{search_device} = uri_for('/search', {tab => 'device'});
foreach my $col (@{ var('node_options') }) {
next unless $col->{default} eq 'on';
$tokens->{search_node}->query_param($col->{name}, 'checked');
}
foreach my $col (@{ var('device_options') }) {
next unless $col->{default} eq 'on';
$tokens->{search_device}->query_param($col->{name}, 'checked');
}
return unless (request->path eq uri_for('/search')->path
or index(request->path, uri_for('/ajax/content/search')->path) == 0);