From ce57cdba6997b0b66d447ddb864ecf5374ab4d55 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Fri, 20 Sep 2013 09:23:57 +0100 Subject: [PATCH] Based on jeneric's CSV download templates; - try to reduce code duplication by using same route handler for ajax and csv, using request->is_ajax to switch the template, and set content-type - use new HTML5 "download" attribute on links so content-disposition header is no longer necessary - download CSV icon is placed on all tables (per report/device/serach section) - update download CSV link using javascript just before table content is fetched - this is necessary to make sure updated sidebar query params are included The idea here is to allow us to support CSV download in the pages which display tables by only doing the following: - (existing routes:) replace "ajax" with "get" route handler - add logic to switch template in handler, based on request->is_ajax - write _csv.tt version of the template, to spit out CSV file content This makes it much easier for new devs to write reports supporting CSV, I think? --- .../Netdisco/Web/Plugin/Report/HalfDuplex.pm | 26 ++++--------------- .../share/views/ajax/report/halfduplex.tt | 1 - Netdisco/share/views/js/common.js | 20 ++++++++++++-- Netdisco/share/views/report.tt | 4 +++ 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/Report/HalfDuplex.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/Report/HalfDuplex.pm index 64a41186..065786ac 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/Report/HalfDuplex.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/Report/HalfDuplex.pm @@ -1,7 +1,6 @@ package App::Netdisco::Web::Plugin::Report::HalfDuplex; use Dancer ':syntax'; -use Dancer::Plugin::Ajax; use Dancer::Plugin::DBIC; use Dancer::Plugin::Auth::Extensible; @@ -13,19 +12,6 @@ register_report({ label => 'Ports in Half Duplex Mode', }); -ajax '/ajax/content/report/halfduplex' => require_login sub { - my $set = schema('netdisco')->resultset('DevicePort')->search( - { up => 'up', duplex => { '-ilike' => 'half' } }, - { order_by => [qw/device.dns port/], prefetch => 'device' }, - ); - return unless $set->count; - - content_type('text/html'); - template 'ajax/report/halfduplex.tt', { - results => $set, - }, { layout => undef }; -}; - get '/ajax/content/report/halfduplex' => require_login sub { my $format = param('format'); my $set = schema('netdisco')->resultset('DevicePort')->search( @@ -34,16 +20,14 @@ get '/ajax/content/report/halfduplex' => require_login sub { ); return unless $set->count; - if ( $format eq 'csv' ) { - - header( 'Content-Type' => 'text/comma-separated-values' ); - header( 'Content-Disposition' => - "attachment; filename=\"halfduplex.csv\"" ); - template 'ajax/report/halfduplex_csv.tt', { results => $set, }, + if (request->is_ajax) { + template 'ajax/report/halfduplex.tt', { results => $set, }, { layout => undef }; } else { - return; + header( 'Content-Type' => 'text/comma-separated-values' ); + template 'ajax/report/halfduplex_csv.tt', { results => $set, }, + { layout => undef }; } }; diff --git a/Netdisco/share/views/ajax/report/halfduplex.tt b/Netdisco/share/views/ajax/report/halfduplex.tt index 178e1594..f5594292 100644 --- a/Netdisco/share/views/ajax/report/halfduplex.tt +++ b/Netdisco/share/views/ajax/report/halfduplex.tt @@ -1,4 +1,3 @@ -[% INCLUDE "download_as.tt" %] diff --git a/Netdisco/share/views/js/common.js b/Netdisco/share/views/js/common.js index d66e9e64..48098ad8 100644 --- a/Netdisco/share/views/js/common.js +++ b/Netdisco/share/views/js/common.js @@ -1,3 +1,18 @@ + // csv download icon on any table page + // needs to be dynamically updated to use current search options + function update_csv_download_link (type, tab) { + var form = '#' + tab + '_form'; + var query = $(form).serialize(); + + // this is needed otherwise we can 404 on url with traling slash + if (query.length) { query = '/' + query } + + $('#nd_csv-download').attr('href', '/ajax/content/' + type + '/' + tab + query); + } + + // page title includes tab name and possibly device name + // this is nice for when you have multiple netdisco pages open in the + // browser function update_page_title (tab) { var pgtitle = 'Netdisco'; if ($('#nd_device-name').text().length) { @@ -74,7 +89,8 @@ [% IF report %] // for the report pages $('[% "#${report.tag}_form" %]').submit(function (event) { - update_page_title('[% tab.tag %]'); + update_page_title('[% report.tag %]'); + update_csv_download_link('report', '[% report.tag %]'); do_search(event, '[% report.tag %]'); }); [% END -%] @@ -82,7 +98,7 @@ [% IF task %] // for the admin pages $('[% "#${task.tag}_form" %]').submit(function (event) { - update_page_title('[% tab.tag %]'); + update_page_title('[% task.tag %]'); do_search(event, '[% task.tag %]'); }); [% END %] diff --git a/Netdisco/share/views/report.tt b/Netdisco/share/views/report.tt index b3cb963b..6db3dce0 100644 --- a/Netdisco/share/views/report.tt +++ b/Netdisco/share/views/report.tt @@ -28,6 +28,10 @@