X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/190d0f110dab5d51dcfa7b13ff658583fdd21a5d..31e1d10eb4a54b13f55b3c9638f46032be633ff9:/apps/workbench/app/assets/javascripts/tab_panes.js diff --git a/apps/workbench/app/assets/javascripts/tab_panes.js b/apps/workbench/app/assets/javascripts/tab_panes.js index ec4a519e96..0430ca9e3e 100644 --- a/apps/workbench/app/assets/javascripts/tab_panes.js +++ b/apps/workbench/app/assets/javascripts/tab_panes.js @@ -1,38 +1,181 @@ // Load tab panes on demand. See app/views/application/_content.html.erb -// Fire when a tab is selected/clicked. Check whether the content in -// the corresponding pane is loaded (or is being loaded). If not, -// start an AJAX request to load the content. -$(document).on('shown.bs.tab', '[data-toggle="tab"]', function(e) { - var content_url = $(e.target).attr('data-pane-content-url'); - var $pane = $($(e.target).attr('href')); - if ($pane.hasClass('loaded')) - return; - $.ajax(content_url, {dataType: 'html', type: 'GET', context: $pane}). - done(function(data, status, jqxhr) { - $('> div > div', this).html(data); - $(this).addClass('loaded'); - }); +// Fire when a tab is selected/clicked. +$(document).on('shown.bs.tab', '[data-toggle="tab"]', function(event) { + // When we switch tabs, remove "active" from any refreshable panes within + // the previous tab content so they don't continue to refresh unnecessarily, and + // add "active" to any refreshable panes under the newly shown tab content. + + var tgt = $($(event.relatedTarget).attr('href')); + $(".pane-anchor", tgt).each(function (i, e) { + var a = $($(e).attr('href')); + a.removeClass("active"); + }); + + tgt = $($(event.target).attr('href')); + $(".pane-anchor", tgt).each(function (i, e) { + var a = $($(e).attr('href')); + a.addClass("active"); + }); + + // Now trigger reload of the newly shown tab pane. + $(event.target).trigger('arv:pane:reload'); }); -// Fire when the content in a tab pane becomes stale/dirty. If the -// pane is visible now, reload it right away. Otherwise, just replace -// the current content with a spinner for now: there's no need to load -// the new content unless/until the user clicks the corresponding tab. -$(document).on('arv:pane:reload', '.tab-pane', function() { - // Unload a single pane. Reload it if it's active. - $(this).removeClass('loaded'); - if ($(this).hasClass('active')) { - $('[href=#' + $(this).attr('id') + ']').trigger('shown.bs.tab'); +// Ask a refreshable pane to reload via ajax. +// Target of this event is the anchoring element that manages the pane. +// +// Panes can be in one of three primary states, managed by setting CSS classes +// on the object: not loaded (no pane-* state classes), pane-loading, pane-loaded +// +// not loaded means the pane needs to be loaded when the pane becomes active +// +// pane-loading means there is a current AJAX call outstanding to reload the pane +// +// pane-loaded means the pane is believe to be up to date +// +// There are two additional states: pane-stale and pane-reload-pending +// +// pane-stale indicates a pane that is already loading has been invalidated and +// should schedule a reload immediately when the current load completes. (This +// happens if there are clusters of events, where the reload is trigged by the +// first event, but we actually want to display the state after the final event +// has been processed.) +// +// pane-reload-pending indicates a reload is scheduled, to suppress +// scheduling any additional reloads. +$(document).on('arv:pane:reload', function(e) { + e.stopPropagation(); + + // '$anchor' is the event target, which is a .pane-anchor or a bootstrap + // tab anchor. This is the element that stores the state of the pane. The + // actual element that will contain the content is pointed to in the 'href' + // attribute of etarget. + var $anchor = $(e.target); + + if ($anchor.hasClass('pane-loading')) { + // Already loading, mark stale to schedule a reload after this one. + $anchor.addClass('pane-stale'); + return; + } + + if ($anchor.hasClass('pane-no-auto-reload') && $anchor.hasClass('pane-loaded')) { + // Have to explicitly remove pane-loaded if we want it to reload. + return; + } + + var throttle = $anchor.attr('data-load-throttle'); + if (!throttle) { + throttle = 3000; + } + var now = (new Date()).getTime(); + var loaded_at = $anchor.attr('data-loaded-at'); + var since_last_load = now - loaded_at; + if (loaded_at && (since_last_load < throttle)) { + if (!$anchor.hasClass('pane-reload-pending')) { + $anchor.addClass('pane-reload-pending'); + setTimeout((function() { + $anchor.trigger('arv:pane:reload'); + }), throttle - since_last_load); + } + return; + } + + // We know this doesn't have 'pane-loading' because we tested for it above + $anchor.removeClass('pane-reload-pending'); + $anchor.removeClass('pane-loaded'); + $anchor.removeClass('pane-stale'); + + // $pane is the actual content area that is going to be updated. + var $pane = $($anchor.attr('href')); + if ($pane.hasClass('active')) { + $anchor.addClass('pane-loading'); + + var content_url = $anchor.attr('data-pane-content-url'); + $.ajax(content_url, {dataType: 'html', type: 'GET', context: $pane}). + done(function(data, status, jqxhr) { + // Preserve collapsed state + var collapsable = {}; + $(".collapse", $(this)).each(function(i, c) { + collapsable[c.id] = $(c).hasClass('in'); + }); + var tmp = $(data); + $(".collapse", tmp).each(function(i, c) { + if (collapsable[c.id]) { + $(c).addClass('in'); + } else { + $(c).removeClass('in'); + } + }); + $(this).html(tmp); + $anchor.removeClass('pane-loading'); + $anchor.addClass('pane-loaded'); + $anchor.attr('data-loaded-at', (new Date()).getTime()); + $(this).trigger('arv:pane:loaded'); + + if ($anchor.hasClass('pane-stale')) { + $anchor.trigger('arv:pane:reload'); + } + }).fail(function(jqxhr, status, error) { + var errhtml; + if (jqxhr.getResponseHeader('Content-Type').match(/\btext\/html\b/)) { + var $response = $(jqxhr.responseText); + var $wrapper = $('div#page-wrapper', $response); + if ($wrapper.length) { + errhtml = $wrapper.html(); + } else { + errhtml = jqxhr.responseText; + } + } else { + errhtml = ("An error occurred: " + + (jqxhr.responseText || status)). + replace(/&/g, '&'). + replace(//g, '>'); + } + $(this).html('

