4084: Move all pane state into $pane, flatten panes with multiple entry points.
[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.
4 $(document).on('shown.bs.tab', '[data-toggle="tab"]', function(event) {
5     // reload the pane (unless it's already loaded)
6     $($(event.target).attr('href')).
7         not('.pane-loaded').
8         trigger('arv:pane:reload');
9 });
10
11 // Ask a refreshable pane to reload via ajax.
12 //
13 // Target of this event is the anchor element that manages the pane.  A reload
14 // consists of an AJAX call to load the "data-pane-content-url" and replace the
15 // contents of the DOM node pointed to by "href".
16 //
17 // There are four CSS classes set on the object to indicate its state:
18 // pane-loading, pane-stale, pane-loaded, pane-reload-pending
19 //
20 // There are five states based on the presence or absence of css classes:
21 //
22 // 1. no pane-* states means the pane must be loaded when the pane becomes active
23 //
24 // 2. "pane-loading" means an AJAX call has been made to reload the pane and we are
25 // waiting on a result
26 //
27 // 3. "pane-loading pane-stale" indicates a pane that is already loading has
28 // been invalidated and should schedule a reload immediately when the current
29 // load completes.  (This happens when there is a cluster of events, where the
30 // reload is triggered by the first event, but we want ensure that we
31 // eventually load the final quiescent state).
32 //
33 // 4. "pane-loaded" means the pane is up to date
34 //
35 // 5. "pane-loaded pane-reload-pending" indicates a reload is scheduled (but has
36 // not started yet), suppressing scheduling of any further reloads.
37 //
38 $(document).on('arv:pane:reload', '[data-pane-content-url]', function(e) {
39     // $pane, the event target, is an element whose content is to be
40     // replaced. Pseudoclasses on $pane (pane-loading, etc) encode the
41     // current loading state.
42     var $pane = $(e.target);
43
44     var content_url = $pane.attr('data-pane-content-url');
45     if (!content_url) {
46         // When reloadable elements are nested, we can receive
47         // arv:pane:reload events even though the selector in .on()
48         // does not match e.target. Ignore such events.
49         return;
50     }
51
52     e.stopPropagation();
53
54     if ($pane.hasClass('pane-loading')) {
55         // Already loading, mark stale to schedule a reload after this one.
56         $pane.addClass('pane-stale');
57         return;
58     }
59
60     // The default throttle (mininum milliseconds between refreshes)
61     // can be overridden by an .arv-log-refresh-control element inside
62     // the pane -- or, failing that, the pane element itself -- with a
63     // data-load-throttle attribute. This allows the server to adjust
64     // the throttle depending on the pane content.
65     var throttle =
66         $pane.find('.arv-log-refresh-control').attr('data-load-throttle') ||
67         $pane.attr('data-load-throttle') ||
68         15000;
69     var now = (new Date()).getTime();
70     var loaded_at = $pane.attr('data-loaded-at');
71     var since_last_load = now - loaded_at;
72     if (loaded_at && (since_last_load < throttle)) {
73         if (!$pane.hasClass('pane-reload-pending')) {
74             $pane.addClass('pane-reload-pending');
75             setTimeout((function() {
76                 $pane.trigger('arv:pane:reload');
77             }), throttle - since_last_load);
78         }
79         return;
80     }
81
82     // We know this doesn't have 'pane-loading' because we tested for it above
83     $pane.removeClass('pane-reload-pending');
84     $pane.removeClass('pane-loaded');
85     $pane.removeClass('pane-stale');
86
87     if (!$pane.hasClass('active')) {
88         // When the user selects e.target tab, show a spinner instead of
89         // old content while loading.
90         $pane.html('<div class="spinner spinner-32px spinner-h-center"></div>');
91         return;
92     }
93
94     $pane.addClass('pane-loading');
95
96     $.ajax(content_url, {dataType: 'html', type: 'GET', context: $pane}).
97         done(function(data, status, jqxhr) {
98             // Preserve collapsed state
99             var $pane = this;
100             var collapsable = {};
101             $(".collapse", this).each(function(i, c) {
102                 collapsable[c.id] = $(c).hasClass('in');
103             });
104             var tmp = $(data);
105             $(".collapse", tmp).each(function(i, c) {
106                 if (collapsable[c.id]) {
107                     $(c).addClass('in');
108                 } else {
109                     $(c).removeClass('in');
110                 }
111             });
112             $pane.html(tmp);
113             $pane.removeClass('pane-loading');
114             $pane.addClass('pane-loaded');
115             $pane.attr('data-loaded-at', (new Date()).getTime());
116             $pane.trigger('arv:pane:loaded');
117
118             if ($pane.hasClass('pane-stale')) {
119                 $pane.trigger('arv:pane:reload');
120             }
121         }).fail(function(jqxhr, status, error) {
122             var $pane = this;
123             var errhtml;
124             var contentType = jqxhr.getResponseHeader('Content-Type');
125             if (contentType && contentType.match(/\btext\/html\b/)) {
126                 var $response = $(jqxhr.responseText);
127                 var $wrapper = $('div#page-wrapper', $response);
128                 if ($wrapper.length) {
129                     errhtml = $wrapper.html();
130                 } else {
131                     errhtml = jqxhr.responseText;
132                 }
133             } else {
134                 errhtml = ("An error occurred: " +
135                            (jqxhr.responseText || status)).
136                     replace(/&/g, '&amp;').
137                     replace(/</g, '&lt;').
138                     replace(/>/g, '&gt;');
139             }
140             $pane.html('<div><p>' +
141                       '<a href="#" class="btn btn-primary tab_reload">' +
142                       '<i class="fa fa-fw fa-refresh"></i> ' +
143                       'Reload tab</a></p><iframe style="width: 100%"></iframe></div>');
144             $('.tab_reload', $pane).click(function() {
145                 $(this).
146                     html('<div class="spinner spinner-32px spinner-h-center"></div>').
147                     closest('.pane-loaded').
148                     attr('data-loaded-at', 0).
149                     trigger('arv:pane:reload');
150             });
151             // We want to render the error in an iframe, in order to
152             // avoid conflicts with the main page's element ids, etc.
153             // In order to do that dynamically, we have to set a
154             // timeout on the iframe window to load our HTML *after*
155             // the default source (e.g., about:blank) has loaded.
156             var iframe = $('iframe', $pane)[0];
157             iframe.contentWindow.setTimeout(function() {
158                 $('body', iframe.contentDocument).html(errhtml);
159                 iframe.height = iframe.contentDocument.body.scrollHeight + "px";
160             }, 1);
161             $pane.removeClass('pane-loading');
162             $pane.addClass('pane-loaded');
163         });
164 });
165
166 // Mark all panes as stale/dirty. Refresh any 'active' panes.
167 $(document).on('arv:pane:reload:all', function() {
168     $('[data-pane-content-url]').trigger('arv:pane:reload');
169 });
170
171 $(document).on('arv-log-event', '.arv-refresh-on-log-event', function(e) {
172     // Panes marked arv-refresh-on-log-event should be refreshed
173     $(e.target).trigger('arv:pane:reload');
174 });
175
176 // If there is a 'tab counts url' in the nav-tabs element then use it to get some javascript that will update them
177 $(document).on('ready count-change', function() {
178     var tabCountsUrl = $('ul.nav-tabs').data('tab-counts-url');
179     if( tabCountsUrl && tabCountsUrl.length ) {
180         $.get( tabCountsUrl );
181     }
182 });