4088: use filterable.js to filter on client side
[arvados.git] / apps / workbench / app / assets / javascripts / tab_panes.js
index 411a009f5c6badd4d4f566d6632618351c19504f..cca49b2799873211cfbfc636fea1be5ea13047e3 100644 (file)
@@ -1,77 +1,91 @@
 // 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.
+// Fire when a tab is selected/clicked.
 $(document).on('shown.bs.tab', '[data-toggle="tab"]', function(e) {
-    var content_url = $(e.target).attr('data-pane-content-url');
+    $(this).trigger('arv:pane:reload');
+});
+
+// Fire when the content in a pane becomes stale/dirty. If the pane is
+// 'active', reload it right away. Otherwise, just replace the current content
+// with a spinner for now, don't load the new content unless/until the pane
+// becomes active.
+$(document).on('arv:pane:reload', function(e) {
+    // Unload a single pane. Reload it if it's active.
+    $(e.target).removeClass('loaded');
     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');
-            $(this).trigger('arv:pane:loaded');
-        }).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();
+    if ($pane.hasClass('active')) {
+        var content_url = $(e.target).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", $pane).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');
+                    }
+                });
+                $pane.html(tmp);
+                $(e.target).addClass('loaded');
+                $pane.trigger('arv:pane:loaded');
+            }).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 = jqxhr.responseText;
+                    errhtml = ("An error occurred: " +
+                               (jqxhr.responseText || status)).
+                        replace(/&/g, '&').
+                        replace(/</g, '&lt;').
+                        replace(/>/g, '&gt;');
                 }
-            } else {
-                errhtml = ("An error occurred: " +
-                           (jqxhr.responseText || status)).
-                    replace(/&/g, '&amp;').
-                    replace(/</g, '&lt;').
-                    replace(/>/g, '&gt;');
-            }
-            $('> div > div', this).html(
-                '<div><p>' +
-                    '<a href="#" class="btn btn-primary tab_reload">' +
-                    '<i class="fa fa-fw fa-refresh"></i> ' +
-                    'Reload tab</a></p><iframe></iframe></div>');
-            $('.tab_reload', this).click(function() {
-                $('> div > div', $pane).html(
-                    '<div class="spinner spinner-32px spinner-h-center"></div>');
-                $pane.trigger('arv:pane:reload');
+                $pane.html('<div><p>' +
+                        '<a href="#" class="btn btn-primary tab_reload">' +
+                        '<i class="fa fa-fw fa-refresh"></i> ' +
+                        'Reload tab</a></p><iframe></iframe></div>');
+                $('.tab_reload', $pane).click(function() {
+                    $pane.html('<div class="spinner spinner-32px spinner-h-center"></div>');
+                    $(e.target).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', e.target)[0];
+                iframe.contentWindow.setTimeout(function() {
+                    $('body', iframe.contentDocument).html(errhtml);
+                    iframe.height = iframe.contentDocument.body.scrollHeight + "px";
+                }, 1);
+                $(e.target).addClass('loaded');
             });
-            // 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);
-            $(this).addClass('loaded');
-        });
-});
-
-// 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');
     } 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('<div class="spinner spinner-32px spinner-h-center"></div>');
+        $pane.html('<div class="spinner spinner-32px spinner-h-center"></div>');
     }
 });
 
 // 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');
+    $('.pane-anchor.loaded').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 );
+    }
 });