1 // Load tab panes on demand. See app/views/application/_content.html.erb
3 // Fire when a tab is selected/clicked. Check whether the content in
4 // the corresponding pane is loaded (or is being loaded). If not,
5 // start an AJAX request to load the content.
6 $(document).on('shown.bs.tab', '[data-toggle="tab"]', function(e) {
7 var content_url = $(e.target).attr('data-pane-content-url');
8 var $pane = $($(e.target).attr('href'));
9 if ($pane.hasClass('loaded'))
11 $.ajax(content_url, {dataType: 'html', type: 'GET', context: $pane}).
12 done(function(data, status, jqxhr) {
13 $('> div > div', this).html(data);
14 $(this).addClass('loaded');
15 }).fail(function(jqxhr, status, error) {
17 if (jqxhr.getResponseHeader('Content-Type').match(/\btext\/html\b/)) {
18 var $response = $(jqxhr.responseText);
19 var $wrapper = $('div#page-wrapper', $response);
20 if ($wrapper.length) {
21 errhtml = $wrapper.html();
23 errhtml = jqxhr.responseText;
26 errhtml = ("An error occurred: " +
27 (jqxhr.responseText || status)).
28 replace(/&/g, '&').
29 replace(/</g, '<').
30 replace(/>/g, '>');
32 $('> div > div', this).html(
34 '<a href="#" class="btn btn-primary tab_reload">' +
35 '<i class="fa fa-fw fa-refresh"></i> ' +
36 'Reload tab</a></p><iframe></iframe></div>');
37 $('.tab_reload', this).click(function() {
38 $('> div > div', $pane).html(
39 '<div class="spinner spinner-32px spinner-h-center"></div>');
40 $pane.trigger('arv:pane:reload');
42 // We want to render the error in an iframe, in order to
43 // avoid conflicts with the main page's element ids, etc.
44 // In order to do that dynamically, we have to set a
45 // timeout on the iframe window to load our HTML *after*
46 // the default source (e.g., about:blank) has loaded.
47 var iframe = $('iframe', this)[0];
48 iframe.contentWindow.setTimeout(function() {
49 $('body', iframe.contentDocument).html(errhtml);
50 iframe.height = iframe.contentDocument.body.scrollHeight + "px";
52 $(this).addClass('loaded');
56 // Fire when the content in a tab pane becomes stale/dirty. If the
57 // pane is visible now, reload it right away. Otherwise, just replace
58 // the current content with a spinner for now: there's no need to load
59 // the new content unless/until the user clicks the corresponding tab.
60 $(document).on('arv:pane:reload', '.tab-pane', function() {
61 // Unload a single pane. Reload it if it's active.
62 $(this).removeClass('loaded');
63 if ($(this).hasClass('active')) {
64 $('[href=#' + $(this).attr('id') + ']').trigger('shown.bs.tab');
66 // When the user selects this tab, show a spinner instead of
67 // old content while loading.
68 $('> div > div', this).
69 html('<div class="spinner spinner-32px spinner-h-center"></div>');
73 // Mark all panes as stale/dirty. Refresh the active pane.
74 $(document).on('arv-log-event arv:pane:reload:all', function() {
75 $('.tab-pane.loaded').trigger('arv:pane:reload');