From 8ae1bb1d6d8ba0321f398b3c39e9b7ac34c02a78 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Mon, 27 Aug 2012 19:54:31 +0100 Subject: [PATCH] Typeahead on the main navbar search (for devices only) --- Changes | 1 + Netdisco/lib/Netdisco/Web/Device.pm | 9 +++++++++ Netdisco/views/js/common.js | 9 +++++++++ 3 files changed, 19 insertions(+) diff --git a/Changes b/Changes index 9002ea85..0dc23e0e 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,7 @@ [NEW FEATURES] * Icon to copy Navbar search text to Device Search Options field (closes #11) + * Typeahead on the main navbar search (for devices only) [ENHANCEMENTS] diff --git a/Netdisco/lib/Netdisco/Web/Device.pm b/Netdisco/lib/Netdisco/Web/Device.pm index 5e37451b..7669c64f 100644 --- a/Netdisco/lib/Netdisco/Web/Device.pm +++ b/Netdisco/lib/Netdisco/Web/Device.pm @@ -171,6 +171,15 @@ ajax '/ajax/content/device/details' => sub { }, { layout => undef }; }; +# support typeahead with simple AJAX query for device names +ajax '/ajax/data/device/typeahead' => sub { + my $q = param('query'); + my $set = schema('netdisco')->resultset('Device')->search_fuzzy($q); + + content_type 'application/json'; + return to_json [map {$_->dns || $_->name || $_->ip} $set->all]; +}; + get '/device' => sub { my $ip = NetAddr::IP::Lite->new(param('q')); if (! $ip) { diff --git a/Netdisco/views/js/common.js b/Netdisco/views/js/common.js index 7c6b6541..2761c91e 100644 --- a/Netdisco/views/js/common.js +++ b/Netdisco/views/js/common.js @@ -8,4 +8,13 @@ [% IF params.tab %] $('#[% params.tab %]_form').trigger("submit"); [% END %] + + // enable typeahead on the main search box, for device names only + $('#nq').typeahead({ + source: function (query, process) { + return $.get('/ajax/data/device/typeahead', { query: query }, function (data) { + return process(data); + }); + } + }); });