complete defaults into templates, attempt to set cookie in handler

This commit is contained in:
Oliver Gorwits
2017-12-10 18:09:10 +00:00
parent 88cb2b0b06
commit 53358c5311
10 changed files with 38 additions and 39 deletions

View File

@@ -74,7 +74,8 @@ Module::Build->new(
'Term::UI' => '0', 'Term::UI' => '0',
'Try::Tiny' => '0', 'Try::Tiny' => '0',
'URI' => '0', 'URI' => '0',
'URL::Encode' => '0.01', 'URL::Encode' => '0.03',
'URL::Encode::XS' => 0,
'YAML' => '0.84', 'YAML' => '0.84',
'YAML::XS' => '0.41', 'YAML::XS' => '0.41',
}, },

View File

@@ -81,11 +81,14 @@ hook 'before' => sub {
$key =~ s|.*/(\w+)/(\w+)$|${1}_${2}|; $key =~ s|.*/(\w+)/(\w+)$|${1}_${2}|;
vars->{'sidebar_key'} = $key; vars->{'sidebar_key'} = $key;
# search or report from navbar can ignore params
return if param('firstsearch');
my $defaults = dclone (setting('sidebar_defaults')->{$key} or return); my $defaults = dclone (setting('sidebar_defaults')->{$key} or return);
push @{ vars->{'guards'} }, push @{ vars->{'guards'} },
guard { setting('sidebar_defaults')->{$key} = $defaults }; guard { setting('sidebar_defaults')->{$key} = $defaults };
# new searches will use these defaults in their sidebars # otherwise update defaults to contain the passed url params
setting('sidebar_defaults')->{$key}->{$_}->{'default'} = params->{$_} setting('sidebar_defaults')->{$key}->{$_}->{'default'} = params->{$_}
for keys %{ $defaults }; for keys %{ $defaults };
}; };
@@ -126,6 +129,7 @@ hook 'before_template' => sub {
$tokens->{$sidebar_key}->query_param($col, $tokens->{$sidebar_key}->query_param($col,
setting('sidebar_defaults')->{$sidebar_key}->{$col}->{'default'}); setting('sidebar_defaults')->{$sidebar_key}->{$col}->{'default'});
# used by the sidebar templates when first rendering
$tokens->{"${sidebar_key}_defaults"}->{$col} $tokens->{"${sidebar_key}_defaults"}->{$col}
= setting('sidebar_defaults')->{$sidebar_key}->{$col}->{'default'}; = setting('sidebar_defaults')->{$sidebar_key}->{$col}->{'default'};
} }

View File

