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);

View File

@@ -85,6 +85,25 @@ web_plugins:
- Device::Neighbors
- Device::Addresses
extra_web_plugins: []
sidebar_defaults:
search_node:
stamps: checked
deviceports: checked
show_vendor: null
archived: null
partial: null
age_invert: null
daterange: null
mac_format: IEEE
search_port:
partial: null
uplink: null
ethernet: checked
search_device:
matchall: checked
report_moduleinventory:
fruonly: checked
matchall: checked
jobqueue_refresh: 10
safe_password_store: true
reports: []

View File

@@ -20,9 +20,9 @@
[%- ELSE -%]
<span><i class="icon-leaf"></i>&nbsp;
[%- END -%]
<a href="[% uri_for('/report/moduleinventory') %]?description=[% nodes.$item.module.description | uri %]">[% nodes.$item.module.description -%]</a>
<a href="[% report_moduleinventory %]&description=[% nodes.$item.module.description | uri %]">[% nodes.$item.module.description -%]</a>
[%- IF nodes.$item.module.name -%]
<a href="[% uri_for('/report/moduleinventory') %]?name=[% nodes.$item.module.name | uri %]">([% nodes.$item.module.name %])</a>
<a href="[% report_moduleinventory %]&name=[% nodes.$item.module.name | uri %]">([% nodes.$item.module.name %])</a>
[%- END -%]
[%- IF nodes.$item.module.fw_ver -%]
fw: [% nodes.$item.module.fw_ver %]
@@ -34,13 +34,13 @@
sw: [% nodes.$item.module.sw_ver %]
[%- END -%]
[%- IF nodes.$item.module.serial -%]
<a href="[% uri_for('/report/moduleinventory') %]?serial=[% nodes.$item.module.serial | uri %]">[serial: [% nodes.$item.module.serial %]]</a>
<a href="[% report_moduleinventory %]&serial=[% nodes.$item.module.serial | uri %]">[serial: [% nodes.$item.module.serial %]]</a>
[%- END -%]
[%- IF nodes.$item.module.type -%]
/ <a href="[% uri_for('/report/moduleinventory') %]?type=[% nodes.$item.module.type | uri %]">[% nodes.$item.module.type %]</a>
/ <a href="[% report_moduleinventory %]&type=[% nodes.$item.module.type | uri %]">[% nodes.$item.module.type %]</a>
[%- END -%]
[%- IF nodes.$item.module.model -%]
/ <a href="[% uri_for('/report/moduleinventory') %]?model=[% nodes.$item.module.model | uri %]">[% nodes.$item.module.model %]</a>
/ <a href="[% report_moduleinventory %]&model=[% nodes.$item.module.model | uri %]">[% nodes.$item.module.model %]</a>
[%- END -%]
[%- IF nodes.$item.module.fru -%]
<b>[FRU]</b>

View File

@@ -44,32 +44,32 @@ $(document).ready(function() {
}, {
"data": 'description',
"render": function(data, type, row, meta) {
return '<a href="[% uri_for('/report/moduleinventory') %]?description=' + encodeURIComponent(data) + '">' + he.encode(data || '') + '</a>';
return '<a href="[% report_moduleinventory %]&description=' + encodeURIComponent(data) + '">' + he.encode(data || '') + '</a>';
}
}, {
"data": 'name',
"render": function(data, type, row, meta) {
return '<a href="[% uri_for('/report/moduleinventory') %]?name=' + encodeURIComponent(data) + '">' + he.encode(data || '') + '</a>';
return '<a href="[% report_moduleinventory %]&name=' + encodeURIComponent(data) + '">' + he.encode(data || '') + '</a>';
}
}, {
"data": 'class',
"render": function(data, type, row, meta) {
return '<a href="[% uri_for('/report/moduleinventory') %]?class=' + encodeURIComponent(data) + '">' + he.encode(capitalizeFirstLetter(data + '')) + '</a>';
return '<a href="[% report_moduleinventory %]&class=' + encodeURIComponent(data) + '">' + he.encode(capitalizeFirstLetter(data + '')) + '</a>';
}
}, {
"data": 'type',
"render": function(data, type, row, meta) {
return '<a href="[% uri_for('/report/moduleinventory') %]?type=' + encodeURIComponent(data) + '">' + he.encode(data || '') + '</a>';
return '<a href="[% report_moduleinventory %]&type=' + encodeURIComponent(data) + '">' + he.encode(data || '') + '</a>';
}
}, {
"data": 'model',
"render": function(data, type, row, meta) {
return '<a href="[% uri_for('/report/moduleinventory') %]?model=' + encodeURIComponent(data) + '">' + he.encode(data || '') + '</a>';
return '<a href="[% report_moduleinventory %]&model=' + encodeURIComponent(data) + '">' + he.encode(data || '') + '</a>';
}
}, {
"data": 'serial',
"render": function(data, type, row, meta) {
return '<a href="[% uri_for('/report/moduleinventory') %]?serial=' + encodeURIComponent(data) + '">' + he.encode(data || '') + '</a>';
return '<a href="[% report_moduleinventory %]&serial=' + encodeURIComponent(data) + '">' + he.encode(data || '') + '</a>';
}
}, {
"data": 'hw_ver',
@@ -95,7 +95,7 @@ $(document).ready(function() {
{
"data": 'class',
"render": function(data, type, row, meta) {
return '<a href="[% uri_for('/report/moduleinventory') %]?class=' + encodeURIComponent(data) + '">' + he.encode(capitalizeFirstLetter(data + '')) + '</a>';
return '<a href="[% report_moduleinventory %]&class=' + encodeURIComponent(data) + '">' + he.encode(capitalizeFirstLetter(data + '')) + '</a>';
}
}, {
"data": 'count',

View File

@@ -86,7 +86,7 @@
<ul class="dropdown-menu">
[% FOREACH item IN settings._reports_menu.$category %]
[% NEXT IF settings._reports.$item.hidden %]
<li><a href="[% uri_for('/report/' _ item) %]">[% settings._reports.$item.label | html_entity %]</a></li>
<li><a href="[% uri_for('/report/' _ item) %]?firstsearch=on">[% settings._reports.$item.label | html_entity %]</a></li>
[% END %]
</ul>
</li>