' + + '' + + ' ' + + 'Reload tab

'); + $('.tab_reload', $(this)).click(function() { + $(this).html('
'); + $anchor.trigger('arv:pane:reload'); + }); + // We want to render the error in an iframe, in order to + // avoid conflicts with the main page's element ids, etc. + // In order to do that dynamically, we have to set a + // timeout on the iframe window to load our HTML *after* + // the default source (e.g., about:blank) has loaded. + var iframe = $('iframe', $(this))[0]; + iframe.contentWindow.setTimeout(function() { + $('body', iframe.contentDocument).html(errhtml); + iframe.height = iframe.contentDocument.body.scrollHeight + "px"; + }, 1); + $anchor.addClass('pane-loaded'); + }); } else { - // When the user selects this tab, show a spinner instead of + // When the user selects e.target tab, show a spinner instead of // old content while loading. - $('> div > div', this). - html('
'); + $pane.html('
'); } }); -// Mark all panes as stale/dirty. Refresh the active pane. -$(document).on('arv-log-event arv:pane:reload:all', function() { - $('.tab-pane.loaded').trigger('arv:pane:reload'); +// Mark all panes as stale/dirty. Refresh any 'active' panes. +$(document).on('arv:pane:reload:all', function() { + $('.pane-anchor').trigger('arv:pane:reload'); +}); + +$(document).on('ready ajax:complete', function() { + // Panes marked arv-refresh-on-log-event should be refreshed + $('.pane-anchor.arv-refresh-on-log-event').on('arv-log-event', function(e) { + $(e.target).trigger('arv:pane:reload'); + }); +}); + +// If there is a 'tab counts url' in the nav-tabs element then use it to get some javascript that will update them +$(document).on('ready count-change', function() { + var tabCountsUrl = $('ul.nav-tabs').data('tab-counts-url'); + if( tabCountsUrl && tabCountsUrl.length ) { + $.get( tabCountsUrl ); + } });