@@ -5,6 +5,7 @@ use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC; use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible; use Dancer::Plugin::Auth::Extensible;
use URI ();
use Storable 'dclone'; use Storable 'dclone';
use Scope::Guard 'guard'; use Scope::Guard 'guard';
use URL::Encode 'url_params_mixed'; use URL::Encode 'url_params_mixed';
@@ -50,20 +51,22 @@ hook 'before' => sub {
push @{ vars->{'guards'} }, push @{ vars->{'guards'} },
guard { setting('sidebar_defaults')->{'device_ports'} = $defaults }; guard { setting('sidebar_defaults')->{'device_ports'} = $defaults };
foreach my $key (keys %{ setting('sidebar_defaults')->{'device_ports'} }) { foreach my $key (keys %{ $defaults }) {
next unless defined $cdata->{$key}
and $cdata->{$key} =~ m/^[[:alnum:]_]+$/;
setting('sidebar_defaults')->{'device_ports'}->{$key}->{'default'} setting('sidebar_defaults')->{'device_ports'}->{$key}->{'default'}
= $cdata->{$key}; = $cdata->{$key};
} }
} }
} }
};
return unless (request->path eq uri_for('/device')->path hook 'before_template' => sub {
or index(request->path, uri_for('/ajax/content/device')->path) == 0); return unless var('sidebar_key') and (var('sidebar_key') eq 'device_ports');
# TODO set cookie my $uri = URI->new();
# if (param('reset') or not param('tab') or param('tab') ne 'ports') { foreach my $key (keys %{ setting('sidebar_defaults')->{'device_ports'} }) {
$uri->query_param($key => params->{$key}) if params->{$key};
}
cookie('nd_ports-form' => $uri->query(), expires => '365 days');
}; };
get '/device' => require_login sub { get '/device' => require_login sub {

View File

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

View File

@@ -92,16 +92,6 @@
update_csv_download_link('device', '[% tab.tag %]', '[% tab.provides_csv %]'); update_csv_download_link('device', '[% tab.tag %]', '[% tab.provides_csv %]');
[% IF tab.tag == 'ports' %] [% IF tab.tag == 'ports' %]
// to be fair I can't remember why we do this in JS and not from the app
// perhaps because selecting form fields to go in the cookie is easier?
// var cookie = $('#ports_form').find('input,select')
// .not('#nd_port-query,input[name="q"],input[name="tab"]')
// .serializeArray();
// $('#ports_form').find('input[type="checkbox"]').map(function() {
// cookie.push({'name': 'columns', 'value': $(this).attr('name')});
// });
// $.cookie('nd_ports-form', $.param(cookie) ,{ expires: 365 });
// form reset icon on ports tab // form reset icon on ports tab
$('#nd_sidebar-reset-link').attr('href', uri_base + '/device?tab=[% tab.tag %]&reset=on&' + $('#nd_sidebar-reset-link').attr('href', uri_base + '/device?tab=[% tab.tag %]&reset=on&' +
$('#ports_form') $('#ports_form')

View File

@@ -86,7 +86,7 @@
<ul class="dropdown-menu"> <ul class="dropdown-menu">
[% FOREACH item IN settings._reports_menu.$category %] [% FOREACH item IN settings._reports_menu.$category %]
[% NEXT IF settings._reports.$item.hidden %] [% 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 %] [% END %]
</ul> </ul>
</li> </li>
@@ -138,6 +138,7 @@
<form class="navbar-search pull-left" method="get" action="[% uri_for('/search') %]"> <form class="navbar-search pull-left" method="get" action="[% uri_for('/search') %]">
<input placeholder="Find Anything" class="search-query span3" <input placeholder="Find Anything" class="search-query span3"
id="nq" name="q" value="[% display_name %]" type="text" autocomplete="off"/> id="nq" name="q" value="[% display_name %]" type="text" autocomplete="off"/>
<input name="firstsearch" type="hidden" value="on">
<div class="btn-group nd_navbar-search-group"> <div class="btn-group nd_navbar-search-group">
<button class="btn btn-inverse nd_navbar-search-icon"> <button class="btn btn-inverse nd_navbar-search-icon">
<span style="font-size: 18px;"> <span style="font-size: 18px;">

View File

@@ -58,7 +58,7 @@
<div class="clearfix input-prepend"> <div class="clearfix input-prepend">
<label class="add-on"> <label class="add-on">
<input type="checkbox" id="fruonly" <input type="checkbox" id="fruonly"
name="fruonly"[% ' checked="checked"' IF params.fruonly %]/> name="fruonly"[% ' checked="checked"' IF report_moduleinventory_defaults.fruonly %]/>
</label> </label>
<label class="nd_checkboxlabel" for="fruonly"> <label class="nd_checkboxlabel" for="fruonly">
<span class="nd_searchcheckbox uneditable-input">FRU Only</span> <span class="nd_searchcheckbox uneditable-input">FRU Only</span>
@@ -67,7 +67,7 @@
<div class="clearfix input-prepend"> <div class="clearfix input-prepend">
<label class="add-on"> <label class="add-on">
<input type="checkbox" id="matchall" <input type="checkbox" id="matchall"
name="matchall"[% ' checked="checked"' IF params.matchall %]/> name="matchall"[% ' checked="checked"' IF report_moduleinventory_defaults.matchall %]/>
</label> </label>
<label class="nd_checkboxlabel" for="matchall"> <label class="nd_checkboxlabel" for="matchall">
<span class="nd_searchcheckbox uneditable-input">Match All Options</span> <span class="nd_searchcheckbox uneditable-input">Match All Options</span>

View File

@@ -89,7 +89,7 @@
<div class="clearfix input-prepend"> <div class="clearfix input-prepend">
<label class="add-on"> <label class="add-on">
<input type="checkbox" id="matchall" <input type="checkbox" id="matchall"
name="matchall"[% ' checked="checked"' IF params.matchall %]/> name="matchall"[% ' checked="checked"' IF search_device_defaults.matchall %]/>
</label> </label>
<label class="nd_checkboxlabel" for="matchall"> <label class="nd_checkboxlabel" for="matchall">
<span class="nd_searchcheckbox uneditable-input">Match All Options</span> <span class="nd_searchcheckbox uneditable-input">Match All Options</span>

View File

@@ -4,7 +4,7 @@
<div class="clearfix input-prepend"> <div class="clearfix input-prepend">
<label class="add-on"> <label class="add-on">
<input type="checkbox" id="stamps" <input type="checkbox" id="stamps"
name="stamps"[% ' checked="checked"' IF params.stamps %]/> name="stamps"[% ' checked="checked"' IF search_node_defaults.stamps %]/>
</label> </label>
<label class="nd_checkboxlabel" for="stamps"> <label class="nd_checkboxlabel" for="stamps">
<span class="nd_searchcheckbox uneditable-input">Time Stamps</span> <span class="nd_searchcheckbox uneditable-input">Time Stamps</span>
@@ -13,7 +13,7 @@
<div class="clearfix input-prepend"> <div class="clearfix input-prepend">
<label class="add-on"> <label class="add-on">
<input type="checkbox" id="deviceports" <input type="checkbox" id="deviceports"
name="deviceports"[% ' checked="checked"' IF params.deviceports %]/> name="deviceports"[% ' checked="checked"' IF search_node_defaults.deviceports %]/>
</label> </label>
<label class="nd_checkboxlabel" for="deviceports"> <label class="nd_checkboxlabel" for="deviceports">
<span class="nd_searchcheckbox uneditable-input">Device Ports</span> <span class="nd_searchcheckbox uneditable-input">Device Ports</span>
@@ -22,7 +22,7 @@
<div class="clearfix input-prepend"> <div class="clearfix input-prepend">
<label class="add-on"> <label class="add-on">
<input type="checkbox" id="show_vendor" <input type="checkbox" id="show_vendor"
name="show_vendor"[% ' checked="checked"' IF params.show_vendor %]/> name="show_vendor"[% ' checked="checked"' IF search_node_defaults.show_vendor %]/>
</label> </label>
<label class="nd_checkboxlabel" for="show_vendor"> <label class="nd_checkboxlabel" for="show_vendor">
<span class="nd_searchcheckbox uneditable-input">Vendor</span> <span class="nd_searchcheckbox uneditable-input">Vendor</span>
@@ -31,7 +31,7 @@
<div class="clearfix input-prepend"> <div class="clearfix input-prepend">
<label class="add-on"> <label class="add-on">
<input type="checkbox" id="archived" <input type="checkbox" id="archived"
name="archived"[% ' checked="checked"' IF params.archived %]/> name="archived"[% ' checked="checked"' IF search_node_defaults.archived %]/>
</label> </label>
<label class="nd_checkboxlabel" for="archived"> <label class="nd_checkboxlabel" for="archived">
<span class="nd_searchcheckbox uneditable-input"> <span class="nd_searchcheckbox uneditable-input">
@@ -41,7 +41,7 @@
<div class="clearfix input-prepend"> <div class="clearfix input-prepend">
<label class="add-on nd_sidebar-legend"> <label class="add-on nd_sidebar-legend">
<input type="checkbox" id="node_partial" <input type="checkbox" id="node_partial"
name="partial"[% ' checked="checked"' IF params.partial %]/> name="partial"[% ' checked="checked"' IF search_node_defaults.partial %]/>
</label> </label>
<label class="nd_checkboxlabel" for="node_partial"> <label class="nd_checkboxlabel" for="node_partial">
<span class="nd_searchcheckbox uneditable-input">Partial Match</span> <span class="nd_searchcheckbox uneditable-input">Partial Match</span>
@@ -52,20 +52,20 @@
<div class="clearfix input-prepend"> <div class="clearfix input-prepend">
<label class="add-on"> <label class="add-on">
<input type="checkbox" id="age_invert" <input type="checkbox" id="age_invert"
name="age_invert"[% ' checked="checked"' IF params.age_invert %]/> name="age_invert"[% ' checked="checked"' IF search_node_defaults.age_invert %]/>
</label> </label>
<label class="nd_checkboxlabel" for="age_invert"> <label class="nd_checkboxlabel" for="age_invert">
<span class="nd_searchcheckbox uneditable-input">Not within...</span> <span class="nd_searchcheckbox uneditable-input">Not within...</span>
</label> </label>
</div> </div>
<input class="nd_side-input" id="daterange" <input class="nd_side-input" id="daterange"
type="text" name="daterange" value="[% params.daterange | html_entity %]"/> type="text" name="daterange" value="[% search_node_defaults.daterange | html_entity %]"/>
</div> </div>
<div class="clearfix"> <div class="clearfix">
<em class="muted">MAC address format:</em><br/> <em class="muted">MAC address format:</em><br/>
<select id="nd_node-mac-format" name="mac_format"> <select id="nd_node-mac-format" name="mac_format">
[% FOREACH format IN [ 'IEEE', 'Cisco', 'Microsoft', 'Sun' ] %] [% FOREACH format IN [ 'IEEE', 'Cisco', 'Microsoft', 'Sun' ] %]
<option[% ' selected="selected"' IF params.mac_format == format %]>[% format %]</option> <option[% ' selected="selected"' IF search_node_defaults.mac_format == format %]>[% format %]</option>
[% END %] [% END %]
</select> </select>
</div> </div>

View File

@@ -4,7 +4,7 @@
<div class="clearfix input-prepend"> <div class="clearfix input-prepend">
<label class="add-on"> <label class="add-on">
<input type="checkbox" id="port_partial" <input type="checkbox" id="port_partial"
name="partial"[% ' checked="checked"' IF params.partial %]/> name="partial"[% ' checked="checked"' IF search_port_defaults.partial %]/>
</label> </label>
<label class="nd_checkboxlabel" for="port_partial"> <label class="nd_checkboxlabel" for="port_partial">
<span class="nd_searchcheckbox uneditable-input">Partial Match</span> <span class="nd_searchcheckbox uneditable-input">Partial Match</span>
@@ -13,7 +13,7 @@
<div class="clearfix input-prepend"> <div class="clearfix input-prepend">
<label class="add-on"> <label class="add-on">
<input type="checkbox" id="port_uplink" <input type="checkbox" id="port_uplink"
name="uplink"[% ' checked="checked"' IF params.uplink %]/> name="uplink"[% ' checked="checked"' IF search_port_defaults.uplink %]/>
</label> </label>
<label class="nd_checkboxlabel" for="port_uplink"> <label class="nd_checkboxlabel" for="port_uplink">
<span class="nd_searchcheckbox uneditable-input">Uplinks</span> <span class="nd_searchcheckbox uneditable-input">Uplinks</span>
@@ -22,7 +22,7 @@
<div class="clearfix input-prepend"> <div class="clearfix input-prepend">
<label class="add-on"> <label class="add-on">
<input type="checkbox" id="port_ethernet" <input type="checkbox" id="port_ethernet"
name="ethernet"[% ' checked="checked"' IF params.ethernet %]/> name="ethernet"[% ' checked="checked"' IF search_port_defaults.ethernet %]/>
</label> </label>
<label class="nd_checkboxlabel" for="port_ethernet"> <label class="nd_checkboxlabel" for="port_ethernet">
<span class="nd_searchcheckbox uneditable-input">Ethernet Only</span> <span class="nd_searchcheckbox uneditable-input">Ethernet Only</span>