Merge branch 'master' into 3193-manage-account
[arvados.git] / apps / workbench / app / assets / javascripts / tab_panes.js
1 // Load tab panes on demand. See app/views/application/_content.html.erb
2
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'))
10         return;
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) {
16             var errhtml;
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();
22                 } else {
23                     errhtml = jqxhr.responseText;
24                 }
25             } else {
26                 errhtml = ("An error occurred: " +
27                            (jqxhr.responseText || status)).
28                     replace(/&/g, '&').
29                     replace(/</g, '&lt;').
30                     replace(/>/g, '&gt;');
31             }
32             $('> div > div', this).html(
33                 '<div><p>' +
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');
41             });
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";
51             }, 1);
52             $(this).addClass('loaded');
53         });
54 });
55
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');
65     } else {
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>');
70     }
71 });
72
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');
76 });