Files
netdisco/Netdisco/share/views/js/common.js
Oliver Gorwits ce604ca20b CSV download support via provides_csv plugin attr
Squashed commit of the following:

commit e44f203f47
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Fri Sep 20 20:00:46 2013 +0100

    also set filename on csv download link

commit 26b47d1296
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Fri Sep 20 19:56:38 2013 +0100

    conditionally show csv icon for device and search tabs

commit 119d2c672d
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Fri Sep 20 19:48:24 2013 +0100

    tweak look of csv icon

commit 89816892be
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Fri Sep 20 19:38:31 2013 +0100

    enable csv icon link rewrite for all pages

commit ac29769402
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Fri Sep 20 19:28:49 2013 +0100

    use single route handler for ajax and csv response

commit 4b6f89635d
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Fri Sep 20 18:59:08 2013 +0100

    no need for separate csv link template

commit 1021329e1c
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Fri Sep 20 18:57:20 2013 +0100

    display download as csv icon conditionally

commit ce57cdba69
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Fri Sep 20 09:23:57 2013 +0100

    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?

commit d3553d2623
Author: Eric A. Miller <emiller@cpan.org>
Date:   Thu Sep 19 22:30:27 2013 -0400

    add csv download to duplex mismatch, half duplex, and port utilization reports

commit 5d4df72a24
Author: Eric A. Miller <emiller@cpan.org>
Date:   Wed Sep 18 23:50:27 2013 -0400

    add csv download to ap channel distribution report

commit 0824d7936a
Author: Eric A. Miller <emiller@cpan.org>
Date:   Wed Sep 18 23:42:53 2013 -0400

    need Template::Plugin::CSV for csv output

commit bb1f842d92
Author: Eric A. Miller <emiller@cpan.org>
Date:   Wed Sep 18 23:35:28 2013 -0400

    add csv download option to device addresses

commit 99ada1132c
Author: Eric A. Miller <emiller@cpan.org>
Date:   Wed Sep 18 23:32:57 2013 -0400

    add csv download option to device, port, and vlan search pages
2013-09-20 20:15:32 +01:00

121 lines
4.0 KiB
JavaScript

// csv download icon on any table page
// needs to be dynamically updated to use current search options
function update_csv_download_link (type, tab, show) {
var form = '#' + tab + '_form';
var query = $(form).serialize();
if (show == '1') {
$('#nd_csv-download')
.attr('href', '/ajax/content/' + type + '/' + tab + '?' + query)
.attr('download', 'netdisco-' + type + '-' + tab + '.csv')
.show();
}
else {
$('#nd_csv-download').hide();
}
}
// 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) {
var pgtitle = $('#nd_device-name').text() +' - '+ $('#'+ tab + '_link').text();
}
return pgtitle;
}
// update browser search history with the new query.
// however if it's the same tab, this is a *replace* of the query url.
// and just skip this bit if it's the report or admin display.
function update_browser_history (tab, pgtitle) {
var form = '#' + tab + '_form';
var query = $(form).serialize();
if (window.History && window.History.enabled) {
is_from_history_plugin = 1;
window.History.replaceState(
{name: tab, fields: $(form).serializeArray()},
pgtitle, uri_base + '/' + path + '?' + query
);
is_from_history_plugin = 0;
}
}
// each sidebar search form has a hidden copy of the main navbar search
// query. when the tab query takes place, copy the navbar locally, then
// replicate to all other tabs.
function copy_navbar_to_sidebar (tab) {
var form = '#' + tab + '_form';
if ($('#nq').val()) {
$(form).find("input[name=q]").val( $('#nq').val() );
}
$('form').find("input[name=q]").each( function() {
$(this).val( $(form).find("input[name=q]").val() );
});
}
$(document).ready(function() {
[% IF search %]
// search tabs
[% FOREACH tab IN settings._search_tabs %]
$('[% "#${tab.tag}_form" %]').submit(function (event) {
var pgtitle = update_page_title('[% tab.tag %]');
update_csv_download_link('search', '[% tab.tag %]', '[% tab.provides_csv %]');
update_browser_history('[% tab.tag %]', pgtitle);
copy_navbar_to_sidebar('[% tab.tag %]');
do_search(event, '[% tab.tag %]');
});
[% END %]
[% END %]
[% IF device %]
// device tabs
[% FOREACH tab IN settings._device_tabs %]
$('[% "#${tab.tag}_form" %]').submit(function (event) {
var pgtitle = update_page_title('[% tab.tag %]');
update_csv_download_link('device', '[% tab.tag %]', '[% tab.provides_csv %]');
update_browser_history('[% tab.tag %]', pgtitle);
copy_navbar_to_sidebar('[% tab.tag %]');
[% IF tab.tag == 'ports' %]
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 });
[% END %]
do_search(event, '[% tab.tag %]');
});
[% END %]
[% END %]
[% IF report %]
// for the report pages
$('[% "#${report.tag}_form" %]').submit(function (event) {
update_page_title('[% report.tag %]');
update_csv_download_link('report', '[% report.tag %]', '1');
do_search(event, '[% report.tag %]');
});
[% END -%]
[% IF task %]
// for the admin pages
$('[% "#${task.tag}_form" %]').submit(function (event) {
update_page_title('[% task.tag %]');
update_csv_download_link('task', '[% task.tag %]', '1');
do_search(event, '[% task.tag %]');
});
[% END %]
// on page load, load the content for the active tab
[% IF params.tab %]
$('#[% params.tab %]_form').trigger("submit");
[% END %]
});