App-Netdisco

 view release on metacpan or  search on metacpan

share/public/javascripts/netdisco.js  view on Meta::CPAN

// parameterised for the active tab - submits search form and injects
// HTML response into the tab pane, or an error/empty-results message
function do_search (event, tab) {
  var form   = '#' + tab + '_form';
  var target = '#' + tab + '_pane';
  var query  = $(form).serialize();

  // stop form from submitting normally
  event.preventDefault();

  // hide or show sidebars depending on previous state,
  // and whether the sidebar contains any content (detected by TT)
  if (has_sidebar[tab] == 0) {
    $('.nd_sidebar, #nd_sidebar-toggle-img-out').hide();
    $('.content').css('margin-right', '10px');
  }
  else {
    if (sidebar_hidden) {
      $('#nd_sidebar-toggle-img-out').show();
    }
    else {
      $('.content').css('margin-right', '215px');
      $('.nd_sidebar').show();
    }
  }

  // in case of slow data load, let the user know
  if (tab != 'jobqueue') {
    $(target).html(
      '<div class="span2 alert"><i class="icon-spinner icon-spin"></i> Waiting for results...</div>'
    );
  }

  // submit the query and put results into the tab pane
  fetch( uri_base + '/ajax/content/' + path + '/' + tab + '?' + query,
    { headers: { 'X-Requested-With': 'XMLHttpRequest' } })
    .then( response => {
      if (! response.ok) {
        $(target).html(
          '<div class="span5 alert alert-error"><i class="icon-warning-sign"></i> ' +
          'Search failed! Please contact your site administrator (server error).</div>'
        );
        return;
        // throw new Error('Network response was not ok');
      }
      return response.text();
    })
    .then( content => {
      if (content == "") {
        $(target).html('<div class="span2 alert alert-info">No matching records.</div>');
      }
      else {
        $(target).html(content);
        // delegate to any [device|search] specific JS code
        $('div.content > div.tab-content table.nd_floatinghead').floatThead({
          scrollingTop: 40
          ,useAbsolutePositioning: false
        });
        inner_view_processing(tab);
      }
    })
    .catch( error => {
      $(target).html(
        '<div class="span5 alert alert-error"><i class="icon-warning-sign"></i> ' +
        'Search failed! Please contact your site administrator (network error: ' + error + ').</div>'
      );
      console.error('There has been a problem with your fetch operation:', error);
    });
}

// keep track of which tabs have a sidebar, for when switching tab
var has_sidebar = {};
var sidebar_hidden = 0;

// the history.js plugin is great, but fires statechange at pushState
// so we have these semaphpores to help avoid messing the History.

// set true when faking a user click on a tab
var is_from_state_event = 0;
// set true when the history plugin does pushState - to prevent loop
var is_from_history_plugin = 0;

// on tab change, hide previous tab's search form and show new tab's
// search form. also trigger to load the content for the newly active tab.
function update_content(from, to) {
  $('#' + from + '_search').toggleClass('active');
  $('#' + to + '_search').toggleClass('active');

  var to_form = '#' + to + '_form';
  var from_form = '#' + from + '_form';

  // page title
  var pgtitle = default_pgtitle;
  if ($('#nd_device-name').text().length) {
    var pgtitle = $.trim($('#nd_device-name').text()) +' - '+ $('#'+ to + '_link').text();
  }

  // navbar text decoration special case
  if (to != 'device') {
    $('#nq').css('text-decoration', 'none');
  }
  else {
    form_inputs.each(function() {device_form_state($(this))});
  }



( run in 0.446 second using v1.01-cache-2.11-cpan-39bf76dae61 )