From 5192b52d550b8fdb80494cbfaac0f35e7daa1cd7 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Mon, 9 Apr 2012 22:55:10 +0100 Subject: [PATCH] removing JS-only links (closes #21) --- Changes | 7 ++- Netdisco/lib/Netdisco/Web.pm | 12 +---- Netdisco/lib/Netdisco/Web/Device.pm | 46 ++++++++++++++--- Netdisco/lib/Netdisco/Web/Search.pm | 63 +++++++++++++++++------ Netdisco/public/css/netdisco.css | 5 -- Netdisco/views/ajax/device/addresses.tt | 4 +- Netdisco/views/ajax/device/details.tt | 8 +-- Netdisco/views/ajax/device/ports.tt | 16 +++--- Netdisco/views/ajax/search/node_by_ip.tt | 8 +-- Netdisco/views/ajax/search/node_by_mac.tt | 12 ++--- Netdisco/views/ajax/search/port.tt | 2 +- Netdisco/views/ajax/search/vlan.tt | 12 ++--- Netdisco/views/inventory.tt | 6 +-- Netdisco/views/js/device.js | 6 ++- 14 files changed, 132 insertions(+), 75 deletions(-) diff --git a/Changes b/Changes index 042350df..92c3c49f 100644 --- a/Changes +++ b/Changes @@ -1,10 +1,15 @@ 0.x + [NEW FEATURES] + + * Icon to copy Navbar search text to Device Search Options field (closes #11) + [ENHANCEMENTS] * Native copy of the Netdisco sort_port routine (#17) * Refactor begin hooks (closes #16) - * Icon to copy Navbar search text to Device Search Options field (closes #11) + * Re-engineer link generation to ensure sane defaults and remembered options. + This came as a side-effect of removing JS-only links (closes #21) [BUG FIXES] diff --git a/Netdisco/lib/Netdisco/Web.pm b/Netdisco/lib/Netdisco/Web.pm index 6fe60d7c..a856250a 100644 --- a/Netdisco/lib/Netdisco/Web.pm +++ b/Netdisco/lib/Netdisco/Web.pm @@ -6,6 +6,7 @@ use Dancer::Plugin::DBIC; use Socket6 (); # to ensure dependency is met use HTML::Entities (); # to ensure dependency is met +use URI::QueryParam (); # part of URI, to add helper methods use Netdisco::Web::AuthN; use Netdisco::Web::Search; @@ -26,17 +27,6 @@ hook 'before_template' => sub { $Template::Directive::WHILE_MAX = 10_000; }; -# set up query strings which describe default search options. -# these are used in both Device and Search templates -hook 'before_template' => sub { - var('query_defaults' => { map { ($_ => "tab=$_") } qw/node device/ }); - - var('query_defaults')->{node} .= "\&$_=". (param($_) || '') - for qw/stamps vendor archived partial/; - var('query_defaults')->{device} .= "\&$_=". (param($_) || '') - for qw/matchall/; -}; - get '/' => sub { template 'index'; }; diff --git a/Netdisco/lib/Netdisco/Web/Device.pm b/Netdisco/lib/Netdisco/Web/Device.pm index ca83b16d..5e37451b 100644 --- a/Netdisco/lib/Netdisco/Web/Device.pm +++ b/Netdisco/lib/Netdisco/Web/Device.pm @@ -33,18 +33,48 @@ hook 'before' => sub { { name => 'n_archived', label => 'Archived Data', default => '' }, ]); - if (request->path eq uri_for('/device')->path) { - # new searches will have these defaults in the ports sidebar + # new searches will use these defaults in their sidebars + var('device_ports' => uri_for('/device', { + tab => 'ports', + age_num => 3, + age_unit => 'months', + })); + + foreach my $col (@{ var('port_columns') }) { + next unless $col->{default} eq 'on'; + var('device_ports')->query_param($col->{name}, 'checked'); + } + + foreach my $col (@{ var('connected_properties') }) { + next unless $col->{default} eq 'on'; + var('device_ports')->query_param($col->{name}, 'checked'); + } + + if (request->path eq uri_for('/device')->path + or index(request->path, uri_for('/ajax/content/device')->path) == 0) { + + foreach my $col (@{ var('port_columns') }) { + next unless $col->{default} eq 'on'; + params->{$col->{name}} = 'checked' + if not param('tab') or param('tab') ne 'ports'; + } + + foreach my $col (@{ var('connected_properties') }) { + next unless $col->{default} eq 'on'; + params->{$col->{name}} = 'checked' + if not param('tab') or param('tab') ne 'ports'; + } + if (not param('tab') or param('tab') ne 'ports') { - foreach my $col (@{ var('port_columns') }) { - params->{$col->{name}} = 'checked' if $col->{default} eq 'on'; - } - foreach my $col (@{ var('connected_properties') }) { - params->{$col->{name}} = 'checked' if $col->{default} eq 'on'; - } params->{'age_num'} = 3; params->{'age_unit'} = 'months'; } + + # for templates to link to same page with modified query but same options + my $self_uri = uri_for(request->path, scalar params); + $self_uri->query_param_delete('q'); + $self_uri->query_param_delete('f'); + var('self_options' => $self_uri->query_form_hash); } }; diff --git a/Netdisco/lib/Netdisco/Web/Search.pm b/Netdisco/lib/Netdisco/Web/Search.pm index 01159ccf..68d36b51 100644 --- a/Netdisco/lib/Netdisco/Web/Search.pm +++ b/Netdisco/lib/Netdisco/Web/Search.pm @@ -10,25 +10,43 @@ use List::MoreUtils (); use Net::DNS (); hook 'before' => sub { - if (request->path eq uri_for('/search')->path) { - # new searches have these defaults in their sidebars - if (not param('tab') or param('tab') ne 'node') { - params->{'stamps'} = 'checked'; - } - if (not param('tab') or param('tab') ne 'device') { - params->{'matchall'} = 'checked'; + # view settings for node options + var('node_options' => [ + { name => 'stamps', label => 'Time Stamps', default => 'on' }, + ]); + # view settings for device options + var('device_options' => [ + { name => 'matchall', label => 'Match All Options', default => 'on' }, + ]); + + # new searches will use these defaults in their sidebars + var('search_node' => uri_for('/search', {tab => 'node'})); + var('search_device' => uri_for('/search', {tab => 'device'})); + + foreach my $col (@{ var('node_options') }) { + next unless $col->{default} eq 'on'; + var('search_node')->query_param($col->{name}, 'checked'); + } + + foreach my $col (@{ var('device_options') }) { + next unless $col->{default} eq 'on'; + var('search_device')->query_param($col->{name}, 'checked'); + } + + if (request->path eq uri_for('/search')->path + or index(request->path, uri_for('/ajax/content/search')->path) == 0) { + + foreach my $col (@{ var('node_options') }) { + next unless $col->{default} eq 'on'; + params->{$col->{name}} = 'checked' + if not param('tab') or param('tab') ne 'node'; } - # used in the device search sidebar to populate select inputs - var('model_list' => [ - schema('netdisco')->resultset('Device')->get_distinct('model') - ]); - var('os_ver_list' => [ - schema('netdisco')->resultset('Device')->get_distinct('os_ver') - ]); - var('vendor_list' => [ - schema('netdisco')->resultset('Device')->get_distinct('vendor') - ]); + foreach my $col (@{ var('device_options') }) { + next unless $col->{default} eq 'on'; + params->{$col->{name}} = 'checked' + if not param('tab') or param('tab') ne 'device'; + } # used in the device search sidebar template to set selected items foreach my $opt (qw/model vendor os_ver/) { @@ -215,6 +233,17 @@ get '/search' => sub { } } + # used in the device search sidebar to populate select inputs + var('model_list' => [ + schema('netdisco')->resultset('Device')->get_distinct('model') + ]); + var('os_ver_list' => [ + schema('netdisco')->resultset('Device')->get_distinct('os_ver') + ]); + var('vendor_list' => [ + schema('netdisco')->resultset('Device')->get_distinct('vendor') + ]); + # list of tabs var('tabs' => [ { id => 'device', label => 'Device' }, diff --git a/Netdisco/public/css/netdisco.css b/Netdisco/public/css/netdisco.css index 518a7ef9..11f98ecd 100644 --- a/Netdisco/public/css/netdisco.css +++ b/Netdisco/public/css/netdisco.css @@ -87,11 +87,6 @@ body { height: 100%; } -/* link port name to restricted search */ -.this_port_only { - cursor: pointer; -} - /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /* many styles for the collapsing lists */ diff --git a/Netdisco/views/ajax/device/addresses.tt b/Netdisco/views/ajax/device/addresses.tt index dfadf46e..a195b03f 100644 --- a/Netdisco/views/ajax/device/addresses.tt +++ b/Netdisco/views/ajax/device/addresses.tt @@ -14,10 +14,10 @@ [% row.alias %] [% row.dns.remove(settings.domain_suffix) %] [% row.port %] + href="[% vars.device_ports %]&q=[% row.ip %]&f=[% row.port %]">[% row.port %] [% row.device_port.name %] [% row.subnet %] + href="[% vars.search_device %]&ip=[% row.subnet %]">[% row.subnet %] [% END %] diff --git a/Netdisco/views/ajax/device/details.tt b/Netdisco/views/ajax/device/details.tt index d966e769..99021329 100644 --- a/Netdisco/views/ajax/device/details.tt +++ b/Netdisco/views/ajax/device/details.tt @@ -8,7 +8,7 @@ Location [% d.location %] + href="[% vars.search_device %]&location=[% d.location | uri %]">[% d.location %] @@ -19,10 +19,10 @@ Vendor / Model [% d.vendor %] + href="[% vars.search_device %]&vendor=[% d.vendor | uri %]">[% d.vendor %] / [% d.model %] + href="[% vars.search_device %]&model=[% d.model | uri %]">[% d.model %] @@ -30,7 +30,7 @@ [% d.os %] / [% d.os_ver %] + href="[% vars.search_device %]&os_ver=[% d.os_ver | uri %]">[% d.os_ver %] diff --git a/Netdisco/views/ajax/device/ports.tt b/Netdisco/views/ajax/device/ports.tt index 098f67c4..fb618a4f 100644 --- a/Netdisco/views/ajax/device/ports.tt +++ b/Netdisco/views/ajax/device/ports.tt @@ -23,7 +23,10 @@ [% END %] [% IF params.c_port %] - [% row.port | html_entity %] + + [% row.port | html_entity %] + [% END %] [% IF params.c_descr %] [% row.descr | html_entity %] @@ -82,12 +85,13 @@ [% IF row.remote_ip %] [% IF row.neighbor %] - + [% row.neighbor.dns.remove(settings.domain_suffix) || row.neighbor.ip %] ([% row.remote_port | html_entity %]) [% ELSE %] N - + [% row.remote_ip %] (port: [% row.remote_port %] id: [% (row.remote_type _ ' / ') IF row.remote_type %][% row.remote_id %]) [% END %] @@ -95,16 +99,16 @@ [% FOREACH node IN row.$nodes %] [% '
' IF row.remote_ip OR NOT loop.first %] [% 'A  ' IF NOT node.active %] - [% node.mac %] + [% node.mac %] [% ' (' _ node.time_last_age _ ')' IF params.n_age %] [% IF params.n_ip %] [% FOREACH ip IN node.ips %]
  [% 'A  ' IF NOT ip.active %] [% SET dns = ip.dns %] [% IF dns %] - [% dns %] ([% ip.ip %]) + [% dns %] ([% ip.ip %]) [% ELSE %] - [% ip.ip %] + [% ip.ip %] [% END %] [% END %] [% END %] diff --git a/Netdisco/views/ajax/search/node_by_ip.tt b/Netdisco/views/ajax/search/node_by_ip.tt index d3f7d928..0b4eddd2 100644 --- a/Netdisco/views/ajax/search/node_by_ip.tt +++ b/Netdisco/views/ajax/search/node_by_ip.tt @@ -17,12 +17,12 @@ [% WHILE (row = macs.next) %] [% row.mac %] + href="[% vars.search_node %]&q=[% row.mac | uri %]">[% row.mac %] [% IF params.vendor %] [% row.oui.company %] [% END %] IP → MAC - [% row.ip %] + [% row.ip %] [% ' (' _ row.dns.remove(settings.domain_suffix) _ ')' IF row.dns %] [% ' A' IF NOT row.active %] @@ -38,7 +38,7 @@   [% END %] Switch Port - [% node.switch %] [ [% node.port %] ] + [% node.switch %] [ [% node.port %] ] [% ' (' _ node.device.dns.remove(settings.domain_suffix) _ ')' IF node.device.dns %] [% ' A' IF NOT node.active %] @@ -55,7 +55,7 @@   [% END %] MAC → IP - [% nodeip.ip %] + [% nodeip.ip %] [% ' (' _ nodeip.dns.remove(settings.domain_suffix) _ ')' IF nodeip.dns %] [% ' A' IF NOT nodeip.active %] diff --git a/Netdisco/views/ajax/search/node_by_mac.tt b/Netdisco/views/ajax/search/node_by_mac.tt index 4adf1788..651cce23 100644 --- a/Netdisco/views/ajax/search/node_by_mac.tt +++ b/Netdisco/views/ajax/search/node_by_mac.tt @@ -20,7 +20,7 @@ [% IF first_row %] [% row.mac %] + href="[% vars.search_node %]&q=[% row.mac | uri %]">[% row.mac %] [% ELSE %]   [% END %] @@ -35,7 +35,7 @@ [% END %] MAC → IP - [% row.ip %] + [% row.ip %] [% ' (' _ row.dns.remove(settings.domain_suffix) _ ')' IF row.dns %] [% ' A' IF NOT row.active %] @@ -51,7 +51,7 @@ [% IF first_row %] [% node.mac %] + href="[% vars.search_node %]&q=[% node.mac | uri %]">[% node.mac %] [% ELSE %]   [% END %] @@ -66,7 +66,7 @@ [% END %] Switch Port - [% node.switch %] [ [% node.port %] ] + [% node.switch %] [ [% node.port %] ] [% ' (' _ node.device.dns.remove(settings.domain_suffix) _ ')' IF node.device.dns %] [% ' A' IF NOT node.active %] @@ -82,7 +82,7 @@ [% IF first_row %] [% port.mac %] + href="[% vars.search_node %]&q=[% port.mac | uri %]">[% port.mac %] [% ELSE %]   [% END %] @@ -97,7 +97,7 @@ [% END %] Switch Port - [% port.ip %] [ [% port.port %] ] + [% port.ip %] [ [% port.port %] ] [% ' (' _ port.device.dns.remove(settings.domain_suffix) _ ')' IF port.device.dns %] [% IF params.stamps %] diff --git a/Netdisco/views/ajax/search/port.tt b/Netdisco/views/ajax/search/port.tt index 8e0b06ce..8642c401 100644 --- a/Netdisco/views/ajax/search/port.tt +++ b/Netdisco/views/ajax/search/port.tt @@ -11,7 +11,7 @@ [% WHILE (row = results.next) %] [% row.name %] - [% row.ip %] [ [% row.port %] ] + [% row.ip %] [ [% row.port %] ] [% ' (' _ row.device.dns.remove(settings.domain_suffix) _ ')' IF row.device.dns %] [% row.descr %] diff --git a/Netdisco/views/ajax/search/vlan.tt b/Netdisco/views/ajax/search/vlan.tt index feb3de4e..e5d7fe57 100644 --- a/Netdisco/views/ajax/search/vlan.tt +++ b/Netdisco/views/ajax/search/vlan.tt @@ -13,17 +13,17 @@ [% WHILE (row = results.next) %] [% row.vlan.vlan %] + href="[% vars.device_ports %]&q=[% row.ip %]&f=[% row.vlan.vlan %]">[% row.vlan.vlan %] [% row.dns %] + href="[% vars.device_ports %]&q=[% row.ip %]&f=[% row.vlan.vlan %]">[% row.dns %] [% row.vlan.description %] + href="[% vars.device_ports %]&q=[% row.ip %]&f=[% row.vlan.vlan %]">[% row.vlan.description %] [% row.model %] + href="[% vars.device_ports %]&q=[% row.ip %]&f=[% row.vlan.vlan %]">[% row.model %] [% row.os %] + href="[% vars.device_ports %]&q=[% row.ip %]&f=[% row.vlan.vlan %]">[% row.os %] [% row.vendor %] + href="[% vars.device_ports %]&q=[% row.ip %]&f=[% row.vlan.vlan %]">[% row.vendor %] [% END %] diff --git a/Netdisco/views/inventory.tt b/Netdisco/views/inventory.tt index d2d8f8bb..5540e75b 100644 --- a/Netdisco/views/inventory.tt +++ b/Netdisco/views/inventory.tt @@ -16,12 +16,12 @@ + href="[% vars.search_device %]&vendor=[% platform.vendor %]"> [% platform.vendor %] + href="[% vars.search_device %]&model=[% platform.model %]"> [% platform.model %] [% platform.get_column('count') %] @@ -46,7 +46,7 @@ [% release.os %] + href="[% vars.search_device %]&os_ver=[% release.os_ver %]"> [% release.os_ver %] [% release.get_column('count') %] diff --git a/Netdisco/views/js/device.js b/Netdisco/views/js/device.js index 19b1a7a2..cf8d526d 100644 --- a/Netdisco/views/js/device.js +++ b/Netdisco/views/js/device.js @@ -70,10 +70,14 @@ $('#ports_form').trigger('submit'); }); - // make all port names clickable to restrict results to that port only + // clickable device port names can simply resubmit AJAX rather than + // fetch the whole page again. $('body').delegate('.this_port_only', 'click', function() { + event.preventDefault(); // link is real so prevent page submit + var port = $(this).text(); sweep.val(port); + $('.field_clear_icon').show(); $('#ports_form').trigger('submit'); });