1 // Load tab panes on demand. See app/views/application/_content.html.erb
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')).
8 trigger('arv:pane:reload');
11 // Ask a refreshable pane to reload via ajax.
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".
17 // There are four CSS classes set on the object to indicate its state:
18 // pane-loading, pane-stale, pane-loaded, pane-reload-pending
20 // There are five states based on the presence or absence of css classes:
22 // 1. no pane-* states means the pane must be loaded when the pane becomes active
24 // 2. "pane-loading" means an AJAX call has been made to reload the pane and we are
25 // waiting on a result
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).
33 // 4. "pane-loaded" means the pane is up to date
35 // 5. "pane-loaded pane-reload-pending" indicates a reload is scheduled (but has
36 // not started yet), suppressing scheduling of any further reloads.
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);
44 var content_url = $pane.attr('data-pane-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.
54 if ($pane.hasClass('pane-loading')) {
55 // Already loading, mark stale to schedule a reload after this one.
56 $pane.addClass('pane-stale');
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.
66 $pane.find('.arv-log-refresh-control').attr('data-load-throttle') ||
67 $pane.attr('data-load-throttle') ||
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);
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');
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>');
94 $pane.addClass('pane-loading');
96 $.ajax(content_url, {dataType: 'html', type: 'GET', context: $pane}).
97 done(function(data, status, jqxhr) {
98 // Preserve collapsed state
100 var collapsable = {};
101 $(".collapse", this).each(function(i, c) {
102 collapsable[c.id] = $(c).hasClass('in');
105 $(".collapse", tmp).each(function(i, c) {
106 if (collapsable[c.id]) {
109 $(c).removeClass('in');
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');
118 if ($pane.hasClass('pane-stale')) {
119 $pane.trigger('arv:pane:reload');
121 }).fail(function(jqxhr, status, error) {
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();
131 errhtml = jqxhr.responseText;
134 errhtml = ("An error occurred: " +
135 (jqxhr.responseText || status)).
136 replace(/&/g, '&').
137 replace(/</g, '<').
138 replace(/>/g, '>');
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() {
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');
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";
161 $pane.removeClass('pane-loading');
162 $pane.addClass('pane-loaded');
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');
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');
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 );